00001 #ifndef fl_factory_h
00002 #define fl_factory_h
00003
00004
00005 #include <string>
00006 #include <map>
00007
00008
00009 namespace fl
00010 {
00011 template<class B>
00012 class Factory
00013 {
00014 public:
00015 typedef B * productFunction (std::istream & stream);
00016 typedef std::map<std::string, productFunction *> productMap;
00017
00018 static B * read (std::istream & stream)
00019 {
00020 std::string name;
00021 getline (stream, name);
00022 typename productMap::iterator entry = products.find (name);
00023 if (entry == products.end ())
00024 {
00025 std::string error = "Unknown class name in stream: ";
00026 error += name;
00027 throw error.c_str ();
00028 }
00029 return (*entry->second) (stream);
00030 }
00031
00032 static productMap products;
00033 };
00034
00035 template<class B, class C>
00036 class Product
00037 {
00038 public:
00039 static B * read (std::istream & stream)
00040 {
00041 return new C (stream);
00042 }
00043 static void add ()
00044 {
00045 Factory<B>::products.insert (make_pair (typeid (C).name (), &read));
00046 }
00047 };
00048 }
00049
00050
00051 #endif