Line data Source code
1 :
2 : /* Copyright (c) 2012-2013, Daniel Nachbaur <danielnachbaur@googlemail.com>
3 : * 2012-2014, Stefan Eilemann <eile@eyescale.ch>
4 : *
5 : * This file is part of Collage <https://github.com/Eyescale/Collage>
6 : *
7 : * This library is free software; you can redistribute it and/or modify it under
8 : * the terms of the GNU Lesser General Public License version 2.1 as published
9 : * by the Free Software Foundation.
10 : *
11 : * This library is distributed in the hope that it will be useful, but WITHOUT
12 : * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 : * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
14 : * details.
15 : *
16 : * You should have received a copy of the GNU Lesser General Public License
17 : * along with this library; if not, write to the Free Software Foundation, Inc.,
18 : * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 : */
20 :
21 : #ifndef CO_OBJECTMAP_H
22 : #define CO_OBJECTMAP_H
23 :
24 : #include <co/serializable.h> // base class
25 :
26 : namespace co
27 : {
28 : namespace detail { class ObjectMap; }
29 :
30 : /**
31 : * A distributed object registry.
32 : *
33 : * The object map takes care of distribution and synchronization of registered
34 : * objects across all slave instances. Objects are registered with an
35 : * additional type to resolve the creation of new objects during mapping.
36 : * This creation is handled by an ObjectFactory which has to be provided and
37 : * implemented for the desired object types.
38 : */
39 : class ObjectMap : public Serializable
40 : {
41 : public:
42 : /**
43 : * Construct a new object map.
44 : *
45 : * @param handler used for object registration and mapping
46 : * @param factory to create & destroy slave objects
47 : * @version 1.0
48 : */
49 : CO_API ObjectMap( ObjectHandler& handler, ObjectFactory& factory );
50 :
51 : /**
52 : * Destroy this object map.
53 : *
54 : * All registered and mapped objects will be deregistered and unmapped.
55 : * All mapped and owned objects will be destroyed using the object factory.
56 : * @version 1.0
57 : */
58 : CO_API virtual ~ObjectMap();
59 :
60 : /**
61 : * Add and register a new object as master instance to this object map.
62 : *
63 : * Upon registering using the map's object handler, this object will be
64 : * remembered for serialization on the next commit of this object map.
65 : *
66 : * @param object the new object to add and register
67 : * @param type unique object type to create object via slave factory
68 : * @return false on failed ObjectHandler::registerObject, true otherwise
69 : * @version 1.0
70 : */
71 : CO_API bool register_( Object* object, const uint32_t type );
72 :
73 : /**
74 : * Remove and deregister an object from this object map.
75 : *
76 : * Upon deregistering using the map's object handler, this object will be
77 : * remembered for unmap and possible deletion on the next commit of this
78 : * object map.
79 : *
80 : * @param object the object to remove and deregister
81 : * @return false on if object was not registered, true otherwise
82 : * @version 1.0
83 : */
84 : CO_API bool deregister( Object* object );
85 :
86 : /**
87 : * Map and return an object.
88 : *
89 : * The object is either created via its type specified upon registering or
90 : * an already created instance is used if passed to this function. Passed
91 : * instances will not be considered for deletion during explicit unmap(),
92 : * implicit unmap caused by deregister(), or destruction of this object map.
93 : *
94 : * The object will be mapped to the version that was current on registration
95 : * time.
96 : *
97 : * @param identifier unique object identifier used for map operation
98 : * @param instance already created instance to skip factory creation
99 : * @return 0 if not registered, the valid instance otherwise
100 : * @version 1.0
101 : */
102 : CO_API Object* map( const uint128_t& identifier, Object* instance = 0 );
103 :
104 : /**
105 : * Unmap an object from the object map.
106 : *
107 : * The object is unmapped using the map's object handler and will not be
108 : * considered for further synchronization. The object will be destructed if
109 : * if was created by the object map.
110 : *
111 : * @param object the object to unmap
112 : * @return false on if object was not mapped, true otherwise
113 : * @version 1.0
114 : */
115 : CO_API bool unmap( Object* object );
116 :
117 : /** Deregister or unmap all registered and mapped objects. @version 1.0 */
118 : CO_API void clear();
119 :
120 : /** Commit all registered objects. @version 1.0 */
121 : CO_API uint128_t commit( const uint32_t incarnation =
122 : CO_COMMIT_NEXT ) override;
123 :
124 : protected:
125 : CO_API bool isDirty() const override; //!< @internal
126 :
127 : /** @internal */
128 : CO_API void serialize( DataOStream& os,
129 : const uint64_t dirtyBits ) override;
130 :
131 : /** @internal */
132 : CO_API void deserialize( DataIStream& is,
133 : const uint64_t dirtyBits ) override;
134 : /** @internal */
135 2 : ChangeType getChangeType() const override { return DELTA; }
136 : CO_API void notifyAttached() override; //!< @internal
137 :
138 : /** @internal The changed parts of the object since the last serialize(). */
139 : enum DirtyBits
140 : {
141 : DIRTY_ADDED = Serializable::DIRTY_CUSTOM << 0, // 1
142 : DIRTY_REMOVED = Serializable::DIRTY_CUSTOM << 1, // 2
143 : DIRTY_CHANGED = Serializable::DIRTY_CUSTOM << 2, // 4
144 : DIRTY_CUSTOM = Serializable::DIRTY_CUSTOM << 3 // 8
145 : };
146 :
147 : private:
148 : detail::ObjectMap* const _impl;
149 :
150 : /** @internal Commit and note new master versions. */
151 : void _commitMasters( const uint32_t incarnation );
152 : };
153 : }
154 : #endif // CO_OBJECTMAP_H
|