UniversalManager< T, ParrentClass > Class Template Reference

base class for various manager implementation. More...

#include <universal_manager.h>

Inherits Singleton< ParrentClass >.

Inheritance diagram for UniversalManager< T, ParrentClass >:

Inheritance graph
[legend]
Collaboration diagram for UniversalManager< T, ParrentClass >:

Collaboration graph
[legend]
List of all members.

Public Types

typedef RefCounterPtr< ManagedItem,
T > 
TRef
 reference to managed type T
typedef STL_EXT_NS::hash_map<
std::string, ManagedItem * > 
TContainer
 containter of managed items
typedef std::pair< std::string,
ManagedItem * > 
TPair
 (key, data)-pair used for inserting into container of managed items
typedef UniversalManager<
T, ParrentClass > 
TUniversalManager
 self-type of this manager

Public Member Functions

int size (void)
 returns the number of items in registry
void erase (void)
 destroys all unreferenced registered elements and clears the registry

Protected Member Functions

 UniversalManager ()
 protected ctor (singleton)
 ~UniversalManager ()
virtual ManagedItemcreate (std::string key, T *data)
 callback for creating new managed item from its key
virtual void destroyContent (T *data)
 callback for destroying the data that ManagedItem wraps
virtual void destroy (ManagedItem *item)
 destroys given managed item
ManagedItemfind (std::string key)
 searches for managed item that is registered with the given key and returns null if not found
ManagedItemcreateAndRegister (std::string key, T *data)
 creates and registers new item

Protected Attributes

TContainer mRegister
 register of managed items

Private Member Functions

void OnZeroRefCount (ManagedItem *p)

Classes

class  ManagedItem
 a class wrapping some managed-information for UniversalManager More...

Detailed Description

template<class T, class ParrentClass>
class UniversalManager< T, ParrentClass >

base class for various manager implementation.

usage:

        class MyManager: public UniversalManager<MyData, MyManager>
        {
        // A. Singleton DP stuffs:
        protected:
                        MyManager(){}
                        ~MyManager(){}
                        friend class Singleton<MyManager>;

        // B. User's add(...) methods
        public:
                TRef add(std::string key, int param1)
                {
                        // I. FIND
                        ManagedItem * p;
                        if ((p = find(key)) != 0)
                                return p;

                        // II. INIT
                        MyData * pc = new MyData(param1);

                        // III. REGISTER
                        return createAndRegister(key, pc);
                }

        // C. User's create(), destroy() and destroyContent() callback-methods overriding
        // ...

        }

Derive your own manager from UniversalManager. The first template argument of UniversalManager is the desired managed type, the second *MUST* be the class name of the top-level derived manager due to Singleton stuffs (this "throws polymorphysm out of window", but extensibility is what we need). Define protected (or private) ctor and dtor as well. This is due to Singleton DP and ensures that the creation and destruction of the manager will be handled by the Singleton-class. Define your own add() methods. There you can declare parameters that are needed for adding new items (i.e. the file-name string and the font size int if you'd like to manage fonts). Each add-method should implement three parts - FIND, INIT and REGISTER. FIND-part must test if the item you are trying to add is already registered or not and return the item, if it is already registered. This can be easily done with the find(key_string) method. If the item is not registered, continue to the INIT-part, where you create the item you want to register and initialize it from arguments of add-method. Last thing to do is the registration of the item. Use the createAndRegister() method. You can redefine create(), destroy() and destroyContent() callback methods if you need to. But, in common case, this shouldn't be needed.

See also:
remarks on RefCounter (static variables issues)

Definition at line 68 of file universal_manager.h.


Member Typedef Documentation

template<class T, class ParrentClass>
typedef STL_EXT_NS::hash_map<std::string, ManagedItem *> UniversalManager< T, ParrentClass >::TContainer
 

containter of managed items

Definition at line 180 of file universal_manager.h.

template<class T, class ParrentClass>
typedef std::pair<std::string, ManagedItem *> UniversalManager< T, ParrentClass >::TPair
 

(key, data)-pair used for inserting into container of managed items

Definition at line 181 of file universal_manager.h.

template<class T, class ParrentClass>
typedef RefCounterPtr<ManagedItem, T> UniversalManager< T, ParrentClass >::TRef
 

reference to managed type T

Definition at line 179 of file universal_manager.h.

template<class T, class ParrentClass>
typedef UniversalManager<T, ParrentClass> UniversalManager< T, ParrentClass >::TUniversalManager
 

self-type of this manager

Definition at line 182 of file universal_manager.h.


Constructor & Destructor Documentation

template<class T, class ParrentClass>
UniversalManager< T, ParrentClass >::UniversalManager  )  [inline, protected]
 

protected ctor (singleton)

Definition at line 202 of file universal_manager.h.

template<class T, class ParrentClass>
UniversalManager< T, ParrentClass >::~UniversalManager  )  [inline, protected]
 

Definition at line 215 of file universal_manager.h.


Member Function Documentation

template<class T, class ParrentClass>
virtual ManagedItem* UniversalManager< T, ParrentClass >::create std::string  key,
T *  data
[inline, protected, virtual]
 

callback for creating new managed item from its key

This is used by manager when registering the item by createAndRegister()

Parameters:
key unique identifier, under its name the item will be registered
data pointer to data that the new instance of ManagedItem will contain

Definition at line 225 of file universal_manager.h.

Referenced by UniversalManager< T, ParrentClass >::createAndRegister().

template<class T, class ParrentClass>
UniversalManager< T, ParrentClass >::ManagedItem * UniversalManager< T, ParrentClass >::createAndRegister std::string  key,
T *  data
[protected]
 

creates and registers new item

This assumes that the item isn't registered yet. This is supportive method for writing user's own TRef add(key [, params]) methods.

Parameters:
key unique identifier, under its name the item will be registered
data pointer to data that the new instance of ManagedItem will contain

Definition at line 308 of file universal_manager.h.

References UniversalManager< T, ParrentClass >::create(), and UniversalManager< T, ParrentClass >::mRegister.

Here is the call graph for this function:

template<class T, class ParrentClass>
virtual void UniversalManager< T, ParrentClass >::destroy ManagedItem item  )  [inline, protected, virtual]
 

destroys given managed item

This is NOT the unregistering itself, but only the destruction routine of managed item and its data. Unregistering is done automatically by the OnZeroRefCount() callback. However, by overriding this method you are able to write your own destruction code.

Parameters:
item pointer to managed item to destroy

Definition at line 247 of file universal_manager.h.

Referenced by UniversalManager< T, ParrentClass >::erase(), and UniversalManager< T, ParrentClass >::OnZeroRefCount().

template<class T, class ParrentClass>
virtual void UniversalManager< T, ParrentClass >::destroyContent T *  data  )  [inline, protected, virtual]
 

callback for destroying the data that ManagedItem wraps

It is called by ManagedData::disableManaging() - data is filled by this instance

Parameters:
data pointer to data

Definition at line 235 of file universal_manager.h.

template<class T, class ParrentClass>
void UniversalManager< T, ParrentClass >::erase void   ) 
 

destroys all unreferenced registered elements and clears the registry

However, if there exists some managed items with non-zero reference count, they are NOT deleted, but some warning message is logged for each item and this item becomes a zombie. Zombie means, that data wrapped by this item is destroyed by manager and manager removes this item from registry and gives up ownership (zombie owns itself). Reference counting is still active and when it counts zero, zombie deletes itself.

This is used when the manager is being destroyed ( ~UniversalManager() ) or when there is the need of releasing registered items (for example, the graphical subsystem shutdown is the good moment for releasing textures - after that it wouldn't been possible).

Note:
Strongly thread-unsafe! ;-)

Definition at line 328 of file universal_manager.h.

References UniversalManager< T, ParrentClass >::destroy(), LOGW, UniversalManager< T, ParrentClass >::mRegister, and TIN.

Referenced by UniversalManager< Texture, TextureManager >::~UniversalManager().

Here is the call graph for this function:

template<class T, class ParrentClass>
UniversalManager< T, ParrentClass >::ManagedItem * UniversalManager< T, ParrentClass >::find std::string  key  )  [protected]
 

searches for managed item that is registered with the given key and returns null if not found

This is a supportive method for writing user's own add(key [, params]) methods.

Parameters:
key unique identifier, under its name the item will be registered
Returns:
item pointer on success, null if not found

Definition at line 317 of file universal_manager.h.

References UniversalManager< T, ParrentClass >::mRegister.

template<class T, class ParrentClass>
void UniversalManager< T, ParrentClass >::OnZeroRefCount ManagedItem p  )  [private]
 

Definition at line 287 of file universal_manager.h.

References assertL, UniversalManager< T, ParrentClass >::destroy(), UniversalManager< T, ParrentClass >::ManagedItem::getKey(), UniversalManager< T, ParrentClass >::mRegister, and TIN.

Here is the call graph for this function:

template<class T, class ParrentClass>
int UniversalManager< T, ParrentClass >::size void   )  [inline]
 

returns the number of items in registry

Returns:
number of registered items

Definition at line 175 of file universal_manager.h.


Member Data Documentation

template<class T, class ParrentClass>
TContainer UniversalManager< T, ParrentClass >::mRegister [protected]
 

register of managed items

Definition at line 272 of file universal_manager.h.

Referenced by UniversalManager< T, ParrentClass >::createAndRegister(), UniversalManager< T, ParrentClass >::erase(), UniversalManager< T, ParrentClass >::find(), UniversalManager< T, ParrentClass >::OnZeroRefCount(), and UniversalManager< Texture, TextureManager >::size().


The documentation for this class was generated from the following file:
Generated on Wed Apr 12 14:46:34 2006 for bjs by  doxygen 1.4.5