iCub-main
FactoryT.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2007-2010 RobotCub Consortium, European Commission FP6 Project IST-004370
3  * author: Arjan Gijsberts
4  * email: arjan.gijsberts@iit.it
5  * website: www.robotcub.org
6  * Permission is granted to copy, distribute, and/or modify this program
7  * under the terms of the GNU General Public License, version 2 or any
8  * later version published by the Free Software Foundation.
9  *
10  * A copy of the license can be found at
11  * http://www.robotcub.org/icub/license/gpl.txt
12  *
13  * This program is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
16  * Public License for more details
17  */
18 
19 #ifndef LM_FACTORY_TEMPLATE__
20 #define LM_FACTORY_TEMPLATE__
21 
22 #include <map>
23 #include <vector>
24 #include <string>
25 #include <cassert>
26 #include <stdexcept>
27 #include <sstream>
28 
29 namespace iCub {
30 namespace learningmachine {
31 
40 template<class K, class T>
41 class FactoryT {
42 private:
46  std::map<K, T*> map;
47 
51  FactoryT() { }
52 
56  FactoryT(const FactoryT<K, T>& other);
57 
63  virtual ~FactoryT() {
64  for(typename std::map<K, T* >::iterator it = this->map.begin(); it != this->map.end(); it++) {
65  delete it->second;
66  }
67  }
68 
72  FactoryT<K, T>& operator=(const FactoryT<K, T>& other);
73 
74 public:
87  static FactoryT<K, T> instance;
88  return instance;
89  }
90 
101  void registerPrototype(T* prototype) {
102  assert(prototype != (T*) 0);
103 
104  if(prototype->getName() == "") {
105  throw std::runtime_error("Cannot register prototype with empty key; please specify a unique key.");
106  }
107 
108  if(this->map.count(prototype->getName()) > 0) {
109  std::ostringstream buffer;
110  buffer << "Prototype '" << prototype->getName()
111  << "' has already been registered; please specify a unique key.";
112  throw std::runtime_error(buffer.str());
113  }
114 
115  this->map[prototype->getName()] = prototype;
116  }
117 
118 
127  T* create(const K& key) {
128  if(this->map.count(key) == 0) {
129  std::ostringstream buffer;
130  buffer << "Could not find prototype '" << key
131  << "'; please specify a valid key.";
132  throw std::runtime_error(buffer.str());
133  }
134 
135  return this->map.find(key)->second->clone();
136  }
137 
143  std::vector<K> getKeys() {
144  std::vector<K> v;
145  for(typename std::map<K,T*>::iterator it = this->map.begin(); it != this->map.end(); ++it) {
146  v.push_back(it->first);
147  }
148  return v;
149  }
150 };
151 
152 } // learningmachine
153 } // iCub
154 
155 #endif
A template class for the factory pattern.
Definition: FactoryT.h:41
T * create(const K &key)
Creates a new object given a specific type of key.
Definition: FactoryT.h:127
void registerPrototype(T *prototype)
Registers a prototype object that can be used to create clones.
Definition: FactoryT.h:101
static FactoryT< K, T > & instance()
An instance retrieval method that follows the Singleton pattern.
Definition: FactoryT.h:86
std::vector< K > getKeys()
Returns a vector of the registered keys.
Definition: FactoryT.h:143
This file contains the definition of unique IDs for the body parts and the skin parts of the robot.