diff --git a/exile.hpp b/exile.hpp index 7cc86a9..fb3d98f 100644 --- a/exile.hpp +++ b/exile.hpp @@ -174,3 +174,26 @@ typename std::enable_if_t && std::is_copy_const } +template +std::basic_string deserialize_stdstring(const char *buf, size_t n) +{ + return std::basic_string { buf, n }; +} + +template +size_t serialize_stdstring(const std::basic_string &t, char *buf, size_t n) +{ + if(n < t.size()) + { + return 0; + } + memcpy(buf, t.data(), t.size()); + return t.size(); +} + + +template +std::basic_string exile_launch(struct exile_policy *policy, U fn, Args && ... args) +{ + return exile_launch(policy, &serialize_stdstring, &deserialize_stdstring, fn, std::forward(args) ...); +} diff --git a/test.cpp b/test.cpp index 1b14c44..bce4007 100644 --- a/test.cpp +++ b/test.cpp @@ -8,21 +8,6 @@ std::string sandboxed_reverse(std::string str) return str; } -std::string deserialize_stdstring(const char *buf, size_t n) -{ - return std::string { buf, n }; -} - -size_t serialize_stdstring(const std::string &t, char *buf, size_t n) -{ - if(n < t.size()) - { - return 0; - } - memcpy(buf, t.data(), t.size()); - return t.size(); -} - size_t stdstrlen(const std::string &str) { return str.size(); @@ -40,15 +25,41 @@ int test_exile_launch_trivial() return 0; } -int test_exile_launch() +int test_exile_launch_stdstring() { std::string str = "abc123"; - std::string reversed = exile_launch(exile_init_policy(), &serialize_stdstring, &deserialize_stdstring, &sandboxed_reverse, str); - + std::string reversed = exile_launch(exile_init_policy(), &sandboxed_reverse, str); assert(reversed == "321cba"); return 0; } +struct not_trivially_copyable +{ +public: + std::string somecontent; +}; + +int test_exile_launch_serializer() +{ + static_assert(! std::is_trivially_copyable_v); + + auto serializer = [](const not_trivially_copyable &obj, char *buf, size_t n){ + serialize_stdstring(obj.somecontent, buf, n); + return obj.somecontent.size(); + }; + + auto deserializer = [](const char *buffer, size_t n) { + not_trivially_copyable obj; + obj.somecontent = deserialize_stdstring(buffer, n); + return obj; + }; + + not_trivially_copyable result = exile_launch(exile_init_policy(), serializer, deserializer, []() {not_trivially_copyable obj; obj.somecontent = "Just something"; return obj;}); + + assert(result.somecontent == "Just something"); + return 0; +} + int main(int argc, char *argv[]) { if(argc < 2) @@ -58,7 +69,8 @@ int main(int argc, char *argv[]) } std::map map = { { "launch-trivial-cpp", &test_exile_launch_trivial} , - { "launch-cpp", &test_exile_launch } + { "launch-stdstring-cpp", &test_exile_launch_stdstring }, + { "launch-serializer-cpp", &test_exile_launch_serializer }, }; std::string test = argv[1];