Skip to content

Commit 82b5a6e

Browse files
authored
Merge pull request #7 from Z80coder/refactor
Refactor
2 parents e1003a3 + 2c733cd commit 82b5a6e

File tree

8 files changed

+105
-20
lines changed

8 files changed

+105
-20
lines changed

.vscode/tasks.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@
77
"label": "build",
88
"type": "shell",
99
"command": "cd build; make",
10-
"problemMatcher": [
11-
"$gcc"
12-
],
10+
"problemMatcher": {
11+
"base": "$gcc",
12+
"fileLocation": ["relative", "../"]
13+
},
1314
"group": {
1415
"kind": "build",
1516
"isDefault": true

src/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,9 @@ add_executable(variable_test ../tests/variable_test.cpp)
3535
target_include_directories(variable_test PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
3636
target_link_libraries(variable_test tests_main)
3737
target_compile_definitions(variable_test PUBLIC UNIX)
38+
39+
# tuple_binding_test target
40+
add_executable(tuple_binding_test ../tests/tuple_binding_test.cpp)
41+
target_include_directories(tuple_binding_test PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
42+
target_link_libraries(tuple_binding_test tests_main)
43+
target_compile_definitions(tuple_binding_test PUBLIC UNIX)

src/Datalog.h

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,45 +12,54 @@
1212
#include <tuple>
1313

1414
#include "tuple_hash.h"
15-
#include "Variable.h"
15+
#include "variable.h"
16+
#include "tuple_binding.h"
1617

1718
namespace datalog
1819
{
1920

2021
using namespace std;
2122

23+
// High-level API functions
24+
25+
/**
26+
* @brief create a new variable
27+
*
28+
* @tparam T
29+
* @return Variable<T>*
30+
*/
2231
template <typename T>
2332
Variable<T> *var()
2433
{
2534
return new Variable<T>();
2635
}
2736

37+
/**
38+
* @brief get the value of a variable
39+
*
40+
* @tparam T
41+
* @param t
42+
* @return T
43+
*/
2844
template <typename T>
2945
T val(Variable<T> *t)
3046
{
3147
return t->value();
3248
}
3349

50+
/**
51+
* @brief delete a variable
52+
*
53+
* @tparam T
54+
* @param v
55+
*/
3456
template <typename T>
3557
void deleteVar(Variable<T> *v)
3658
{
3759
delete v;
3860
}
3961

40-
template <typename T>
41-
void unbind(Variable<T> *t)
42-
{
43-
t->unbind();
44-
}
45-
46-
template <typename T>
47-
void unbind(const T &t) {}
48-
49-
template <typename... Ts>
50-
void unbind(const tuple<Ts...> &tuple)
51-
{
52-
apply([](auto &&... args) { ((unbind(args), ...)); }, tuple);
53-
}
62+
// TODO: all functions below here to be refactored into separate files
5463

5564
template <typename T>
5665
bool bind(const T &a, const T &b)

src/tuple_binding.h

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#ifndef TUPLES_H
2+
#define TUPLES_H
3+
4+
#include "variable.h"
5+
6+
namespace datalog {
7+
8+
// TODO: reify the concept of a tuple of values and pointers to Variables
9+
10+
/**
11+
* @brief unbind a variable
12+
*
13+
* @tparam T
14+
* @param t
15+
*/
16+
template <typename T>
17+
void unbind(Variable<T> *t)
18+
{
19+
t->unbind();
20+
}
21+
22+
/**
23+
* @brief unbind no-operation for types that are not variables (i.e. values)
24+
*
25+
* @tparam T
26+
* @param t
27+
*/
28+
template <typename T>
29+
void unbind(const T &t) {
30+
// If t is not a Variable then perform no-op
31+
}
32+
33+
/**
34+
* @brief apply unbind to a tuple of variables and values
35+
*
36+
* @tparam Ts
37+
* @param tuple
38+
*/
39+
template <typename... Ts>
40+
void unbind(const tuple<Ts...> &tuple)
41+
{
42+
apply([](auto &&... args) { ((unbind(args), ...)); }, tuple);
43+
}
44+
45+
}
46+
47+
#endif // TUPLES_H
File renamed without changes.

tests/run_tests.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#!/bin/bash
22
set -e
33
../build/types_test
4-
../build/variable_test
4+
../build/variable_test
5+
../build/tuple_binding_test

tests/tuple_binding_test.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#include "catch.hpp"
2+
#include "Datalog.h"
3+
4+
using namespace datalog;
5+
using namespace std;
6+
7+
bool unbindTest()
8+
{
9+
auto v = var<int>();
10+
v->bind(3);
11+
tuple<decltype(v), int> t{v, 3};
12+
v->unbind();
13+
bool returnVal = !get<0>(t)->isBound();
14+
deleteVar(v);
15+
return returnVal;
16+
}
17+
18+
TEST_CASE("tuple binding test", "[tuple-binding]")
19+
{
20+
REQUIRE(unbindTest());
21+
}

tests/variable_test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#include "catch.hpp"
2-
#include "Variable.h"
2+
#include "variable.h"
33

44
using namespace datalog;
55

0 commit comments

Comments
 (0)