c++: Add explicit exile_launch() std::basic_string variant

Šī revīzija ir iekļauta:
Albert S. 2022-02-04 21:46:41 +01:00
vecāks 97e2025758
revīzija 95fa11e928
2 mainīti faili ar 54 papildinājumiem un 19 dzēšanām

Parādīt failu

@ -174,3 +174,26 @@ typename std::enable_if_t<!std::is_trivially_copyable_v<T> && std::is_copy_const
}
template<class T>
std::basic_string<typename T::value_type> deserialize_stdstring(const char *buf, size_t n)
{
return std::basic_string<typename T::value_type> { buf, n };
}
template<class T>
size_t serialize_stdstring(const std::basic_string<typename T::value_type> &t, char *buf, size_t n)
{
if(n < t.size())
{
return 0;
}
memcpy(buf, t.data(), t.size());
return t.size();
}
template<typename T, typename U, typename ... Args>
std::basic_string<typename T::value_type> exile_launch(struct exile_policy *policy, U fn, Args && ... args)
{
return exile_launch<T, U, Args...>(policy, &serialize_stdstring<T>, &deserialize_stdstring<T>, fn, std::forward<Args>(args) ...);
}

Parādīt failu

@ -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<std::string>(exile_init_policy(), &serialize_stdstring, &deserialize_stdstring, &sandboxed_reverse, str);
std::string reversed = exile_launch<std::string>(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<not_trivially_copyable>);
auto serializer = [](const not_trivially_copyable &obj, char *buf, size_t n){
serialize_stdstring<std::string>(obj.somecontent, buf, n);
return obj.somecontent.size();
};
auto deserializer = [](const char *buffer, size_t n) {
not_trivially_copyable obj;
obj.somecontent = deserialize_stdstring<std::string>(buffer, n);
return obj;
};
not_trivially_copyable result = exile_launch<not_trivially_copyable>(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<std::string, int (*)()> 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];