DOOM-3-BFG

DOOM 3 BFG Edition
Log | Files | Refs

Array.h (4046B)


      1 /*
      2 ===========================================================================
      3 
      4 Doom 3 BFG Edition GPL Source Code
      5 Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company. 
      6 
      7 This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code").  
      8 
      9 Doom 3 BFG Edition Source Code is free software: you can redistribute it and/or modify
     10 it under the terms of the GNU General Public License as published by
     11 the Free Software Foundation, either version 3 of the License, or
     12 (at your option) any later version.
     13 
     14 Doom 3 BFG Edition Source Code is distributed in the hope that it will be useful,
     15 but WITHOUT ANY WARRANTY; without even the implied warranty of
     16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     17 GNU General Public License for more details.
     18 
     19 You should have received a copy of the GNU General Public License
     20 along with Doom 3 BFG Edition Source Code.  If not, see <http://www.gnu.org/licenses/>.
     21 
     22 In addition, the Doom 3 BFG Edition Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 BFG Edition Source Code.  If not, please request a copy in writing from id Software at the address below.
     23 
     24 If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
     25 
     26 ===========================================================================
     27 */
     28 #ifndef __ARRAY_H__
     29 #define __ARRAY_H__
     30 
     31 /*
     32 ================================================
     33 idArray is a replacement for a normal C array.
     34 
     35 int		myArray[ARRAY_SIZE];
     36 
     37 becomes:
     38 
     39 idArray<int,ARRAY_SIZE>	myArray;
     40 
     41 Has no performance overhead in release builds, but
     42 does index range checking in debug builds.
     43 
     44 Unlike idTempArray, the memory is allocated inline with the
     45 object, rather than on the heap.
     46 
     47 Unlike idStaticList, there are no fields other than the
     48 actual raw data, and the size is fixed.
     49 ================================================
     50 */
     51 template<class T_, int numElements > class idArray {
     52 public:
     53 	// returns number of elements in list
     54 	int				Num() const { return numElements; }
     55 
     56 	// returns the number of bytes the array takes up
     57 	int				ByteSize() const { return sizeof( ptr ); }
     58 
     59 	// memset the entire array to zero
     60 	void			Zero() { memset( ptr, 0, sizeof( ptr ) ); }
     61 
     62 	// memset the entire array to a specific value
     63 	void			Memset( const char fill ) { memset( ptr, fill, numElements * sizeof( *ptr ) ); }
     64 
     65 	// array operators
     66 	const T_ &		operator[]( int index ) const { assert( (unsigned)index < (unsigned)numElements ); return ptr[index]; }
     67 	T_ &			operator[]( int index ) { assert( (unsigned)index < (unsigned)numElements ); return ptr[index]; }
     68 
     69 	// returns a pointer to the list
     70 	const T_ *		Ptr() const { return ptr; }
     71 	T_ *			Ptr() { return ptr; }
     72 
     73 private:
     74 	T_				ptr[numElements];
     75 };
     76 
     77 #define ARRAY_COUNT( arrayName ) ( sizeof( arrayName )/sizeof( arrayName[0] ) )
     78 #define ARRAY_DEF( arrayName ) arrayName, ARRAY_COUNT( arrayName )
     79 
     80 
     81 /*
     82 ================================================
     83 id2DArray is essentially a typedef (as close as we can
     84 get for templates before C++11 anyway) to make
     85 declaring two-dimensional idArrays easier.
     86 
     87 Usage:
     88 	id2DArray< int, 5, 10 >::type someArray;
     89 
     90 ================================================
     91 */
     92 template<class _type_, int _dim1_, int _dim2_ >
     93 struct id2DArray {
     94 	typedef idArray< idArray< _type_, _dim2_ >, _dim1_ > type;
     95 };
     96 
     97 
     98 /*
     99 ================================================
    100 idTupleSize
    101 Generic way to get the size of a tuple-like type.
    102 Add specializations as needed.
    103 This is modeled after std::tuple_size from C++11,
    104 which works for std::arrays also.
    105 ================================================
    106 */
    107 template< class _type_ >
    108 struct idTupleSize;
    109 
    110 template< class _type_, int _num_ >
    111 struct idTupleSize< idArray< _type_, _num_ > > {
    112 	enum { value = _num_ };
    113 };
    114 
    115 #endif // !__ARRAY_H__