mirror of
https://codeberg.org/DawnMagnet/reflect-cpp
synced 2026-12-03 08:46:19 +08:00
Use string_view to compare the fields
This commit is contained in:
@@ -21,6 +21,11 @@ struct StringLiteral {
|
||||
|
||||
/// Returns the value as a string.
|
||||
std::string str() const { return std::string(value_); }
|
||||
|
||||
/// Returns the value as a string.
|
||||
std::string_view string_view() const {
|
||||
return std::string_view(value_, N - 1);
|
||||
}
|
||||
};
|
||||
|
||||
template <size_t N1, size_t N2>
|
||||
|
||||
@@ -103,14 +103,15 @@ struct Reader {
|
||||
}
|
||||
|
||||
std::vector<std::optional<InputVarType>> to_fields_vec(
|
||||
const std::unordered_map<std::string, size_t>& _field_indices,
|
||||
const std::unordered_map<std::string_view, size_t>& _field_indices,
|
||||
InputObjectType* _obj) const noexcept {
|
||||
std::vector<std::optional<InputVarType>> f_vec(_field_indices.size());
|
||||
yyjson_obj_iter iter;
|
||||
yyjson_obj_iter_init(_obj->val_, &iter);
|
||||
yyjson_val* key;
|
||||
while ((key = yyjson_obj_iter_next(&iter))) {
|
||||
const auto it = _field_indices.find(yyjson_get_str(key));
|
||||
const char* k = yyjson_get_str(key);
|
||||
const auto it = _field_indices.find(std::string_view(k, strlen(k)));
|
||||
if (it != _field_indices.end()) {
|
||||
f_vec[it->second] = InputVarType(yyjson_obj_iter_get_val(key));
|
||||
}
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
#include <memory>
|
||||
#include <set>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <tuple>
|
||||
#include <type_traits>
|
||||
#include <unordered_map>
|
||||
@@ -466,7 +467,7 @@ struct Parser<ReaderType, WriterType, NamedTuple<FieldTypes...>> {
|
||||
using FieldType =
|
||||
typename std::tuple_element<_i,
|
||||
std::tuple<FieldTypes...>>::type;
|
||||
const auto name = FieldType::name_.str();
|
||||
const auto name = FieldType::name_.string_view();
|
||||
Parser<ReaderType, WriterType,
|
||||
NamedTuple<FieldTypes...>>::field_indices_[name] = _i;
|
||||
set_field_indices<_i + 1>();
|
||||
@@ -475,7 +476,7 @@ struct Parser<ReaderType, WriterType, NamedTuple<FieldTypes...>> {
|
||||
|
||||
private:
|
||||
/// Maps each of the field names to an index signifying their order.
|
||||
static inline std::unordered_map<std::string, size_t> field_indices_;
|
||||
static inline std::unordered_map<std::string_view, size_t> field_indices_;
|
||||
};
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
Reference in New Issue
Block a user