iLab Neuromorphic Robotics Toolkit  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
FactoryImpl.H
Go to the documentation of this file.
1 /*! @file
2  @author
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_DESIGN_DETAILS_FACTORYIMPL_H
36 #define INCLUDE_NRT_CORE_DESIGN_DETAILS_FACTORYIMPL_H
37 
38 #include <nrt/Core/Debugging/Log.H>
40 #include <sstream>
41 
42 // ######################################################################
43 inline nrt::exception::FactoryException::FactoryException(std::string const & problem) throw()
44 try : nrt::exception::Exception("Factory Exception, and further error while constructing the exception")
45  { theProblem = problem; whatptr = theProblem.c_str(); } catch (...) { }
46 
47 // ######################################################################
48 template <class Product, typename IDtype, class ProductCreator, class MetaDataType>
50 registerType(IDtype const & idString, ProductCreator creator, MetaDataType const & metaData)
51 {
52  std::vector<std::string> splitVec = nrt::splitString(idString, '|');
53 
54  for (std::string const & id : splitVec)
55  {
56  bool success = _creatorCallbacks.
57  insert(typename CallbackMap_t::value_type(id, std::make_pair(creator, metaData))).second;
58 
59  if (!success)
60  {
61  std::ostringstream problem;
62  problem << "Could Not Create Object ID of Type " << id;
63  throw nrt::exception::FactoryException(problem.str());
64  }
65  }
66 }
67 
68 // ######################################################################
69 template <class Product, typename IDtype, class ProductCreator, class MetaDataType>
70 typename std::result_of<ProductCreator()>::type
72 create(IDtype const & id)
73 {
74  typename CallbackMap_t::const_iterator callIt = _creatorCallbacks.find(id);
75 
76  if (callIt == _creatorCallbacks.end())
77  {
78  std::ostringstream problem;
79  problem << "No object with ID [" << id << "] found!";
80  throw nrt::exception::FactoryException(problem.str());
81  }
82 
83  return (callIt->second.first)();
84 }
85 
86 // ######################################################################
87 template <class Product, typename IDtype, class ProductCreator, class MetaDataType>
89 isRegistered(IDtype const & id)
90 {
91  return _creatorCallbacks.find(id) != _creatorCallbacks.end();
92 }
93 
94 // ######################################################################
95 template <class Product, typename IDtype, class ProductCreator, class MetaDataType>
97 getMetaData(IDtype const & id)
98 {
99  typename CallbackMap_t::const_iterator callIt = _creatorCallbacks.find(id);
100 
101  if (callIt == _creatorCallbacks.end())
102  {
103  std::ostringstream problem;
104  problem << "No object with ID [" << id << "] found!";
105  throw nrt::exception::FactoryException(problem.str());
106  }
107 
108  return (callIt->second.second);
109 }
110 
111 // ######################################################################
112 template <class Product, typename IDtype, class ProductCreator, class MetaDataType>
115 {
116  // Return all the keys from our callback map:
117  std::vector<IDtype> ids;
118  for (auto const & i : _creatorCallbacks) ids.push_back(i.first);
119  return ids;
120 }
121 
122 #endif // INCLUDE_NRT_CORE_DESIGN_DETAILS_FACTORYIMPL_H