HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ScalarSample.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_ScalarSample_h
38 #define Alembic_AbcCoreAbstract_ScalarSample_h
39 
40 #include <Alembic/Util/Export.h>
43 
44 namespace Alembic {
45 namespace AbcCoreAbstract {
46 namespace ALEMBIC_VERSION_NS {
47 
48 //-*****************************************************************************
49 //! ScalarSample is purely a helper class for implementations.
50 //! It is not a required object of exchange within Alembic, and you wouldn't
51 //! want this to be the carrier of data with scalars because you'd be
52 //! carrying around the extra 2 bytes of "DataType" data every time you
53 //! passed data, which is wasteful and unnecessary.
54 //!
55 //! However, since the Scalar Readers and Writers will always be obligated
56 //! to compare samples to previously written samples and copy sample values,
57 //! this class will be helpful in that regard.
58 //!
59 //! Plus - and this is just a hunch - I suspect that as Alembic evolves,
60 //! there will be a need for this extra bit of encapsulation at the abstract
61 //! level, which is why I'm putting it here.
63  : public Alembic::Util::totally_ordered<ScalarSample>
64 {
65 public:
66  //-*************************************************************************
67  // Data
68  //-*************************************************************************
69  class Data
70  {
71  public:
72  virtual ~Data() {}
73 
74  virtual void setToDefault() = 0;
75  virtual void copyFrom( const void *iData ) = 0;
76  virtual bool equalTo( const void *iData ) const = 0;
77  virtual bool equalEpsilon( const void *iData,
78  double iEpsilon ) const = 0;
79  virtual bool lessThan( const void *iData ) const = 0;
80  virtual const void *getData() const = 0;
81  };
82 
83  //! Construct from given data type and data. Data will be
84  //! copied. If given data is NULL, internal data will be
85  //! set to default value.
86  explicit ScalarSample( const DataType &iDataType );
87 
88  //! Assignment to just data.
89  //! Will assume data is of the same type as described
90  //! internally by our data type.
91  //! is invalid to set to NULL here.
92  void copyFrom( const void *iData )
93  {
94  m_data->copyFrom( iData );
95  }
96 
97  //-*************************************************************************
98  // Data Access
99  //-*************************************************************************
100 
101  //! Returns the datatype.
102  //! ...
103  const DataType &getDataType() const { return m_dataType; }
104 
105  //! Returns the memory address corresponding to the data
106  //! ...
107  const void *getData() const { return m_data->getData(); }
108 
109  //-*************************************************************************
110  // Comparison and Equality
111  //-*************************************************************************
112 
113  //! Assuming the passed memory address points to data of the
114  //! same type as us, are they equal? An element-by-element
115  //! comparison is done.
116  bool operator==( const void *iRhs ) const
117  {
118  return m_data->equalTo( iRhs );
119  }
120 
121  //! Are the data types exactly equal?
122  //! This will do an element-by-element comparison.
123  bool operator==( const ScalarSample &iRhs ) const
124  {
125  return ( iRhs.getDataType() == m_dataType ) &&
126  m_data->equalTo( iRhs.getData() );
127  }
128 
129  //! Are the data types equal with some precision. This only applies
130  //! to floating point types, but will just ignore the relAbsError
131  //! for the non-floating-point types.
132  bool equalWithRelAbsError( const void *iRhs,
133  double iRelAbsError ) const
134  {
135  return m_data->equalEpsilon( iRhs, iRelAbsError );
136  }
137 
138  //! Same as precision-bound equality operator above.
139  //! ...
141  double iRelAbsError ) const
142  {
143  return ( iRhs.getDataType() == m_dataType ) &&
144  m_data->equalEpsilon( iRhs.getData(), iRelAbsError );
145  }
146 
147  //! Sorting operator. Compares element by element, with
148  //! first elements having precedence over later ones.
149  bool operator<( const void *iRhs ) const
150  {
151  return m_data->lessThan( iRhs );
152  }
153 
154  //! Sorting operator. Compares element by element, with
155  //! first elements having precedence over later ones.
156  bool operator<( const ScalarSample &iRhs ) const
157  {
158  if ( m_dataType < iRhs.getDataType() )
159  {
160  return true;
161  }
162  else if ( m_dataType > iRhs.getDataType() )
163  {
164  return false;
165  }
166  else
167  {
168  return m_data->lessThan( iRhs.getData() );
169  }
170  }
171 
172  //-*************************************************************************
173  // Useful functions
174  //-*************************************************************************
175 
176  //! Sets to to default values for whichever POD types are contained.
177  //! ...
179  {
180  m_data->setToDefault();
181  }
182 
183 private:
184  DataType m_dataType;
185  Alembic::Util::unique_ptr<Data> m_data;
186 };
187 
188 } // End namespace ALEMBIC_VERSION_NS
189 
190 using namespace ALEMBIC_VERSION_NS;
191 
192 } // End namespace AbcCoreAbstract
193 } // End namespace Alembic
194 
195 #endif
#define ALEMBIC_EXPORT
Definition: Export.h:51
bool operator==(const ScalarSample &iRhs) const
Definition: ScalarSample.h:123
bool operator<(const ScalarSample &iRhs) const
Definition: ScalarSample.h:156
bool equalWithRelAbsError(const void *iRhs, double iRelAbsError) const
Definition: ScalarSample.h:132
bool equalWithRelAbsError(const ScalarSample &iRhs, double iRelAbsError) const
Definition: ScalarSample.h:140
#define ALEMBIC_VERSION_NS
Definition: Foundation.h:88