SMTK  @SMTK_VERSION@
Simulation Modeling Tool Kit
EvaluatorManager.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 #ifndef smtk_attribute_EvaluatorManager_h
11 #define smtk_attribute_EvaluatorManager_h
12 
13 #include "smtk/CoreExports.h"
14 #include "smtk/PublicPointerDefs.h"
15 
16 #include "smtk/attribute/Resource.h"
17 
18 #include "smtk/common/TypeName.h"
19 
20 #include "smtk/resource/Manager.h"
21 #include "smtk/resource/Observer.h"
22 
23 #include <memory>
24 
25 namespace smtk
26 {
27 namespace attribute
28 {
29 
30 // A manager for registering Evaluators to attribute resources'
31 // EvaluatorFactory
32 //
33 // EvaluatorManager is an interface for registering Evaluators. SMTK Plugins can
34 // interact with the EvaluatorManager by adding methods similar to the following
35 // to their Registrar:
36 //
37 // registerTo(const smtk::attribute::EvaluatorManager::Ptr& manager)
38 // {
39 // manager->registerEvaluator<EvaluatorT>("alias");
40 // }
41 //
42 // unregisterFrom(const smtk::attribute::EvaluatorManager::Ptr& manager)
43 // {
44 // manager->unregisterEvaluator<EvaluatorT>();
45 // }
46 //
47 // Additionally, the `smtk_add_plugin()` call for the plugin should be extended
48 // to include `smtk::attribute::EvaluatorManager` in its list of managers. Upon
49 // registration, attribute resources (a) associated with the same resource
50 // manager as the EvaluatorManager or (b) constructed by an operation that is
51 // managed by an operation manager with access to the EvaluatorManager will be
52 // able to create Evaluators for Attributes whose Definitions are associated to
53 // the registered Evalators.
54 class SMTKCORE_EXPORT EvaluatorManager : public std::enable_shared_from_this<EvaluatorManager>
55 {
56 public:
58 
59  static std::shared_ptr<EvaluatorManager> create()
60  {
61  return smtk::shared_ptr<EvaluatorManager>(new EvaluatorManager());
62  }
63 
64  EvaluatorManager(const EvaluatorManager&) = delete;
66 
67  EvaluatorManager& operator=(const EvaluatorManager&) = delete;
68  EvaluatorManager& operator=(EvaluatorManager&&) = delete;
69 
70  virtual ~EvaluatorManager();
71 
72  // Registers to a resource manager. All attrribute resources managed by the
73  // resource manager (present and future) will be able to create Evalators for
74  // Attributes whose Definitions are associated to the registered Evaluators.
75  bool registerResourceManager(smtk::resource::Manager::Ptr&);
76 
77  // Explicitly add the contained Evaluator types to an attribute resource.
78  bool registerEvaluatorsTo(smtk::attribute::Resource::Ptr& resource) const
79  {
80  for (const auto& registerFunction : m_registerFunctions)
81  {
82  registerFunction.second(*resource);
83  }
84 
85  return true;
86  }
87 
88  // Registers <EvaluatorType> to all attribute resources.
89  template<typename EvaluatorType>
90  bool registerEvaluator(const std::string& alias)
91  {
92  // Construct a functor for adding the Evaluator type to an attribute
93  // resource.
94  auto registerEvaluatorType = [alias](smtk::attribute::Resource& resource) {
95  resource.evaluatorFactory().registerEvaluator<EvaluatorType>(alias);
96  };
97 
98  // Add the functor to our container of register functions.
99  m_registerFunctions.insert(
100  std::make_pair(typeid(EvaluatorType).hash_code(), registerEvaluatorType));
101 
102  // If we currently have an associated resource manager...
103  if (auto manager = m_manager.lock())
104  {
105  // ...add an observer that adds the Evaluator to all current and future
106  // attribute resources associated with this manager.
107  auto registerEvaluatorObserver = [registerEvaluatorType](
108  const smtk::resource::Resource& resource,
109  smtk::resource::EventType eventType) -> void {
110  if (eventType == smtk::resource::EventType::ADDED)
111  {
112  if (
113  const smtk::attribute::Resource* attributeResource =
114  dynamic_cast<const smtk::attribute::Resource*>(&resource))
115  {
116  registerEvaluatorType(const_cast<smtk::attribute::Resource&>(*attributeResource));
117  }
118  }
119  };
120 
121  // Associated the observer key with the Evaluator type, so we can remove
122  // it later if requested.
123  m_observers.insert(std::make_pair(
124  typeid(EvaluatorType).hash_code(),
125  manager->observers().insert(
126  registerEvaluatorObserver,
127  "Register Evaluator type <" + smtk::common::typeName<EvaluatorType>() + "> with alias " +
128  alias + ".")));
129  }
130 
131  return true;
132  }
133 
134  // Unregister <EvaluatorType> from all attribute resources.
135  template<typename EvaluatorType>
136  bool unregisterEvaluator()
137  {
138  // Remove the Evaluator from the container of register functions.
139  m_registerFunctions.erase(typeid(EvaluatorType).hash_code());
140 
141  // Also, remove the observer associated with the Evaluator, if it exists.
142  m_observers.erase(typeid(EvaluatorType).hash_code());
143 
144  // If there is an associated resource manager...
145  if (auto manager = m_manager.lock())
146  {
147  for (const auto& resource : manager->find<smtk::attribute::Resource>())
148  {
149  // ...remove the Evaluator from all of its attribute resources.
150  resource->evaluatorFactory().unregisterEvaluator<EvaluatorType>();
151  }
152  }
153 
154  return true;
155  }
156 
157 protected:
160 
161  std::unordered_map<std::size_t, std::function<void(smtk::attribute::Resource&)>>
162  m_registerFunctions;
163 
164  std::weak_ptr<smtk::resource::Manager> m_manager;
165 
166  std::unordered_map<std::size_t, smtk::resource::Observers::Key> m_observers;
167 };
168 
169 } // namespace attribute
170 } // namespace smtk
171 
172 #endif // smtk_attribute_EvaluatorManager_h
smtk
The main namespace for the Simulation Modeling Tool Kit (SMTK).
Definition: doc.h:33
PublicPointerDefs.h
smtkTypedefs
#define smtkTypedefs(...)
Used by smtkTypeMacro()
Definition: SharedFromThis.h:21
smtk::attribute::EvaluatorManager
Definition: EvaluatorManager.h:54
smtk::resource::EventType::ADDED
@ ADDED
A new resource's contents now available in memory.
smtk::resource::EventType
EventType
Enumerate events that the resource manager may encounter.
Definition: Observer.h:32
TypeName.h
Named type functions.
smtk::attribute::Resource
Store information about attribute definitions and instances.
Definition: Resource.h:77
smtk::resource::Resource
An abstract base class for SMTK resources.
Definition: Resource.h:60
smtk::resource::ManagerPtr
smtk::shared_ptr< smtk::resource::Manager > ManagerPtr
Definition: PublicPointerDefs.h:281