iLab Neuromorphic Robotics Toolkit  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ComponentFactoryImpl.H
Go to the documentation of this file.
1 /*! @file
2  @author Laurent Itti
3  @copyright GNU Public License (GPL v3)
4  @section License
5  @verbatim
6  // ////////////////////////////////////////////////////////////////////////
7  // The iLab Neuromorphic Robotics Toolkit (NRT) //
8  // Copyright 2010-2012 by the University of Southern California (USC) //
9  // and the iLab at USC. //
10  // //
11  // iLab - University of Southern California //
12  // Hedco Neurociences Building, Room HNB-10 //
13  // Los Angeles, Ca 90089-2520 - USA //
14  // //
15  // See http://ilab.usc.edu for information about this project. //
16  // ////////////////////////////////////////////////////////////////////////
17  // This file is part of The iLab Neuromorphic Robotics Toolkit. //
18  // //
19  // The iLab Neuromorphic Robotics Toolkit is free software: you can //
20  // redistribute it and/or modify it under the terms of the GNU General //
21  // Public License as published by the Free Software Foundation, either //
22  // version 3 of the License, or (at your option) any later version. //
23  // //
24  // The iLab Neuromorphic Robotics Toolkit is distributed in the hope //
25  // that it will be useful, but WITHOUT ANY WARRANTY; without even the //
26  // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR //
27  // PURPOSE. See the GNU General Public License for more details. //
28  // //
29  // You should have received a copy of the GNU General Public License //
30  // along with The iLab Neuromorphic Robotics Toolkit. If not, see //
31  // <http://www.gnu.org/licenses/>. //
32  // ////////////////////////////////////////////////////////////////////////
33  @endverbatim */
34 
35 #ifndef INCLUDE_NRT_CORE_MODEL_DETAILS_COMPONENTFACTORYIMPL_H
36 #define INCLUDE_NRT_CORE_MODEL_DETAILS_COMPONENTFACTORYIMPL_H
37 
40 #include <nrt/Core/Debugging/Log.H>
42 
43 #include <sstream>
44 
45 // ######################################################################
46 template <typename IDtype, class Creator, class MetaDataType>
48 registerType(IDtype const & idString, Creator creator, MetaDataType const & metaData)
49 {
50  std::vector<std::string> splitVec = nrt::splitString(idString, '|');
51 
52  for (std::string const & id : splitVec)
53  {
54  bool success = _creatorCallbacks.
55  insert(typename CallbackMap_t::value_type(id, std::make_pair(creator, metaData))).second;
56 
57  if (!success)
58  {
59  std::ostringstream problem;
60  problem << "Could Not Create Object ID of Type " << id;
61  throw nrt::exception::FactoryException(problem.str());
62  }
63  }
64 }
65 
66 // ######################################################################
67 template <typename IDtype, class Creator, class MetaDataType>
68 typename std::result_of<Creator(nrt::Component *, std::string const &)>::type
70 create(IDtype const & id, nrt::Component * parent, std::string const & instanceName)
71 {
72  typename CallbackMap_t::const_iterator callIt = _creatorCallbacks.find(id);
73 
74  if (callIt == _creatorCallbacks.end())
75  {
76  std::ostringstream problem;
77  problem << "No object with ID [" << id << "] found!";
78  throw nrt::exception::FactoryException(problem.str());
79  }
80 
81  return (callIt->second.first)(parent, instanceName);
82 }
83 
84 // ######################################################################
85 template <typename IDtype, class Creator, class MetaDataType>
87 isRegistered(IDtype const & id)
88 {
89  return _creatorCallbacks.find(id) != _creatorCallbacks.end();
90 }
91 
92 // ######################################################################
93 template <typename IDtype, class Creator, class MetaDataType>
95 getMetaData(IDtype const & id)
96 {
97  typename CallbackMap_t::const_iterator callIt = _creatorCallbacks.find(id);
98 
99  if (callIt == _creatorCallbacks.end())
100  {
101  std::ostringstream problem;
102  problem << "No object with ID [" << id << "] found!";
103  throw nrt::exception::FactoryException(problem.str());
104  }
105 
106  return (callIt->second.second);
107 }
108 
109 // ######################################################################
110 template <typename IDtype, class Creator, class MetaDataType>
113 {
114  // Return all the keys from our callback map:
115  std::vector<IDtype> ids;
116  for (auto const & i : _creatorCallbacks) ids.push_back(i.first);
117  return ids;
118 }
119 
120 #endif // INCLUDE_NRT_CORE_MODEL_DETAILS_COMPONENTFACTORYIMPL_H