Skip to content

Commit 0af61de

Browse files
committed
Initial support of string_id
1 parent 98f3377 commit 0af61de

File tree

6 files changed

+153
-1
lines changed

6 files changed

+153
-1
lines changed

CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,4 +217,8 @@ add_executable(math_test tests/math.cpp)
217217
target_link_libraries(math_test ${LIBS})
218218
ADD_CATCH_TESTS(math_test)
219219

220+
add_executable(string_id_test tests/string_id.cpp)
221+
target_link_libraries(string_id_test ${LIBS})
222+
ADD_CATCH_TESTS(string_id_test)
223+
220224

biicode.cmake

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
set(BII_LIB_DEPS "pthread -ldl")
1+
set(BII_LIB_DEPS "pthread -ldl ../../lib/libfoonathan_string_id.a")
22

33
ADD_BIICODE_TARGETS()
4+
TARGET_INCLUDE_DIRECTORIES(${BII_BLOCK_TARGET} INTERFACE "${CMAKE_CURRENT_BINARY_DIR}/../foonathan_string_id")
45

56

67
IF(APPLE)

biicode.conf

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Biicode configuration file
2+
3+
[requirements]
4+
ChaiScript/ChaiScript: 1
5+
foonathan/string_id: 7
6+
7+
[parent]
8+
# The parent version of this block. Must match folder name. E.g.
9+
# user/block # No version number means not published yet
10+
# You can change it to publish to a different track, and change version, e.g.
11+
# user/block(track): 7
12+
13+
[paths]
14+
# Local directories to look for headers (within block)
15+
# /
16+
# include
17+
18+
[dependencies]
19+
# Manual adjust file implicit dependencies, add (+), remove (-), or overwrite (=)
20+
# hello.h + hello_imp.cpp hello_imp2.cpp
21+
# *.h + *.cpp
22+
23+
[mains]
24+
# Manual adjust of files that define an executable
25+
# !main.cpp # Do not build executable from this file
26+
# main2.cpp # Build it (it doesnt have a main() function, but maybe it includes it)
27+
!tests/catch.hpp
28+
tests/*.cpp
29+
main.cpp
30+
31+
[tests]
32+
# Manual adjust of files that define a CTest test
33+
# test/* pattern to evaluate this test/ folder sources like tests
34+
tests/*.cpp
35+
36+
[hooks]
37+
# These are defined equal to [dependencies],files names matching bii*stage*hook.py
38+
# will be launched as python scripts at stage = {post_process, clean}
39+
# CMakeLists.txt + bii/my_post_process1_hook.py bii_clean_hook.py
40+
41+
[includes]
42+
# Mapping of include patterns to external blocks
43+
# hello*.h: user3/depblock # includes will be processed as user3/depblock/hello*.h
44+
chaiscript/*.hpp: ChaiScript/ChaiScript/include
45+
string_id/*.hpp: foonathan
46+
47+
[data]
48+
# Manually define data files dependencies, that will be copied to bin for execution
49+
# By default they are copied to bin/user/block/... which should be taken into account
50+
# when loading from disk such data
51+
# image.cpp + image.jpg # code should write open("user/block/image.jpg")
52+
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
2+
#include <cmath>
3+
#include <memory>
4+
5+
#include <chaiscript/chaiscript.hpp>
6+
#include <string_id/string_id.hpp>
7+
#include <string_id/database.hpp>
8+
#include <string_id/basic_database.hpp>
9+
10+
namespace chaiscript {
11+
namespace extras {
12+
namespace string_id {
13+
14+
ModulePtr bootstrap(ModulePtr m = std::make_shared<Module>())
15+
{
16+
17+
using namespace foonathan;
18+
19+
// default_database
20+
m->add(user_type<foonathan::string_id::default_database>(), "default_database");
21+
m->add(constructor<foonathan::string_id::default_database ()>(), "default_database");
22+
23+
// basic_database
24+
m->add(user_type<foonathan::string_id::basic_database>(), "basic_database");
25+
m->add(base_class<foonathan::string_id::basic_database, foonathan::string_id::default_database>());
26+
27+
// string_id
28+
m->add(user_type<foonathan::string_id::string_id>(), "string_id");
29+
m->add(constructor<foonathan::string_id::string_id (const foonathan::string_id::string_id &)>(), "string_id");
30+
m->add(constructor<foonathan::string_id::string_id (const foonathan::string_id::string_info &, foonathan::string_id::basic_database &)>(), "string_id");
31+
m->add(constructor<foonathan::string_id::string_id (const foonathan::string_id::string_id &, foonathan::string_id::string_info)>(), "string_id");
32+
m->add(fun(&foonathan::string_id::string_id::hash_code), "hash_code");
33+
m->add(fun(&foonathan::string_id::string_id::database), "database");
34+
m->add(fun<bool (const foonathan::string_id::string_id &, const foonathan::string_id::string_id &)>([](const foonathan::string_id::string_id &t_lhs, const foonathan::string_id::string_id &t_rhs){ return t_lhs == t_rhs; }), "==");
35+
m->add(fun<bool (foonathan::string_id::hash_type, const foonathan::string_id::string_id &)>([](foonathan::string_id::hash_type t_lhs, const foonathan::string_id::string_id &t_rhs){ return t_lhs == t_rhs; }), "==");
36+
m->add(fun<bool (const foonathan::string_id::string_id &, foonathan::string_id::hash_type)>([](const foonathan::string_id::string_id &t_lhs, foonathan::string_id::hash_type t_rhs){ return t_lhs == t_rhs; }), "==");
37+
m->add(fun<bool (const foonathan::string_id::string_id &, const foonathan::string_id::string_id &)>([](const foonathan::string_id::string_id &t_lhs, const foonathan::string_id::string_id &t_rhs){ return t_lhs != t_rhs; }), "!=");
38+
m->add(fun<bool (foonathan::string_id::hash_type, const foonathan::string_id::string_id &)>([](foonathan::string_id::hash_type t_lhs, const foonathan::string_id::string_id &t_rhs){ return t_lhs != t_rhs; }), "!=");
39+
m->add(fun<bool (const foonathan::string_id::string_id &, foonathan::string_id::hash_type)>([](const foonathan::string_id::string_id &t_lhs, foonathan::string_id::hash_type t_rhs){ return t_lhs != t_rhs; }), "!=");
40+
41+
// string_info
42+
m->add(user_type<foonathan::string_id::string_info>(), "string_info");
43+
m->add(fun(&foonathan::string_id::string_info::string), "string");
44+
m->add(fun(&foonathan::string_id::string_info::length), "length");
45+
46+
47+
48+
// type conversions
49+
m->add(type_conversion<const std::string &, foonathan::string_id::string_info>(
50+
[](const std::string &t_str) {
51+
return foonathan::string_id::string_info(t_str.c_str(), t_str.size());
52+
}
53+
));
54+
55+
// free functions
56+
m->add(fun(&foonathan::string_id::detail::sid_hash), "sid_hash");
57+
58+
return m;
59+
}
60+
}
61+
}
62+
}
63+

main.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
#include "chaiscript/chaiscript.hpp"
22
#include "chaiscript/chaiscript_stdlib.hpp"
33
#include "include/chaiscript/extras/math.hpp"
4+
#include "include/chaiscript/extras/string_id.hpp"
45

56
#include <iostream>
67

78
int main()
89
{
910
auto stdlib = chaiscript::Std_Lib::library();
1011
auto mathlib = chaiscript::extras::math::bootstrap();
12+
auto string_idlib = chaiscript::extras::string_id::bootstrap();
1113

1214
chaiscript::ChaiScript chai(stdlib);
1315
chai.add(mathlib);
16+
chai.add(string_idlib);
1417
}

tests/string_id.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#define CATCH_CONFIG_MAIN // This tells Catch to provide a main() - only do this in one cpp file
2+
//#include <sstream>
3+
#include "catch.hpp"
4+
5+
#include <chaiscript/chaiscript.hpp>
6+
#include <chaiscript/chaiscript_stdlib.hpp>
7+
#include "../include/chaiscript/extras/string_id.hpp"
8+
9+
#include <iostream>
10+
11+
TEST_CASE( "string_id functions work", "[string_id]" ) {
12+
auto stdlib = chaiscript::Std_Lib::library();
13+
auto string_idlib = chaiscript::extras::string_id::bootstrap();
14+
15+
chaiscript::ChaiScript chai(stdlib);
16+
chai.add(string_idlib);
17+
18+
foonathan::string_id::default_database database;
19+
chai.add(chaiscript::var(std::ref(database)), "database");
20+
21+
chai.eval(R""(
22+
var id = string_id("Test0815", database);
23+
)"");
24+
25+
using namespace foonathan::string_id::literals;
26+
CHECK(chai.eval<const foonathan::string_id::string_id &>("id") == "Test0815"_id);
27+
}
28+
29+

0 commit comments

Comments
 (0)