3 /*-------------------------------------------------------------------------
4 * drawElements C++ Base Library
5 * -----------------------------
7 * Copyright 2014 The Android Open Source Project
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
23 * \brief Basic definitions.
24 *//*--------------------------------------------------------------------*/
28 #if !defined(__cplusplus)
29 # error "C++ is required"
35 //!< Compute absolute value of x.
36 template<typename T> inline T abs (T x) { return x < T(0) ? -x : x; }
38 //!< Get minimum of x and y.
39 template<typename T> inline T min (T x, T y) { return x <= y ? x : y; }
41 //!< Get maximum of x and y.
42 template<typename T> inline T max (T x, T y) { return x >= y ? x : y; }
44 //!< Clamp x in range a <= x <= 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); }
47 //!< Test if x is in bounds a <= x < b.
48 template<typename T> inline bool inBounds (T x, T a, T b) { return a <= x && x < b; }
50 //!< Test if x is in range a <= x <= b.
51 template<typename T> inline bool inRange (T x, T a, T b) { return a <= x && x <= b; }
53 //! Helper for DE_CHECK() macros.
54 void throwRuntimeError (const char* message, const char* expr, const char* file, int line);
57 template<typename T> struct DefaultDeleter
59 inline DefaultDeleter (void) {}
60 template<typename U> inline DefaultDeleter (const DefaultDeleter<U>&) {}
61 template<typename U> inline DefaultDeleter<T>& operator= (const DefaultDeleter<U>&) { return *this; }
62 inline void operator() (T* ptr) const { delete ptr; }
65 //! A deleter for arrays
66 template<typename T> struct ArrayDeleter
68 inline ArrayDeleter (void) {}
69 template<typename U> inline ArrayDeleter (const ArrayDeleter<U>&) {}
70 template<typename U> inline ArrayDeleter<T>& operator= (const ArrayDeleter<U>&) { return *this; }
71 inline void operator() (T* ptr) const { delete[] ptr; }
74 template <typename T, bool Cond>
81 struct EnableIf<T, true>
86 //! Get an element of an array with a specified size.
87 template <int LastElementIndex, int Size, typename Elem>
88 const Elem& getSizedArrayElement (const Elem (&array)[Size], typename de::EnableIf<int, LastElementIndex==Size>::Type offset)
90 DE_ASSERT(inBounds(offset, 0, Size));
94 //! Get an element of an array with a compile-time constant size.
95 template <int Size, typename Elem>
96 const Elem& getArrayElement (const Elem (&array)[Size], int offset)
98 DE_ASSERT(inBounds(offset, 0, Size));
104 /*--------------------------------------------------------------------*//*!
105 * \brief Throw runtime error if condition is not met.
106 * \param X Condition to check.
108 * This macro throws std::runtime_error if condition X is not met.
109 *//*--------------------------------------------------------------------*/
110 #define DE_CHECK_RUNTIME_ERR(X) do { if ((!deGetFalse() && (X)) ? DE_FALSE : DE_TRUE) ::de::throwRuntimeError(DE_NULL, #X, __FILE__, __LINE__); } while(deGetFalse())
112 /*--------------------------------------------------------------------*//*!
113 * \brief Throw runtime error if condition is not met.
114 * \param X Condition to check.
115 * \param MSG Additional message to include in the exception.
117 * This macro throws std::runtime_error with message MSG if condition X is
119 *//*--------------------------------------------------------------------*/
120 #define DE_CHECK_RUNTIME_ERR_MSG(X, MSG) do { if ((!deGetFalse() && (X)) ? DE_FALSE : DE_TRUE) ::de::throwRuntimeError(MSG, #X, __FILE__, __LINE__); } while(deGetFalse())
122 //! Get array start pointer.
123 #define DE_ARRAY_BEGIN(ARR) (&(ARR)[0])
125 //! Get array end pointer.
126 #define DE_ARRAY_END(ARR) (DE_ARRAY_BEGIN(ARR) + DE_LENGTH_OF_ARRAY(ARR))
128 //! Empty C++ compilation unit silencing.
129 #if (DE_COMPILER == DE_COMPILER_MSC)
130 # define DE_EMPTY_CPP_FILE namespace { deUint8 unused; }
132 # define DE_EMPTY_CPP_FILE
135 #endif // _DEDEFS_HPP