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 #ifndef Alembic_Abc_OTypedScalarProperty_h
37 #define Alembic_Abc_OTypedScalarProperty_h
38 
39 #include <Alembic/Abc/Foundation.h>
40 #include <Alembic/Abc/OScalarProperty.h>
41 #include <Alembic/Abc/TypedPropertyTraits.h>
42 
43 namespace Alembic {
44 namespace Abc {
45 namespace ALEMBIC_VERSION_NS {
46 
47 //-*****************************************************************************
48 template <class TRAITS>
49 class OTypedScalarProperty : public OScalarProperty
50 {
51 public:
52     //-*************************************************************************
53     // TYPE IDENTIFICATION
54     //-*************************************************************************
55     typedef TRAITS traits_type;
56     typedef OTypedScalarProperty<TRAITS> this_type;
57     typedef typename TRAITS::value_type value_type;
58 
59     //! Return the interpretation expected of this
60     //! property. An empty interpretation matches everything
getInterpretation()61     static const char * getInterpretation()
62     {
63         return TRAITS::interpretation();
64     }
65 
66     //! This will check whether or not a given entity (as represented by
67     //! a metadata) strictly matches the interpretation of this
68     //! schema object
69     static bool matches( const AbcA::MetaData &iMetaData,
70                          SchemaInterpMatching iMatching = kStrictMatching )
71     {
72         return ( iMetaData.get( "interpretation" ) ==
73                  getInterpretation() );
74     }
75 
76     //! This will check whether or not a given object (as represented by
77     //! an object header) strictly matches the interpretation of this
78     //! schema object, as well as the data type.
79     static bool matches( const AbcA::PropertyHeader &iHeader,
80                          SchemaInterpMatching iMatching = kStrictMatching )
81     {
82         return ( iHeader.getDataType().getPod() ==
83                  TRAITS::dataType().getPod() &&
84                  ( iHeader.getDataType().getExtent() ==
85                    TRAITS::dataType().getExtent() ||
86                    std::string() == getInterpretation() ) ) &&
87                iHeader.isScalar() &&
88                matches( iHeader.getMetaData(), iMatching );
89     }
90 
91     //-*************************************************************************
92     // CONSTRUCTION, DESTRUCTION, ASSIGNMENT
93     //-*************************************************************************
94 
95     //! Create a default (empty) TypedScalarProperty
96     //! ...
OTypedScalarProperty()97     OTypedScalarProperty() {}
98 
99     //! Create a new TypedScalarProperty
100     //! as a child of the passed iParent
101     //! Arguments can specify metadata, timesampling, and error handling.
102     OTypedScalarProperty(
103         AbcA::CompoundPropertyWriterPtr iParent,
104         const std::string &iName,
105 
106         const Argument &iArg0 = Argument(),
107         const Argument &iArg1 = Argument(),
108         const Argument &iArg2 = Argument(),
109         const Argument &iArg3 = Argument() )
110     {
111         init( iParent, iName, iArg0, iArg1, iArg2, iArg3 );
112     }
113 
114     //! Create a new TypedScalarProperty
115     //! as a child of the passed iParent
116     //! Arguments can specify metadata, timesampling, and error handling.
117     OTypedScalarProperty(
118         OCompoundProperty iParent,
119         const std::string &iName,
120 
121         const Argument &iArg0 = Argument(),
122         const Argument &iArg1 = Argument(),
123         const Argument &iArg2 = Argument() )
124     {
125         init( iParent.getPtr(), iName, GetErrorHandlerPolicy( iParent ),
126               iArg0, iArg1, iArg2 );
127     }
128 
129     //! Wrap an existing scalar property,
130     //! checking to make sure it matches data type and also
131     //! (if requested) interpretation.
132     OTypedScalarProperty(
133         AbcA::ScalarPropertyWriterPtr iProp,
134         const Argument &iArg0 = Argument(),
135         const Argument &iArg1 = Argument() )
OScalarProperty(iProp,GetErrorHandlerPolicy (iProp,iArg0,iArg1))136     : OScalarProperty( iProp, GetErrorHandlerPolicy( iProp, iArg0, iArg1 ) )
137     {
138         ALEMBIC_ABC_SAFE_CALL_BEGIN(
139             "OTypedScalarProperty::OTypedScalarProperty()" );
140 
141         const AbcA::PropertyHeader &pheader = iProp->getHeader();
142 
143         ABCA_ASSERT( matches( pheader, GetSchemaInterpMatching( iArg0,iArg1 ) ),
144 
145                      "Incorrect match of header datatype: "
146                      << pheader.getDataType()
147                      << " to expected: "
148                      << TRAITS::dataType()
149                      << ",\n...or incorrect match of interpretation: "
150                      << pheader.getMetaData().get( "interpretation" )
151                      << " to expected: "
152                      << TRAITS::interpretation() );
153 
154         ALEMBIC_ABC_SAFE_CALL_END_RESET();
155     }
156 
157     // Deprecated in favor of the constructor above
158     OTypedScalarProperty(
159         AbcA::ScalarPropertyWriterPtr iProp,
160         WrapExistingFlag iWrapFlag,
161         const Argument &iArg0 = Argument(),
162         const Argument &iArg1 = Argument() )
163     {
164         *this = OTypedScalarProperty( iProp, iArg0, iArg1 );
165     }
166 
167     //-*************************************************************************
168     // SCALAR PROPERTY FEATURES
169     //-*************************************************************************
170 
171     //! Set a sample using a reference to a value-type,
172     //! instead of a void*
set(const value_type & iVal)173     void set( const value_type &iVal )
174     {
175         OScalarProperty::set( reinterpret_cast<const void *>( &iVal ) );
176     }
177 
178 private:
179 
init(AbcA::CompoundPropertyWriterPtr iParent,const std::string & iName,const Argument & iArg0,const Argument & iArg1,const Argument & iArg2,const Argument & iArg3)180     void init( AbcA::CompoundPropertyWriterPtr iParent,
181                const std::string &iName,
182                const Argument &iArg0,
183                const Argument &iArg1,
184                const Argument &iArg2,
185                const Argument &iArg3 )
186     {
187         Arguments args;
188         iArg0.setInto( args );
189         iArg1.setInto( args );
190         iArg2.setInto( args );
191         iArg3.setInto( args );
192 
193         getErrorHandler().setPolicy( args.getErrorHandlerPolicy() );
194 
195         ALEMBIC_ABC_SAFE_CALL_BEGIN( "OTypedScalarProperty::init()" );
196 
197         ABCA_ASSERT( iParent, "NULL CompoundPropertyWriterPtr" );
198 
199         AbcA::MetaData mdata = args.getMetaData();
200         if ( std::string() != getInterpretation() )
201         {
202             mdata.set( "interpretation", getInterpretation() );
203         }
204 
205         AbcA::TimeSamplingPtr tsPtr = args.getTimeSampling();
206 
207         uint32_t tsIndex = args.getTimeSamplingIndex();
208 
209         // if we specified a valid TimeSamplingPtr, use it to determine the
210         // index otherwise we'll use the index, which defaults to the intrinsic
211         // 0 index
212         if (tsPtr)
213         {
214             tsIndex = iParent->getObject()->getArchive()->addTimeSampling(
215                 *tsPtr);
216         }
217 
218         m_property = iParent->createScalarProperty( iName, mdata,
219             TRAITS::dataType(), tsIndex );
220 
221         ALEMBIC_ABC_SAFE_CALL_END_RESET();
222     }
223 
224 };
225 
226 //-*****************************************************************************
227 //-*****************************************************************************
228 //-*****************************************************************************
229 
230 typedef OTypedScalarProperty<BooleanTPTraits>         OBoolProperty;
231 typedef OTypedScalarProperty<Uint8TPTraits>           OUcharProperty;
232 typedef OTypedScalarProperty<Int8TPTraits>            OCharProperty;
233 typedef OTypedScalarProperty<Uint16TPTraits>          OUInt16Property;
234 typedef OTypedScalarProperty<Int16TPTraits>           OInt16Property;
235 typedef OTypedScalarProperty<Uint32TPTraits>          OUInt32Property;
236 typedef OTypedScalarProperty<Int32TPTraits>           OInt32Property;
237 typedef OTypedScalarProperty<Uint64TPTraits>          OUInt64Property;
238 typedef OTypedScalarProperty<Int64TPTraits>           OInt64Property;
239 typedef OTypedScalarProperty<Float16TPTraits>         OHalfProperty;
240 typedef OTypedScalarProperty<Float32TPTraits>         OFloatProperty;
241 typedef OTypedScalarProperty<Float64TPTraits>         ODoubleProperty;
242 typedef OTypedScalarProperty<StringTPTraits>          OStringProperty;
243 typedef OTypedScalarProperty<WstringTPTraits>         OWstringProperty;
244 
245 typedef OTypedScalarProperty<V2sTPTraits>             OV2sProperty;
246 typedef OTypedScalarProperty<V2iTPTraits>             OV2iProperty;
247 typedef OTypedScalarProperty<V2fTPTraits>             OV2fProperty;
248 typedef OTypedScalarProperty<V2dTPTraits>             OV2dProperty;
249 
250 typedef OTypedScalarProperty<V3sTPTraits>             OV3sProperty;
251 typedef OTypedScalarProperty<V3iTPTraits>             OV3iProperty;
252 typedef OTypedScalarProperty<V3fTPTraits>             OV3fProperty;
253 typedef OTypedScalarProperty<V3dTPTraits>             OV3dProperty;
254 
255 typedef OTypedScalarProperty<P2sTPTraits>             OP2sProperty;
256 typedef OTypedScalarProperty<P2iTPTraits>             OP2iProperty;
257 typedef OTypedScalarProperty<P2fTPTraits>             OP2fProperty;
258 typedef OTypedScalarProperty<P2dTPTraits>             OP2dProperty;
259 
260 typedef OTypedScalarProperty<P3sTPTraits>             OP3sProperty;
261 typedef OTypedScalarProperty<P3iTPTraits>             OP3iProperty;
262 typedef OTypedScalarProperty<P3fTPTraits>             OP3fProperty;
263 typedef OTypedScalarProperty<P3dTPTraits>             OP3dProperty;
264 
265 typedef OTypedScalarProperty<Box2sTPTraits>           OBox2sProperty;
266 typedef OTypedScalarProperty<Box2iTPTraits>           OBox2iProperty;
267 typedef OTypedScalarProperty<Box2fTPTraits>           OBox2fProperty;
268 typedef OTypedScalarProperty<Box2dTPTraits>           OBox2dProperty;
269 
270 typedef OTypedScalarProperty<Box3sTPTraits>           OBox3sProperty;
271 typedef OTypedScalarProperty<Box3iTPTraits>           OBox3iProperty;
272 typedef OTypedScalarProperty<Box3fTPTraits>           OBox3fProperty;
273 typedef OTypedScalarProperty<Box3dTPTraits>           OBox3dProperty;
274 
275 typedef OTypedScalarProperty<M33fTPTraits>            OM33fProperty;
276 typedef OTypedScalarProperty<M33dTPTraits>            OM33dProperty;
277 typedef OTypedScalarProperty<M44fTPTraits>            OM44fProperty;
278 typedef OTypedScalarProperty<M44dTPTraits>            OM44dProperty;
279 
280 typedef OTypedScalarProperty<QuatfTPTraits>           OQuatfProperty;
281 typedef OTypedScalarProperty<QuatdTPTraits>           OQuatdProperty;
282 
283 typedef OTypedScalarProperty<C3hTPTraits>             OC3hProperty;
284 typedef OTypedScalarProperty<C3fTPTraits>             OC3fProperty;
285 typedef OTypedScalarProperty<C3cTPTraits>             OC3cProperty;
286 
287 typedef OTypedScalarProperty<C4hTPTraits>             OC4hProperty;
288 typedef OTypedScalarProperty<C4fTPTraits>             OC4fProperty;
289 typedef OTypedScalarProperty<C4cTPTraits>             OC4cProperty;
290 
291 typedef OTypedScalarProperty<N2fTPTraits>             ON2fProperty;
292 typedef OTypedScalarProperty<N2dTPTraits>             ON2dProperty;
293 
294 typedef OTypedScalarProperty<N3fTPTraits>             ON3fProperty;
295 typedef OTypedScalarProperty<N3dTPTraits>             ON3dProperty;
296 
297 } // End namespace ALEMBIC_VERSION_NS
298 
299 using namespace ALEMBIC_VERSION_NS;
300 
301 } // End namespace Abc
302 } // End namespace Alembic
303 
304 #endif
305