Mitsuba Renderer  0.5.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
serialization.h
Go to the documentation of this file.
1 /*
2  This file is part of Mitsuba, a physically based rendering system.
3 
4  Copyright (c) 2007-2014 by Wenzel Jakob and others.
5 
6  Mitsuba is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License Version 3
8  as published by the Free Software Foundation.
9 
10  Mitsuba is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 #pragma once
20 #if !defined(__MITSUBA_CORE_SERIALIZATION_H_)
21 #define __MITSUBA_CORE_SERIALIZATION_H_
22 
23 #include <mitsuba/mitsuba.h>
24 
26 
27 /** \brief Base class of all reference-counted objects with serialization support
28  *
29  * To support unserialization from a stream, the implementation should use one of the
30  * RTTI macros \ref MTS_IMPLEMENT_CLASS_S or \ref MTS_IMPLEMENT_CLASS_IS.
31  *
32  * \ingroup libcore
33  * \ingroup libpython
34  */
36 public:
37  /// Unserialize a serializable object
38  SerializableObject(Stream *stream, InstanceManager *manager);
39 
40  /// Serialize this object to a stream
41  virtual void serialize(Stream *stream, InstanceManager *manager) const = 0;
42 
44 protected:
45  /// Construct a serializable object
46  inline SerializableObject() { }
47 
48  /// Virtual deconstructor
49  virtual ~SerializableObject() { }
50 };
51 
52 /** \brief Coordinates the serialization and unserialization of object graphs
53  *
54  * When serializaing a complicated object graph to a binary data stream,
55  * the instance manager annotates the data stream to avoid serializing
56  * objects twice or becoming stuck in a cyclic dependency. This allows
57  * arbitrary connected graphs to be serialized.
58  *
59  * Similarly when unserializing a stream, it ensures that the resulting
60  * object graph has the same structure.
61  *
62  * \ingroup libcore
63  * \ingroup libpython
64  */
66  friend class SerializableObject;
67 public:
68  /// \brief Construct a new instance manager
70 
71  /// Retrieve an instance from the given stream
72  SerializableObject *getInstance(Stream *stream);
73 
74  /// Store an instance to the given stream
75  void serialize(Stream *stream, const SerializableObject *inst);
76 
78 private:
79  /// Virtual destructor
80  virtual ~InstanceManager();
81 
82  /// Called from the unserialization constructor of SerializableObject
83  void registerInstance(SerializableObject *object);
84 private:
85  unsigned int m_counter, m_lastID;
86  std::vector<SerializableObject *> m_fullyAllocated;
87  std::map<unsigned int, SerializableObject *> m_idToObj;
88  std::map<const SerializableObject *, unsigned int> m_objToId;
89 };
90 
92 
93 #endif /* __MITSUBA_CORE_SERIALIZATION_H_ */
virtual ~SerializableObject()
Virtual deconstructor.
Definition: serialization.h:49
Base class of all reference-counted objects with serialization support.
Definition: serialization.h:35
#define MTS_EXPORT_CORE
Definition: getopt.h:29
#define MTS_NAMESPACE_BEGIN
Definition: platform.h:137
Abstract seekable stream class.
Definition: stream.h:58
#define MTS_DECLARE_CLASS()
This macro must be used in the initial definition in classes that derive from Object.
Definition: class.h:158
Parent of all Mitsuba classes.
Definition: object.h:38
Coordinates the serialization and unserialization of object graphs.
Definition: serialization.h:65
#define MTS_NAMESPACE_END
Definition: platform.h:138