libjmmcg  release_579_6_g8cffd
A C++ library containing an eclectic mix of useful, advanced components.
factory_impl.hpp
Go to the documentation of this file.
1 /******************************************************************************
2 ** Copyright © 2005 by J.M.McGuiness, coder@hussar.me.uk
3 **
4 ** This library is free software; you can redistribute it and/or
5 ** modify it under the terms of the GNU Lesser General Public
6 ** License as published by the Free Software Foundation; either
7 ** version 2.1 of the License, or (at your option) any later version.
8 **
9 ** This library is distributed in the hope that it will be useful,
10 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 ** Lesser General Public License for more details.
13 **
14 ** You should have received a copy of the GNU Lesser General Public
15 ** License along with this library; if not, write to the Free Software
16 ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18 
19 namespace jmmcg { namespace LIBJMMCG_VER_NAMESPACE { namespace factory {
20 
21  template<typename ID_, class Ret_, class Except_> inline Ret_
22  not_found<ID_, Ret_, Except_>::execute(const ID_ &id) noexcept(false) {
23  throw exception_type(_T("Unable to create object with the given id."), info::function(__LINE__, __PRETTY_FUNCTION__, typeid(&not_found<ID_, Ret_, Except_>::execute), info::function::argument(_T("const ID_ &"), tostring(id))), JMMCG_REVISION_HDR(_T(LIBJMMCG_VERSION_NUMBER)));
24  }
25 
26  template<typename ID_,class Obj_,class Except_,class Ret_,class NotFound_,typename CreatFn_,class Cont_> inline
27  base<ID_,Obj_,Except_,Ret_,NotFound_,CreatFn_,Cont_>::base(void) {
28  }
29 
30  template<typename ID_,class Obj_,class Except_,class Ret_,class NotFound_,typename CreatFn_,class Cont_> inline
31  base<ID_,Obj_,Except_,Ret_,NotFound_,CreatFn_,Cont_>::~base(void) {
32  }
33 
34  template<typename ID_,class Obj_,class Except_,class Ret_,class NotFound_,typename CreatFn_,class Cont_> inline bool
35  base<ID_,Obj_,Except_,Ret_,NotFound_,CreatFn_,Cont_>::insert(const value_type &createable) {
36  return createables_.insert(createable).second;
37  }
38 
39  template<typename ID_,class Obj_,class Except_,class Ret_,class NotFound_,typename CreatFn_,class Cont_> inline bool
40  base<ID_,Obj_,Except_,Ret_,NotFound_,CreatFn_,Cont_>::insert(const id_type &id,const createable_type &createable) {
41  return insert(value_type(id,createable));
42  }
43 
44  template<typename ID_,class Obj_,class Except_,class Ret_,class NotFound_,typename CreatFn_,class Cont_> inline bool
45  base<ID_,Obj_,Except_,Ret_,NotFound_,CreatFn_,Cont_>::find(const id_type &id) const {
46  return createables_.find(id)!=createables_.end();
47  }
48 
49  template<typename ID_,class Obj_,class Except_,class Ret_,class NotFound_,typename CreatFn_,class Cont_> inline bool
50  base<ID_,Obj_,Except_,Ret_,NotFound_,CreatFn_,Cont_>::empty(void) const noexcept(true) {
51  return createables_.empty();
52  }
53 
54  template<typename ID_,class Obj_,class Except_,class Ret_,class NotFound_,typename CreatFn_,class Cont_> inline const typename base<ID_,Obj_,Except_,Ret_,NotFound_,CreatFn_,Cont_>::size_type
55  base<ID_,Obj_,Except_,Ret_,NotFound_,CreatFn_,Cont_>::size(void) const noexcept(true) {
56  return createables_.size();
57  }
58 
59  template<typename ID_,class Obj_,class Except_,class Ret_,class NotFound_,typename CreatFn_,class Cont_> inline void
60  base<ID_,Obj_,Except_,Ret_,NotFound_,CreatFn_,Cont_>::reserve(const size_type sz) {
61  return createables_.reserve(sz);
62  }
63 
64  template<typename ID_,class Obj_,class Except_,class Ret_,class NotFound_,typename CreatFn_,class Cont_> inline const typename base<ID_,Obj_,Except_,Ret_,NotFound_,CreatFn_,Cont_>::container_type &
65  base<ID_,Obj_,Except_,Ret_,NotFound_,CreatFn_,Cont_>::createables(void) const noexcept(true) {
66  return createables_;
67  }
68 
69  template<typename ID_,class Obj_,class Except_,class Ret_,class NotFound_,typename CreatFn_,class Cont_> inline typename base<ID_,Obj_,Except_,Ret_,NotFound_,CreatFn_,Cont_>::container_type &
70  base<ID_,Obj_,Except_,Ret_,NotFound_,CreatFn_,Cont_>::createables(void) noexcept(true) {
71  return createables_;
72  }
73 
74  template<typename ID_,class Obj_,class Except_,class Ret_,class NotFound_,typename CreatFn_,class Cont_> inline constexpr
75  creator<ID_,Obj_,Except_,Ret_,NotFound_,CreatFn_,Cont_>::creator(void)
76  : base<ID_,Obj_,Except_,Ret_,NotFound_,CreatFn_,Cont_>() {
77  }
78 
79  template<typename ID_,class Obj_,class Except_,class Ret_,class NotFound_,typename CreatFn_,class Cont_> inline
80  creator<ID_,Obj_,Except_,Ret_,NotFound_,CreatFn_,Cont_>::~creator(void) {
81  }
82 
83  template<typename ID_,class Obj_,class Except_,class Ret_,class NotFound_,typename CreatFn_,class Cont_> inline typename creator<ID_,Obj_,Except_,Ret_,NotFound_,CreatFn_,Cont_>::created_type
84  creator<ID_,Obj_,Except_,Ret_,NotFound_,CreatFn_,Cont_>::make(const id_type &id) const {
85  const typename Cont_::const_iterator createable(base_t::createables_.find(id));
86  if (createable!=base_t::createables_.end()) {
87  return createable->second();
88  }
89  return NotFound_::execute(id);
90  }
91 
92  template<typename ID_,class Obj_,class Except_,class Ret_,class NotFound_,typename CreatFn_,class Cont_> inline constexpr
93  clone<ID_,Obj_,Except_,Ret_,NotFound_,CreatFn_,Cont_>::clone(void)
94  : base<ID_,Obj_,Except_,Ret_,NotFound_,CreatFn_,Cont_>() {
95  }
96 
97  template<typename ID_,class Obj_,class Except_,class Ret_,class NotFound_,typename CreatFn_,class Cont_> inline
98  clone<ID_,Obj_,Except_,Ret_,NotFound_,CreatFn_,Cont_>::~clone(void) {
99  }
100 
101  template<typename ID_,class Obj_,class Except_,class Ret_,class NotFound_,typename CreatFn_,class Cont_> inline typename clone<ID_,Obj_,Except_,Ret_,NotFound_,CreatFn_,Cont_>::created_type
102  clone<ID_,Obj_,Except_,Ret_,NotFound_,CreatFn_,Cont_>::make(const id_type &id) const {
103  const typename Cont_::const_iterator createable(base_t::createables_.find(id));
104  if (createable!=base_t::createables_.end()) {
105  const createable_type creator(createable->second);
106  return creator.second(*creator.first);
107  }
108  return NotFound_::execute(id);
109  }
110 
111 } } }