1 #ifndef _DEDEFS_HPP
2 #define _DEDEFS_HPP
3 /*-------------------------------------------------------------------------
4 * drawElements C++ Base Library
5 * -----------------------------
6 *
7 * Copyright 2014 The Android Open Source Project
8 *
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 *
21 *//*!
22 * \file
23 * \brief Basic definitions.
24 *//*--------------------------------------------------------------------*/
25
26 #include "deDefs.h"
27
28 #if !defined(__cplusplus)
29 # error "C++ is required"
30 #endif
31
32 namespace de
33 {
34
35 //! Compute absolute value of x.
abs(T x)36 template<typename T> inline T abs (T x) { return x < T(0) ? -x : x; }
37
38 //! Get minimum of x and y.
min(T x,T y)39 template<typename T> inline T min (T x, T y) { return x <= y ? x : y; }
40
41 //! Get maximum of x and y.
max(T x,T y)42 template<typename T> inline T max (T x, T y) { return x >= y ? x : y; }
43
44 //! Clamp x in range a <= x <= b.
clamp(T x,T a,T b)45 template<typename T> inline T clamp (T x, T a, T b) { DE_ASSERT(a <= b); return x < a ? a : (x > b ? b : x); }
46
47 //! Test if x is in bounds a <= x < b.
inBounds(T x,T a,T b)48 template<typename T> inline bool inBounds (T x, T a, T b) { return a <= x && x < b; }
49
50 //! Test if x is in range a <= x <= b.
inRange(T x,T a,T b)51 template<typename T> inline bool inRange (T x, T a, T b) { return a <= x && x <= b; }
52
53 //! Return T with low n bits set
rightSetMask(T n)54 template<typename T> inline T rightSetMask (T n) { DE_ASSERT(n < T(sizeof(T) * 8)); T one = T(1); return T((one << n) - one); }
55
56 //! Return T with low n bits reset
rightZeroMask(T n)57 template<typename T> inline T rightZeroMask (T n) { return T(~rightSetMask(n)); }
58
59 //! Return T with high n bits set
leftSetMask(T n)60 template<typename T> inline T leftSetMask (T n) { const T tlen = T(sizeof(T) * 8); return T(~rightSetMask(tlen >= n ? tlen - n : T(0))); }
61
62 //! Return T with high n bits reset
leftZeroMask(T n)63 template<typename T> inline T leftZeroMask (T n) { return T(~leftSetMask(n)); }
64
65 //! Helper for DE_CHECK() macros.
66 void throwRuntimeError (const char* message, const char* expr, const char* file, int line);
67
68 //! Default deleter.
69 template<typename T> struct DefaultDeleter
70 {
DefaultDeleterde::DefaultDeleter71 inline DefaultDeleter (void) {}
DefaultDeleterde::DefaultDeleter72 template<typename U> inline DefaultDeleter (const DefaultDeleter<U>&) {}
operator =de::DefaultDeleter73 template<typename U> inline DefaultDeleter<T>& operator= (const DefaultDeleter<U>&) { return *this; }
operator ()de::DefaultDeleter74 inline void operator() (T* ptr) const { delete ptr; }
75 };
76
77 //! A deleter for arrays
78 template<typename T> struct ArrayDeleter
79 {
ArrayDeleterde::ArrayDeleter80 inline ArrayDeleter (void) {}
ArrayDeleterde::ArrayDeleter81 template<typename U> inline ArrayDeleter (const ArrayDeleter<U>&) {}
operator =de::ArrayDeleter82 template<typename U> inline ArrayDeleter<T>& operator= (const ArrayDeleter<U>&) { return *this; }
operator ()de::ArrayDeleter83 inline void operator() (T* ptr) const { delete[] ptr; }
84 };
85
86 //! Get required memory alignment for type
87 template<typename T>
alignOf(void)88 size_t alignOf (void)
89 {
90 struct PaddingCheck { deUint8 b; T t; };
91 return (size_t)DE_OFFSET_OF(PaddingCheck, t);
92 }
93
94 } // de
95
96 /*--------------------------------------------------------------------*//*!
97 * \brief Throw runtime error if condition is not met.
98 * \param X Condition to check.
99 *
100 * This macro throws std::runtime_error if condition X is not met.
101 *//*--------------------------------------------------------------------*/
102 #define DE_CHECK_RUNTIME_ERR(X) do { if ((!deGetFalse() && (X)) ? DE_FALSE : DE_TRUE) ::de::throwRuntimeError(DE_NULL, #X, __FILE__, __LINE__); } while(deGetFalse())
103
104 /*--------------------------------------------------------------------*//*!
105 * \brief Throw runtime error if condition is not met.
106 * \param X Condition to check.
107 * \param MSG Additional message to include in the exception.
108 *
109 * This macro throws std::runtime_error with message MSG if condition X is
110 * not met.
111 *//*--------------------------------------------------------------------*/
112 #define DE_CHECK_RUNTIME_ERR_MSG(X, MSG) do { if ((!deGetFalse() && (X)) ? DE_FALSE : DE_TRUE) ::de::throwRuntimeError(MSG, #X, __FILE__, __LINE__); } while(deGetFalse())
113
114 //! Get array start pointer.
115 #define DE_ARRAY_BEGIN(ARR) (&(ARR)[0])
116
117 //! Get array end pointer.
118 #define DE_ARRAY_END(ARR) (DE_ARRAY_BEGIN(ARR) + DE_LENGTH_OF_ARRAY(ARR))
119
120 //! Empty C++ compilation unit silencing.
121 #if (DE_COMPILER == DE_COMPILER_MSC)
122 # define DE_EMPTY_CPP_FILE namespace { deUint8 unused; }
123 #else
124 # define DE_EMPTY_CPP_FILE
125 #endif
126
127 // Warn if type is constructed, but left unused
128 //
129 // Used in types with non-trivial ctor/dtor but with ctor-dtor pair causing no (observable)
130 // side-effects.
131 //
132 // \todo add attribute for GCC
133 #if (DE_COMPILER == DE_COMPILER_CLANG) && defined(__has_attribute)
134 # if __has_attribute(warn_unused)
135 # define DE_WARN_UNUSED_TYPE __attribute__((warn_unused))
136 # else
137 # define DE_WARN_UNUSED_TYPE
138 # endif
139 #else
140 # define DE_WARN_UNUSED_TYPE
141 #endif
142
143 #endif // _DEDEFS_HPP
144