13c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#ifndef _DEDEFS_HPP
23c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#define _DEDEFS_HPP
33c827367444ee418f129b2c238299f49d3264554Jarkko Poyry/*-------------------------------------------------------------------------
43c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * drawElements C++ Base Library
53c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * -----------------------------
63c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *
73c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * Copyright 2014 The Android Open Source Project
83c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *
93c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * Licensed under the Apache License, Version 2.0 (the "License");
103c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * you may not use this file except in compliance with the License.
113c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * You may obtain a copy of the License at
123c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *
133c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *      http://www.apache.org/licenses/LICENSE-2.0
143c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *
153c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * Unless required by applicable law or agreed to in writing, software
163c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * distributed under the License is distributed on an "AS IS" BASIS,
173c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
183c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * See the License for the specific language governing permissions and
193c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * limitations under the License.
203c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *
213c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *//*!
223c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * \file
233c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * \brief Basic definitions.
243c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *//*--------------------------------------------------------------------*/
253c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
263c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "deDefs.h"
273c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
283c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#if !defined(__cplusplus)
293c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#	error "C++ is required"
303c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#endif
313c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
323c827367444ee418f129b2c238299f49d3264554Jarkko Poyrynamespace de
333c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
343c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
353c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//!< Compute absolute value of x.
363c827367444ee418f129b2c238299f49d3264554Jarkko Poyrytemplate<typename T> inline T		abs			(T x)			{ return x < T(0) ? -x : x; }
373c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
383c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//!< Get minimum of x and y.
393c827367444ee418f129b2c238299f49d3264554Jarkko Poyrytemplate<typename T> inline T		min			(T x, T y)		{ return x <= y ? x : y; }
403c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
413c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//!< Get maximum of x and y.
423c827367444ee418f129b2c238299f49d3264554Jarkko Poyrytemplate<typename T> inline T		max			(T x, T y)		{ return x >= y ? x : y; }
433c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
443c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//!< Clamp x in range a <= x <= b.
453c827367444ee418f129b2c238299f49d3264554Jarkko Poyrytemplate<typename T> inline T		clamp		(T x, T a, T b)	{ DE_ASSERT(a <= b); return x < a ? a : (x > b ? b : x); }
463c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
473c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//!< Test if x is in bounds a <= x < b.
483c827367444ee418f129b2c238299f49d3264554Jarkko Poyrytemplate<typename T> inline bool	inBounds	(T x, T a, T b)	{ return a <= x && x < b; }
493c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
503c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//!< Test if x is in range a <= x <= b.
513c827367444ee418f129b2c238299f49d3264554Jarkko Poyrytemplate<typename T> inline bool	inRange		(T x, T a, T b)	{ return a <= x && x <= b; }
523c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
533c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//! Helper for DE_CHECK() macros.
543c827367444ee418f129b2c238299f49d3264554Jarkko Poyryvoid throwRuntimeError (const char* message, const char* expr, const char* file, int line);
553c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
563c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//! Default deleter.
573c827367444ee418f129b2c238299f49d3264554Jarkko Poyrytemplate<typename T> struct DefaultDeleter
583c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
593c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	inline DefaultDeleter (void) {}
603c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	template<typename U> inline DefaultDeleter (const DefaultDeleter<U>&) {}
613c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	template<typename U> inline DefaultDeleter<T>& operator= (const DefaultDeleter<U>&) { return *this; }
623c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	inline void operator() (T* ptr) const { delete ptr;	}
633c827367444ee418f129b2c238299f49d3264554Jarkko Poyry};
643c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
653c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//! A deleter for arrays
663c827367444ee418f129b2c238299f49d3264554Jarkko Poyrytemplate<typename T> struct ArrayDeleter
673c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
683c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	inline ArrayDeleter (void) {}
693c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	template<typename U> inline ArrayDeleter (const ArrayDeleter<U>&) {}
703c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	template<typename U> inline ArrayDeleter<T>& operator= (const ArrayDeleter<U>&) { return *this; }
713c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	inline void operator() (T* ptr) const { delete[] ptr; }
723c827367444ee418f129b2c238299f49d3264554Jarkko Poyry};
733c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
743c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//! Get an element of an array with a specified size.
753c827367444ee418f129b2c238299f49d3264554Jarkko Poyrytemplate <int Size, typename Elem>
763c827367444ee418f129b2c238299f49d3264554Jarkko Poyryconst Elem& getSizedArrayElement(const Elem (&array)[Size], int offset)
773c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
783c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	DE_ASSERT(inBounds(offset, 0, Size));
793c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	return array[offset];
803c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
813c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
823c827367444ee418f129b2c238299f49d3264554Jarkko Poyry} // de
833c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
843c827367444ee418f129b2c238299f49d3264554Jarkko Poyry/*--------------------------------------------------------------------*//*!
853c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * \brief Throw runtime error if condition is not met.
863c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * \param X		Condition to check.
873c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *
883c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * This macro throws std::runtime_error if condition X is not met.
893c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *//*--------------------------------------------------------------------*/
903c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#define DE_CHECK_RUNTIME_ERR(X)				do { if ((!deGetFalse() && (X)) ? DE_FALSE : DE_TRUE) ::de::throwRuntimeError(DE_NULL, #X, __FILE__, __LINE__); } while(deGetFalse())
913c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
923c827367444ee418f129b2c238299f49d3264554Jarkko Poyry/*--------------------------------------------------------------------*//*!
933c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * \brief Throw runtime error if condition is not met.
943c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * \param X		Condition to check.
953c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * \param MSG	Additional message to include in the exception.
963c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *
973c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * This macro throws std::runtime_error with message MSG if condition X is
983c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * not met.
993c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *//*--------------------------------------------------------------------*/
1003c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#define DE_CHECK_RUNTIME_ERR_MSG(X, MSG)	do { if ((!deGetFalse() && (X)) ? DE_FALSE : DE_TRUE) ::de::throwRuntimeError(MSG, #X, __FILE__, __LINE__); } while(deGetFalse())
1013c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1023c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//! Get array start pointer.
1033c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#define DE_ARRAY_BEGIN(ARR) (&(ARR)[0])
1043c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1053c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//! Get array end pointer.
1063c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#define DE_ARRAY_END(ARR)	(DE_ARRAY_BEGIN(ARR) + DE_LENGTH_OF_ARRAY(ARR))
1073c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1083c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//! Empty C++ compilation unit silencing.
1093c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#if (DE_COMPILER == DE_COMPILER_MSC)
1103c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#	define DE_EMPTY_CPP_FILE namespace { deUint8 unused; }
1113c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#else
1123c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#	define DE_EMPTY_CPP_FILE
1133c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#endif
1143c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1153c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#endif // _DEDEFS_HPP
116