rawData.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifndef _RAWDATA_H_
  23. #define _RAWDATA_H_
  24. #ifndef _PLATFORM_H_
  25. # include "platform/platform.h"
  26. #endif
  27. #ifndef _TYPETRAITS_H_
  28. # include "platform/typetraits.h"
  29. #endif
  30. template< typename T >
  31. class RawDataT
  32. {
  33. public:
  34. typedef void Parent;
  35. typedef RawDataT< T > ThisType;
  36. /// Type of elements in the buffer.
  37. typedef T ValueType;
  38. /// If true, the structure own the data buffer and will
  39. /// delete[] it on destruction.
  40. bool ownMemory;
  41. /// The data buffer.
  42. T *data;
  43. /// Number of elements in the buffer.
  44. U32 size;
  45. RawDataT()
  46. : ownMemory(false), data(NULL), size(0)
  47. {
  48. }
  49. RawDataT( T* data, U32 size, bool ownMemory = false )
  50. : ownMemory( ownMemory ), data( data ), size( size ) {}
  51. RawDataT(const ThisType& rd)
  52. {
  53. data = rd.data;
  54. size = rd.size;
  55. ownMemory = false;
  56. }
  57. ~RawDataT()
  58. {
  59. reset();
  60. }
  61. void reset()
  62. {
  63. if (ownMemory)
  64. delete [] data;
  65. data = NULL;
  66. ownMemory = false;
  67. size = 0;
  68. }
  69. void alloc(const U32 newSize)
  70. {
  71. reset();
  72. ownMemory = true;
  73. size = newSize;
  74. data = new ValueType[newSize];
  75. }
  76. void operator =(const ThisType& rd)
  77. {
  78. data = rd.data;
  79. size = rd.size;
  80. ownMemory = false;
  81. }
  82. /// Allocate a RawDataT instance inline with its data elements.
  83. ///
  84. /// @param Self RawDataT instance type; this is a type parameter so this
  85. /// can work with types derived from RawDataT.
  86. template< class Self >
  87. static Self* allocInline( U32 numElements TORQUE_TMM_ARGS_DECL )
  88. {
  89. const char* file = __FILE__;
  90. U32 line = __LINE__;
  91. #ifndef TORQUE_DISABLE_MEMORY_MANAGER
  92. file = fileName;
  93. line = lineNum;
  94. #endif
  95. Self* inst = ( Self* ) dMalloc_r( sizeof( Self ) + numElements * sizeof( ValueType ), file, line );
  96. ValueType* data = ( ValueType* ) ( inst + 1 );
  97. constructArray< ValueType >( data, numElements );
  98. return constructInPlace< Self >( inst, data, numElements );
  99. }
  100. };
  101. template< typename T >
  102. struct TypeTraits< RawDataT< T >* > : public TypeTraits< typename RawDataT< T >::Parent* >
  103. {
  104. struct Construct
  105. {
  106. template< typename R >
  107. static R* single( U32 size )
  108. {
  109. typedef typename TypeTraits< R >::BaseType Type;
  110. return Type::template allocInline< Type >( size TORQUE_TMM_LOC );
  111. }
  112. };
  113. struct Destruct
  114. {
  115. template< typename R >
  116. static void single( R* ptr )
  117. {
  118. destructInPlace( ptr );
  119. dFree( ptr );
  120. }
  121. };
  122. };
  123. /// Raw byte buffer.
  124. /// This isn't a typedef to allow forward declarations.
  125. class RawData : public RawDataT< S8 >
  126. {
  127. public:
  128. typedef RawDataT< S8 > Parent;
  129. RawData() {}
  130. RawData( S8* data, U32 size, bool ownMemory = false )
  131. : Parent( data, size, ownMemory ) {}
  132. };
  133. #endif // _RAWDATA_H_