diff --git a/.vscode/tasks.json b/.vscode/tasks.json index 9f777b5..75110f1 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -7,9 +7,10 @@ "label": "build", "type": "shell", "command": "cd build; make", - "problemMatcher": [ - "$gcc" - ], + "problemMatcher": { + "base": "$gcc", + "fileLocation": ["relative", "../"] + }, "group": { "kind": "build", "isDefault": true diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index e12911c..3bbdeb5 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -35,3 +35,9 @@ add_executable(variable_test ../tests/variable_test.cpp) target_include_directories(variable_test PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) target_link_libraries(variable_test tests_main) target_compile_definitions(variable_test PUBLIC UNIX) + +# tuple_binding_test target +add_executable(tuple_binding_test ../tests/tuple_binding_test.cpp) +target_include_directories(tuple_binding_test PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) +target_link_libraries(tuple_binding_test tests_main) +target_compile_definitions(tuple_binding_test PUBLIC UNIX) diff --git a/src/Datalog.h b/src/Datalog.h index c537100..cdb279b 100644 --- a/src/Datalog.h +++ b/src/Datalog.h @@ -12,45 +12,54 @@ #include #include "tuple_hash.h" -#include "Variable.h" +#include "variable.h" +#include "tuple_binding.h" namespace datalog { using namespace std; +// High-level API functions + +/** + * @brief create a new variable + * + * @tparam T + * @return Variable* + */ template Variable *var() { return new Variable(); } +/** + * @brief get the value of a variable + * + * @tparam T + * @param t + * @return T + */ template T val(Variable *t) { return t->value(); } +/** + * @brief delete a variable + * + * @tparam T + * @param v + */ template void deleteVar(Variable *v) { delete v; } -template -void unbind(Variable *t) -{ - t->unbind(); -} - -template -void unbind(const T &t) {} - -template -void unbind(const tuple &tuple) -{ - apply([](auto &&... args) { ((unbind(args), ...)); }, tuple); -} +// TODO: all functions below here to be refactored into separate files template bool bind(const T &a, const T &b) diff --git a/src/tuple_binding.h b/src/tuple_binding.h new file mode 100644 index 0000000..6a23add --- /dev/null +++ b/src/tuple_binding.h @@ -0,0 +1,47 @@ +#ifndef TUPLES_H +#define TUPLES_H + +#include "variable.h" + +namespace datalog { + +// TODO: reify the concept of a tuple of values and pointers to Variables + +/** + * @brief unbind a variable + * + * @tparam T + * @param t + */ +template +void unbind(Variable *t) +{ + t->unbind(); +} + +/** + * @brief unbind no-operation for types that are not variables (i.e. values) + * + * @tparam T + * @param t + */ +template +void unbind(const T &t) { + // If t is not a Variable then perform no-op +} + +/** + * @brief apply unbind to a tuple of variables and values + * + * @tparam Ts + * @param tuple + */ +template +void unbind(const tuple &tuple) +{ + apply([](auto &&... args) { ((unbind(args), ...)); }, tuple); +} + +} + +#endif // TUPLES_H \ No newline at end of file diff --git a/src/Variable.h b/src/variable.h similarity index 100% rename from src/Variable.h rename to src/variable.h diff --git a/tests/run_tests.sh b/tests/run_tests.sh index d5b72c4..2aead19 100755 --- a/tests/run_tests.sh +++ b/tests/run_tests.sh @@ -1,4 +1,5 @@ #!/bin/bash set -e ../build/types_test -../build/variable_test \ No newline at end of file +../build/variable_test +../build/tuple_binding_test \ No newline at end of file diff --git a/tests/tuple_binding_test.cpp b/tests/tuple_binding_test.cpp new file mode 100644 index 0000000..5a76daa --- /dev/null +++ b/tests/tuple_binding_test.cpp @@ -0,0 +1,21 @@ +#include "catch.hpp" +#include "Datalog.h" + +using namespace datalog; +using namespace std; + +bool unbindTest() +{ + auto v = var(); + v->bind(3); + tuple t{v, 3}; + v->unbind(); + bool returnVal = !get<0>(t)->isBound(); + deleteVar(v); + return returnVal; +} + +TEST_CASE("tuple binding test", "[tuple-binding]") +{ + REQUIRE(unbindTest()); +} diff --git a/tests/variable_test.cpp b/tests/variable_test.cpp index d5de203..3cdae51 100644 --- a/tests/variable_test.cpp +++ b/tests/variable_test.cpp @@ -1,5 +1,5 @@ #include "catch.hpp" -#include "Variable.h" +#include "variable.h" using namespace datalog;