sources/basics.h

changeset 191
2e6cbacafdc7
parent 190
90bf9049e5eb
child 192
94c67ae846fc
equal deleted inserted replaced
190:90bf9049e5eb 191:2e6cbacafdc7
28 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */ 29 */
30 30
31 #pragma once 31 #pragma once
32 #include <cassert> 32 #include <cassert>
33 #include <cstdlib>
33 #include <type_traits> 34 #include <type_traits>
34 #include <stdlib.h>
35 35
36 #if !defined(_MSC_VER) && !defined(__cdecl) 36 #if !defined(_MSC_VER) && !defined(__cdecl)
37 # define __cdecl 37 # define __cdecl
38 #endif 38 #endif
39 39
72 #undef min 72 #undef min
73 #undef max 73 #undef max
74 74
75 BEGIN_ZFC_NAMESPACE 75 BEGIN_ZFC_NAMESPACE
76 76
77 template<typename T> 77 template<typename T, typename TT>
78 T min (T a, T b) 78 constexpr std::common_type_t<T, TT> min(T a, TT b)
79 { 79 {
80 return (a < b) ? a : b; 80 return (a < b) ? a : b;
81 } 81 }
82 82
83 template<typename ContainerType> 83 template<typename ContainerType>
89 result = min(result, element); 89 result = min(result, element);
90 90
91 return result; 91 return result;
92 } 92 }
93 93
94 template<typename T> 94 template<typename T, typename TT>
95 T max (T a, T b) 95 constexpr std::common_type_t<T, TT> max(T a, TT b)
96 { 96 {
97 return (a > b) ? a : b; 97 return (a > b) ? a : b;
98 } 98 }
99 99
100 template<typename ContainerType> 100 template<typename ContainerType>
107 107
108 return result; 108 return result;
109 } 109 }
110 110
111 template<typename T, typename TT, typename TTT> 111 template<typename T, typename TT, typename TTT>
112 std::common_type_t<T, TT, TTT> clamp(T a, TT b, TTT c) 112 constexpr std::common_type_t<T, TT, TTT> clamp(T a, TT b, TTT c)
113 { 113 {
114 return (a < b) ? b : (a > c) ? c : a; 114 return (a < b) ? b : (a > c) ? c : a;
115 } 115 }
116 116
117 inline const char *plural(int value) 117 inline const char *plural(int value)
118 { 118 {
119 return value != 1 ? "s" : ""; 119 return value != 1 ? "s" : "";
120 } 120 }
121 121
122 template <typename T, size_t N> 122 template<typename T, std::size_t N>
123 char (&_ArraySizeHelper(T (&array)[N]))[N]; 123 constexpr std::size_t countof(T(&)[N])
124 #define countof(array) (sizeof(_ArraySizeHelper( array ))) 124 {
125 return N;
126 }
125 127
126 struct Exitception {}; 128 struct Exitception {};
127 129
128 #ifdef __GNUC__ 130 #ifdef __GNUC__
129 # define GNUATTRIBUTE(X) __attribute__(X) 131 # define GNUATTRIBUTE(X) __attribute__(X)

mercurial