5#ifndef UGMISC_MEMBER_HPP
6#define UGMISC_MEMBER_HPP
148#define UGMISC_DECL_MEMBER_ACCESS(TNAME, NAME) \
150 struct ugmisc_priv { \
153 template<class ugmisc__T, class, class...ugmisc__A> \
154 struct static_caller_as_function { \
155 static constexpr bool is_callable = false; \
158 template<class ugmisc__T, class...ugmisc__A> \
159 struct static_caller_as_function<ugmisc__T, std::void_t<decltype(ugmisc__T::NAME(std::declval<ugmisc__A>()...))>, ugmisc__A...> { \
160 static constexpr bool is_callable = true; \
162 static constexpr decltype(auto) call(ugmisc__A...args) { \
163 return ugmisc__T::NAME(std::forward<ugmisc__A>(args)...); \
167 template<class ugmisc__T, bool, class...ugmisc__A> \
168 struct static_caller_as_invoke { \
169 static constexpr bool is_callable = false; \
172 template<class ugmisc__T, class...ugmisc__A> \
173 struct static_caller_as_invoke<ugmisc__T, std::is_invocable_v<decltype((ugmisc__T::NAME)), ugmisc__A...>, ugmisc__A...> { \
174 static constexpr bool is_callable = true; \
176 static constexpr decltype(auto) call(ugmisc__A...args) { \
177 return std::invoke(ugmisc__T::NAME, std::forward<ugmisc__A>(args)...); \
181 template<class ugmisc__T, class...ugmisc__A> \
182 using static_caller = std::conditional_t< \
183 static_caller_as_function<ugmisc__T, void, ugmisc__A...>::is_callable, \
184 static_caller_as_function<ugmisc__T, void, ugmisc__A...>, \
185 static_caller_as_invoke<ugmisc__T, true, ugmisc__A...> \
190 template<class ugmisc__T, class, class...ugmisc__A> \
191 struct caller_as_method { \
192 static constexpr bool is_callable = false; \
195 template<class ugmisc__T, class...ugmisc__A> \
196 struct caller_as_method<ugmisc__T, std::void_t<decltype(std::declval<ugmisc__T>().NAME(std::declval<ugmisc__A>()...))>, ugmisc__A...> { \
197 static constexpr bool is_callable = true; \
199 static constexpr decltype(auto) call(ugmisc__T& obj, ugmisc__A...args) { \
200 return std::forward<ugmisc__T>(obj).NAME(std::forward<ugmisc__A>(args)...); \
204 template<class ugmisc__T, bool I, class...ugmisc__A> \
205 struct caller_as_invoke { \
206 static constexpr bool is_callable = false; \
209 template<class ugmisc__T, class...ugmisc__A> \
210 struct caller_as_invoke<ugmisc__T, \
211 std::is_invocable_v<decltype(std::declval<ugmisc__T>().NAME), ugmisc__A...>, \
215 static constexpr bool is_callable = true; \
216 static constexpr decltype(auto) call(ugmisc__T& obj, ugmisc__A...args) { \
217 return std::invoke(std::forward<ugmisc__T>(obj).NAME, std::forward<ugmisc__A>(args)...); \
221 template<class ugmisc__T, class...ugmisc__A> \
222 using caller = std::conditional_t< \
223 caller_as_method<ugmisc__T, void, ugmisc__A...>::is_callable, \
224 caller_as_method<ugmisc__T, void, ugmisc__A...>, \
225 caller_as_invoke<ugmisc__T, true, ugmisc__A...> \
229 template<class ugmisc__T, class=void> \
230 struct static_member { \
231 static constexpr bool has_member = false; \
234 template<class ugmisc__T> \
235 struct static_member<ugmisc__T, std::void_t<decltype(ugmisc__T::NAME)>> { \
236 static constexpr bool has_member = true; \
238 static constexpr auto& get() { \
239 return ugmisc__T::NAME; \
245 template<class ugmisc__T, class=void> \
247 static constexpr bool has_member = false; \
250 template<class ugmisc__T> \
251 struct member<ugmisc__T, std::void_t<decltype(std::declval<ugmisc__T>().NAME)>> { \
252 static constexpr bool has_member = true; \
254 static constexpr auto& get(ugmisc__T& obj) { \
258 using member_type = decltype(std::declval<std::remove_reference_t<ugmisc__T>>().NAME); \
262 template< class ugmisc__T, class=void > struct GetType { \
263 static constexpr bool has_member_type = false; \
264 using safe_type = void; \
267 template< class ugmisc__T > struct GetType< ugmisc__T, std::void_t< typename ugmisc__T::NAME > > { \
268 static constexpr bool has_member_type = true; \
269 using safe_type = typename ugmisc__T::NAME; \
270 using type = typename ugmisc__T::NAME; \
277 template<class ugmisc__T, class...ugmisc__A> static constexpr bool is_static_callable = \
278 ugmisc_priv::template static_caller<ugmisc__T, ugmisc__A...>::is_callable; \
280 template<class ugmisc__T, class...ugmisc__A> \
281 static constexpr auto static_call(ugmisc__A&&...args) \
283 ugmisc_priv::template static_caller<ugmisc__T, ugmisc__A...>::call( \
284 std::forward<ugmisc__A>(args)... \
287 return ugmisc_priv::template static_caller<ugmisc__T, ugmisc__A...>::call( \
288 std::forward<ugmisc__A>(args)... \
293 template<class ugmisc__T, class...ugmisc__A> static constexpr bool is_callable = \
294 ugmisc_priv::template caller<ugmisc__T, ugmisc__A...>::is_callable; \
296 template<class ugmisc__T, class...ugmisc__A> \
297 static constexpr auto call(ugmisc__T& obj, ugmisc__A&&...args) \
299 ugmisc_priv::template caller<ugmisc__T, ugmisc__A...>::call( \
301 std::forward<ugmisc__A>(args)... \
304 return ugmisc_priv::template caller<ugmisc__T, ugmisc__A...>::call( \
306 std::forward<ugmisc__A>(args)... \
311 template< class ugmisc__T > \
312 static constexpr bool has_static_member \
313 = ugmisc_priv::template static_member<ugmisc__T>::has_member; \
314 template< class ugmisc__T> \
315 static constexpr auto& static_get() { \
316 return ugmisc_priv::template static_member<ugmisc__T>::get(); \
320 template< class ugmisc__T > \
321 static constexpr bool has_member \
322 = ugmisc_priv::template member<ugmisc__T>::has_member; \
323 template< class ugmisc__T > \
324 using member_type = typename ugmisc_priv::member<ugmisc__T>::member_type; \
325 template< class ugmisc__T> \
326 static constexpr auto& get(ugmisc__T&& obj) { \
327 return ugmisc_priv::template member<ugmisc__T>::get(obj); \
332 template< class ugmisc__T > using type = typename ugmisc_priv::GetType<ugmisc__T>::type; \
333 template< class ugmisc__T > using safe_type = \
334 typename ugmisc_priv::GetType<ugmisc__T>::safe_type; \
335 template< class ugmisc__T > static constexpr bool has_member_type = \
336 ugmisc_priv::template GetType<ugmisc__T>::has_member_type; \
363template<
class V,
class F,
class...A>
364struct can_simple_call :
public std::false_type {};
366template<
class F,
class...A>
367struct can_simple_call< std::void_t<decltype(std::declval<F>()(std::declval<A>()...))>, F, A...> :
public std::true_type {};
369template<
class F,
class...A>
370constexpr bool can_simple_call_v = can_simple_call<void, F, A...>::value;
377template<
class F,
class...A>
378constexpr auto invoke(F&& func, A&&...args)
379-> std::invoke_result_t<F, A...>
383 return std::invoke(std::forward<F>(func), std::forward<A>(args)...);
387 if constexpr ( can_simple_call_v<F, A...> ) {
388 return std::forward<F>(func)(std::forward<A>(args)...);
390 return std::invoke(std::forward<F>(func), std::forward<A>(args)...);
397struct get_is_default_type :
public std::false_type {};
400struct get_is_default_type< default_type<T> > :
public std::true_type {};
403struct get_is_not_default_type {
405 static constexpr bool value = !get_is_default_type<T>::value;
408template<
class T>
static inline bool is_default_type =
409 get_is_default_type<T>::value;
434template<
class StaticCaller,
class Functor>
435class fallback_caller {
442 template<
class Dummy,
class...T>
443 constexpr auto forward(Dummy*, T&&...args)
const
444 ->
decltype(invoke(m_functor, std::forward<T>(args)...))
446 return invoke(m_functor, std::forward<T>(args)...);
449 template<
class Dummy,
class...T>
450 constexpr decltype(
auto) forward(Dummy, T&&...args)
const {
456 constexpr fallback_caller(Functor f) : m_functor(std::forward<Functor>(f)) {}
458 fallback_caller(fallback_caller&&) =
default;
459 fallback_caller(
const fallback_caller&) =
delete;
462 constexpr decltype(
auto) call(T&&...args)
const {
463 if constexpr ( StaticCaller::template is_matched_v<T...> ) {
464 return StaticCaller::call(std::forward<T>(args)...);
466 auto dummy = (
int*)
nullptr;
467 return forward(dummy, std::forward<T>(args)...);
472 constexpr decltype(
auto)
operator()(T&&...args)
const {
473 return call(std::forward<T>(args)...);
478template<
class Access,
class T,
class...A>
479inline constexpr bool is_static_callable_v =
480 Access::ugmisc::template is_static_callable<T, A...>;
483template<
class Access,
class T,
class...A>
484constexpr bool is_static_callable(A&&...) {
485 return is_static_callable_v<Access, T, A...>;
570template<
class Access,
class...T>
573 template<
class V,
class List,
class...Args>
575 :
public caller<V, type_list_remove_prefix<1, List>, Args...> {
578 template<
class...Args>
struct caller<void,
type_list<>, Args...> {
579 static constexpr bool is_callable =
false;
580 using safe_type = void;
583 template<
class List,
class...Args>
585 std::enable_if_t< member_::is_static_callable_v<Access, type_list_member<0, List>, Args...>>,
590 static constexpr bool is_callable =
true;
592 using safe_type = void;
603 template<
class...Args>
605 caller<void, types_list, Args...>::is_callable;
610 template<
class...Args>
619 template<
class...Args>
628 template<
class...Args>
629 using safe_matched_type =
630 typename caller<void, types_list, Args...>::safe_type;
641 template<
class...Args>
642 static constexpr decltype(
auto)
call(Args&&...args) {
646 "None of the types is callable with these argument types."
648 return Access::ugmisc::template static_call<type>(std::forward<Args>(args)...);
656 template<
class...Args>
657 constexpr decltype(
auto)
operator()(Args&&...args)
const {
658 return call(std::forward<Args>(args)...);
668 member_::fallback_caller<static_member_caller, F>
677 template<
class...Args>
684 template<class...Args>
692 template<class...Args>
698 template<class...Args>
749template<class Access>
760 struct wrapped_objects_base {
764 "A specialisation should have been used instead of this template."
767 template<
class...A>
static constexpr bool is_matched_v =
false;
768 template<
class...A>
static constexpr bool is_matched(A&&...) {
772 wrapped_objects_base() =
default;
773 wrapped_objects_base(
const wrapped_objects_base&) =
default;
776 constexpr void call(A&&...)
const {
777 static_assert(
false,
"There are no more objects to call.");
780 static constexpr bool safe_refs =
true;
783 static constexpr bool matched_here =
false;
786 template<
class T1,
class...T>
787 struct wrapped_objects_base<T1, T...> :
public wrapped_objects_base<T...>
793 static constexpr bool safe_refs =
794 std::is_lvalue_reference_v<T1> &&
795 wrapped_objects_base<T...>::safe_refs;
799 static constexpr bool matched_here =
800 Access::ugmisc::template is_callable<T1, A...>;
803 constexpr decltype(
auto) call(A&&...args)
const {
804 if constexpr ( matched_here<A...> ) {
805 return Access::ugmisc::template call(
807 std::forward<A>(args)...
810 return this->wrapped_objects_base<T...>::call(std::forward<A>(args)...);
815 constexpr wrapped_objects_base(T1& obj, T&...t)
816 : wrapped_objects_base<T...>(t...), obj_ref(obj)
819 wrapped_objects_base(wrapped_objects_base&&) =
default;
821 constexpr wrapped_objects_base(
const wrapped_objects_base& other)
822 : wrapped_objects_base<T...>((wrapped_objects_base<T...>)other),
823 obj_ref(other.obj_ref)
827 "Can't copy a member_caller::wrapped_objects if the "
828 "referenced objects aren't all lvalues."
833 static constexpr bool is_matched_v =
834 matched_here<A...> ||
835 wrapped_objects_base<T...>::template matched_here<A...>;
838 static constexpr bool is_matched(A&&...) {
839 return is_matched_v<A...>;
847 template<
class F,
class...T>
848 class wrapped_objects :
public wrapped_objects<void, T...> {
850 using wrapped_objects<void, T...>::wrapped_objects;
852 template<
class,
class...A>
struct forwarder {
853 static constexpr decltype(
auto) call(F& f, A&...args) {
860 std::void_t< decltype(member_::invoke(std::declval<F&>(), std::declval<A>()...)) >,
864 static constexpr decltype(
auto) call(F& f, A&...args) {
865 return member_::invoke(f, std::forward<A>(args)...);
870 constexpr decltype(
auto) call_default(A&&...args) {
871 return forwarder<void, A...>::call(functor, args...);
875 constexpr wrapped_objects(F f, T...t)
876 : wrapped_objects<void, T...>(t...),
877 functor(std::forward<F>(f))
881 constexpr decltype(
auto)
operator()(A&&...args) {
882 if constexpr ( wrapped_objects::template is_matched_v<A...> ) {
883 return this->call( std::forward<A>(args)... );
885 return call_default(std::forward<A>(args)...);
891 class wrapped_objects<void, T...> :
public wrapped_objects_base<T...> {
893 using wrapped_objects_base<T...>::wrapped_objects_base;
896 constexpr decltype(
auto)
operator() (A&&...args)
const {
898 this->call(std::forward<A>(args)...);
909 class default_function {
912 constexpr default_function(F f) : functor(std::forward<F>(f)) {}
913 default_function(default_function&&) =
default;
914 default_function(
const default_function&) =
delete;
917 constexpr wrapped_objects<F, T...> operator() (T&&...objs)
const {
918 return {functor, objs...};
933 static constexpr default_function<F>
fallback(F&& f) {
return f; }
939 static constexpr wrapped_objects<void, T...>
with(T&&...objs) {
944 constexpr auto operator() (T&&...objs)
const {
return with<T...>(objs...); }
1049template<
class Access, member_value_copy CopyValue = MEMBER_COPY_AUTO>
1058 template<
class F,
class...A>
1059 constexpr decltype(
auto) get(F&& func, A&&...args)
const {
1060 return member_::invoke(
1061 std::forward<F>(func), std::forward<A>(args)...
1065 static constexpr bool has_member =
false;
1067 constexpr void get()
const {
1070 "Can't get member that was not found in any object."
1075 constexpr decltype(
auto)
operator() (T&&...args)
const {
1076 return get(std::forward<T>(args)...);
1081 class wrapped_object_member {
1082 using object_type = T;
1085 using member_type =
typename Access::ugmisc::template member_type<T>;
1087 using effective_member_type =
1088 decltype(Access::ugmisc::get(std::declval<T&>()));
1090 using deref_effective_member_type =
1091 std::remove_reference_t<effective_member_type>;
1093 static constexpr bool rvalue_object =
1094 !std::is_lvalue_reference_v<object_type>;
1096 static constexpr bool const_object =
1097 std::is_const_v<object_type>;
1099 static constexpr bool volatile_object =
1100 std::is_volatile_v<object_type>;
1102 static constexpr bool member_is_reference =
1103 std::is_lvalue_reference_v<member_type>;
1105 static constexpr bool volatile_member =
1106 std::is_volatile_v<deref_effective_member_type>;
1108 static constexpr bool const_member =
1109 std::is_const_v<deref_effective_member_type>;
1111 static constexpr bool rvalue_member =
1112 (! member_is_reference) && rvalue_object;
1118 static constexpr bool copy_member =
1122 && (!volatile_member)
1123 && (const_member || rvalue_member)
1124 && std::is_trivially_copyable_v< member_type >
1131 static constexpr bool return_rvalue_member =
1132 (!copy_member) && rvalue_member;
1134 static constexpr bool return_lvalue_member =
1135 (!return_rvalue_member || copy_member);
1137 using member_store_type = std::conditional_t<
1139 const std::remove_volatile_t<deref_effective_member_type>,
1140 effective_member_type
1143 using member_return_type = std::conditional_t<
1145 std::remove_const_t<member_store_type>,
1147 return_rvalue_member,
1148 deref_effective_member_type&&,
1149 deref_effective_member_type&
1155 member_store_type member;
1157 static constexpr bool has_member =
true;
1159 constexpr wrapped_object_member( object_type& obj )
1160 : member( Access::ugmisc::get(obj) )
1163 wrapped_object_member(
const wrapped_object_member&) =
default;
1164 wrapped_object_member(wrapped_object_member&&) =
default;
1167 constexpr member_return_type get(A&&...args)
const {
return member; }
1170 constexpr member_return_type operator()(A&&...args)
const {
return member; }
1173 static constexpr empty find_ref() {
return {}; }
1175 template<
class T1,
class...T>
1176 static constexpr auto find_ref(T1&& obj, T&&...objs) {
1177 if constexpr ( Access::ugmisc::template has_member<T1> ) {
1178 return wrapped_object_member<T1>{ obj };
1180 return find_ref(std::forward<T>(objs)...);
1194 static constexpr auto with(T&&...objs) {
1195 return find_ref(std::forward<T>(objs)...);
1199 constexpr auto operator() (T&&...objs)
const {
return with<T...>(objs...); }
1245template<
class Access,
class TypeList >
class member_type_access_traits {
1246 using all_types = TypeList;
1247 using normal_types =
1248 filter_type_list<member_::get_is_default_type, all_types, invert_test>;
1249 using default_types =
1250 filter_type_list<member_::get_is_default_type, all_types>;
1252 static_assert( default_types::size <= 1 );
1253 static_assert( default_types::size + normal_types::size == all_types::size );
1255 using access = Access;
1257 template<
bool Found,
class U>
struct result {
1258 static constexpr bool found = Found;
1266 template<
class seq = normal_types >
1267 static constexpr auto find_type_pair() {
1269 if constexpr ( seq::size == 0 ) {
1270 return result<false, void>{};
1272 using first_type = type_list_member<0, seq>;
1273 if constexpr ( access::ugmisc::template has_member_type<first_type> ) {
1276 typename access::ugmisc::template type<first_type>
1279 return find_type_pair< type_list_remove_prefix<1, seq> >();
1284 using result_t =
decltype(find_type_pair());
1290 static constexpr auto find_default_type_pair() {
1291 if constexpr ( default_types::size ) {
1292 return result<true, type_list_member<0, default_types>>{};
1294 return result<false, void>{};
1298 using default_t =
decltype(find_default_type_pair());
1302 static constexpr bool has_member = result_t::found;
1303 static constexpr bool has_default = default_t::found;
1304 static constexpr bool has_type = has_member || has_default;
1305 using type = std::conditional_t<
1307 typename result_t::type,
1308 typename default_t::type
1322 bool HasType = member_type_access_traits<Access, TypeList>::has_type
1324struct member_type_access {
1325 static_assert( ! HasType,
"There is a missing specialisation." );
1326 static constexpr bool has_member =
false;
1327 static constexpr bool has_default =
false;
1328 static constexpr bool has_type =
false;
1330 using safe_type = void;
1334template<
class Access,
class TypeList>
1335struct member_type_access<Access, TypeList, true> {
1336 static constexpr bool has_member
1337 = member_type_access_traits<Access, TypeList>::has_member;
1339 static constexpr bool has_default
1340 = member_type_access_traits<Access, TypeList>::has_default;
1342 static constexpr bool has_type =
true;
1345 =
typename member_type_access_traits<Access, TypeList>::type;
1347 using safe_type = type;
1353template<
class Access,
class...T>
1354class static_member_value_base {
1355 template<
class U>
struct access {
1357 static constexpr bool has_member = Access::ugmisc::template has_static_member<U>;
1358 static constexpr auto& get() {
return Access::ugmisc::template static_get<U>(); }
1361 struct void_wrapper {
1365 template<
bool Found,
class Type=
void_wrapper>
1368 static constexpr bool found = Found;
1373 template<
class accessors = accessor_types>
1374 static constexpr auto find_matching_type() {
1375 if constexpr ( accessors::size == 0 ) {
1376 return result<false>{};
1378 using first_type = type_list_member<0, accessors>;
1379 if constexpr ( first_type::has_member ) {
1380 return result<true, first_type>{};
1382 return find_matching_type<type_list_remove_prefix<1, accessors>>();
1387 using result_type =
decltype(find_matching_type());
1388 using getter_type =
typename result_type::type;
1391 static constexpr bool has_member = result_type::found;
1393 template<
class F,
class...A>
1394 static constexpr decltype(
auto) get(F&& default_get, A&&...args) {
1395 if constexpr ( has_member ) {
1396 return getter_type::get();
1398 return default_get(std::forward<A>(args)...);
1402 static constexpr auto& get() {
return getter_type::get(); }
1404 using safe_matched_type =
typename getter_type::type;
1408template<
bool WithType,
class Access,
class...T>
1409struct static_member_value;
1411template<
class Access,
class...T>
1412struct static_member_value<true, Access, T...>
1413:
public static_member_value_base<Access, T...>
1415 using matched_type =
1416 typename static_member_value_base<Access, T...>::safe_matched_type;
1419template<
class Access,
class...T>
1420struct static_member_value<false, Access, T...>
1421:
public static_member_value_base<Access, T...>
1425template<
class Access,
class...T>
1426using static_member_value_t =
1427 static_member_value<
1428 static_member_value_base<Access, T...>::has_member,
1439template<
class Access,
class...T >
class member_type_access
1440:
public member_::member_type_access<Access, flatten_t<T...>>
1561template<
class Access,
class...T>
1581 template<
class F,
class...A>
1582 static constexpr decltype(
auto)
get(F&& default_get, A&&...args);
1589 static constexpr auto&
get() {
return getter_type::get(); }
@ MEMBER_COPY_NEVER
Always take a reference to the original object.
@ MEMBER_COPY_AUTO
Default. Copy value if safe and trivial to do so.
@ MEMBER_COPY_ALWAYS
Always copy the value.
static constexpr wrapped_objects< void, T... > with(T &&...objs)
static constexpr default_function< F > fallback(F &&f)
static constexpr bool has_member
static constexpr bool has_default
static constexpr bool has_type
static constexpr auto with(T &&...objs)
static constexpr member_value< Access, MEMBER_COPY_AUTO > copy_auto
static constexpr member_value< Access, MEMBER_COPY_ALWAYS > copy_always
static constexpr member_value< Access, MEMBER_COPY_NEVER > copy_never
constexpr decltype(auto) operator()(Args &&...args) const
static constexpr bool is_matched(Args &&...)
static matched_type< Args... > declval_matched(Args &&...) noexcept
static constexpr bool is_matched_v
static constexpr decltype(auto) call(Args &&...args)
typename caller< void, types_list, Args... >::type matched_type
static safe_matched_type< Args... > safe_declval_matched(Args &&...) noexcept
static constexpr member_::fallback_caller< static_member_caller, F > fallback(F &&functor)
static constexpr decltype(auto) get(F &&default_get, A &&...args)
static constexpr auto & get()
static constexpr bool has_member
Encapsulates a list of types.
Lists of types which may be used in some of the ugmisc templates where a single type would usually be...
typename flatten< T... >::type flatten_t
Converts the template parameters into a type_list.
type_list_apply_each_class< F, flatten_t< T... > > flatten_apply_each_class
typename typelist_::get_type_list_member< I, T >::type type_list_member