..
This commit is contained in:
80
include/snipplib/utils/snplib_keyvalue.h
Normal file
80
include/snipplib/utils/snplib_keyvalue.h
Normal file
@@ -0,0 +1,80 @@
|
||||
#pragma once
|
||||
|
||||
#include "../concepts/snplib_concepts.h"
|
||||
#include "../serialization/snplib_serialization.h"
|
||||
|
||||
namespace snplib
|
||||
{
|
||||
|
||||
template <typename T>
|
||||
concept snplib_serializable_c =
|
||||
requires { []<typename VT>(std::type_identity<VT>) {}(std::type_identity<std::remove_cvref_t<T>>{}); };
|
||||
|
||||
template <snplib_char_range_c ByteSeqT>
|
||||
class snplib_keyvalue_t
|
||||
{
|
||||
public:
|
||||
static constexpr bool mutableContent = snplib_output_char_range_c<ByteSeqT>;
|
||||
|
||||
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 <snplib_char_range_c R>
|
||||
requires mutableContent
|
||||
error_t fromByteSequence(R&& r)
|
||||
{
|
||||
}
|
||||
|
||||
template <snplib_char_range_c R>
|
||||
constexpr R key() const
|
||||
{
|
||||
return snplib_transform_char_range<R>(_key);
|
||||
}
|
||||
|
||||
constexpr std::string_view key() const
|
||||
{
|
||||
return key<std::string_view>();
|
||||
}
|
||||
|
||||
template <snplib_char_range_c R>
|
||||
constexpr R value() const
|
||||
{
|
||||
return snplib_transform_char_range<R>(_value);
|
||||
}
|
||||
|
||||
constexpr std::string_view value() const
|
||||
{
|
||||
return value<std ::string_view>();
|
||||
}
|
||||
|
||||
|
||||
template <snplib_serializable_c KeyT, snplib_serialization_params_c SParT = snplib_serialization_params_t>
|
||||
requires mutableContent
|
||||
error_t setKey(KeyT&& key, SParT const& sparams = snplib_serialization_params_t{})
|
||||
{
|
||||
auto err = snplib_serializer_t<std::remove_cvref_t<KeyT>>(_key, std::forward<KeyT>(key), sparams);
|
||||
if (err) {
|
||||
return snplib_deduced_error(err, std::errc::invalid_argument);
|
||||
}
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
protected:
|
||||
std::conditional_t<mutableContent, std::string, std::string_view> _key;
|
||||
std::conditional_t<mutableContent, std::string, std::string_view> _value;
|
||||
std::conditional_t<mutableContent, std::vector<std::string>, std::vector<std::string_view>> _valueSeq;
|
||||
};
|
||||
|
||||
} // namespace snplib
|
||||
@@ -81,7 +81,7 @@ constexpr static std::string_view snplib_trim_spaces_as_view(R&& r,
|
||||
|
||||
|
||||
template <snplib_input_char_range_c R1, snplib_input_char_range_c R2>
|
||||
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 <snplib_char_range_c OR, snplib_char_range_c IR, typename... CTorTs>
|
||||
static constexpr OR snplib_transform_char_range(IR&& r, CTorTs&&... or_ctor_args)
|
||||
{
|
||||
using val_t = std::remove_cvref_t<IR>;
|
||||
|
||||
if constexpr (std::same_as<OR, val_t>) { // just a copy
|
||||
return r;
|
||||
} else if constexpr (snplib_char_view_c<OR> && std::ranges::viewable_range<val_t>) {
|
||||
return OR{r.begin(), r.end()};
|
||||
} else if (snplib_output_char_range_c<OR>) {
|
||||
OR res(std::forward<CTorTs>(or_ctor_args)...);
|
||||
std::ranges::copy(std::forward<IR>(r), std::back_inserter(res));
|
||||
} else {
|
||||
static_assert(false, "Incompatible types!");
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T, snplib_input_char_range_c R>
|
||||
static std::optional<T> snplib_num_from_range(R&& r)
|
||||
|
||||
Reference in New Issue
Block a user