openModeller  Version 1.5.0
PreFactory.hh
Go to the documentation of this file.
1 
26 #ifndef PRE_FACTORY_HH
27 #define PRE_FACTORY_HH
28 
29 #include <map>
30 #include <string>
31 
32 using namespace std;
33 
34 //adapted from Terralib to OM Pre-analysis ( Missae DPI/INPE )
35 
50 template <class T, class Arg>
52 {
53 public:
54 
55  // Dictionary of factories (indexed by name)
56  typedef map<string, PreFactory<T,Arg>* > PreFactoryMap;
57 
58  // Returns the single instance of the factory dictionay
60  {
61  static PreFactoryMap Fmap_;
62  return Fmap_;
63 
64  }
65 
66  // Normal Constructor
67  PreFactory (const string& factoryName);
68 
69  virtual ~PreFactory() {}
70 
71  // Virtual constructor using name
72  static T* make ( string name, const Arg& arg );
73 
74 protected:
75 
76  // Builds a new type (should be implemented by descendants)
77  virtual T* build ( const Arg& arg ) = 0;
78 
79 private:
80  string Fname_;
81 };
82 
83 // Constructor
84 template <class T, class Arg>
85 PreFactory<T,Arg>::PreFactory(const string& name):
86  Fname_(name)
87 {
88  PreFactory<T,Arg>::instance()[name] = this;
89 }
90 
91 // Builds an object, based on the input parameters
92 template <class T, class Arg>
93 T*
94 PreFactory<T,Arg>::make ( string name, const Arg& arg )
95 {
96  // try to find the name on the factory dictionary
97  typename PreFactoryMap::iterator i = PreFactory<T,Arg>::instance().find ( name );
98 
99  // Not found ?
100  if ( i == PreFactory<T,Arg>::instance().end() )
101  {
102  std::string msg = "PreFactory<T,Arg>::make( string name, const Arg& arg ): Not found the name on the factory\n";
103  Log::instance()->error( msg.c_str() );
104  throw InvalidParameterException( msg );
105  }
106 
107  // Create an object, based on the input parameters
108  return (*i).second->build ( arg );
109  return 0;
110 
111 }
112 
113 #endif
114 
virtual ~PreFactory()
Definition: PreFactory.hh:69
static PreFactoryMap & instance()
Definition: PreFactory.hh:59
static Log * instance()
Returns the instance pointer, creating the object on the first call.
Definition: Log.cpp:45
PreFactory(const string &factoryName)
Definition: PreFactory.hh:85
void error(const char *format,...)
'Error' level.
Definition: Log.cpp:290
string Fname_
Definition: PreFactory.hh:80
static T * make(string name, const Arg &arg)
Definition: PreFactory.hh:94
map< string, PreFactory< T, Arg > * > PreFactoryMap
Definition: PreFactory.hh:56