libjmmcg  release_579_6_g8cffd
A C++ library containing an eclectic mix of useful, advanced components.
factory.hpp
Go to the documentation of this file.
1 #ifndef LIBJMMCG_CORE_FACTORY_HPP
2 #define LIBJMMCG_CORE_FACTORY_HPP
3 
4 /******************************************************************************
5 ** Copyright © 2005 by J.M.McGuiness, coder@hussar.me.uk
6 **
7 ** This library is free software; you can redistribute it and/or
8 ** modify it under the terms of the GNU Lesser General Public
9 ** License as published by the Free Software Foundation; either
10 ** version 2.1 of the License, or (at your option) any later version.
11 **
12 ** This library 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 GNU
15 ** Lesser General Public License for more details.
16 **
17 ** You should have received a copy of the GNU Lesser General Public
18 ** License along with this library; if not, write to the Free Software
19 ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21 
22 #include "exception.hpp"
23 #include "multimap.hpp"
24 #include "shared_ptr.hpp"
25 
26 namespace jmmcg { namespace LIBJMMCG_VER_NAMESPACE { namespace factory {
27 
28  /// The error information if a requested key-type is not registered with the factory.
29  template<
30  typename ID_, ///< The type of the key.
31  class Ret_, ///< The type that the key should have returned.
32  class Excpt_ ///< The base exception class from which this should derive to place it in the appropriate position in the exception hierarchy.
33  >
34  struct not_found {
35  typedef Excpt_ exception_type;
36 
37  /// This function throws the above exception for the specified ID_ type.
38  static Ret_ __fastcall execute(const ID_ &);
39  };
40 
41  /// The base factory-type.
42  /**
43  Note that this class is threading-agnostic.
44  */
45  template<
46  typename ID_, ///< The key-type of the factory.
47  class Obj_, ///< The type of item to be returned by the key-type.
48  class Except_, ///< The exception-type to root the exception hierarchy onto.
49  class Ret_, ///< Exactly how the item returned by the key-type should be returned, e.g. via transfer of ownership.
50  class NotFound_, ///< The type of exception thrown if the specified key-type is not registered with the factory.
51  typename CreatFn_, ///< Some kind of function or functor that is used to create the item to be returned from a specific key-type.
52  class Cont_ ///< The type of associative container that should be used by the factory for identifying the method by which the specified instance of key-type should return the value-type.
53  >
54  class base {
55  public:
56  typedef Cont_ container_type;
57  typedef typename container_type::key_type id_type;
61  typedef typename container_type::size_type size_type;
62  typedef Ret_ created_type;
63 
64  base(base const &)=delete;
65  virtual __stdcall ~base();
66 
67  /// Register a specific key & manufacturing method with the factory.
68  bool __fastcall insert(const value_type &);
69  /// Register a specific key & manufacturing method with the factory.
70  bool __fastcall insert(const id_type &,const createable_type &);
71  /// Unregister a specific key from the factory.
72  const typename container_type::size_type __fastcall erase(const id_type &id) {
73  return createables_.erase(id);
74  }
75  /// Return true if that key has been registered with the factory.
76  bool __fastcall find(const id_type &) const;
77  /// Return true if any key has been registered with the factory.
78  bool __fastcall empty(void) const noexcept(true);
79  /// Return the total number of unique keys registered with the factory.
80  const size_type __fastcall size(void) const noexcept(true);
81  /// Reserve capacity for a specified number of keys with the factory.
82  void __fastcall reserve(const size_type);
83 
84  /// Access the internal associative collection used to store the keys and methods for creating the return items.
85  const container_type & __fastcall createables(void) const noexcept(true);
86  /// Access the internal associative collection used to store the keys and methods for creating the return items.
87  container_type & __fastcall createables(void) noexcept(true);
88 
89  protected:
91 
92  __stdcall base(void);
93  };
94 
95  /// A factory that manufactures a new, unique instance of the specified return-type for a specified key that has been registered with the factory.
96  /**
97  Note that this class is not thread-safe by default, at a minimum the container would need to be thread-safe.
98  */
99  template<
100  typename ID_, ///< The key-type of the factory.
101  class Obj_, ///< The type of item to be returned by the key-type.
102  class Except_, ///< The exception-type to root the exception hierarchy onto.
103  class Ret_=std::unique_ptr<Obj_>, ///< Exactly how the item returned by the key-type should be returned, e.g. via transfer of ownership.
104  class NotFound_=not_found<ID_, Ret_, Except_>, ///< The type of exception thrown if the specified key-type is not registered with the factory.
105  typename CreatFn_=Ret_ (__fastcall *)(void), ///< The type of method by which the factory may manufacture a new, unique instance of the specified item to be returned.
106  class Cont_=rapid_insert_lookup::multimap<ID_, CreatFn_> ///< The type of associative container that should be used by the factory for identifying the method by which the specified instance of key-type should return the value-type.
107  >
108  class creator : public base<ID_, Obj_, Except_, Ret_, NotFound_, CreatFn_, Cont_> {
109  public:
110  typedef base<ID_, Obj_, Except_, Ret_, NotFound_, CreatFn_, Cont_> base_t;
111  typedef typename base_t::id_type id_type;
112  typedef typename base_t::created_type created_type;
113 
114  constexpr __stdcall creator(void);
115  virtual __stdcall ~creator(void);
116 
117  /// Returns a new, unique new, unique instance of the specified return-type for a specified key that has been registered with the factory.
118  virtual created_type __fastcall make(const id_type &id) const;
119  };
120 
121  /// A factory that manufactures a clone of an instance of the specified return-type for a specified key that has been registered with the factory.
122  /**
123  Note that this class is not thread-safe by default, at a minimum the container and shared pointer-type would need to be thread-safe.
124  */
125  template<
126  typename ID_, ///< The key-type of the factory.
127  class Obj_, ///< The type of item to be returned by the key-type.
128  class Except_, ///< The exception-type to root the exception hierarchy onto.
129  class Ret_=std::unique_ptr<Obj_>, ///< Exactly how the item returned by the key-type should be returned, e.g. via transfer of ownership.
130  class NotFound_=not_found<ID_, Ret_, Except_>, ///< The type of exception thrown if the specified key-type is not registered with the factory.
131  typename CreatFn_=Ret_ (__fastcall *)(const Obj_ &), ///< Return a copy of the instance of the specified item to be returned.
132  class Cont_=rapid_insert_lookup::multimap<ID_, std::pair<shared_ptr<Obj_, ppd::api_lock_traits<ppd::platform_api, ppd::sequential_mode>>, CreatFn_> ///< The type of associative container that should be used by the factory for identifying the method by which the specified instance of key-type should return the value-type.
133  >
134  >
135  class clone : public base<ID_, Obj_, Except_, Ret_, NotFound_, CreatFn_, Cont_> {
136  public:
137  typedef base<ID_, Obj_, Except_, Ret_, NotFound_, CreatFn_, Cont_> base_t;
138  typedef typename base_t::id_type id_type;
140  typedef typename base_t::created_type created_type;
142 
143  constexpr __stdcall clone();
144  virtual __stdcall ~clone();
145 
146  /// Returns a clone of an instance of the specified return-type for a specified key that has been registered with the factory.
147  virtual created_type __fastcall make(const id_type &) const;
148  };
149 
150 } } }
151 
152 #include "factory_impl.hpp"
153 
154 #endif