Commit 82806014 authored by Dominik Charousset's avatar Dominik Charousset

Add boolean to primitive variant, close #245

parent 51d5d9d3
......@@ -37,7 +37,7 @@ using primitive_variant = variant<int8_t, int16_t, int32_t, int64_t,
uint8_t, uint16_t, uint32_t, uint64_t,
float, double, long double,
std::string, std::u16string, std::u32string,
atom_value>;
atom_value, bool>;
} // namespace caf
......
......@@ -135,6 +135,11 @@ struct pt_reader : static_visitor<> {
inline void operator()(none_t&) {
// nop
}
inline void operator()(bool& value) {
uint8_t intval;
(*this)(intval);
value = static_cast<bool>(intval);
}
template <class T>
inline void operator()(T& value) {
begin = read_range(begin, end, value);
......
......@@ -43,6 +43,10 @@ class binary_writer : public static_visitor<> {
f(first, last);
}
void operator()(const bool& value) const {
write_int(m_out, static_cast<uint8_t>(value));
}
template <class T>
typename std::enable_if<std::is_integral<T>::value>::type
operator()(const T& value) const {
......
......@@ -87,6 +87,10 @@ class string_serializer : public serializer, public dummy_backend {
void operator()(const T& value) {
out << value;
}
void operator()(const bool& value) {
out << (value ? "true" : "false");
}
// make sure char's are treated as int8_t number, not as character
inline void operator()(const char& value) {
out << static_cast<int>(value);
......@@ -141,7 +145,7 @@ class string_serializer : public serializer, public dummy_backend {
m_namespace(*this),
m_after_value(false),
m_obj_just_opened(false) {
// nop
out << std::boolalpha;
}
void begin_object(const uniform_type_info* uti) {
......
......@@ -306,12 +306,12 @@ inline void deserialize_impl(duration& val, deserializer* source) {
val.count = count_val;
}
inline void serialize_impl(bool val, serializer* sink) {
sink->write_value(static_cast<uint8_t>(val ? 1 : 0));
inline void serialize_impl(const bool& val, serializer* sink) {
sink->write_value(val);
}
inline void deserialize_impl(bool& val, deserializer* source) {
val = source->read<uint8_t>() != 0;
val = source->read<bool>();
}
// exit_msg & down_msg have the same fields
......
......@@ -295,17 +295,14 @@ CAF_TEST(test_multiple_messages) {
CAF_CHECK(is_message(m2).equal(i32, te, str, rs));
}
CAF_TEST(test_string_serialization) {
auto input = make_message("hello \"actor world\"!", atom("foo"));
auto s = to_string(input);
CAF_CHECK_EQUAL(s, R"#(@<>+@str+@atom ( "hello \"actor world\"!", 'foo' ))#");
auto m = from_string<message>(s);
if (!m) {
CAF_TEST_ERROR("from_string failed");
return;
}
CAF_CHECK(*m == input);
CAF_CHECK_EQUAL(to_string(*m), to_string(input));
CAF_TEST(strings) {
auto m1 = make_message("hello \"actor world\"!", atom("foo"));
auto s1 = to_string(m1);
CAF_CHECK_EQUAL(s1, R"#(@<>+@str+@atom ( "hello \"actor world\"!", 'foo' ))#");
CAF_CHECK(from_string<message>(s1) == m1);
auto m2 = make_message(true);
auto s2 = to_string(m2);
CAF_CHECK_EQUAL(s2, "@<>+bool ( true )");
}
CAF_TEST_FIXTURE_SCOPE_END()
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment