165#define UGMISC_DECL_MEMBER_ACCESS(TNAME, NAME) \
167 struct ugmisc_priv { \
170 template<class T, class, class...A> \
171 struct static_caller { \
172 static constexpr bool is_callable = false; \
175 template<class T, class...A> \
176 struct static_caller<T, std::void_t<decltype(T::NAME(std::declval<A>()...))>, A...> { \
177 static constexpr bool is_callable = true; \
179 static constexpr decltype(auto) call(A...args) { \
180 return T::NAME(std::forward<A>(args)...); \
186 template<class T, class, class...A> \
188 static constexpr bool is_callable = false; \
191 template<class T, class...A> \
192 struct caller<T, std::void_t<decltype(std::declval<T>().NAME(std::declval<A>()...))>, A...> { \
193 static constexpr bool is_callable = true; \
195 static constexpr decltype(auto) call(T& obj, A...args) { \
196 return std::forward<T>(obj).NAME(std::forward<A>(args)...); \
202 template<class T, class=void> \
203 struct static_member { \
204 static constexpr bool has_member = false; \
208 struct static_member<T, std::void_t<decltype(T::NAME)>> { \
209 static constexpr bool has_member = true; \
211 static constexpr auto& get() { \
218 template<class T, class=void> \
220 static constexpr bool has_member = false; \
224 struct member<T, std::void_t<decltype(std::declval<T>().NAME)>> { \
225 static constexpr bool has_member = true; \
227 static constexpr auto& get(T& obj) { \
231 using member_type = decltype(std::declval<std::remove_reference_t<T>>().NAME); \
235 template< class T, class=void > struct GetType { \
236 static constexpr bool has_member_type = false; \
237 using safe_type = void; \
240 template< class T > struct GetType< T, std::void_t< typename T::NAME > > { \
241 static constexpr bool has_member_type = true; \
242 using safe_type = typename T::NAME; \
243 using type = typename T::NAME; \
250 template<class T, class...A> static constexpr bool is_static_callable = \
251 ugmisc_priv::static_caller<T, void, A...>::is_callable; \
253 template<class T, class...A> \
254 static constexpr auto static_call(A&&...args) \
256 ugmisc_priv::static_caller<T, void, A...>::call( \
257 std::forward<A>(args)... \
260 return ugmisc_priv::static_caller<T, void, A...>::call( \
261 std::forward<A>(args)... \
266 template<class T, class...A> static constexpr bool is_callable = \
267 ugmisc_priv::caller<T, void, A...>::is_callable; \
269 template<class T, class...A> \
270 static constexpr auto call(T& obj, A&&...args) \
272 ugmisc_priv::caller<T, void, A...>::call( \
274 std::forward<A>(args)... \
277 return ugmisc_priv::caller<T, void, A...>::call( \
279 std::forward<A>(args)... \
284 template< class T > \
285 static constexpr bool has_static_member \
286 = ugmisc_priv::static_member<T>::has_member; \
288 static constexpr auto& static_get() { \
289 return ugmisc_priv::static_member<T>::get(); \
293 template< class T > \
294 static constexpr bool has_member \
295 = ugmisc_priv::member<T>::has_member; \
296 template< class T > \
297 using member_type = typename ugmisc_priv::member<T>::member_type; \
299 static constexpr auto& get(T&& obj) { \
300 return ugmisc_priv::member<T>::get(obj); \
305 template< class T > using type = typename ugmisc_priv::GetType<T>::type; \
306 template< class T > using safe_type = \
307 typename ugmisc_priv::GetType<T>::safe_type; \
308 template< class T > static constexpr bool has_member_type = \
309 ugmisc_priv::GetType<T>::has_member_type; \
320#define UGMISC_DECL_STATIC_MEMBER_CALLER(TNAME, NAME) \
321 UGMISC_DECL_MEMBER_ACCESS(TNAME, NAME)
330#define UGMISC_DECL_MEMBER_TYPE_ALIAS(TNAME, NAME) \
331 UGMISC_DECL_MEMBER_ACCESS(TNAME, NAME)
340#define UGMISC_DECL_STATIC_MEMBER_ACCESSOR(TNAME, NAME) \
341 UGMISC_DECL_MEMBER_ACCESS(TNAME, NAME)
369struct get_is_default_type :
public std::false_type {};
372struct get_is_default_type< default_type<T> > :
public std::true_type {};
375struct get_is_not_default_type {
377 static constexpr bool value = !get_is_default_type<T>::value;
380template<
class T>
static inline bool is_default_type =
381 get_is_default_type<T>::value;
400template<
class StaticCaller,
class Functor>
401class fallback_caller {
408 template<
class Dummy,
class...T>
409 constexpr auto forward(Dummy*, T&&...args)
const
410 ->
decltype(m_functor(std::forward<T>(args)...))
412 return m_functor(std::forward<T>(args)...);
415 template<
class Dummy,
class...T>
416 constexpr decltype(
auto) forward(Dummy, T&&...args)
const {
422 constexpr fallback_caller(
const Functor& f) : m_functor(f) {}
423 constexpr fallback_caller(Functor&& f) : m_functor(std::move(f)) {}
425 fallback_caller(fallback_caller&&) =
default;
426 fallback_caller(
const fallback_caller&) =
delete;
429 constexpr decltype(
auto) call(T&&...args)
const {
430 if constexpr ( StaticCaller::template is_matched_v<T...> ) {
431 return StaticCaller::call(std::forward<T>(args)...);
433 auto dummy = (
int*)
nullptr;
434 return forward(dummy, std::forward<T>(args)...);
439 constexpr decltype(
auto)
operator()(T&&...args)
const {
440 return call(std::forward<T>(args)...);
462#define UGMISC_NAMED_MEMBER_TYPE_TEST(TEMPLATENAME, ALIASNAME, HASMEMBERNAME, NAME) \
463 UGMISC__DECL_MEMBER_TYPEDEF_TYPE(TEMPLATENAME, NAME); \
465 template<class T, class D = void> struct TEMPLATENAME : \
466 public ::ugmisc::member_type_access<UGMISC__MEMBER_TYPEDEF_TYPE_NAME(TEMPLATENAME), T, ::ugmisc::default_type<D>> \
470 template<class T, class D = void> using ALIASNAME = \
471 typename TEMPLATENAME<T, D>::type; \
472 template<class...T> static constexpr bool HASMEMBERNAME = \
473 TEMPLATENAME<::ugmisc::type_list<T...>>::has_member
481#define UGMISC_MEMBER_TYPE_TEST(NAME) \
482 UGMISC_NAMED_MEMBER_TYPE_TEST(member_type_test_##NAME, member_type_##NAME, has_member_type_##NAME, NAME)
488#define UGMISC__MEMBER_CALLER_TYPE_NAME(TNAME) _ugmisc_caller_##TNAME
493#define UGMISC__DECL_MEMBER_CALLER_TYPE(TNAME, NAME) \
494UGMISC_DECL_STATIC_MEMBER_CALLER(UGMISC__MEMBER_CALLER_TYPE_NAME(TNAME), NAME)
497#define UGMISC__MEMBER_TYPEDEF_TYPE_NAME(TNAME) _ugmisc_typedef_##TNAME
502#define UGMISC__DECL_MEMBER_TYPEDEF_TYPE(TNAME, NAME) \
503UGMISC_DECL_MEMBER_TYPE_ALIAS(UGMISC__MEMBER_TYPEDEF_TYPE_NAME(TNAME), NAME)
512template<
class Access,
class T,
class...A>
513inline constexpr bool is_static_callable_v =
514 Access::ugmisc::template is_static_callable<T, A...>;
517template<
class Access,
class T,
class...A>
518constexpr bool is_static_callable(A&&...) {
519 return is_static_callable_v<Access, T, A...>;
592template<
class Access,
class...T>
595 template<
class V,
class List,
class...Args>
597 :
public caller<V, type_list_remove_prefix<1, List>, Args...> {
600 template<
class...Args>
struct caller<void, type_list<>, Args...> {
601 static constexpr bool is_callable =
false;
602 using safe_type = void;
605 template<
class List,
class...Args>
607 std::enable_if_t< member_::is_static_callable_v<Access, type_list_member<0, List>, Args...>>,
612 static constexpr bool is_callable =
true;
613 using type = type_list_member<0, List>;
614 using safe_type = void;
617 using types_list = flatten_t<T...>;
625 template<
class...Args>
627 caller<void, types_list, Args...>::is_callable;
632 template<
class...Args>
641 template<
class...Args>
650 template<
class...Args>
652 typename caller<void, types_list, Args...>::safe_type;
663 template<
class...Args>
664 static constexpr decltype(
auto)
call(Args&&...args) {
668 "None of the types is callable with these argument types."
670 return Access::ugmisc::template static_call<type>(std::forward<Args>(args)...);
678 template<
class...Args>
679 constexpr decltype(
auto)
operator()(Args&&...args)
const {
680 return call(std::forward<Args>(args)...);
690 member_::fallback_caller<static_member_caller, std::remove_reference_t<F>>
692 return std::forward<F>(functor);
699 template<
class...Args>
706 template<class...Args>
714 template<class...Args>
720 template<class...Args>
761template<class Access>
772 struct wrapped_objects_base {
776 "A specialisation should have been used instead of this template."
779 template<
class...A>
static constexpr bool is_matched_v =
false;
780 template<
class...A>
static constexpr bool is_matched(A&&...) {
784 wrapped_objects_base() =
default;
785 wrapped_objects_base(
const wrapped_objects_base&) =
default;
788 constexpr void call(A&&...)
const {
789 static_assert(
false,
"There are no more objects to call.");
792 static constexpr bool safe_refs =
true;
795 static constexpr bool matched_here =
false;
798 template<
class T1,
class...T>
799 struct wrapped_objects_base<T1, T...> :
public wrapped_objects_base<T...>
805 static constexpr bool safe_refs =
806 std::is_lvalue_reference_v<T1> &&
807 wrapped_objects_base<T...>::safe_refs;
811 static constexpr bool matched_here =
812 Access::ugmisc::template is_callable<T1, A...>;
815 constexpr decltype(
auto) call(A&&...args)
const {
816 if constexpr ( matched_here<A...> ) {
817 return Access::ugmisc::template call(
819 std::forward<A>(args)...
822 return this->wrapped_objects_base<T...>::call(std::forward<A>(args)...);
827 constexpr wrapped_objects_base(T1& obj, T&...t)
828 : wrapped_objects_base<T...>(t...), obj_ref(obj)
831 wrapped_objects_base(wrapped_objects_base&&) =
default;
833 constexpr wrapped_objects_base(
const wrapped_objects_base& other)
834 : wrapped_objects_base<T...>((wrapped_objects_base<T...>)other),
835 obj_ref(other.obj_ref)
839 "Can't copy a member_caller::wrapped_objects if the "
840 "referenced objects aren't all lvalues."
845 static constexpr bool is_matched_v =
846 matched_here<A...> ||
847 wrapped_objects_base<T...>::template matched_here<A...>;
850 static constexpr bool is_matched(A&&...) {
851 return is_matched_v<A...>;
859 template<
class F,
class...T>
860 class wrapped_objects :
public wrapped_objects<void, T...> {
862 using wrapped_objects<void, T...>::wrapped_objects;
864 template<
class,
class...A>
struct forwarder {
865 static constexpr decltype(
auto) call(F& f, A&...args) {
872 std::void_t< decltype(std::declval<F>()(std::declval<A>()...)) >,
876 static constexpr decltype(
auto) call(F& f, A&...args) {
877 return f(std::forward<A>(args)...);
882 constexpr decltype(
auto) call_default(A&&...args)
const {
883 return forwarder<void, A...>::call(functor, args...);
887 constexpr wrapped_objects(F& f, T...t)
888 : wrapped_objects<void, T...>(t...), functor(f)
892 constexpr decltype(
auto)
operator()(A&&...args)
const {
893 if constexpr ( wrapped_objects::template is_matched_v<A...> ) {
894 return this->call( std::forward<A>(args)... );
896 return call_default(std::forward<A>(args)...);
902 class wrapped_objects<void, T...> :
public wrapped_objects_base<T...> {
904 using wrapped_objects_base<T...>::wrapped_objects_base;
907 constexpr decltype(
auto)
operator() (A&&...args)
const {
909 this->call(std::forward<A>(args)...);
918 class default_function {
921 constexpr default_function(F& f) : functor_ref(f) {}
922 default_function(default_function&&) =
default;
923 default_function(
const default_function&) =
delete;
926 constexpr wrapped_objects<F, T...> operator() (T&&...objs)
const {
927 return {functor_ref, objs...};
942 static constexpr default_function<F>
fallback(F&& f) {
return f; }
948 static constexpr wrapped_objects<void, T...>
with(T&&...objs) {
953 constexpr auto operator() (T&&...objs)
const {
return with<T...>(objs...); }
1053template<
class Access, member_value_copy CopyValue = MEMBER_COPY_AUTO>
1062 template<
class F,
class...A>
1063 constexpr decltype(
auto) get(F&& func, A&&...args)
const {
1064 return std::forward<F>(func)(std::forward<A>(args)...);
1067 static constexpr bool has_member =
false;
1069 constexpr void get()
const {
1072 "Can't get member that was not found in any object."
1078 class wrapped_object_member {
1079 using object_type = T;
1082 using member_type =
typename Access::ugmisc::template member_type<T>;
1084 using effective_member_type =
1085 decltype(Access::ugmisc::get(std::declval<T&>()));
1087 using deref_effective_member_type =
1088 std::remove_reference_t<effective_member_type>;
1090 static constexpr bool rvalue_object =
1091 !std::is_lvalue_reference_v<object_type>;
1093 static constexpr bool const_object =
1094 std::is_const_v<object_type>;
1096 static constexpr bool volatile_object =
1097 std::is_volatile_v<object_type>;
1099 static constexpr bool member_is_reference =
1100 std::is_lvalue_reference_v<member_type>;
1102 static constexpr bool volatile_member =
1103 std::is_volatile_v<deref_effective_member_type>;
1105 static constexpr bool const_member =
1106 std::is_const_v<deref_effective_member_type>;
1108 static constexpr bool rvalue_member =
1109 (! member_is_reference) && rvalue_object;
1115 static constexpr bool copy_member =
1119 && (!volatile_member)
1120 && (const_member || rvalue_member)
1121 && std::is_trivially_copyable_v< member_type >
1128 static constexpr bool return_rvalue_member =
1129 (!copy_member) && rvalue_member;
1131 static constexpr bool return_lvalue_member =
1132 (!return_rvalue_member || copy_member);
1134 using member_store_type = std::conditional_t<
1136 const std::remove_volatile_t<deref_effective_member_type>,
1137 effective_member_type
1140 using member_return_type = std::conditional_t<
1142 std::remove_const_t<member_store_type>,
1144 return_rvalue_member,
1145 deref_effective_member_type&&,
1146 deref_effective_member_type&
1152 member_store_type member;
1154 static constexpr bool has_member =
true;
1156 constexpr wrapped_object_member( object_type& obj )
1157 : member( Access::ugmisc::get(obj) )
1160 wrapped_object_member(
const wrapped_object_member&) =
default;
1161 wrapped_object_member(wrapped_object_member&&) =
default;
1164 constexpr member_return_type get(A&&...args)
const {
return member; }
1167 static constexpr empty find_ref() {
return {}; }
1169 template<
class T1,
class...T>
1170 static constexpr auto find_ref(T1&& obj, T&&...objs) {
1171 if constexpr ( Access::ugmisc::template has_member<T1> ) {
1172 return wrapped_object_member<T1>{ obj };
1174 return find_ref(std::forward<T>(objs)...);
1188 static constexpr auto with(T&&...objs) {
1189 return find_ref(std::forward<T>(objs)...);
1193 constexpr auto operator() (T&&...objs)
const {
return with<T...>(objs...); }
1239template<
class Access,
class TypeList >
class member_type_access_traits {
1240 using all_types = TypeList;
1241 using normal_types =
1242 filter_type_list<member_::get_is_default_type, all_types, invert_test>;
1243 using default_types =
1244 filter_type_list<member_::get_is_default_type, all_types>;
1246 static_assert( default_types::size <= 1 );
1247 static_assert( default_types::size + normal_types::size == all_types::size );
1249 using access = Access;
1251 template<
bool Found,
class U>
struct result {
1252 static constexpr bool found = Found;
1260 template<
class seq = normal_types >
1261 static constexpr auto find_type_pair() {
1263 if constexpr ( seq::size == 0 ) {
1264 return result<false, void>{};
1266 using first_type = type_list_member<0, seq>;
1267 if constexpr ( access::ugmisc::template has_member_type<first_type> ) {
1270 typename access::ugmisc::template type<first_type>
1273 return find_type_pair< type_list_remove_prefix<1, seq> >();
1278 using result_t =
decltype(find_type_pair());
1284 static constexpr auto find_default_type_pair() {
1285 if constexpr ( default_types::size ) {
1286 return result<true, type_list_member<0, default_types>>{};
1288 return result<false, void>{};
1292 using default_t =
decltype(find_default_type_pair());
1296 static constexpr bool has_member = result_t::found;
1297 static constexpr bool has_default = default_t::found;
1298 static constexpr bool has_type = has_member || has_default;
1299 using type = std::conditional_t<
1301 typename result_t::type,
1302 typename default_t::type
1316 bool HasType = member_type_access_traits<Access, TypeList>::has_type
1318struct member_type_access {
1319 static_assert( ! HasType,
"There is a missing specialisation." );
1320 static constexpr bool has_member =
false;
1321 static constexpr bool has_default =
false;
1322 static constexpr bool has_type =
false;
1324 using safe_type = void;
1328template<
class Access,
class TypeList>
1329struct member_type_access<Access, TypeList, true> {
1330 static constexpr bool has_member
1331 = member_type_access_traits<Access, TypeList>::has_member;
1333 static constexpr bool has_default
1334 = member_type_access_traits<Access, TypeList>::has_default;
1336 static constexpr bool has_type =
true;
1339 =
typename member_type_access_traits<Access, TypeList>::type;
1341 using safe_type = type;
1347template<
class Access,
class...T>
1348class static_member_value_base {
1349 template<
class U>
struct access {
1351 static constexpr bool has_member = Access::ugmisc::template has_static_member<U>;
1352 static constexpr auto& get() {
return Access::ugmisc::template static_get<U>(); }
1355 struct void_wrapper {
1359 template<
bool Found,
class Type=
void_wrapper>
1362 static constexpr bool found = Found;
1367 template<
class accessors = accessor_types>
1368 static constexpr auto find_matching_type() {
1369 if constexpr ( accessors::size == 0 ) {
1370 return result<false>{};
1372 using first_type = type_list_member<0, accessors>;
1373 if constexpr ( first_type::has_member ) {
1374 return result<true, first_type>{};
1376 return find_matching_type<type_list_remove_prefix<1, accessors>>();
1381 using result_type =
decltype(find_matching_type());
1382 using getter_type =
typename result_type::type;
1385 static constexpr bool has_member = result_type::found;
1387 template<
class F,
class...A>
1388 static constexpr decltype(
auto) get(F&& default_get, A&&...args) {
1389 if constexpr ( has_member ) {
1390 return getter_type::get();
1392 return default_get(std::forward<A>(args)...);
1396 static constexpr auto& get() {
return getter_type::get(); }
1398 using safe_matched_type =
typename getter_type::type;
1402template<
bool WithType,
class Access,
class...T>
1403struct static_member_value;
1405template<
class Access,
class...T>
1406struct static_member_value<true, Access, T...>
1407:
public static_member_value_base<Access, T...>
1409 using matched_type =
1410 typename static_member_value_base<Access, T...>::safe_matched_type;
1413template<
class Access,
class...T>
1414struct static_member_value<false, Access, T...>
1415:
public static_member_value_base<Access, T...>
1419template<
class Access,
class...T>
1420using static_member_value_t =
1421 static_member_value<
1422 static_member_value_base<Access, T...>::has_member,
1433template<
class Access,
class...T >
class member_type_access
1434:
public member_::member_type_access<Access, flatten_t<T...>>
1555template<
class Access,
class...T>
1575 template<
class F,
class...A>
1576 static constexpr decltype(
auto)
get(F&& default_get, A&&...args);
1583 static constexpr auto&
get() {
return getter_type::get(); }
1622#define UGMISC_NAMED_MEMBER_STATIC_METHOD_CALL(TEMPLATENAME, NAME) \
1623 UGMISC__DECL_MEMBER_CALLER_TYPE(TEMPLATENAME, NAME); \
1624 template<class...T> class TEMPLATENAME \
1625 : public ::ugmisc::static_member_caller< \
1626 UGMISC__MEMBER_CALLER_TYPE_NAME(TEMPLATENAME), \
1630 static constexpr const char *template_name = #TEMPLATENAME; \
1631 static constexpr const char *function_name = #NAME; \
1640#define UGMISC_MEMBER_STATIC_METHOD_CALL(NAME) \
1641 UGMISC_NAMED_MEMBER_STATIC_METHOD_CALL(member_call_##NAME, NAME)
member_value_copy
Definition member.hpp:962
@ MEMBER_COPY_AUTO
Default. Copy value if safe and trivial to do so.
Definition member.hpp:963
@ MEMBER_COPY_ALWAYS
Always copy the value.
Definition member.hpp:965
@ MEMBER_COPY_NEVER
Always take a reference to the original object.
Definition member.hpp:964
Definition member.hpp:762
static constexpr wrapped_objects< void, T... > with(T &&...objs)
Definition member.hpp:948
static constexpr default_function< F > fallback(F &&f)
Definition member.hpp:942
Definition member.hpp:1474
static constexpr bool has_type
Definition member.hpp:1492
??? type
Definition member.hpp:1505
static constexpr bool has_default
Definition member.hpp:1486
static constexpr bool has_member
Definition member.hpp:1479
??? safe_type
Definition member.hpp:1511
Definition member.hpp:1054
static constexpr member_value< Access, MEMBER_COPY_ALWAYS > copy_always
Definition member.hpp:1217
static constexpr auto with(T &&...objs)
Definition member.hpp:1188
static constexpr member_value< Access, MEMBER_COPY_NEVER > copy_never
Definition member.hpp:1227
static constexpr member_value< Access, MEMBER_COPY_AUTO > copy_auto
Definition member.hpp:1207
Definition member.hpp:593
static constexpr bool is_matched(Args &&...)
Definition member.hpp:633
static constexpr member_::fallback_caller< static_member_caller, std::remove_reference_t< F > > fallback(F &&functor)
Definition member.hpp:691
typename caller< void, types_list, Args... >::safe_type safe_matched_type
Definition member.hpp:651
typename caller< void, types_list, Args... >::type matched_type
Definition member.hpp:642
static constexpr decltype(auto) call(Args &&...args)
Definition member.hpp:664
static safe_matched_type< Args... > safe_declval_matched(Args &&...) noexcept
static matched_type< Args... > declval_matched(Args &&...) noexcept
static constexpr bool is_matched_v
Definition member.hpp:626
Definition member.hpp:1557
static constexpr auto & get()
Definition member.hpp:1583
static constexpr bool has_member
Definition member.hpp:1563
static constexpr decltype(auto) get(F &&default_get, A &&...args)
??? matched_type
Definition member.hpp:1598
??? safe_matched_type
Definition member.hpp:1591
Definition member.hpp:355
Lists of types which may be used in some of the ugmisc templates where a single type would usually be...
type_list_apply_each_class< F, flatten_t< T... > > flatten_apply_each_class
Definition typelist.hpp:371