diff --git a/examples/hmap_example.cpp b/examples/hmap_example.cpp index 581031f..fc1dcec 100644 --- a/examples/hmap_example.cpp +++ b/examples/hmap_example.cpp @@ -162,5 +162,66 @@ int main() } } + + std::println("\n\n\n\n"); + + snplib::hmap hm; + + std::string k1{"k_int"}, k2{"k_str"}, k3("k_dbl"); + int i1 = 10; + double f1 = 7.7; + std::string s1 = "test-str"; + + std::print("push element with key '{}' and value {}\t...", k1, i1); + ok = hm.push(k1, i1, std::tuple{}); + if (!ok) { + std::println("cannot insert the value {}", i1); + } else { + std::println("OK"); + } + + std::print("push element with key '{}' and value {}\t...", k2, s1); + ok = hm.push( + k2, s1, + [](auto const& v) -> decltype(hm)::hmap_expected_t { + return v.c_str(); + // if (v) { + // return v.c_str(); + // } else { + // return std::unexpected(v.error()); + // } + }, + [](auto const& v) -> decltype(hm)::hmap_expected_t { + return std::string(v.value()); + // if (v) { + // return std::string(v.value()); + // } else { + // return std::unexpected(v.error()); + // } + }); + if (!ok) { + std::println("cannot insert the value {}", s1); + } else { + std::println("OK"); + } + + std::print("push element with key '{}' and value {}\t...", k3, f1); + ok = hm.push(k3, f1, std::tuple{}); + if (!ok) { + std::println("cannot insert the value {}", f1); + } else { + std::println("OK"); + } + + std::println("\n"); + + std::print("get '{}'-element as double: ", k1); + auto f2 = hm.get(k1); + if (f2) { + std::println("{}", f2.value()); + } else { + std::println("FAIL"); + } + return 0; } \ No newline at end of file diff --git a/include/snipplib/containers/snplib_hmap.h b/include/snipplib/containers/snplib_hmap.h index 2dbd23f..121d249 100644 --- a/include/snipplib/containers/snplib_hmap.h +++ b/include/snipplib/containers/snplib_hmap.h @@ -454,64 +454,34 @@ protected: inline static std::vector> _containsFunc{}; - // trivial conversional functors - struct trivial_conv_from_t { - hmap const* obj; - KeyT const& key; - - template - hmap_expected_t operator()(hmap_expected_t const&) - { - auto v = obj->get(key); - if (v) { - return static_cast(v.value()); - } - - return hmap_error_e::ERROR_USER_CONV; - } - }; - - struct trivial_conv_to_t { - hmap const* obj; - KeyT const& key; - - template - hmap_expected_t operator()(hmap_expected_t const& uval) - { - if (uval) { - auto err = obj->set(key, uval.value()); - if (err) { - return std::unexpected(err); - } - - // return - } - - return hmap_error_e::ERROR_USER_CONV; - } - }; - + // trivial conversional functions template requires requires(VT v, UT u) { v = u; } - inline static std::function(hmap_expected_t const&)> trivial_conv_from = - [](hmap_expected_t const& val) -> hmap_expected_t { - if (val) { - return static_cast(val.value()); - } else { - return val.error(); - } + inline static std::function(VT const&)> trivial_conv_from = + [](VT const& val) -> hmap_expected_t { + // inline static std::function(hmap_expected_t const&)> trivial_conv_from = + // [](hmap_expected_t const& val) -> hmap_expected_t { + return static_cast(val); + // if (val) { + // return static_cast(val.value()); + // } else { + // return val.error(); + // } }; template requires requires(VT v, UT u) { u = v; } - inline static std::function(hmap_expected_t const&)> trivial_conv_to = - [](hmap_expected_t const& uval) -> hmap_expected_t { - if (uval) { - return static_cast(uval.value()); - } else { - return uval.error(); - } + inline static std::function(UT const&)> trivial_conv_to = + [](UT const& uval) -> hmap_expected_t { + // inline static std::function(hmap_expected_t const&)> trivial_conv_to = + // [](hmap_expected_t const& uval) -> hmap_expected_t { + return static_cast(uval); + // if (uval) { + // return static_cast(uval.value()); + // } else { + // return uval.error(); + // } }; public: @@ -692,7 +662,7 @@ public: } auto add_cnv_func = [this](KeyT const& kk, auto&& from_cnv_func, auto&& to_cnv_func) { - using u_t = std::invoke_result_t; + using u_t = std::invoke_result_t::value_type; // user type must differ from inserted one static_assert(!std::same_as, "INVALID CONVERSIONAL 'FROM-FUNCTION' SIGNATURE!"); @@ -704,16 +674,17 @@ public: return std::forward(from_cnv_func_arg)(_values[obj][kk]); }); - _setter[this].emplace(kk, [kk, to_cnv_func_arg = std::forward(to_cnv_func)]( - const u_t& v, const hmap* obj) mutable { - auto val = std::forward(to_cnv_func_arg)(v); - if (val) { - _values[obj][kk] = val; - return hmap_error_e::ERROR_OK; - } else { - return val.error(); - } - }); + _setter[this].emplace(kk, + [kk, to_cnv_func_arg = std::forward(to_cnv_func)]( + const u_t& v, const hmap* obj) mutable -> hmap::error_t { + auto val = std::forward(to_cnv_func_arg)(v); + if (val) { + _values[obj][kk] = val.value(); + return hmap_error_e::ERROR_OK; + } else { + return val.error(); + } + }); _clearFunc.emplace_back([](hmap* obj) { _getter[obj].clear(); @@ -741,14 +712,15 @@ public: requires(sizeof...(Ts) > 0) auto push(KeyT const& key, VT&& value, std::tuple) { + using v_t = std::decay_t; using tp_t = std::tuple; - return [&key, val = std::forward(value), this](std::index_sequence) { + return [&key, val = std::forward(value), this](std::index_sequence) mutable { return std::apply([this](auto&&... args) { return push(std::forward(args)...); }, std::tuple_cat(std::forward_as_tuple(key, std::forward(val)), std::tuple_cat(std::forward_as_tuple( - hmap::trivial_conv_from>, - hmap::trivial_conv_to>)...))); + hmap::trivial_conv_from>, + hmap::trivial_conv_to>)...))); }(std::make_index_sequence{}); }