HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ArraySample.h
Go to the documentation of this file.
1 //-*****************************************************************************
2 //
3 // Copyright (c) 2009-2012,
4 // Sony Pictures Imageworks, Inc. and
5 // Industrial Light & Magic, a division of Lucasfilm Entertainment Company Ltd.
6 //
7 // All rights reserved.
8 //
9 // Redistribution and use in source and binary forms, with or without
10 // modification, are permitted provided that the following conditions are
11 // met:
12 // * Redistributions of source code must retain the above copyright
13 // notice, this list of conditions and the following disclaimer.
14 // * Redistributions in binary form must reproduce the above
15 // copyright notice, this list of conditions and the following disclaimer
16 // in the documentation and/or other materials provided with the
17 // distribution.
18 // * Neither the name of Sony Pictures Imageworks, nor
19 // Industrial Light & Magic nor the names of their contributors may be used
20 // to endorse or promote products derived from this software without specific
21 // prior written permission.
22 //
23 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 //
35 //-*****************************************************************************
36 
37 #ifndef Alembic_AbcCoreAbstract_ArraySample_h
38 #define Alembic_AbcCoreAbstract_ArraySample_h
39 
40 #include <Alembic/Util/Export.h>
44 
45 namespace Alembic {
46 namespace AbcCoreAbstract {
47 namespace ALEMBIC_VERSION_NS {
48 
49 //-*****************************************************************************
50 //! The ArraySample class is a reference to a block of memory corresponding
51 //! to an array of instances of DataTypes. The array may be multi-rank, with
52 //! different sizes in each dimension, but with its data ultimately stored
53 //! contiguously. The class is basically just a bundle of a memory address,
54 //! stored as a void*, a DataType, and a Dimension.
55 //!
56 //! The ArraySample itself does not pretend to own the data referred to
57 //! memory address "data". It is just a reference. For data retention mgmt,
58 //! see the note on \ref ArraySamplePtr below.
60 {
61 public:
64  typedef Key key_type;
65 
66  //! Default constructor creates NULL bytes with degenerate dimensions.
67  //! ...
69  : m_data( NULL )
70  , m_dataType()
71  , m_dimensions() {}
72 
73  //! Explicit constructor takes bytes and dims by reference
74  //! and creates its own reference to them.
75  ArraySample( const void * iData,
76  const DataType &iDataType,
77  const Dimensions & iDims )
78  : m_data( iData )
79  , m_dataType( iDataType )
80  , m_dimensions( iDims ) {}
81 
82  //! Using Default Copy Constructor
83  //! Using Default Assignment Operator
84 
85  //! Return the data as a raw pointer
86  //! ...
87  const void* getData() const { return m_data; }
88 
89  //! Return the datatype.
90  //! ...
91  const DataType &getDataType() const { return m_dataType; }
92 
93  //! Return the dimensions
94  //! ...
95  const Dimensions& getDimensions() const { return m_dimensions; }
96 
97  //! Return the "size", which is getDimensions().numPoints()
98  //! ...
99  size_t size() const { return m_dimensions.numPoints(); }
100 
101  //! Compute the Key.
102  //! This is a calculation.
103  Key getKey() const;
104 
105  //! Return if it is valid.
106  //! An empty ArraySample is valid.
107  //! however, an ArraySample that is empty and has a scalar
108  //! dimension is invalid. This is how we discriminate between
109  //! setting a sample of length zero (useful in particle systems)
110  //! vs. indicating an invalid sample (NULL).
111  bool valid() const
112  {
113  return ( m_dataType.getPod() != kUnknownPOD ) &&
114  !( m_data == NULL && m_dimensions.rank() < 1 );
115  }
116 
117  //! Reset the array sample to an empty, invalid state.
118  //! Basically the same as calling *this = ArraySample();
119  void reset()
120  {
121  m_data = NULL;
122  m_dataType = DataType();
123  m_dimensions = Dimensions();
124  }
125 
126 private:
127  const void *m_data;
128  DataType m_dataType;
129  Dimensions m_dimensions;
130 };
131 
132 //-*****************************************************************************
133 //! The ArraySamplePtr can be used not only to share this ArraySample, but
134 //! also to manage the data referred to by the memory address in the pointer,
135 //! by way of a custom deleter. In this manner, ArraySample and ArraySamplePtr
136 //! can be used both as a reference to data and as an explicit ownership of
137 //! data. This greatly reduces the redundancy of this library's code.
138 typedef Alembic::Util::shared_ptr<ArraySample> ArraySamplePtr;
139 
140 //-*****************************************************************************
141 //! When creating an actual buffer for reading an array sample into,
142 //! we need to allocate an array of some number of bytes, and then delete
143 //! it with a special deleter. This function will return an array sample
144 //! that is managed in this way.
145 //! Dimensions tells us how many instances of the DataType to create
146 //! DataType tells us what the instance is - and this works for
147 //! pretty much every case, including std::string and std::wstring.
149 AllocateArraySample( const DataType &iDtype,
150  const Dimensions &iDims );
151 
152 //-*****************************************************************************
153 //-*****************************************************************************
154 //-*****************************************************************************
155 // HELPER STUFF!
156 //-*****************************************************************************
157 //-*****************************************************************************
158 //-*****************************************************************************
159 
160 //-*****************************************************************************
161 template <class T>
163 {
164  void operator()( void *memory ) const
165  {
166  ArraySample *arraySample = static_cast<ArraySample*>( memory );
167  if ( arraySample )
168  {
169  T *data = reinterpret_cast<T*>(
170  const_cast<void*>( arraySample->getData() ) );
171 
172  // Delete[] NULL is okay.
173  delete[] data;
174  }
175  delete arraySample;
176  }
177 };
178 
179 //-*****************************************************************************
180 template <class T>
181 ArraySamplePtr TAllocateArraySample( size_t iDataTypeExtent,
182  const Dimensions &iDims )
183 {
184  DataType dtype( PODTraitsFromType<T>::pod_enum, iDataTypeExtent );
185  size_t numPODs = iDims.numPoints() * iDataTypeExtent;
186  if ( numPODs > 0 )
187  {
188  T *data = new T[numPODs];
189  ArraySamplePtr ret(
190  new ArraySample( reinterpret_cast<const void *>( data ),
191  dtype, iDims ),
192  TArrayDeleter<T>() );
193  return ret;
194  }
195  else
196  {
197  ArraySamplePtr ret(
198  new ArraySample( ( const void * )NULL,
199  dtype, iDims ) );
200  return ret;
201  }
202 }
203 
204 } // End namespace ALEMBIC_VERSION_NS
205 
206 using namespace ALEMBIC_VERSION_NS;
207 
208 } // End namespace AbcCoreAbstract
209 } // End namespace Alembic
210 
211 #endif
GLsizei GLenum GLsizei GLsizei GLuint memory
Definition: RE_OGL.h:202
GLboolean * data
Definition: glcorearb.h:131
#define ALEMBIC_EXPORT
Definition: Export.h:51
Alembic::Util::shared_ptr< ArraySample > ArraySamplePtr
Definition: ArraySample.h:138
ArraySamplePtr TAllocateArraySample(size_t iDataTypeExtent, const Dimensions &iDims)
Definition: ArraySample.h:181
ALEMBIC_EXPORT ArraySamplePtr AllocateArraySample(const DataType &iDtype, const Dimensions &iDims)
BaseDimensions< Alembic::Util::uint64_t > Dimensions
Definition: Dimensions.h:189
Definition: format.h:895
#define ALEMBIC_VERSION_NS
Definition: Foundation.h:88
ArraySample(const void *iData, const DataType &iDataType, const Dimensions &iDims)
Definition: ArraySample.h:75