SMTK  @SMTK_VERSION@
Simulation Modeling Tool Kit
Manager.h
1 //=========================================================================
2 // Copyright (c) Kitware, Inc.
3 // All rights reserved.
4 // See LICENSE.txt for details.
5 //
6 // This software is distributed WITHOUT ANY WARRANTY; without even
7 // the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
8 // PURPOSE. See the above copyright notice for more information.
9 //=========================================================================
10 
11 #ifndef smtk_operation_Manager_h
12 #define smtk_operation_Manager_h
13 
14 #include "smtk/CoreExports.h"
15 #include "smtk/PublicPointerDefs.h"
16 #include "smtk/SharedFromThis.h"
17 
18 #include "smtk/common/Managers.h"
19 #include "smtk/common/TypeName.h"
20 
21 #include "smtk/operation/Group.h"
22 #include "smtk/operation/Launcher.h"
23 #include "smtk/operation/Metadata.h"
24 #include "smtk/operation/MetadataContainer.h"
25 #include "smtk/operation/Observer.h"
26 #include "smtk/operation/Operation.h"
27 
28 #include <array>
29 #include <string>
30 #include <tuple>
31 #include <type_traits>
32 #include <typeinfo>
33 
34 namespace smtk
35 {
36 namespace operation
37 {
42 class SMTKCORE_EXPORT Manager : smtkEnableSharedPtr(Manager)
43 {
44 public:
47 
48  virtual ~Manager();
49 
51  template<typename OperationType>
52  bool registerOperation();
53 
55  template<typename OperationType>
56  bool registerOperation(const std::string&);
57 
59  bool registerOperation(Metadata&&);
60 
62  template<typename Tuple>
64  {
65  return Manager::registerOperations<0, Tuple>();
66  }
67 
70  template<typename Tuple>
71  bool registerOperations(const std::array<std::string, std::tuple_size<Tuple>::value>& typeNames)
72  {
73  return Manager::registerOperations<0, Tuple>(typeNames);
74  }
75 
77  template<typename OperationType>
78  bool unregisterOperation();
79 
81  bool unregisterOperation(const std::string&);
82 
84  bool unregisterOperation(const Operation::Index&);
85 
86  // Unregister a tuple of operations identified by their class types.
87  template<typename Tuple>
88  bool unregisterOperations()
89  {
90  return Manager::unregisterOperations<0, Tuple>();
91  }
92 
93  bool registered(const std::string&) const;
94  bool registered(const Operation::Index&) const;
95  template<typename OperationNtype>
96  bool registered() const;
97 
99  std::shared_ptr<smtk::operation::Operation> create(const std::string&);
100 
102  std::shared_ptr<smtk::operation::Operation> create(const Operation::Index&);
103 
105  template<typename OperationType>
106  smtk::shared_ptr<OperationType> create();
107 
108  // We expose the underlying containers for metadata; this means of access
109  // should not be necessary for most use cases.
110 
112  MetadataContainer& metadata() { return m_metadata; }
113 
115  Launchers& launchers() { return m_launchers; }
116  const Launchers& launchers() const { return m_launchers; }
117 
119  Observers& observers() { return m_observers; }
120  const Observers& observers() const { return m_observers; }
121 
123  Group::Observers& groupObservers() { return m_groupObservers; }
124  const Group::Observers& groupObservers() const { return m_groupObservers; }
125 
127  Metadata::Observers& metadataObservers() { return m_metadataObservers; }
128  const Metadata::Observers& metadataObservers() const { return m_metadataObservers; }
129 
130  // Return the managers instance that contains this manager, if it exists.
131  smtk::common::Managers::Ptr managers() const { return m_managers.lock(); }
132  void setManagers(const smtk::common::Managers::Ptr& managers) { m_managers = managers; }
133 
140  bool registerResourceManager(smtk::resource::ManagerPtr&);
141 
143  std::set<std::string> availableOperations() const;
144 
147  std::set<Operation::Index> availableOperations(const smtk::resource::ComponentPtr&) const;
148 
150  std::set<std::string> availableGroups() const;
151 
152 private:
153  Manager();
154 
155  template<std::size_t I, typename Tuple>
156  inline typename std::enable_if<I != std::tuple_size<Tuple>::value, bool>::type
157  registerOperations()
158  {
159  bool registered = this->registerOperation<typename std::tuple_element<I, Tuple>::type>();
160  return registered && Manager::registerOperations<I + 1, Tuple>();
161  }
162 
163  template<std::size_t I, typename Tuple>
164  inline typename std::enable_if<I == std::tuple_size<Tuple>::value, bool>::type
165  registerOperations()
166  {
167  return true;
168  }
169 
170  template<std::size_t I, typename Tuple>
171  inline typename std::enable_if<I != std::tuple_size<Tuple>::value, bool>::type registerOperations(
172  const std::array<std::string, std::tuple_size<Tuple>::value>& typeNames)
173  {
174  bool registered =
175  this->registerOperation<typename std::tuple_element<I, Tuple>::type>(typeNames.at(I));
176  return registered && Manager::registerOperations<I + 1, Tuple>(typeNames);
177  }
178 
179  template<std::size_t I, typename Tuple>
180  inline typename std::enable_if<I == std::tuple_size<Tuple>::value, bool>::type registerOperations(
181  const std::array<std::string, std::tuple_size<Tuple>::value>&)
182  {
183  return true;
184  }
185 
186  template<std::size_t I, typename Tuple>
187  inline typename std::enable_if<I != std::tuple_size<Tuple>::value, bool>::type
188  unregisterOperations()
189  {
190  bool unregistered = this->unregisterOperation<typename std::tuple_element<I, Tuple>::type>();
191  return unregistered && Manager::unregisterOperations<I + 1, Tuple>();
192  }
193 
194  template<std::size_t I, typename Tuple>
195  inline typename std::enable_if<I == std::tuple_size<Tuple>::value, bool>::type
196  unregisterOperations()
197  {
198  return true;
199  }
200 
202  Launchers m_launchers;
203 
205  Observers m_observers;
206 
208  Group::Observers m_groupObservers;
209 
211  Metadata::Observers m_metadataObservers;
212 
214  Observers::Key m_resourceObserver;
215 
217  MetadataContainer m_metadata;
218 
221  std::weak_ptr<smtk::common::Managers> m_managers;
222 };
223 
224 template<typename OperationType>
226 {
227  return this->unregisterOperation(std::type_index(typeid(OperationType)).hash_code());
228 }
229 
230 template<typename OperationType>
231 smtk::shared_ptr<OperationType> Manager::create()
232 {
233  return smtk::static_pointer_cast<OperationType>(
234  this->create(std::type_index(typeid(OperationType)).hash_code()));
235 }
236 
237 template<typename OperationType>
239 {
240  return Manager::registerOperation<OperationType>(smtk::common::typeName<OperationType>());
241 }
242 
243 template<typename OperationType>
244 bool Manager::registerOperation(const std::string& typeName)
245 {
246  // For standard operations (i.e. those defined in C++), the pattern is to use
247  // the hash of the type_index as its index, the specification defined by
248  // OperationType::createSpecification() and the creation method
249  // OperationType::create(). This method is simply a shorthand that constructs a
250  // metadata instance that adheres to this convention.
251 
253  typeName,
254  std::type_index(typeid(OperationType)).hash_code(),
255  std::dynamic_pointer_cast<Operation>(OperationType::create())->createSpecification(),
256  []() { return OperationType::create(); }));
257 }
258 
259 template<typename OperationType>
260 bool Manager::registered() const
261 {
262  return Manager::registered(std::type_index(typeid(OperationType)).hash_code());
263 }
264 } // namespace operation
265 } // namespace smtk
266 
267 #endif // smtk_operation_Manager_h
smtk
The main namespace for the Simulation Modeling Tool Kit (SMTK).
Definition: doc.h:33
PublicPointerDefs.h
smtk::operation::Manager
An operation Manager is responsible for creating new operations and filtering operations based on inp...
Definition: Manager.h:42
smtk::operation::Metadata
Operations are registered to an operation manager at runtime with an instance of smtk::operation::Met...
Definition: Metadata.h:35
smtk::operation::Manager::groupObservers
Group::Observers & groupObservers()
Return the group observers associated with this manager.
Definition: Manager.h:123
smtk::operation::Manager::registerOperations
bool registerOperations()
Register a tuple of operations identified by their class types.
Definition: Manager.h:63
smtkTypedefs
#define smtkTypedefs(...)
Used by smtkTypeMacro()
Definition: SharedFromThis.h:21
smtk::operation::Manager::metadata
MetadataContainer & metadata()
Return the map of metadata.
Definition: Manager.h:112
smtk::operation::Manager::registerOperations
bool registerOperations(const std::array< std::string, std::tuple_size< Tuple >::value > &typeNames)
Register a tuple of operations identified by their class types and type names.
Definition: Manager.h:71
smtk::common::Observers< Observer >
smtk::operation::Manager::launchers
Launchers & launchers()
Return the launchers associated with this manager.
Definition: Manager.h:115
smtkCreateMacro
#define smtkCreateMacro(...)
Add static create() methods to a class.
Definition: SharedFromThis.h:113
smtkEnableSharedPtr
#define smtkEnableSharedPtr(...)
An abbreviation for enabling shared pointers.
Definition: SharedFromThis.h:154
smtk::operation::Observers
smtk::common::Observers< Observer > Observers
Operation observers are a specialization of the common SMTK observer pattern.
Definition: Observer.h:45
TypeName.h
Named type functions.
smtk::operation::Manager::unregisterOperation
bool unregisterOperation()
Unegister an operation identified by its class type.
Definition: Manager.h:225
smtk::operation::Launchers
A functor for executing operations and returning futures of the result.
Definition: Launcher.h:31
smtk::operation::MetadataContainer
boost::multi_index_container< Metadata, indexed_by< ordered_unique< tag< NameTag >, const_mem_fun< Metadata, const std::string &, &Metadata::typeName > >, ordered_unique< tag< IndexTag >, const_mem_fun< Metadata, const smtk::operation::Operation::Index &, &Metadata::index > > > > MetadataContainer
A multi-index container for accessing operation metadata.
Definition: MetadataContainer.h:47
smtk::plugin::Manager
smtk::common::Singleton< detail::Manager > Manager
The Manager is a singleton interface for registering available plugins to manager instances.
Definition: Manager.h:92
smtk::common::typeName
std::string typeName()
Return the name of a class.
Definition: TypeName.h:276
smtk::operation::Manager::observers
Observers & observers()
Return the observers associated with this manager.
Definition: Manager.h:119
smtk::operation::Manager::metadataObservers
Metadata::Observers & metadataObservers()
Return the metadata observers associated with this manager.
Definition: Manager.h:127
smtk::operation::Manager::registerOperation
bool registerOperation()
Register an operation identified by its class type.
Definition: Manager.h:238
SharedFromThis.h
Macros for dealing with shared-pointer classes.
smtk::resource::ManagerPtr
smtk::shared_ptr< smtk::resource::Manager > ManagerPtr
Definition: PublicPointerDefs.h:281
smtk::resource::ComponentPtr
smtk::shared_ptr< smtk::resource::Component > ComponentPtr
Definition: PublicPointerDefs.h:297
smtk::operation::Manager::create
smtk::shared_ptr< OperationType > create()
Construct an operation identified by its class type.
Definition: Manager.h:231