diff options
author | Claudius 'keldu' Holeksa <mail@keldu.de> | 2024-08-15 17:15:16 +0200 |
---|---|---|
committer | Claudius 'keldu' Holeksa <mail@keldu.de> | 2024-08-15 17:15:16 +0200 |
commit | e827200803c1421777c14c62a1b9edf7e139e6c0 (patch) | |
tree | 322e01b3076e6034b5e4be1cd115f9c458d5fe41 | |
parent | d8fe9dc48b640fc977cd03b483e6fd6b620785ad (diff) |
Partially fixed map
-rw-r--r-- | modules/codec/c++/data.hpp | 58 | ||||
-rw-r--r-- | modules/codec/c++/schema_meta.hpp | 12 |
2 files changed, 70 insertions, 0 deletions
diff --git a/modules/codec/c++/data.hpp b/modules/codec/c++/data.hpp index e955af0..78fef35 100644 --- a/modules/codec/c++/data.hpp +++ b/modules/codec/c++/data.hpp @@ -9,6 +9,7 @@ #include <algorithm> #include <array> #include <concepts> +#include <map> #include <variant> #include <vector> @@ -481,6 +482,47 @@ public: } }; +template<typename Key, typename Value> +class data<schema::Map<Key,Value>, encode::Native> { +public: + using Schema = schema::Map<Key,Value>; + using MetaSchema = typename meta_schema<Schema>::MetaSchema; +private: + std::map<data<Key,encode::Native>, data<Value, encode::Native>> vals_; +public: + data() = default; + + SAW_DEFAULT_COPY(data); + SAW_DEFAULT_MOVE(data); + + data(data<MetaSchema, encode::Native> init){} + + error_or<void> emplace(ref<data<Key, encode::Native>> key, ref<data<Value,encode::Native>> value){ + auto insert = vals_.emplace(std::make_pair(std::move(key()), std::move(value()))); + if(!insert.second){ + return make_error<err::already_exists>(); + } + return make_void(); + } + + error_or<void> erase(ref<data<Key,encode::Native>> key){ + auto erase = vals_.erase(key()); + if(erase == 0u){ + return make_error<err::not_found>(); + } + return make_void(); + } + + error_or<ptr<data<Value,encode::Native>>> find(ref<data<Value,encode::Native>> key){ + auto find = vals_.find(key()); + if(find == vals_.end()){ + return make_error<err::not_found>(); + } + + return ptr<data<Value,encode::Native>>{find->second}; + } +}; + template<typename T, size_t Dim> class data<schema::Array<T,Dim>, encode::Native, storage::Default> { public: @@ -766,6 +808,22 @@ public: value_.at(i) = val; } + std::string_view stl_view() { + return value_; + } + + template<typename Enc2> + bool operator==(const data<schema::String, Enc2>& rhs){ + if(size() != rhs.size()){ + return false; + } + bool eq = true; + for(uint64_t i = 0; i < size(); ++i){ + eq = eq && (get_at(i) == rhs.get_at(i)); + } + return eq; + } + /** * Check if a string_view is equal to this type. */ diff --git a/modules/codec/c++/schema_meta.hpp b/modules/codec/c++/schema_meta.hpp index a37197a..9b111e3 100644 --- a/modules/codec/c++/schema_meta.hpp +++ b/modules/codec/c++/schema_meta.hpp @@ -15,6 +15,12 @@ struct meta_schema<schema::Void> { using MetaSchema = schema::Void; }; +template<> +struct meta_schema<schema::Bool> { + using Schema = schema::Bool; + using MetaSchema = schema::Void; +}; + template<typename T, uint64_t N> struct meta_schema<schema::Primitive<T,N>> { using MetaSchema = schema::Void; @@ -27,6 +33,12 @@ struct meta_schema<schema::MixedPrecision<schema::Primitive<TA,NA>, schema::Prim using MetaSchema = schema::Void; }; +template<typename Key, typename Value> +struct meta_schema<schema::Map<Key,Value>> { + using MetaSchema = schema::Void; + using Schema = schema::Map<Key,Value>; +}; + template<typename... T, string_literal... Lit> struct meta_schema<schema::Struct<schema::Member<T,Lit>...>> { using MetaSchema = schema::Struct<schema::Member<typename meta_schema<T>::MetaSchema,Lit>...>; |