Skip to content

Commit 2373ced

Browse files
Fixed union
1 parent 14af0f3 commit 2373ced

6 files changed

Lines changed: 15 additions & 18 deletions

File tree

include/rfl/cereal/Reader.hpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -107,11 +107,7 @@ struct Reader {
107107
for (::cereal::size_type i = 0; i < size; ++i) {
108108
std::string key;
109109
(*_map.archive_)(key);
110-
const auto err = _map_reader.read(std::string_view(key),
111-
InputVarType{_map.archive_});
112-
if (err) {
113-
return err;
114-
}
110+
_map_reader.read(std::string_view(key), InputVarType{_map.archive_});
115111
}
116112
return std::nullopt;
117113
} catch (std::exception& e) {
@@ -135,7 +131,7 @@ struct Reader {
135131
template <class T, class UnionReader>
136132
rfl::Result<T> read_union(const InputUnionType& _union) const noexcept {
137133
try {
138-
std::int32_t index;
134+
size_t index;
139135
(*_union.archive_)(index);
140136
return UnionReader::read(*this, index, InputVarType{_union.archive_});
141137
} catch (std::exception& e) {

include/rfl/cereal/Writer.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ class Writer {
8585

8686
OutputArrayType add_array_to_union(const size_t _index, const size_t _size,
8787
OutputUnionType* _parent) const {
88+
(*archive_)(_index);
8889
(*archive_)(::cereal::make_size_tag(_size));
8990
return OutputArrayType{};
9091
}
@@ -111,6 +112,7 @@ class Writer {
111112

112113
OutputMapType add_map_to_union(const size_t _index, const size_t _size,
113114
OutputUnionType* _parent) const {
115+
(*archive_)(_index);
114116
(*archive_)(::cereal::make_size_tag(_size));
115117
return OutputMapType{};
116118
}
@@ -134,6 +136,7 @@ class Writer {
134136

135137
OutputObjectType add_object_to_union(const size_t _index, const size_t _size,
136138
OutputUnionType* _parent) const {
139+
(*archive_)(_index);
137140
return OutputObjectType{};
138141
}
139142

@@ -153,6 +156,7 @@ class Writer {
153156

154157
OutputUnionType add_union_to_union(const size_t _index,
155158
OutputUnionType* _parent) const {
159+
(*archive_)(_index);
156160
return OutputUnionType{};
157161
}
158162

@@ -179,6 +183,7 @@ class Writer {
179183
template <class T>
180184
OutputVarType add_value_to_union(const size_t _index, const T& _var,
181185
OutputUnionType* _parent) const {
186+
(*archive_)(_index);
182187
(*archive_)(_var);
183188
return OutputVarType{};
184189
}
@@ -199,6 +204,7 @@ class Writer {
199204

200205
OutputVarType add_null_to_union(const size_t _index,
201206
OutputUnionType* _parent) const {
207+
(*archive_)(_index);
202208
return OutputVarType{};
203209
}
204210

include/rfl/cereal/write.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ std::vector<char> write(const auto& _obj) {
3535
/// Writes Cereal binary format into an ostream.
3636
template <class... Ps>
3737
std::ostream& write(const auto& _obj, std::ostream& _stream) {
38-
::cereal::BinaryOutputArchive archive(_stream);
38+
::cereal::PortableBinaryOutputArchive archive(_stream);
3939
write<std::remove_cvref_t<decltype(_obj)>, Ps...>(_obj, archive);
4040
return _stream;
4141
}

tests/cereal/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
project(reflect-cpp-cereal-tests)
22

3-
file(GLOB_RECURSE SOURCES CONFIGURE_DEPENDS "test_readme_example.cpp")
3+
file(GLOB_RECURSE SOURCES CONFIGURE_DEPENDS "*.cpp")
44

55
add_executable(
66
reflect-cpp-cereal-tests

tests/cereal/test_map.cpp

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,19 @@
88
#include "write_and_read.hpp"
99

1010
namespace test_map {
11+
1112
struct Person {
1213
rfl::Rename<"firstName", std::string> first_name;
1314
rfl::Rename<"lastName", std::string> last_name = "Simpson";
1415
std::unique_ptr<std::map<std::string, Person>> children;
1516
};
1617

1718
TEST(cereal, test_map) {
18-
auto bart = Person{.first_name = "Bart"};
19-
20-
auto lisa = Person{.first_name = "Lisa"};
21-
22-
auto maggie = Person{.first_name = "Maggie"};
19+
auto children = std::make_unique<std::map<std::string, Person>>();
2320

24-
auto children = std::make_unique<std::map<std::string, Person>>(
25-
std::map<std::string, Person>({{"Bart", std::move(bart)},
26-
{"Lisa", std::move(lisa)},
27-
{"Maggie", std::move(maggie)}}));
21+
children->insert(std::make_pair("Bart", Person{.first_name = "Bart"}));
22+
children->insert(std::make_pair("Lisa", Person{.first_name = "Lisa"}));
23+
children->insert(std::make_pair("Maggie", Person{.first_name = "Maggie"}));
2824

2925
const auto homer =
3026
Person{.first_name = "Homer", .children = std::move(children)};

tests/cereal/test_tuple.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
namespace test_tuple {
1111

1212
TEST(cereal, test_tuple) {
13-
using TupleType = std::tuple<int, std::string, bool, double>;
1413
const auto my_tuple = std::make_tuple(42, std::string("Hello"), true, 3.14);
1514
write_and_read(my_tuple);
1615
}

0 commit comments

Comments
 (0)