Array.h 4.0 KB

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