ALib C++ Library
Library Version: 2412 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
tmp.hpp
Go to the documentation of this file.
1//==================================================================================================
2/// \file
3/// This header file is part of the \aliblong. It does not belong to an \alibmod and is
4/// included in any \alibdist.
5///
6/// \emoji :copyright: 2013-2024 A-Worx GmbH, Germany.
7/// Published under \ref mainpage_license "Boost Software License".
8///
9/// \note
10/// To reduce their complexity, this header is not shown in inclusion graphs of this documentation.
11//==================================================================================================
12#ifndef HPP_ALIB_LANG_TMP
13#define HPP_ALIB_LANG_TMP 1
14#pragma once
15#if !defined(DOXYGEN)
16# include "alib/alib.hpp"
17#endif
18
19// is/eq/isof
20#define ATMP_IS_CONST( T ) std::is_const <T>::value
21#define ATMP_IS_ENUM( T ) std::is_enum <T>::value
22#define ATMP_IS_PTR( T ) std::is_pointer <T>::value
23#define ATMP_IS_ARR( T ) std::is_array <T>::value
24#define ATMP_IS_INT( T ) std::is_integral<T>::value
25#define ATMP_IS_UINT( T ) (std::is_integral<T>::value && std::is_unsigned<T>::value)
26#define ATMP_IS_SINT( T ) (std::is_integral<T>::value && std::is_signed <T>::value)
27#define ATMP_EQ( T, TEqual) std::is_same <T, TEqual>::value
28#define ATMP_ISOF( T, TBase) std::is_base_of <TBase, T >::value
29
30// remove
31#define ATMP_RC( T ) typename std::remove_const <T>::type
32#define ATMP_RR( T ) typename std::remove_reference<T>::type
33#define ATMP_RP( T ) typename std::remove_pointer <T>::type
34#define ATMP_RE( T ) typename std::remove_extent <T>::type
35#define ATMP_RCV( T ) typename std::remove_cv <T>::type
36#define ATMP_RCVR( T ) typename std::remove_cv < \
37 typename std::remove_reference<T>::type>::type
38#define ATMP_RCVP( T ) typename std::remove_cv < \
39 typename std::remove_pointer < \
40 typename std::remove_reference<T>::type>::type>::type
41#define ATMP_RECVP( T ) typename std::remove_extent < \
42 typename std::remove_cv < \
43 typename std::remove_pointer < \
44 typename std::remove_reference<T>::type>::type>::type>::type
45
46// enable_if / conditional
47#define ATMP_VOID_IF( Cond ) typename std::enable_if<Cond,void >::type
48#define ATMP_BOOL_IF( Cond ) typename std::enable_if<Cond,bool >::type
49#define ATMP_T_IF( T, Cond ) typename std::enable_if<Cond,T >::type
50#define ATMP_IF_T_F( Cond, T, F ) typename std::conditional<Cond,T,F>::type
51#define ATMP_HAS_METHOD(T,Method,...) !ATMP_EQ(lang::TMPUnknownType, decltype(std::declval<T>(). Method( __VA_ARGS__ )))
52#define ATMP_RESULT_TYPE(T,Method,...) decltype(std::declval<T>(). Method( __VA_ARGS__ ))
53
54
55// method selection
56#define ATMP_IF( Cond ) typename = typename std::enable_if<Cond,void >::type
57
58#define ATMP_SELECT_IF_1TP( TParam, ... ) \
59 template <TParam, typename std::enable_if<__VA_ARGS__ ,int>::type = 0 > \
60 ALIB_FORCE_INLINE \
61
62#define ATMP_SELECT_IF_2TP( TParam1, TParam2, ... ) \
63 template <TParam1, TParam2, typename std::enable_if<__VA_ARGS__ ,int>::type = 0 > \
64 ALIB_FORCE_INLINE \
65
66#define ATMP_SELECT_IF_3TP( TParam1, TParam2, TParam3, ... ) \
67 template <TParam1, TParam2, TParam3, typename std::enable_if<__VA_ARGS__ ,int>::type = 0 > \
68 ALIB_FORCE_INLINE \
69
70#define ATMP_SELECT_IF_4TP( TParam1, TParam2, TParam3, TParam4, ... ) \
71 template <TParam1, TParam2, \
72 TParam3, TParam4, typename std::enable_if<__VA_ARGS__ ,int>::type = 0 > \
73 ALIB_FORCE_INLINE \
74
75
76// method selection using return type.
77#define ATMP_RETURN_IF_1TP( TReturn, TParam, ... ) \
78 template <TParam> \
79 ALIB_FORCE_INLINE \
80 ATMP_T_IF( TReturn, (__VA_ARGS__)) \
81
82#define ATMP_RETURN_IF_2TP( TReturn, TParam1, TParam2, ... ) \
83 template <TParam1, TParam2> \
84 ALIB_FORCE_INLINE \
85 ATMP_T_IF( TReturn, (__VA_ARGS__)) \
86
87
88// common type tags
89
90namespace alib::lang {
91
92/// Template meta programming struct that is a type not equal to any other type.
93/// For example, used with \ref ATMP_HAS_METHOD.
95
96/// Cast function that chooses either <c>static_cast</c> or <c>dynamic_cast</c>, dependent
97/// on whether type \p{TTo} is polymorphic or not.
98/// @tparam TTo The type to cast down to.
99/// @tparam TFrom The type to cast from.
100/// @param derived A pointer to the derived type.
101/// @return A pointer to the base type.
102template <typename TTo, typename TFrom>
103TTo* SafeCast(TFrom* derived)
104{
105 // Ensure TTo is a base of TFrom
106 ALIB_STATIC_ASSERT( SafeCast_not_allowed, std::is_base_of<TTo ALIB_COMMA TFrom>::value
107 || std::is_base_of<TFrom ALIB_COMMA TTo >::value,
108 "TFrom and TTo must be related by inheritance.")
109
110 if constexpr (std::is_polymorphic<TTo>::value) return dynamic_cast<TTo*>(derived);
111 else return static_cast<TTo*>(derived);
112}
113
114
115} // namespace [alib::lang]
116
117#endif // HPP_ALIB_LANG_TMP
118
#define ALIB_STATIC_ASSERT(CondVariable, Cond, Message)
Definition alib.hpp:990
TTo * SafeCast(TFrom *derived)
Definition tmp.hpp:103