iLab Neuromorphic Robotics Toolkit  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Factory.H
Go to the documentation of this file.
1 /*! @file
2  @author Rand Voorhies
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_FACTORY_H
36 #define INCLUDE_NRT_CORE_DESIGN_FACTORY_H
37 
40 #include <map>
41 #include <functional>
42 
43 namespace nrt
44 {
45  /*! \defgroup factory Factory related classes and definitions
46  \ingroup design */
47 
48 
49  //! A generic factory
50  /*! The factory holds a collection of product creators, and each is given an ID. One can then create products for any
51  of the registered IDs. In addition, one can store and later retrieve some metadata with each product creator.
52 
53  \tparam Product product type
54  \tparam IDtype type used to represent IDs (typically, std:string)
55  \tparam ProductCreator how Product is created
56  \tparam MetaDataType type used to represent metadata
57 
58  \ingroup factory */
59  template <class Product, typename IDtype, class ProductCreator = std::function<Product*()>,
60  class MetaDataType = std::map<std::string, std::string> >
61  class Factory : public Singleton<Factory<Product, IDtype, ProductCreator, MetaDataType> >
62  {
63  public:
64  //! Register a new product creator (and associated meta-data) under a new ID
65  /*! \note A copy of metaData is stored in the factory, so if your meta-data is large, you may want to define
66  MetaDataType as an std::shared_ptr */
67  void registerType(IDtype const & id, ProductCreator creator, MetaDataType const & metaData = MetaDataType());
68 
69  //! Create a product for a given ID
70  typename std::result_of<ProductCreator()>::type create(IDtype const & id);
71 
72  //! Check whether an ID has been registered
73  bool isRegistered(IDtype const & id);
74 
75  //! Return the metadata for a given ID
76  MetaDataType const & getMetaData(IDtype const & id);
77 
78  //! Return a list of all registered IDs
79  std::vector<IDtype> getIDs();
80 
81  private:
82  typedef std::map<IDtype, std::pair<ProductCreator, MetaDataType> > CallbackMap_t;
83  CallbackMap_t _creatorCallbacks;
84  };
85 
86  namespace exception
87  {
88  //! Exception thrown if some problem is encountered in Factory
89  /*! \ingroup factory */
90  class FactoryException : public Exception
91  {
92  public:
93  //! Constructor from descriptive string
94  FactoryException(std::string const & problem) throw();
95 
96  //! Destructor
97  virtual ~FactoryException() throw() = default;
98 
99  private:
100  std::string theProblem;
101  };
102  }
103 
104 } // namespace nrt
105 
106 // Include implementation details of no interest to end users
108 
109 #endif // INCLUDE_NRT_CORE_DESIGN_FACTORY_H
110