source: rrlib_design_patterns/factory/tFactory.h @ 65:fa85d487f8e3

Last change on this file since 65:fa85d487f8e3 was 65:fa85d487f8e3, checked in by Tobias Föhst <foehst@…>, 8 years ago

Added and updated license information

File size: 4.1 KB
Line 
1//
2// You received this file as part of RRLib
3// Robotics Research Library
4//
5// Copyright (C) Finroc GbR (finroc.org)
6//
7// This program is free software; you can redistribute it and/or modify
8// it under the terms of the GNU General Public License as published by
9// the Free Software Foundation; either version 2 of the License, or
10// (at your option) any later version.
11//
12// This program is distributed in the hope that it will be useful,
13// but WITHOUT ANY WARRANTY; without even the implied warranty of
14// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15// GNU General Public License for more details.
16//
17// You should have received a copy of the GNU General Public License along
18// with this program; if not, write to the Free Software Foundation, Inc.,
19// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20//
21//----------------------------------------------------------------------
22/*!\file    rrlib/design_patterns/factory/tFactory.h
23 *
24 * \author  Tobias Foehst
25 *
26 * \date    2011-01-07
27 *
28 * \brief   Contains tFactory
29 *
30 * \b tFactory
31 *
32 */
33//----------------------------------------------------------------------
34#ifndef __rrlib__design_patterns__factory__tFactory_h__
35#define __rrlib__design_patterns__factory__tFactory_h__
36
37//----------------------------------------------------------------------
38// External includes (system with <>, local with "")
39//----------------------------------------------------------------------
40#include <map>
41#include <functional>
42
43//----------------------------------------------------------------------
44// Internal includes with ""
45//----------------------------------------------------------------------
46
47//----------------------------------------------------------------------
48// Debugging
49//----------------------------------------------------------------------
50#include <cassert>
51
52//----------------------------------------------------------------------
53// Namespace declaration
54//----------------------------------------------------------------------
55namespace rrlib
56{
57namespace design_patterns
58{
59
60//----------------------------------------------------------------------
61// Forward declarations / typedefs / enums
62//----------------------------------------------------------------------
63
64//----------------------------------------------------------------------
65// Class declaration
66//----------------------------------------------------------------------
67//!
68/*!
69 *
70 */
71template <
72typename TAbstractProduct,
73         typename TIdentifier,
74         typename TProductCreator = std::function<TAbstractProduct *()>,
75         template <typename, typename> class TUnknownKeyPolicy = factory::ThrowException
76         >
77class tFactory
78{
79
80//----------------------------------------------------------------------
81// Public methods and typedefs
82//----------------------------------------------------------------------
83public:
84
85  virtual ~tFactory()
86  {};
87
88  const bool Register(const TIdentifier &id, const TProductCreator &product_creator)
89  {
90    return this->id_to_creator_map.insert(std::make_pair(id, product_creator)).second;
91  }
92
93  template <typename TProduct>
94  const bool Register(const TIdentifier &id)
95  {
96    return this->Register(id, factory::DefaultNewCreator<TProduct>());
97  }
98
99  const bool Unregister(const TIdentifier &id)
100  {
101    return this->id_to_creator_map.erase(id) == 1;
102  }
103
104  template <typename ... TArguments>
105  TAbstractProduct *Create(const TIdentifier &id, const TArguments &... arguments) const
106  {
107    typename std::map<TIdentifier, TProductCreator>::const_iterator it = this->id_to_creator_map.find(id);
108    if (it != this->id_to_creator_map.end())
109    {
110      return (it->second)(arguments...);
111    }
112    return TUnknownKeyPolicy<TAbstractProduct, TIdentifier>::OnUnknownType(id);
113  }
114
115//----------------------------------------------------------------------
116// Private fields and methods
117//----------------------------------------------------------------------
118private:
119
120  std::map<TIdentifier, TProductCreator> id_to_creator_map;
121
122};
123
124//----------------------------------------------------------------------
125// End of namespace declaration
126//----------------------------------------------------------------------
127}
128}
129
130#endif
Note: See TracBrowser for help on using the repository browser.