mirror of
https://codeberg.org/DawnMagnet/reflect-cpp
synced 2026-12-06 10:46:18 +08:00
Move some heavy templates into cpp (#204)
This commit is contained in:
@@ -27,33 +27,12 @@ struct Reader {
|
||||
});
|
||||
|
||||
rfl::Result<InputVarType> get_field_from_array(
|
||||
const size_t _idx, const InputArrayType _arr) const noexcept {
|
||||
if (_idx >= _arr.size) {
|
||||
return rfl::Error("Index " + std::to_string(_idx) + " of of bounds.");
|
||||
}
|
||||
return _arr.ptr[_idx];
|
||||
}
|
||||
const size_t _idx, const InputArrayType _arr) const noexcept;
|
||||
|
||||
rfl::Result<InputVarType> get_field_from_object(
|
||||
const std::string& _name, const InputObjectType& _obj) const noexcept {
|
||||
for (uint32_t i = 0; i < _obj.size; ++i) {
|
||||
const auto& key = _obj.ptr[i].key;
|
||||
if (key.type != MSGPACK_OBJECT_STR) {
|
||||
return Error("Key in element " + std::to_string(i) +
|
||||
" was not a string.");
|
||||
}
|
||||
const auto current_name =
|
||||
std::string_view(key.via.str.ptr, key.via.str.size);
|
||||
if (_name == current_name) {
|
||||
return _obj.ptr[i].val;
|
||||
}
|
||||
}
|
||||
return Error("No field named '" + _name + "' was found.");
|
||||
}
|
||||
const std::string& _name, const InputObjectType& _obj) const noexcept ;
|
||||
|
||||
bool is_empty(const InputVarType& _var) const noexcept {
|
||||
return _var.type == MSGPACK_OBJECT_NIL;
|
||||
}
|
||||
bool is_empty(const InputVarType& _var) const noexcept;
|
||||
|
||||
template <class T>
|
||||
rfl::Result<T> to_basic_type(const InputVarType& _var) const noexcept {
|
||||
@@ -96,20 +75,10 @@ struct Reader {
|
||||
}
|
||||
|
||||
rfl::Result<InputArrayType> to_array(
|
||||
const InputVarType& _var) const noexcept {
|
||||
if (_var.type != MSGPACK_OBJECT_ARRAY) {
|
||||
return Error("Could not cast to an array.");
|
||||
}
|
||||
return _var.via.array;
|
||||
}
|
||||
const InputVarType& _var) const noexcept;
|
||||
|
||||
rfl::Result<InputObjectType> to_object(
|
||||
const InputVarType& _var) const noexcept {
|
||||
if (_var.type != MSGPACK_OBJECT_MAP) {
|
||||
return Error("Could not cast to a map.");
|
||||
}
|
||||
return _var.via.map;
|
||||
}
|
||||
const InputVarType& _var) const noexcept;
|
||||
|
||||
template <class ArrayReader>
|
||||
std::optional<Error> read_array(const ArrayReader& _array_reader,
|
||||
@@ -153,4 +122,4 @@ struct Reader {
|
||||
} // namespace msgpack
|
||||
} // namespace rfl
|
||||
|
||||
#endif // JSON_PARSER_HPP_
|
||||
#endif
|
||||
|
||||
@@ -32,53 +32,32 @@ class Writer {
|
||||
using OutputObjectType = MsgpackOutputObject;
|
||||
using OutputVarType = MsgpackOutputVar;
|
||||
|
||||
Writer(msgpack_packer* _pk) : pk_(_pk) {}
|
||||
Writer(msgpack_packer* _pk);
|
||||
|
||||
~Writer() = default;
|
||||
~Writer();
|
||||
|
||||
OutputArrayType array_as_root(const size_t _size) const noexcept {
|
||||
return new_array(_size);
|
||||
}
|
||||
OutputArrayType array_as_root(const size_t _size) const noexcept;
|
||||
|
||||
OutputObjectType object_as_root(const size_t _size) const noexcept {
|
||||
return new_object(_size);
|
||||
}
|
||||
OutputObjectType object_as_root(const size_t _size) const noexcept;
|
||||
|
||||
OutputVarType null_as_root() const noexcept {
|
||||
msgpack_pack_nil(pk_);
|
||||
return OutputVarType{};
|
||||
}
|
||||
OutputVarType null_as_root() const noexcept;
|
||||
|
||||
template <class T>
|
||||
OutputVarType value_as_root(const T& _var) const noexcept {
|
||||
return new_value(_var);
|
||||
}
|
||||
OutputVarType value_as_root(const T& _var) const noexcept;
|
||||
|
||||
OutputArrayType add_array_to_array(const size_t _size,
|
||||
OutputArrayType* _parent) const noexcept {
|
||||
return new_array(_size);
|
||||
}
|
||||
OutputArrayType* _parent) const noexcept;
|
||||
|
||||
OutputArrayType add_array_to_object(
|
||||
const std::string_view& _name, const size_t _size,
|
||||
OutputObjectType* _parent) const noexcept {
|
||||
msgpack_pack_str(pk_, _name.size());
|
||||
msgpack_pack_str_body(pk_, _name.data(), _name.size());
|
||||
return new_array(_size);
|
||||
}
|
||||
OutputObjectType* _parent) const noexcept;
|
||||
|
||||
OutputObjectType add_object_to_array(
|
||||
const size_t _size, OutputArrayType* _parent) const noexcept {
|
||||
return new_object(_size);
|
||||
}
|
||||
const size_t _size, OutputArrayType* _parent) const noexcept;
|
||||
|
||||
OutputObjectType add_object_to_object(
|
||||
const std::string_view& _name, const size_t _size,
|
||||
OutputObjectType* _parent) const noexcept {
|
||||
msgpack_pack_str(pk_, _name.size());
|
||||
msgpack_pack_str_body(pk_, _name.data(), _name.size());
|
||||
return new_object(_size);
|
||||
}
|
||||
OutputObjectType* _parent) const noexcept;
|
||||
|
||||
template <class T>
|
||||
OutputVarType add_value_to_array(const T& _var,
|
||||
@@ -95,33 +74,19 @@ class Writer {
|
||||
return new_value(_var);
|
||||
}
|
||||
|
||||
OutputVarType add_null_to_array(OutputArrayType* _parent) const noexcept {
|
||||
msgpack_pack_nil(pk_);
|
||||
return OutputVarType{};
|
||||
}
|
||||
OutputVarType add_null_to_array(OutputArrayType* _parent) const noexcept;
|
||||
|
||||
OutputVarType add_null_to_object(const std::string_view& _name,
|
||||
OutputObjectType* _parent) const noexcept {
|
||||
msgpack_pack_str(pk_, _name.size());
|
||||
msgpack_pack_str_body(pk_, _name.data(), _name.size());
|
||||
msgpack_pack_nil(pk_);
|
||||
return OutputVarType{};
|
||||
}
|
||||
OutputObjectType* _parent) const noexcept;
|
||||
|
||||
void end_array(OutputArrayType* _arr) const noexcept {}
|
||||
void end_array(OutputArrayType* _arr) const noexcept;
|
||||
|
||||
void end_object(OutputObjectType* _obj) const noexcept {}
|
||||
void end_object(OutputObjectType* _obj) const noexcept;
|
||||
|
||||
private:
|
||||
OutputArrayType new_array(const size_t _size) const noexcept {
|
||||
msgpack_pack_array(pk_, _size);
|
||||
return OutputArrayType{};
|
||||
}
|
||||
OutputArrayType new_array(const size_t _size) const noexcept;
|
||||
|
||||
OutputObjectType new_object(const size_t _size) const noexcept {
|
||||
msgpack_pack_map(pk_, _size);
|
||||
return OutputObjectType{};
|
||||
}
|
||||
OutputObjectType new_object(const size_t _size) const noexcept;
|
||||
|
||||
template <class T>
|
||||
OutputVarType new_value(const T& _var) const noexcept {
|
||||
@@ -155,4 +120,4 @@ class Writer {
|
||||
|
||||
} // namespace rfl::msgpack
|
||||
|
||||
#endif // MSGPACK_PARSER_HPP_
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user