Cortex  10.0.0-a4
Object.h
1 //
3 // Copyright (c) 2007-2013, Image Engine Design Inc. All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 // * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 //
12 // * Redistributions in binary form must reproduce the above copyright
13 // notice, this list of conditions and the following disclaimer in the
14 // documentation and/or other materials provided with the distribution.
15 //
16 // * Neither the name of Image Engine Design nor the names of any
17 // other contributors to this software may be used to endorse or
18 // promote products derived from this software without specific prior
19 // written permission.
20 //
21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
22 // IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
23 // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
25 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 //
34 
35 #ifndef IE_CORE_OBJECT_H
36 #define IE_CORE_OBJECT_H
37 
38 #include <set>
39 #include <map>
40 #include <string>
41 
42 #include "boost/shared_ptr.hpp"
43 #include "IECore/Export.h"
44 #include "IECore/RunTimeTyped.h"
45 #include "IECore/IndexedIO.h"
46 
47 namespace IECore
48 {
49 
50 IE_CORE_FORWARDDECLARE( Object );
51 
52 class MurmurHash;
53 
54 #define IE_CORE_DECLAREOBJECTTYPEDESCRIPTION( TYPENAME ) \
55  private : \
56  static const IECore::Object::TypeDescription<TYPENAME> m_typeDescription; \
57  public : \
58 
59 #define IE_CORE_DECLAREABSTRACTOBJECTTYPEDESCRIPTION( TYPENAME ) \
60  private : \
61  static const IECore::Object::AbstractTypeDescription<TYPENAME> m_typeDescription; \
62  public : \
63 
64 #define IE_CORE_DECLAREOBJECTMEMBERFNS( TYPENAME ) \
65  public : \
66  TYPENAME::Ptr copy() const { return boost::static_pointer_cast<TYPENAME>( IECore::Object::copy() ); } \
67  bool isEqualTo( const IECore::Object *other ) const override; \
68  void hash( IECore::MurmurHash &h ) const override; \
69  protected : \
70  void copyFrom( const IECore::Object *other, IECore::Object::CopyContext *context ) override; \
71  void save( IECore::Object::SaveContext *context ) const override; \
72  void load( IECore::Object::LoadContextPtr context ) override; \
73  void memoryUsage( IECore::Object::MemoryAccumulator & ) const override; \
74 
75 #define IE_CORE_DECLAREOBJECT( TYPE, BASETYPE ) \
76  IE_CORE_DECLARERUNTIMETYPED( TYPE, BASETYPE ); \
77  IE_CORE_DECLAREOBJECTMEMBERFNS( TYPE ); \
78  IE_CORE_DECLAREOBJECTTYPEDESCRIPTION( TYPE ); \
79 
80 #define IE_CORE_DECLAREABSTRACTOBJECT( TYPE, BASETYPE ) \
81  IE_CORE_DECLARERUNTIMETYPED( TYPE, BASETYPE ); \
82  IE_CORE_DECLAREOBJECTMEMBERFNS( TYPE ); \
83  IE_CORE_DECLAREABSTRACTOBJECTTYPEDESCRIPTION( TYPE ); \
84 
85 #define IE_CORE_DECLAREEXTENSIONOBJECT( TYPE, TYPEID, BASETYPE ) \
86  IE_CORE_DECLARERUNTIMETYPEDEXTENSION( TYPE, TYPEID, BASETYPE ); \
87  IE_CORE_DECLAREOBJECTMEMBERFNS( TYPE ); \
88  IE_CORE_DECLAREOBJECTTYPEDESCRIPTION( TYPE ); \
89 
90 #define IE_CORE_DECLAREABSTRACTEXTENSIONOBJECT( TYPE, TYPEID, BASETYPE ) \
91  IE_CORE_DECLARERUNTIMETYPEDEXTENSION( TYPE, TYPEID, BASETYPE ); \
92  IE_CORE_DECLAREOBJECTMEMBERFNS( TYPE ); \
93  IE_CORE_DECLAREABSTRACTOBJECTTYPEDESCRIPTION( TYPE ); \
94 
95 #define IE_CORE_DEFINEOBJECTTYPEDESCRIPTION( TYPENAME ) \
96  const IECore::Object::TypeDescription<TYPENAME> TYPENAME::m_typeDescription \
97 
98 #define IE_CORE_DEFINEABSTRACTOBJECTTYPEDESCRIPTION( TYPENAME ) \
99  const IECore::Object::AbstractTypeDescription<TYPENAME> TYPENAME::m_typeDescription \
100 
101 class IECORE_API Object : public RunTimeTyped
105 {
106  public:
107 
108  Object();
109 
110  ~Object() override;
111 
112  IE_CORE_DECLARERUNTIMETYPED( Object, RunTimeTyped );
113 
123 
124  ObjectPtr copy() const;
130  void copyFrom( const Object *other );
133  void save( IndexedIOPtr ioInterface, const IndexedIO::EntryID &name ) const;
141  virtual bool isEqualTo( const Object *other ) const = 0;
146  virtual bool isNotEqualTo( const Object *other ) const;
148  bool operator==( const Object &other ) const;
150  bool operator!=( const Object &other ) const;
152  size_t memoryUsage() const;
156  MurmurHash hash() const;
160  virtual void hash( MurmurHash &h ) const = 0;
162 
168 
169  static bool isType( TypeId typeId );
172  static bool isType( const std::string &typeName );
175  static bool isAbstractType( TypeId typeId );
177  static bool isAbstractType( const std::string &typeName );
180  static ObjectPtr create( TypeId typeId );
183  static ObjectPtr create( const std::string &typeName );
186  static ObjectPtr load( ConstIndexedIOPtr ioInterface, const IndexedIO::EntryID &name );
188 
189  typedef ObjectPtr (*CreatorFn)( void *data );
190 
192  static void registerType( TypeId typeId, const std::string &typeName, CreatorFn creator, void *data = nullptr );
193 
194 
195  protected :
196 
202  template<class T>
203  class TypeDescription : protected RunTimeTyped::TypeDescription<T>
204  {
205  public :
207  TypeDescription();
208 
210  TypeDescription( TypeId alternateTypeId, const std::string &alternateTypeName );
211  private :
212  static ObjectPtr creator( void *data = nullptr );
213  };
218  template<class T>
219  class AbstractTypeDescription : protected RunTimeTyped::TypeDescription<T>
220  {
221  public :
223  };
224 
229  class IECORE_API CopyContext
230  {
231  public :
233  template<class T>
234  typename T::Ptr copy( const T *toCopy );
235  private :
236  std::map<const Object *, Object *> m_copies;
237  };
238 
244  virtual void copyFrom( const Object *other, CopyContext *context ) = 0;
245 
247  class IECORE_API SaveContext
248  {
249  public :
250  SaveContext( IndexedIOPtr ioInterface );
260  IndexedIOPtr container( const std::string &typeName, unsigned int ioVersion );
263  void save( const Object *toSave, IndexedIO *o, const IndexedIO::EntryID &name );
273  IndexedIO *rawContainer();
274  private :
275 
276  typedef std::map<const Object *, IndexedIO::EntryIDList > SavedObjectMap;
277 
278  SaveContext( IndexedIOPtr ioInterface, boost::shared_ptr<SavedObjectMap> savedObjects );
279 
280  IndexedIOPtr m_ioInterface;
281  boost::shared_ptr<SavedObjectMap> m_savedObjects;
282 
283  };
284 
286  class IECORE_API LoadContext : public RefCounted
287  {
288  public :
289  LoadContext( ConstIndexedIOPtr ioInterface );
297  ConstIndexedIOPtr container( const std::string &typeName, unsigned int &ioVersion, bool throwIfMissing = true );
298  template<class T>
300  typename T::Ptr load( const IndexedIO *container, const IndexedIO::EntryID &name );
303  const IndexedIO *rawContainer();
304 
305  private :
306  typedef std::map< IndexedIO::EntryIDList, ObjectPtr> LoadedObjectMap;
307 
308  LoadContext( ConstIndexedIOPtr ioInterface, boost::shared_ptr<LoadedObjectMap> loadedObjects );
309 
310  ObjectPtr loadObjectOrReference( const IndexedIO *container, const IndexedIO::EntryID &name );
311  ObjectPtr loadObject( const IndexedIO *container );
312 
313  ConstIndexedIOPtr m_ioInterface;
314  boost::shared_ptr<LoadedObjectMap> m_loadedObjects;
315  };
316  IE_CORE_DECLAREPTR( LoadContext );
317 
322  virtual void save( SaveContext *context ) const = 0;
329  virtual void load( LoadContextPtr context ) = 0;
330 
333  class IECORE_API MemoryAccumulator
334  {
335  public :
338  void accumulate( size_t bytes );
341  void accumulate( const Object *object );
344  void accumulate( const void *ptr, size_t bytes );
346  size_t total() const;
347  private :
348  std::set<const void *> m_accumulated;
349  size_t m_total;
350  };
351 
355  virtual void memoryUsage( MemoryAccumulator &accumulator ) const = 0;
356 
357  private :
358 
359  struct TypeInformation;
360  static TypeInformation *typeInformation();
361 
362  static const AbstractTypeDescription<Object> m_typeDescription;
363 
364  static const unsigned int m_ioVersion;
365 
366 };
367 
368 } // namespace IECore
369 
370 #include "IECore/Object.inl"
371 
372 #endif // IE_CORE_OBJECT_H
The class provided to the load() method implemented by subclasses.
Definition: Object.h:286
Definition: Object.h:333
Definition: IndexedIO.h:57
Definition: Object.h:229
Definition: Object.h:104
Definition: Object.h:203
TypeId
Definition: TypeIds.h:46
The class provided to the save() method implemented by subclasses.
Definition: Object.h:247
Definition: InternedString.h:55
Definition: RefCounted.h:124
This namespace contains all components of the core library.
Definition: AddSmoothSkinningInfluencesOp.h:43