rawData.h

Engine/source/core/util/rawData.h

More...

Classes:

Detailed Description

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