From 801dd557990774d27c233a7b9ff5c3e5a1a733d4 Mon Sep 17 00:00:00 2001 From: Andreas Stöckel Date: Fri, 2 Jan 2015 23:31:53 +0100 Subject: Allowing to pass a Listener instance in the constructor and to access the listener instance in derived classes. --- src/core/managed/ManagedContainer.hpp | 137 ++++++++++++++++++++++++++++------ 1 file changed, 113 insertions(+), 24 deletions(-) (limited to 'src') diff --git a/src/core/managed/ManagedContainer.hpp b/src/core/managed/ManagedContainer.hpp index a73b3f5..fd80d1d 100644 --- a/src/core/managed/ManagedContainer.hpp +++ b/src/core/managed/ManagedContainer.hpp @@ -71,7 +71,10 @@ struct MapAccessor { template struct DefaultListener { void addElement(const ValueType &val, Managed *owner) {} - void deleteElement(const ValueType &val, Managed *owner) {} + void deleteElement(const ValueType &val, Managed *owner, + bool fromDestructor) + { + } }; /** @@ -106,17 +109,6 @@ private: */ Managed *owner; - /** - * Accessor used to access the managed instance inside a "reference type". - */ - Accessor accessor; - - /** - * Listener which is notified whenever an element is added to or removed - * from the list. - */ - Listener listener; - /** * Calls the "addElement" function of each element and thus initializes * the references from the owner to the elements. @@ -133,21 +125,37 @@ private: * collection. * * @param collection for which the deleteElement function is invoked. + * @param fromDestructor set to true if the function is called from the + * destructor. */ - void finalize(const Collection &collection) + void finalize(const Collection &collection, bool fromDestructor = false) { for (const auto &elem : collection) { - deleteElement(elem); + deleteElement(elem, fromDestructor); } } /** * Calls the "deleteElement" function for each element in the underlying * STL collection. + * + * @param fromDestructor set to true if the function is called from the + * destructor. */ - void finalize() { finalize(c); } + void finalize(bool fromDestructor = false) { finalize(c, fromDestructor); } protected: + /** + * Accessor used to access the managed instance inside a "reference type". + */ + Accessor accessor; + + /** + * Listener which is notified whenever an element is added to or removed + * from the list. + */ + Listener listener; + /** * Underlying STL collection. */ @@ -180,13 +188,13 @@ protected: * @param elem is a reference to the actual element that is being removed * from the underlying container. */ - void deleteElement(const value_type &elem) + void deleteElement(const value_type &elem, bool fromDestructor = false) { Managed *managed = accessor.getManaged(elem); Manager &manager = owner ? owner->getManager() : managed->getManager(); + listener.deleteElement(elem, owner, fromDestructor); manager.deleteRef(managed, owner); - listener.deleteElement(elem, owner); } public: @@ -196,6 +204,15 @@ public: */ ManagedContainer() : owner(nullptr){}; + /** + * Constructor of the ManagedContainer class with no owner (will contain + * rooted entries) but a listener instance. + * + * @param listener is the Listener instance listener inside the + * ManagedContainer should be initialized with. + */ + ManagedContainer(Listener listener) : owner(nullptr), listener(listener){}; + /** * Constructor of the ManagedContainer class with an initializer list but * no owner (will contain rooted entries). @@ -213,6 +230,17 @@ public: */ ManagedContainer(Handle owner) : owner(owner.get()){}; + /** + * Constructor of the ManagedContainer class. + * + * @param owner is the managed object which owns the collection and all + * handles to other managed objects stored within. + * @param listener is the Listener instance listener inside the + * ManagedContainer should be initialized with. + */ + ManagedContainer(Handle owner, Listener listener) + : owner(owner.get()), listener(listener){}; + /** * Copy constructor. Creates a copy of the given container with the same * owner as the given container. @@ -236,9 +264,30 @@ public: initialize(); } + /** + * Copy constructor. Creates a copy of the given container with another + * owner. + * + * @param owner is the managed object which owns the collection and all + * handles to other managed objects stored within. + * @param other is the other container that should be copied. + * @param listener is the Listener instance listener inside the + * ManagedContainer should be initialized with. + */ + ManagedContainer(Handle owner, Listener listener, + const own_type &other) + : owner(owner.get()), listener(listener), c(other.c) + { + initialize(); + } + /** * Copy constructor. Creates a copy of the given container and takes over * ownership. + * + * @param owner is the managed object which owns the collection and all + * handles to other managed objects stored within. + * @param collection is the other container that should be copied. */ ManagedContainer(Handle owner, const Collection &collection) : owner(owner.get()), c(collection) @@ -246,6 +295,23 @@ public: initialize(); } + /** + * Copy constructor. Creates a copy of the given container and takes over + * ownership. + * + * @param owner is the managed object which owns the collection and all + * handles to other managed objects stored within. + * @param collection is the other container that should be copied. + * @param listener is the Listener instance listener inside the + * ManagedContainer should be initialized with. + */ + ManagedContainer(Handle owner, Listener listener, + const Collection &collection) + : owner(owner.get()), listener(listener), c(collection) + { + initialize(); + } + /** * Move constructor. Moves the other instance to this instance. * @@ -258,9 +324,11 @@ public: } /** - * Copy constructor. Creates a copy of the given container with another - * owner. + * Move constructor. Moves the given container to this instance but + * exchanges the owner. * + * @param owner is the managed object which owns the collection and all + * handles to other managed objects stored within. * @param other is the other container that should be moved. */ ManagedContainer(Handle owner, own_type &&other) @@ -271,6 +339,24 @@ public: other.owner = nullptr; } + /** + * Move constructor. Moves the given container to this instance but + * exchanges the owner and the listener. + * + * @param owner is the managed object which owns the collection and all + * handles to other managed objects stored within. + * @param listener is the Listener instance listener inside the + * ManagedContainer should be initialized with. + * @param other is the other container that should be moved. + */ + ManagedContainer(Handle owner, Listener listener, own_type &&other) + : owner(owner.get()), listener(listener), c(std::move(other.c)) + { + initialize(); + other.finalize(c); + other.owner = nullptr; + } + /** * Copy constructor. Initialize with an iterator from another collection. * @@ -297,7 +383,7 @@ public: * Destructor of the ManagedContainer class. Calls the "deleteElement" * function for each element in the container. */ - ~ManagedContainer() { finalize(); }; + ~ManagedContainer() { finalize(true); }; /** * Copy assignment operator. @@ -399,8 +485,9 @@ public: */ iterator insert(iterator position, value_type val) { + auto res = c.insert(position, val); addElement(val); - return c.insert(position, val); + return res; } /** @@ -503,8 +590,8 @@ public: void push_back(const value_type &val) { - this->addElement(val); Base::c.push_back(val); + this->addElement(val); } void pop_back() @@ -551,14 +638,16 @@ public: std::pair insert(value_type val) { + auto res = Base::c.insert(val); this->addElement(val); - return Base::c.insert(val); + return res; } iterator insert(const_iterator position, value_type val) { + auto res = Base::c.insert(position, val); this->addElement(val); - return Base::c.insert(position, val); + return res; } template -- cgit v1.2.3