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   void unregisterObject (const string &name, bool deleteObject = false);
00082 
00083   void purge ();
00084 
00085 private:
00086   // Member variables
00087   typedef map<string, T*> DataMap;
00088   DataMap _registry;
00089 
00090 public:
00091   // Singleton related functions
00092   static C *getInstance ();
00093   static void kill ();
00094 
00095 private:
00096   // The unique instance of this class
00097   static C *_singleton;
00098 };
00099 
00100 // Include inline function definitions
00101 #include "DataManager.inl"
00102 
00103 #endif // __DATAMANAGER_H__