Added more convenient read functions; #65

This commit is contained in:
Patrick Urbanke
2024-03-03 10:59:18 +01:00
parent 54ffdbc9e9
commit 7f2be9b7cb
2 changed files with 19 additions and 10 deletions
+7 -1
View File
@@ -17,7 +17,7 @@ struct Person {
};
```
A `person` can be turned into a bytes vector like this:
A `person` can be serialized like this:
```cpp
const auto person = Person{...};
@@ -30,6 +30,12 @@ You can parse bytes like this:
const rfl::Result<Person> result = rfl::bson::read<Person>(bytes);
```
You can also parse raw pointers (either `const char*` or `const uint8_t*`):
```cpp
const rfl::Result<Person> result = rfl::bson::read<Person>(ptr, length);
```
## Loading and saving
You can also load and save to disc using a very similar syntax:
+12 -9
View File
@@ -18,29 +18,32 @@ using InputVarType = typename Reader::InputVarType;
/// Parses an object from a BSON var.
template <class T>
auto read(const InputVarType& _obj) {
Result<internal::wrap_in_rfl_array_t<T>> read(const InputVarType& _obj) {
const auto r = Reader();
return Parser<T>::read(r, _obj);
}
/// Parses an object from flexbuf using reflection.
/// Parses an BSON object using reflection.
template <class T>
Result<internal::wrap_in_rfl_array_t<T>> read(char* _bytes,
const size_t _size) {
auto read(const uint8_t* _bytes, const size_t _size) {
Reader::BSONValue value;
value.val_.value.v_doc.data_len = static_cast<uint32_t>(_size);
value.val_.value.v_doc.data = reinterpret_cast<uint8_t*>(_bytes);
value.val_.value.v_doc.data = const_cast<uint8_t*>(_bytes);
value.val_.value_type = BSON_TYPE_DOCUMENT;
auto doc = InputVarType{&value};
auto result = read<T>(doc);
return result;
return read<T>(doc);
}
/// Parses an BSON object using reflection.
template <class T>
auto read(const char* _bytes, const size_t _size) {
return read<T>(reinterpret_cast<const uint8_t*>(_bytes), _size);
}
/// Parses an object from BSON using reflection.
template <class T>
auto read(const std::vector<char>& _bytes) {
auto bytes = _bytes;
return read<T>(bytes.data(), bytes.size());
return read<T>(_bytes.data(), _bytes.size());
}
/// Parses an object from a stream.