/* Copyright (C) 2005-2006 Feeling Software Inc. MIT License: http://www.opensource.org/licenses/mit-license.php */ /* The idea is simple: the object registers itself in the constructor, with one container. It may register/unregister itself with other containers subsequently. It keeps a list of the containers and a pointer to the main container so that the up-classes can easily access it. In the destructor, the object unregisters itself with the container(s). The object has a RTTI-like static class structure so that the container(s) know which object type list to remove this object from when it is deleted. */ /** @file FUObject.h This file contains the FUObject class, the FUObjectContainer class and the FUObjectType class. */ #ifndef _FU_OBJECT_H_ #define _FU_OBJECT_H_ class FUObjectContainer; #include "FUtils/FUObjectType.h" /** A contained object. Each object holds a pointer to the container that contains it. This pointer is useful so that the container can be notified if the object gets released by someone else. @ingroup FUtils */ class FCOLLADA_EXPORT FUObject { private: FUObjectContainer* container; static class FUObjectType* baseObjectType; protected: /** Retrieves the container of the object. @return A pointer to the container. */ inline FUObjectContainer* GetContainer() { return container; } inline const FUObjectContainer* GetContainer() const { return container; } /**< See above. */ public: /** Constructor. Although it is not an abstract class, class is not meant to be used directly. This constructor informs the container of the object's creation. @param container The container that contains this object. */ FUObject(FUObjectContainer* container); /** Destructor. This function informs the container of this object's release. */ ~FUObject(); /** Retrieves the type of the base object class. @return The type of the base object class. */ static const FUObjectType& GetClassType() { return *baseObjectType; } /** Retrieves the type of the object class. @return The type of the base object class. */ virtual const FUObjectType& GetObjectType() const { return *baseObjectType; } }; /** A dynamically-sized array of contained objects. */ typedef vector FUObjectList; /** An object container Each container holds a list of contained objects. It will release all the objects when it is released and the objects inform it of their creation/destruction. @ingroup FUtils */ class FCOLLADA_EXPORT FUObjectContainer { private: friend class FUObject; FUObjectList objects; void RegisterObject(FUObject* object); void UnregisterObject(FUObject* object); public: /** Constructor: empty. */ FUObjectContainer() {} /** Destructor. Releases all the objects contained within this container. */ virtual ~FUObjectContainer(); protected: /** Retrieves the list of objects contained within the container. This list should be accessed for debugging and testing purposes only. @return The list of contained objects. */ inline const FUObjectList& GetObjects() const { return objects; } }; #endif // _FU_OBJECT_H_