1 // This file may be redistributed and modified only under the terms of
2 // the GNU Lesser General Public License (See COPYING for details).
3 // Copyright (C) 2000 Aloril
4 // Copyright (C) 2005 Al Riddoch
5
6 // $Id$
7
8 #ifndef ATLAS_OBJECTS_OBJECTFACTORY_H
9 #define ATLAS_OBJECTS_OBJECTFACTORY_H
10
11 #include <Atlas/Objects/Root.h>
12 #include <Atlas/Objects/SmartPtr.h>
13
14 #include <string>
15 #include <list>
16 #include <map>
17
18 namespace Atlas { namespace Objects {
19
20 class NoSuchFactoryException : public Atlas::Exception
21 {
22 protected:
23 std::string name;
24 public:
NoSuchFactoryException(const std::string & name)25 NoSuchFactoryException(const std::string& name) :
26 Atlas::Exception("No factory for Objects type"), name(name) { }
27 virtual ~NoSuchFactoryException();
getName()28 const std::string & getName() {
29 return name;
30 }
31 };
32
33 template <class T>
factory(const std::string &,int)34 static SmartPtr<RootData> factory(const std::string &, int)
35 {
36 SmartPtr<T> obj;
37 return obj;
38 }
39
40 SmartPtr<RootData> generic_factory(const std::string & name, int no);
41 SmartPtr<RootData> anonymous_factory(const std::string & name, int no);
42
43 typedef Root (*FactoryMethod)(const std::string &, int);
44 typedef std::map<const std::string, std::pair<FactoryMethod, int> > FactoryMap;
45
46 class Factories
47 {
48 public:
49 friend class AddFactories;
50
51 Factories();
52 explicit Factories(const Factories &);
53
54 bool hasFactory(const std::string& name);
55 Root createObject(const std::string& name);
56 Root createObject(const Atlas::Message::MapType & msg);
57 std::list<std::string> getKeys();
58 int addFactory(const std::string& name, FactoryMethod method);
59
60 static Factories * instance();
61 private:
62 FactoryMap m_factories;
63 static Factories * m_instance;
64
65 void addFactory(const std::string& name, FactoryMethod method, int classno);
66 };
67
68 extern std::map<const std::string, Root> objectDefinitions;
69
70 } } // namespace Atlas::Objects
71
72 #endif // ATLAS_OBJECTS_OBJECTFACTORY_H
73