Skip to content

Refactor #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
"label": "build",
"type": "shell",
"command": "cd build; make",
"problemMatcher": [
"$gcc"
],
"problemMatcher": {
"base": "$gcc",
"fileLocation": ["relative", "../"]
},
"group": {
"kind": "build",
"isDefault": true
Expand Down
6 changes: 6 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
39 changes: 24 additions & 15 deletions src/Datalog.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,45 +12,54 @@
#include <tuple>

#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<T>*
*/
template <typename T>
Variable<T> *var()
{
return new Variable<T>();
}

/**
* @brief get the value of a variable
*
* @tparam T
* @param t
* @return T
*/
template <typename T>
T val(Variable<T> *t)
{
return t->value();
}

/**
* @brief delete a variable
*
* @tparam T
* @param v
*/
template <typename T>
void deleteVar(Variable<T> *v)
{
delete v;
}

template <typename T>
void unbind(Variable<T> *t)
{
t->unbind();
}

template <typename T>
void unbind(const T &t) {}

template <typename... Ts>
void unbind(const tuple<Ts...> &tuple)
{
apply([](auto &&... args) { ((unbind(args), ...)); }, tuple);
}
// TODO: all functions below here to be refactored into separate files

template <typename T>
bool bind(const T &a, const T &b)
Expand Down
47 changes: 47 additions & 0 deletions src/tuple_binding.h
Original file line number Diff line number Diff line change
@@ -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 <typename T>
void unbind(Variable<T> *t)
{
t->unbind();
}

/**
* @brief unbind no-operation for types that are not variables (i.e. values)
*
* @tparam T
* @param t
*/
template <typename T>
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 <typename... Ts>
void unbind(const tuple<Ts...> &tuple)
{
apply([](auto &&... args) { ((unbind(args), ...)); }, tuple);
}

}

#endif // TUPLES_H
File renamed without changes.
3 changes: 2 additions & 1 deletion tests/run_tests.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/bin/bash
set -e
../build/types_test
../build/variable_test
../build/variable_test
../build/tuple_binding_test
21 changes: 21 additions & 0 deletions tests/tuple_binding_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include "catch.hpp"
#include "Datalog.h"

using namespace datalog;
using namespace std;

bool unbindTest()
{
auto v = var<int>();
v->bind(3);
tuple<decltype(v), int> t{v, 3};
v->unbind();
bool returnVal = !get<0>(t)->isBound();
deleteVar(v);
return returnVal;
}

TEST_CASE("tuple binding test", "[tuple-binding]")
{
REQUIRE(unbindTest());
}
2 changes: 1 addition & 1 deletion tests/variable_test.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "catch.hpp"
#include "Variable.h"
#include "variable.h"

using namespace datalog;

Expand Down