20 #if _MSVC_LANG < 201703L
21 #error This libary requires C++17 Standard (Visual Studio 2017).
25 #if __cplusplus < 201703
26 #error This library requires C++17 Standard (GNU g++ version 8.0 or clang++ version 8.0 above)
36 template<
typename... ElementTypes>
class any;
43 static constexpr bool value =
false;
46 template<
typename... Types>
49 static constexpr bool value =
true;
58 constexpr bool is_special_type_v = is_constrained_any_v<T> || is_variant_v<T> || types::is_tuple_v<T>;
60 template<auto N,
typename TypeAny>
63 template<
typename... ElementTypes>
72 = type_count_v<element_types_t>;
92 return this->m_any.has_value();
102 this->m_any.swap(other);
105 const std::type_info&
type() const noexcept
107 return this->m_any.type();
110 template<
class ValueType,
class... Args,
111 typename = std::enable_if_t< std::is_constructible_v<ValueType, remove_cv_ref_t<Args>...>>>
115 return this->m_any.
template emplace<ValueType>(std::forward<Args>(args)...);
118 template<
class ValueType,
class U,
class... Args >
120 emplace( std::initializer_list<U> il, Args&&... args )
122 return this->m_any.
template emplace<ValueType>( il, std::forward<Args>(args)...);
128 const std::any&
std() const noexcept {
return m_any; }
137 template<typename Type,
139 any(Type&& arg):
m_any{ std::forward<Type>(arg) } { }
148 template<
typename Type,
152 this->m_any = std::forward<Type>(arg);
158 constexpr size_t Size =
sizeof...(ElementTypes);
162 types::for_workhorse<Size>([&](
auto index)
164 if(
auto pval = std::get_if<index.Index>(&v))
177 template<
typename Type,
182 if constexpr(std::is_pointer_v<remove_cv_ref_t<Type>>)
183 return std::any_cast<std::remove_pointer_t<Type>>(&this->m_any);
185 return std::any_cast<Type>(this->m_any);
188 template<
typename Type,
193 if constexpr(std::is_pointer_v<remove_cv_ref_t<Type>>)
194 return std::any_cast<std::remove_pointer_t<Type>>(&this->m_any);
196 return std::any_cast<Type>(this->m_any);
199 template<
typename Type,
203 return this->get<Type&>();
206 template<
typename Type,
210 return this->get<const Type&>();
213 template<
typename CurrentType,
typename CallbackType,
typename... ArgTypes,
217 if(
auto ptr = std::any_cast<CurrentType>(&this->m_any))
219 callback(*ptr, std::forward<ArgTypes>(args)...);
return true;
224 template<
typename CurrentType,
typename CallbackType,
typename... ArgTypes,
226 bool if_any_type(CallbackType&& callback, ArgTypes&&... args)
const
228 if(
auto ptr = std::any_cast<CurrentType>(&this->m_any))
230 callback(*ptr, std::forward<ArgTypes>(args)...);
return true;
235 template<
auto TypeIndex,
typename CallbackType,
typename... ArgTypes,
236 typename any_type =
types::any<ElementTypes...>,
237 typename element_types =
typename any_type::element_types_t,
242 if(
auto ptr = std::any_cast<CurrentType>(&this->m_any))
243 callback(*ptr, std::forward<ArgTypes>(args)...);
246 template<
auto TypeIndex,
typename CallbackType,
typename... ArgTypes,
247 typename any_type =
types::any<ElementTypes...>,
248 typename element_types =
typename any_type::element_types_t,
253 if(
auto ptr = std::any_cast<CurrentType>(&this->m_any))
254 callback(*ptr, std::forward<ArgTypes>(args)...);
260 template<
typename Type,
265 this->get<type&>() += std::forward<Type>(arg);
269 template<
typename Type,
274 this->get<type&>() -= std::forward<Type>(arg);
278 template<
typename Type,
283 this->get<type&>() *= std::forward<Type>(arg);
287 template<
typename Type,
292 this->get<type&>() /= std::forward<Type>(arg);
332 template<
typename... CallbackTypes>
335 using CallbackTypes::operator()...;
342 template<
typename Type,
typename AnyType,
typename... ArgTypes>
343 std::enable_if_t<is_any_v<remove_cv_ref_t<AnyType>>,
bool>
346 if(
auto ptr = std::any_cast<Type>(&
any))
348 this->operator()(*ptr, std::forward<ArgTypes>(args)...);
355 template<
typename Type,
typename AnyType,
typename... ArgTypes>
356 std::enable_if_t<is_constrained_any_v<remove_cv_ref_t<AnyType>>,
bool>
359 if(
auto ptr = std::any_cast<Type>(&
any.
std()))
361 this->operator()(*ptr, std::forward<ArgTypes>(args)...);
return true;
366 template<
typename AnyType,
typename... ArgTypes,
368 typename = std::enable_if_t<is_constrained_any_v<any_type>>,
370 void apply(AnyType&&
any, ArgTypes&&... args)
const;
372 template<
typename Type,
typename... Types,
typename... ArgTypes,
typename AnyType,
374 typename = std::enable_if_t<is_any_v<any_type> || is_constrained_any_v<any_type>>,
376 void apply(AnyType&&
any, ArgTypes&&... args)
const;
378 template<
typename ContainerType,
typename... ArgTypes>
379 void visit(ContainerType&& container, ArgTypes&&... args)
const
381 for(
auto& a: container) this->
template apply(a, std::forward<ArgTypes>(args)...);
384 template<
typename ContainerType,
typename... ArgTypes>
387 for(
int i = 0; i < (int)container.size(); ++i)
388 this->
template apply(container[i], i, std::forward<ArgTypes>(args)...);
391 template<
typename Type,
typename ContainerType,
typename... ArgTypes>
394 for(
auto& a: container)
395 this->
template if_any_type<Type>(a, std::forward<ArgTypes>(args)... );
398 template<
typename Type,
typename ContainerType,
typename... ArgTypes>
401 for(
int i = 0; i < (int)container.size(); ++i)
402 this->
template if_any_type<Type>(container[i], i, std::forward<ArgTypes>(args)... );
406 template<
typename... CallbackTypes>
409 template<
typename Type,
typename... Types>
412 template<
typename AnyType>
413 static std::enable_if_t<is_constrained_any_v<remove_cv_ref_t<AnyType>>, std::string>
416 if(
auto ptr = std::any_cast<Type>(&a.std()))
420 if constexpr(
sizeof...(Types) != 0)
423 return "To suppress syntax error";
427 template<
typename VariantType,
typename AnyType>
430 if(
auto ptr = std::get_if<Type>(&var))
434 if constexpr(
sizeof...(Types) != 0)
439 template<
typename AnyType,
typename VariantType>
442 if(
auto ptr = std::any_cast<Type>(&
any))
446 if constexpr(
sizeof...(Types) != 0)
452 template<
typename Type,
typename... Types>
455 template<
typename AnyType,
typename Visitors>
458 if(
auto ptr = std::any_cast<Type>(&a.std()))
462 if constexpr(
sizeof...(Types) != 0)
464 visit_any(visitors, std::forward<AnyType>(a));
468 template<
typename Visitors,
typename AnyType,
typename... ArgTypes>
471 if constexpr(types::is_type_list_v<Type>)
474 visitor.template
apply<
Type, Types...>(
any, std::forward<ArgTypes>(args)...);
477 template<
typename AnyType,
typename VariantType>
484 template<
typename Type,
typename... Types>
493 template<
typename Type,
typename... Types>
496 using any_t = std::any; any_t
any;
502 template<
typename... ElementTypes>
503 typename any<ElementTypes...>::variant_t
508 if(!this->has_value())
return var;
516 template<
typename... CallbackTypes>
517 template<
typename Type,
typename... Types,
typename... ArgTypes,
518 typename AnyType,
typename any_type,
typename,
typename arg_list_t>
521 if constexpr(is_constrained_any_v<AnyType>)
522 this->apply<Type, Types...>(
any.
std(), std::forward<ArgTypes>(args)...);
525 if constexpr(is_type_list_v<Type>)
529 if constexpr(
sizeof...(Types) != 0)
530 this->
apply<Types...>(
any, std::forward<ArgTypes>(args)...);
534 if(!this->
template if_any_type<Type>(
any, std::forward<ArgTypes>(args)...))
536 if constexpr(
sizeof...(Types) != 0)
537 this->
apply<Types...>(
any, std::forward<ArgTypes>(args)...);
542 throw std::runtime_error(
"WARNING - Any has value but not handled");
550 template<
typename... CallbackTypes>
551 template<
typename AnyType,
typename... ArgTypes,
typename any_type,
typename,
typename arg_list_t>
555 using element_types =
typename any_t::element_types_t;
560 template<
typename... ElementTypes>
566 template<
typename... ElementTypes>
572 template<
typename Visitors,
typename AnyType,
typename... ArgTypes>
573 void visit_any(Visitors&& visitors, AnyType&& a, ArgTypes&&... args)
578 apply_to_any(visitors, std::forward<AnyType>(a), std::forward<ArgTypes>(args)...);
581 template<
typename CurrentType,
typename AnyType,
typename Handler,
582 typename = std::enable_if<std::is_same_v<remove_cv_ref_t<AnyType>, std::any>>>
585 if(
auto ptr = std::any_cast<CurrentType>(&a)) handler(*ptr);
const std::any & std() const noexcept
union_type_t< element_types_t, pointer_types_t > element_pointer_types_t
std::string current_type() const noexcept
any & operator=(Type &&arg)
any & operator+=(Type &&arg)
any & operator=(any &&)=default
types::to_variant_t< element_types_t > variant_t
unique_types_t< std::add_pointer_t< ElementTypes >... > pointer_types_t
void swap(any &other) noexcept
types::index_tuple_vector_t< param_count > index_vector_t
any & operator*=(Type &&arg)
bool has_value() const noexcept
any & operator=(const std::variant< ElementTypes... > &v)
any & operator/=(Type &&arg)
constexpr auto allowed_types() const noexcept
variant_t to_variant() const noexcept
any & operator=(const any &)=default
any & operator-=(Type &&arg)
void if_any_index(CallbackType &&callback, ArgTypes &&... args)
static constexpr auto param_count
enable_if_in_list_t< ValueType, element_types_t, std::decay_t< ValueType > & > emplace(std::initializer_list< U > il, Args &&... args)
std::any & std() noexcept
enable_if_in_list_t< ValueType, element_types_t, std::decay_t< ValueType > & > emplace(Args &&... args)
bool if_any_type(CallbackType &&callback, ArgTypes &&... args)
bool if_any_index(CallbackType &&callback, ArgTypes &&... args) const
const std::type_info & type() const noexcept
unique_types_t< ElementTypes... > element_types_t
bool if_any_type(CallbackType &&callback, ArgTypes &&... args) const
constexpr decltype(auto) apply(F &&f, T(&&c_array)[N])
Type to string name conversions are defined.
hidden::enable_if_in_list_t< TestType, TypeList, ReturnType > enable_if_in_list_t
any_visitors(CallbackTypes...) -> any_visitors< CallbackTypes... >
void if_any_type(AnyType &&a, Handler &&handler)
hidden::unique_types_t< Types... > unique_types_t
hidden::union_type_t< unique_types_t< LeftList >, unique_types_t< RightList > > union_type_t
constexpr bool is_special_type_v
std::string current_type(const any< ElementTypes... > &a)
any< Type, Types... > variant_to_any(const std::variant< Type, Types... > &var)
constexpr bool is_constrained_any_v
typename hidden::st_index_tuple_vector< std::make_index_sequence< N > >::type index_tuple_vector_t
hidden::to_variant_t< Type, Types... > to_variant_t
select_nth_type_t< N, typename TypeAny::element_types_t > any_element_t
hidden::select_nth_type_t< SelectIndex, Types... > select_nth_type_t
void visit_any(Visitors &&visitors, AnyType &&a, ArgTypes &&... args)
typename hidden::st_build_arg_types< Heads, Tails >::type arg_list_t
std::any variant_to_std_any(const std::variant< Type, Types... > &var)
std::remove_cv_t< std::remove_reference_t< Type > > remove_cv_ref_t
Remove const volatile reference from Type.
std::remove_cv_t< std::remove_reference_t< std::decay_t< Type > > > decay_remove_cv_ref_t
Decay, remove const, volatile, reference.
void visit_with_index(ContainerType &&container, ArgTypes &&... args) const
void visit(ContainerType &&container, ArgTypes &&... args) const
void visit_if_any_type(ContainerType &&container, ArgTypes &&... args) const
bool report_unhandled_value
std::enable_if_t< is_constrained_any_v< remove_cv_ref_t< AnyType > >, bool > if_any_type(AnyType &&any, ArgTypes &&... args) const
returns true if successful, otherwise false
std::enable_if_t< is_any_v< remove_cv_ref_t< AnyType > >, bool > if_any_type(AnyType &&any, ArgTypes &&... args) const
returns true if successful, otherwise false
void visit_if_any_type_with_index(ContainerType &&container, ArgTypes &&... args) const
void apply(AnyType &&any, ArgTypes &&... args) const
static constexpr bool value
static void apply_to_any(Visitors &&visitor, AnyType &&any, ArgTypes &&... args)
static void any_to_variant(const AnyType &any, VariantType &var)
static void visit_any(Visitors &&visitors, AnyType &&a)
static std::enable_if_t< is_constrained_any_v< remove_cv_ref_t< AnyType > >, std::string > current_type(AnyType &&a)
static void any_to_variant(const AnyType &any, VariantType &var)
static void variant_to_any(const VariantType &var, AnyType &any)
This type is used to manipulate type list.
Type functions are implemented.
#define Tpf_GetTypeName(type_arg)
A macro that returns type_arg's string name.