DataManager.h

Go to the documentation of this file.
00001 /* -*- c++ -*- */
00002 /////////////////////////////////////////////////////////////////////////////
00003 //
00004 // DataManager.h  -- Copyright (c) 2006 David Henry
00005 // last modification: feb. 21, 2006
00006 //
00007 // This code is licenced under the MIT license.
00008 //
00009 // This software is provided "as is" without express or implied
00010 // warranties. You may freely copy and compile this source into
00011 // applications you distribute provided that the copyright text
00012 // below is included in the resulting source code.
00013 //
00014 // Definitions of a data manager class.
00015 //
00016 /////////////////////////////////////////////////////////////////////////////
00017 
00018 #ifndef __DATAMANAGER_H__
00019 #define __DATAMANAGER_H__
00020 
00021 #include <stdexcept>
00022 #include <string>
00023 #include <map>
00024 
00025 using std::string;
00026 using std::map;
00027 
00028 
00029 /////////////////////////////////////////////////////////////////////////////
00030 //
00031 // class DataManagerException - Exception class for DataManager classes.
00032 // This acts like a standard runtime_error exception but
00033 // know the name of the resource which caused the exception.
00034 //
00035 /////////////////////////////////////////////////////////////////////////////
00036 
00037 class DataManagerException : public std::runtime_error
00038 {
00039 public:
00040   // Constructors
00041   DataManagerException (const string &error)
00042     : std::runtime_error (error) { }
00043   DataManagerException (const string &error, const string &name)
00044     : std::runtime_error (error), _which (name) { }
00045   virtual ~DataManagerException () throw () { }
00046 
00047 public:
00048   // Public interface
00049   virtual const char *which () const throw () {
00050     return _which.c_str ();
00051   }
00052 
00053 private:
00054   // Member variables
00055   string _which;
00056 };
00057 
00058 
00059 /////////////////////////////////////////////////////////////////////////////
00060 //
00061 // class DataManager -- a data manager which can register/unregister
00062 // generic objects.  Destroy all registred objects at death.
00063 //
00064 // The data manager is a singleton.
00065 //
00066 /////////////////////////////////////////////////////////////////////////////
00067 
00068 template <typename T, typename C>
00069 class DataManager
00070 {
00071 protected:
00072   // Constructor/destructor
00073   DataManager ();
00074   virtual ~DataManager ();
00075 
00076 public:
00077   // Public interface
00078   T *request (const string &name);
00079 
00080   void registerObject (const string &name, T *object)
00081     throw (DataManagerException);
00082   void unregisterObject (const string &name, bool deleteObject = false);
00083 
00084   void purge ();
00085 
00086 private:
00087   // Member variables
00088   typedef map<string, T*> DataMap;
00089   DataMap _registry;
00090 
00091 public:
00092   // Singleton related functions
00093   static C *getInstance ();
00094   static void kill ();
00095 
00096 private:
00097   // The unique instance of this class
00098   static C *_singleton;
00099 };
00100 
00101 // Include inline function definitions
00102 #include "DataManager.inl"
00103 
00104 #endif // __DATAMANAGER_H__