mirror of
https://codeberg.org/DawnMagnet/reflect-cpp
synced 2026-12-03 08:46:19 +08:00
Added more convenient read functions; #65
This commit is contained in:
+7
-1
@@ -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:
|
||||
|
||||
@@ -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.
|
||||
|
||||
Reference in New Issue
Block a user