diff --git a/CMakeLists.txt b/CMakeLists.txt index 9ac8043..a165a31 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required(VERSION 3.20) include(cmake/color.cmake) -set(BLT_VERSION 5.4.6) +set(BLT_VERSION 5.4.7) set(BLT_TARGET BLT) diff --git a/include/blt/logging/logging.h b/include/blt/logging/logging.h index bc2e7a4..e9c56f8 100644 --- a/include/blt/logging/logging.h +++ b/include/blt/logging/logging.h @@ -62,7 +62,7 @@ namespace blt::logging template void handle_func(const T& t) { - m_arg_print_funcs[index] = [&t, this](std::ostream& stream, const fmt_spec_t& type) { + m_arg_print_funcs[index] = [&t](std::ostream& stream, const fmt_spec_t& type) { switch (type.sign) { case fmt_sign_t::SPACE: diff --git a/include/blt/std/variant.h b/include/blt/std/variant.h index aa56eae..c20eb3e 100644 --- a/include/blt/std/variant.h +++ b/include/blt/std/variant.h @@ -45,128 +45,50 @@ namespace blt template struct filter_void { - using type = std::conditional_t, typename filter_void::type, decltype(std::tuple_cat( + using type = std::conditional_t, typename filter_void::type, decltype(std::tuple_cat( std::declval>(), std::declval::type>()))>; }; template using filter_void_t = typename filter_void::type; - template - struct filter_invoke; - - template - struct filter_invoke - { - using type = std::tuple<>; - }; - - template - struct filter_invoke - { - using type = std::conditional_t, decltype(std::tuple_cat( - std::declval>(), std::declval::type>())), - typename filter_invoke::type>; - }; - - template - using filter_invoke_t = typename filter_invoke::type; - - template - struct member_func_meta - { - using can_invoke = std::is_invocable; - - using return_type = std::conditional_t, void>; - }; - - template - struct passthrough_value + template + struct passthrough { using type = T; - using func = Func; - constexpr static size_t index = Index; - - bool has_value; - - explicit passthrough_value(const bool has_value): has_value(has_value) - {} - - explicit operator bool() const - { - return has_value; - } }; - template - struct first_invoke_member_func + template + struct member_func_meta { - template - constexpr static auto find_func(std::index_sequence) - { - return (... || []() { - using Meta = member_func_meta; - if constexpr (Meta::can_invoke::value) - { - return passthrough_value{true}; - } - return passthrough_value{false}; - }()); - } + using can_invoke = std::is_invocable; - using result = decltype(find_func(std::index_sequence_for())); - using return_type = typename result::type; - using func_type = typename result::func; - constexpr static size_t function_index = result::index; + using return_type = typename std::conditional_t, passthrough>::type; }; - template - struct member_func_detail; + template + struct member_call_return_type; - template - struct member_func_detail, std::tuple> + template + struct member_call_return_type, Types...> { - using result_types = std::tuple...>; - using base_type = typename std::tuple_element_t<0, result_types>::return_type; + using result_types = std::tuple...>; + using non_void_result_types = typename filter_void::return_type...>::type; - template - using get_type = typename T::return_type; + static constexpr bool all_void = std::tuple_size_v == 0; + static constexpr bool some_void = std::tuple_size_v != sizeof...(Types); - template - using is_base = std::is_same; + using first_return = std::conditional_t>; - template - using is_base_or_void = std::disjunction, is_base>; - - template typename Functor, template typename PerType, size_t... Indexes> - constexpr static auto for_each_type(std::index_sequence) - { - return std::declval>...>>; - } - - constexpr static bool all_has_void = std::decay_t( - std::index_sequence_for()))>>::value; - constexpr static bool all_has_ret = std::decay_t( - std::index_sequence_for()))>>::value; - constexpr static bool all_has_ret_or_void = std::decay_t( - std::index_sequence_for()))>>::value; - - using non_void_types = typename std::decay_t( - std::index_sequence_for()))>>::type; - - template - static constexpr auto make_variant(std::index_sequence) - { - using variant = variant_t>...>; - return std::declval(); - } - - using make_return_type = std::conditional_t, std::conditional_t< - std::tuple_size_v == 0, void, decltype(make_variant( - std::make_index_sequence>{}))>>>>; + using return_type = std::conditional_t, first_return>>; }; + + template + struct visit_return_type; + + template + struct visit_return_type, std::tuple> + {}; } /* @@ -278,8 +200,7 @@ namespace blt * @param visitees user lambdas */ template - constexpr auto visit( - Visitee&&... visitees) -> typename detail::member_func_detail, std::tuple>::make_return_type + constexpr auto visit(Visitee&&... visitees) -> decltype(auto) { return std::visit(lambda_visitor{std::forward(visitees)...}, m_variant); } @@ -287,21 +208,20 @@ namespace blt template constexpr auto visit_value(Default&& default_value, Visitee&&... visitees) -> decltype(auto) { - return std::visit(lambda_visitor{ - std::forward(visitees)..., - [default_value=std::forward(default_value)](auto&& value) { - return std::forward(value); - } + return visit(std::forward(visitees)..., [default_value=std::forward(default_value)](auto&&) { + return std::forward(default_value); }); } template - constexpr auto call_member(const MemberFunc func, - Args&&... args) -> typename detail::member_func_detail< - std::tuple, std::tuple>::make_return_type + constexpr auto call_member(const MemberFunc func, Args&&... args) { - return std::visit([func,...args=std::forward(args)](auto&& value) { - return ((value).*(func))(std::forward(args)...); + using meta = detail::member_call_return_type, Types...>; + return std::visit([&](auto&& value) -> typename meta::return_type { + if constexpr (std::is_invocable_v) + return ((value).*(func))(std::forward(args)...); + else + return {}; }, m_variant); } diff --git a/libraries/parallel-hashmap b/libraries/parallel-hashmap index 93201da..7ef2e73 160000 --- a/libraries/parallel-hashmap +++ b/libraries/parallel-hashmap @@ -1 +1 @@ -Subproject commit 93201da2ba5a6aba0a6e57ada64973555629b3e3 +Subproject commit 7ef2e733416953b222851f9a360d7fc72d068ee5 diff --git a/src/blt/parse/obj_loader.cpp b/src/blt/parse/obj_loader.cpp index 1449d60..5f0a2bc 100644 --- a/src/blt/parse/obj_loader.cpp +++ b/src/blt/parse/obj_loader.cpp @@ -236,7 +236,7 @@ namespace blt::parse auto loc = vertex_map.find(face); if (loc == vertex_map.end()) { - BLT_DEBUG("DID NOT FIND FACE!"); + BLT_DEBUG("{}", "DID NOT FIND FACE!"); auto index = static_cast(vertex_data.size()); vertex_data.push_back({vertices[vi], uvs[ui], normals[ni]}); BLT_DEBUG("Vertex: ({.4f}, {.4f}, {.4f}), UV: ({.4f}, {.4f}), Normal: ({.4f}, {.4f}, {:.4f})", vertices[vi].x(), vertices[vi].y(), diff --git a/tests/variant_tests.cpp b/tests/variant_tests.cpp index 9adecae..b0c2e81 100644 --- a/tests/variant_tests.cpp +++ b/tests/variant_tests.cpp @@ -121,15 +121,35 @@ struct storing_type2 final : mutate_type float internal; }; +struct no_members +{ + int hello; +}; + int main() { - blt::variant_t v1{type1{}}; blt::variant_t v2{type2{}}; blt::variant_t v3{type3{}}; BLT_TRACE("Variants to_string():"); - BLT_TRACE("V1: {}", v1.call_member(&base_type::to_string)); - BLT_TRACE("V2: {}", v2.call_member(&base_type::to_string)); + + auto v1_result = v1.call_member(&base_type::to_string); + BLT_ASSERT_MSG(v1_result == type1{}.to_string(), ("Expected result to be " + type1{}.to_string() + " but found " + v1_result).c_str()); + BLT_ASSERT_MSG(typeid(v1_result) == typeid(std::string), "Result type expected to be string!"); + BLT_TRACE("V1: {}", v1_result); + + auto v2_result = v2.call_member(&base_type::to_string); + + BLT_TRACE("V2: {}", v2_result); BLT_TRACE("V3: {}", v3.call_member(&base_type::to_string)); + + blt::variant_t member_missing_stored_member{type1{}}; + blt::variant_t member_missing_stored_no_member{no_members{50}}; + + auto stored_member_result = member_missing_stored_member.call_member(&base_type::to_string); + auto no_member_result = member_missing_stored_no_member.call_member(&base_type::to_string); + + BLT_TRACE("Stored: has value? '{}' value: '{}'", stored_member_result.has_value(), *stored_member_result); + BLT_TRACE("No Member: {}", no_member_result.has_value()); } \ No newline at end of file