Go to the documentation of this file.00001 #ifndef FORIA_IDATALOADER_HH
00002 #define FORIA_IDATALOADER_HH
00003
00004 #include "boost/smart_ptr.hpp"
00005 #include <vector>
00006 #include <string>
00007 #include <stdexcept>
00008 #include <typeinfo>
00009 #include <iostream>
00010
00011 namespace ForIA {
00012
00013 class IDataLoader;
00014
00015 typedef boost::shared_ptr<IDataLoader> DataLoaderPtr;
00016
00017 using std::vector;
00018 using std::string;
00019 using std::type_info;
00020
00024 class NoFileException : public std::runtime_error{
00025
00026 public:
00027 NoFileException(const string &fileName):
00028 std::runtime_error(fileName + " could not be accessed!"){};
00029
00030 NoFileException(): std::runtime_error("No file opened for reading"){}
00031 };
00032
00034
00051 class IDataLoader{
00052
00053 public:
00054
00056 virtual ~IDataLoader(){}
00057
00063 static DataLoaderPtr create();
00064
00070 template <class T>
00071 T* retrieve(const string &objectName) throw(NoFileException){
00072 T *object = new T();
00073 const type_info &type = typeid(*object);
00074 delete object;
00075 object=0;
00076 void *tmp = retrieve(objectName, type);
00077 if(tmp != 0){
00078 object = (T*)tmp;
00079 }
00080 return object;
00081 }
00082
00085 virtual bool open(const string &file) throw(NoFileException) =0;
00086
00088 virtual bool close()=0;
00089
00090 private:
00091
00092 virtual void* retrieve(const string &name,
00093 const type_info &type) throw(NoFileException)=0;
00094
00095 class ICreator{
00096
00097 public:
00098 virtual DataLoaderPtr create() const = 0;
00099 virtual ~ICreator(){};
00100 };
00101
00102 static vector<const ICreator*> &s_creators();
00103
00104 public:
00105
00116 template <class T>
00117 class Creator: public ICreator{
00118
00119 public:
00123 Creator(){
00124 IDataLoader::s_creators().push_back(this);
00125 }
00126
00130 DataLoaderPtr create() const{
00131 return DataLoaderPtr(new T());
00132 }
00133
00137 ~Creator(){
00138 for(vector<const ICreator*>::iterator c = s_creators().begin();
00139 c != s_creators().end(); ++c){
00140 if((*c) == this){
00141 s_creators().erase(c);
00142 break;
00143 }
00144 }
00145 }
00146 };
00147
00148 };
00149 }
00150
00151 #endif