RSC  0.17.1
ObservableFactory.h
Go to the documentation of this file.
1 /* ============================================================
2  *
3  * This file is part of the RSC project
4  *
5  * Copyright (C) 2010-2016 Jan Moringen
6  *
7  * This file may be licensed under the terms of the
8  * GNU Lesser General Public License Version 3 (the ``LGPL''),
9  * or (at your option) any later version.
10  *
11  * Software distributed under the License is distributed
12  * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
13  * express or implied. See the LGPL for the specific language
14  * governing rights and limitations.
15  *
16  * You should have received a copy of the LGPL along with this
17  * program. If not, go to http://www.gnu.org/licenses/lgpl.html
18  * or write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  * The development of this software was supported by:
22  * CoR-Lab, Research Institute for Cognition and Robotics
23  * Bielefeld University
24  *
25  * ============================================================ */
26 
27 #pragma once
28 
29 #include <boost/signals2.hpp>
30 
31 #include "Factory.h"
32 
33 namespace rsc {
34 namespace patterns {
35 
42 template<typename Key, typename Interface>
43 class ObservableFactory: public Factory<Key, Interface> {
44 protected:
46 public:
48 
49  typedef boost::signals2::signal<void (const std::string&, const CreateFunction&)>
51 
52  typedef boost::signals2::signal<void (const std::string&, const CreateFunction&)>
54 
59  signalImplAdded() throw ();
60 
65  signalImplRemoved() throw ();
66 protected:
67  typedef typename base::ImplMap ImplMap;
68 
71 
75  void register_(const Key& key, const CreateFunction& create_function_);
76 
80  void unregister(const Key& key);
81 };
82 
86 template<typename Key, typename Interface>
88  Key, Interface> > , public ObservableFactory<Key, Interface> {
89  friend class Singleton<SingletonFactory<Key, Interface> > ;
90 private:
92 };
93 
94 // ObservableFactory implementation
95 
96 template<typename Key, typename Interface>
99  return this->signal_impl_added_;
100 }
101 
102 template<typename Key, typename Interface>
105  return this->signal_impl_removed_;
106 }
107 
108 template<typename Key, typename Interface>
110  const CreateFunction& create_function_) {
111  base::register_(key, create_function_);
112 
113  //
114  if (!this->signal_impl_added_.empty()) {
115  this->signal_impl_added_(key, create_function_);
116  }
117 }
118 
119 template<typename Key, typename Interface>
121  //
122  if (!this->signal_impl_removed_.empty()) {
123  typename ImplMap::iterator it;
124  if ((it = this->impl_map_.find(key)) != this->impl_map_.end()) {
125  this->signal_impl_removed_(it->first, it->second);
126  }
127  }
128 
129  base::unregister(key);
130 }
131 
132 // ObservableSingletonFactory implementation
133 
134 template<typename Key, typename Interface>
136 }
137 
138 }
139 }
boost::signals2::signal< void(const std::string &, const CreateFunction &)> ImplAddedSignal
virtual void register_(const Key &key, const CreateFunction &create_function_)
Definition: Factory.h:308
A factory of which at most one instance exists at any time.
Definition: Factory.h:246
Objects of this class manage a family of named implementations of a particular interface.
Definition: Factory.h:129
virtual void unregister(const Key &key)
Definition: Factory.h:322
boost::function1< void *, const runtime::Properties & > CreateFunction
Definition: Factory.h:71
This template class implements the singleton pattern.
Definition: Singleton.h:51
void register_(const Key &key, const CreateFunction &create_function_)
Factory< Key, Interface > base
base::CreateFunction CreateFunction
ImplRemovedSignal & signalImplRemoved()
Return the "implementation removed" signal.
A specialized factory class objects of which emit signals when implementations are registered or unre...
ImplAddedSignal & signalImplAdded()
Return the "implementation added" signal.
An observable factory of which at most one instance exists at any time.
std::map< Key, CreateFunction > ImplMap
Definition: Factory.h:74
boost::signals2::signal< void(const std::string &, const CreateFunction &)> ImplRemovedSignal