diff --git a/include/snipplib/utils/snplib_keyvalue.h b/include/snipplib/utils/snplib_keyvalue.h new file mode 100644 index 0000000..fed9b8c --- /dev/null +++ b/include/snipplib/utils/snplib_keyvalue.h @@ -0,0 +1,80 @@ +#pragma once + +#include "../concepts/snplib_concepts.h" +#include "../serialization/snplib_serialization.h" + +namespace snplib +{ + +template +concept snplib_serializable_c = + requires { [](std::type_identity) {}(std::type_identity>{}); }; + +template +class snplib_keyvalue_t +{ +public: + static constexpr bool mutableContent = snplib_output_char_range_c; + + typedef std::error_code error_t; + + static constexpr std::string_view DEFAULT_KEYVALUE_DELIM_SEQ{" "}; + static constexpr std::string_view DEFAULT_VALSEQ_DELIM_SEQ{" "}; + + struct config_t { + std::string_view keyval_delim{DEFAULT_KEYVALUE_DELIM_SEQ}; + std::string_view valseq_delim{DEFAULT_VALSEQ_DELIM_SEQ}; + }; + + constexpr snplib_keyvalue_t(ByteSeqT&& bytes, config_t cfg = config_t{}) {} + + virtual ~snplib_keyvalue_t() = default; + + template + requires mutableContent + error_t fromByteSequence(R&& r) + { + } + + template + constexpr R key() const + { + return snplib_transform_char_range(_key); + } + + constexpr std::string_view key() const + { + return key(); + } + + template + constexpr R value() const + { + return snplib_transform_char_range(_value); + } + + constexpr std::string_view value() const + { + return value(); + } + + + template + requires mutableContent + error_t setKey(KeyT&& key, SParT const& sparams = snplib_serialization_params_t{}) + { + auto err = snplib_serializer_t>(_key, std::forward(key), sparams); + if (err) { + return snplib_deduced_error(err, std::errc::invalid_argument); + } + + return {}; + } + +protected: + std::conditional_t _key; + std::conditional_t _value; + std::conditional_t, std::vector> _valueSeq; +}; + +} // namespace snplib \ No newline at end of file diff --git a/include/snipplib/utils/snplib_string.h b/include/snipplib/utils/snplib_string.h index 78ae587..4063878 100644 --- a/include/snipplib/utils/snplib_string.h +++ b/include/snipplib/utils/snplib_string.h @@ -81,7 +81,7 @@ constexpr static std::string_view snplib_trim_spaces_as_view(R&& r, template -bool snplib_char_ranges_compare(R1&& what, R2&& where, bool case_insensitive = false) +static constexpr bool snplib_char_ranges_compare(R1&& what, R2&& where, bool case_insensitive = false) { auto sz1 = std::ranges::distance(what.begin(), what.end()), sz2 = std::ranges::distance(where.begin(), where.end()); @@ -105,6 +105,22 @@ bool snplib_char_ranges_compare(R1&& what, R2&& where, bool case_insensitive = f } +template +static constexpr OR snplib_transform_char_range(IR&& r, CTorTs&&... or_ctor_args) +{ + using val_t = std::remove_cvref_t; + + if constexpr (std::same_as) { // just a copy + return r; + } else if constexpr (snplib_char_view_c && std::ranges::viewable_range) { + return OR{r.begin(), r.end()}; + } else if (snplib_output_char_range_c) { + OR res(std::forward(or_ctor_args)...); + std::ranges::copy(std::forward(r), std::back_inserter(res)); + } else { + static_assert(false, "Incompatible types!"); + } +} template static std::optional snplib_num_from_range(R&& r)