SMTK  @SMTK_VERSION@
Simulation Modeling Tool Kit
Resource.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_resource_Resource_h
12 #define smtk_resource_Resource_h
13 
14 #include "smtk/CoreExports.h"
15 
16 #include "smtk/common/Deprecation.h"
17 #include "smtk/common/UUID.h"
18 
19 #include "smtk/resource/Component.h"
20 #include "smtk/resource/Lock.h"
21 #include "smtk/resource/PersistentObject.h"
22 #include "smtk/resource/ResourceLinks.h"
23 
24 #include "smtk/resource/query/BadTypeError.h"
25 #include "smtk/resource/query/Queries.h"
26 
27 #include <string>
28 #include <typeindex>
29 #include <unordered_map>
30 
31 namespace smtk
32 {
33 namespace operation
34 {
35 class Operation;
36 }
37 namespace resource
38 {
46 class Key
47 {
48  friend class operation::Operation;
50  Key() = default;
51 };
52 
53 template<typename Self, typename Parent>
54 class DerivedFrom;
55 
56 class Manager;
57 class Metadata;
58 
60 class SMTKCORE_EXPORT Resource : public PersistentObject
61 {
62 public:
63  typedef std::size_t Index;
67  typedef query::Queries Queries;
68 
69  friend class Manager;
70 
71  template<typename Child, typename Parent>
72  friend class DerivedFrom;
73 
74  static const Resource::Index type_index;
75 
79  ~Resource() override;
80 
83  virtual Index index() const { return std::type_index(typeid(*this)).hash_code(); }
84 
87  template<class ResourceType>
88  bool isOfType() const
89  {
90  return this->isOfType(std::type_index(typeid(ResourceType)).hash_code());
91  }
92 
95  virtual bool isOfType(const Index& index) const;
96 
99  virtual bool isOfType(const std::string& typeName) const;
100 
104  virtual int numberOfGenerationsFromBase(const std::string& typeName) const;
105 
108  const smtk::common::UUID& id() const override { return m_id; }
109  const std::string& location() const { return m_location; }
110 
111  bool setId(const smtk::common::UUID& myID) override;
112  bool setLocation(const std::string& location);
113 
119  std::string name() const override;
120  bool setName(const std::string& name);
121  bool isNameSet() { return !m_name.empty(); }
122 
132  virtual bool clean() const { return m_clean; }
133  void setClean(bool state = true);
134 
137  void setMarkedForRemoval(bool val) { m_markedForRemoval = val; }
138 
140  virtual bool isMarkedForRemoval() const { return m_markedForRemoval; }
142  ManagerPtr manager() const { return m_manager.lock(); }
143 
145  virtual ComponentPtr find(const smtk::common::UUID& compId) const = 0;
146 
148  virtual Component* component(const smtk::common::UUID& compId) const;
149 
156  template<typename ComponentType>
157  ComponentType* componentAs(const smtk::common::UUID& uuid) const
158  {
159  return dynamic_cast<ComponentType*>(this->component(uuid));
160  }
161 
165  virtual std::function<bool(const Component&)> queryOperation(const std::string&) const;
166 
168  virtual void visit(std::function<void(const ComponentPtr&)>& v) const = 0;
169 
172  SMTK_DEPRECATED_IN_21_11("Replaced by Resource::filter().")
173  ComponentSet find(const std::string& queryString) const { return this->filter(queryString); }
174  ComponentSet filter(const std::string& queryString) const;
175 
179  template<typename Collection>
180  SMTK_DEPRECATED_IN_21_11("Replaced by Resource::filterAs().")
181  Collection findAs(const std::string& queryString) const
182  {
183  return this->filterAs<Collection>(queryString);
184  }
185  template<typename Collection>
186  Collection filterAs(const std::string& queryString) const;
187 
188  Links& links() override { return m_links; }
189  const Links& links() const override { return m_links; }
190 
191  Properties& properties() override { return m_properties; }
192  const Properties& properties() const override { return m_properties; }
193 
194  const Queries& queries() const { return m_queries; }
195  Queries& queries() { return m_queries; }
196 
199  Lock& lock(Key()) const { return m_lock; }
200 
202  LockType locked() const { return m_lock.state(); }
203 
204  Resource(Resource&&) noexcept;
205 
206 protected:
207  // Derived resources should inherit
208  // smtk::resource::DerivedFrom<Self, smtk::resource::Resource>. Resource's
209  // constructors are declared private to enforce this relationship.
210  Resource(const smtk::common::UUID&, ManagerPtr manager = nullptr);
211  Resource(ManagerPtr manager = nullptr);
212 
213  WeakManagerPtr m_manager;
214 
215 private:
220  struct SetId
221  {
222  SetId(const smtk::common::UUID& uid)
223  : m_id(uid)
224  {
225  }
226 
227  void operator()(ResourcePtr& resource) const { resource->m_id = m_id; }
228 
229  smtk::common::UUID id() const { return m_id; }
230 
231  const smtk::common::UUID& m_id;
232  };
233 
238  struct SetLocation
239  {
240  SetLocation(const std::string& url)
241  : m_url(url)
242  {
243  }
244 
245  void operator()(ResourcePtr& resource) const { resource->m_location = m_url; }
246 
247  std::string location() const { return m_url; }
248 
249  const std::string& m_url;
250  };
251 
252  smtk::common::UUID m_id;
253  std::string m_location;
254  std::string m_name;
256  bool m_clean;
257 
258  Links m_links;
259  Properties m_properties;
260  Queries m_queries;
261  bool m_markedForRemoval = false;
262  mutable Lock m_lock;
263 };
264 
265 template<typename Collection>
266 Collection Resource::filterAs(const std::string& queryString) const
267 {
268  // Construct a query operation from the query string
269  auto queryOp = this->queryOperation(queryString);
270 
271  // Construct a component set to fill
272  Collection col;
273 
274  // Visit each component and add it to the set if it satisfies the query
275  smtk::resource::Component::Visitor visitor = [&](const ComponentPtr& component) {
276  if (queryOp(*component))
277  {
278  auto entry =
279  std::dynamic_pointer_cast<typename Collection::value_type::element_type>(component);
280  if (entry)
281  {
282  col.insert(col.end(), entry);
283  }
284  }
285  };
286 
287  this->visit(visitor);
288 
289  return col;
290 }
291 
298 template<typename QueryType>
299 SMTKCORE_NO_EXPORT QueryType& queryForObject(const PersistentObject& object)
300 {
301  const auto* resource = dynamic_cast<const Resource*>(&object);
302  if (!resource)
303  {
304  const auto* const component = dynamic_cast<const Component*>(&object);
305  if (component)
306  {
307  resource = component->resource().get();
308  }
309  }
310  if (resource)
311  {
312  return resource->queries().get<QueryType>();
313  }
314  throw query::BadTypeError(smtk::common::typeName<QueryType>());
315 }
316 } // namespace resource
317 } // namespace smtk
318 
319 #endif // smtk_resource_Resource_h
smtk
The main namespace for the Simulation Modeling Tool Kit (SMTK).
Definition: doc.h:33
smtk::resource::WeakManagerPtr
smtk::weak_ptr< smtk::resource::Manager > WeakManagerPtr
Definition: PublicPointerDefs.h:283
smtk::resource::Metadata
Resources are registered to a resource manager at runtime with an instance of smtk::resource::Metadat...
Definition: Metadata.h:42
smtk::resource::Resource::setMarkedForRemoval
void setMarkedForRemoval(bool val)
Mark the resource to indicate it is about to removed (meaning it is being removed from memory not nec...
Definition: Resource.h:137
smtk::resource::DerivedFrom
Derived resources are subclassed from smtk::resource::Resource.
Definition: DerivedFrom.h:32
smtk::resource::Resource::isMarkedForRemoval
virtual bool isMarkedForRemoval() const
Return whether the object is marked for removal.
Definition: Resource.h:140
smtk::resource::Resource::id
const smtk::common::UUID & id() const override
id and location are run-time intrinsics of the derived resource; we need to allow the user to reset t...
Definition: Resource.h:108
smtk::resource::Resource::componentAs
ComponentType * componentAs(const smtk::common::UUID &uuid) const
A templated version of component() that casts its result to a type.
Definition: Resource.h:157
smtk::resource::Resource::index
virtual Index index() const
index is a compile-time intrinsic of the derived resource; as such, it cannot be set.
Definition: Resource.h:83
smtk::resource::query::BadTypeError
An exception to throw when a type is requested that does not exist in a container.
Definition: BadTypeError.h:34
smtk::common::UUID
Definition: UUID.h:38
smtk::resource::detail::ResourceProperties
This specialization of smtk::resource::Properties completes aforementioned class's implementation by ...
Definition: Properties.h:409
smtk::resource::Resource::component
virtual Component * component(const smtk::common::UUID &compId) const
Given a component's UUID, return a raw pointer to the component.
Definition: Resource.cxx:92
smtk::resource::ComponentSet
std::set< smtk::resource::ComponentPtr > ComponentSet
Definition: PublicPointerDefs.h:319
smtk::resource::Resource::visit
virtual void visit(std::function< void(const ComponentPtr &)> &v) const =0
visit all components in a resource.
smtk::resource::query::Queries
A class for registering and accessing Query types.
Definition: Queries.h:30
smtk::resource::Resource::manager
ManagerPtr manager() const
Resources that are managed have a non-null pointer to their manager.
Definition: Resource.h:142
smtk::resource::ScopedLockSetGuard
A utility for holding multiple lock-guards at once.
Definition: Lock.h:107
smtk::resource::Resource::clean
virtual bool clean() const
Indicate whether the resource is in sync with its location.
Definition: Resource.h:132
smtkSharedFromThisMacro
#define smtkSharedFromThisMacro(...)
A macro to help with derived classes whose bases enable shared_from_this().
Definition: SharedFromThis.h:190
smtk::resource::Key
Operations need the ability to lock and unlock resources, but no additional access privilege is requi...
Definition: Resource.h:46
smtk::operation::Operation
Operation is a base class for all SMTK operations.
Definition: Operation.h:47
smtkTypeMacro
#define smtkTypeMacro(...)
Add typedefs to a class for identifcation.
Definition: SharedFromThis.h:51
smtk::resource::Component::resource
virtual const ResourcePtr resource() const =0
Components are required to return their owning resource (if any).
smtk::resource::Resource::isOfType
bool isOfType() const
given a resource type, return whether or not this resource is or is derived from the resource describ...
Definition: Resource.h:88
smtkSuperclassMacro
#define smtkSuperclassMacro(...)
Add a typedef to the superclass of this class.
Definition: SharedFromThis.h:87
smtk::resource::Resource::queryOperation
virtual std::function< bool(const Component &)> queryOperation(const std::string &) const
given a std::string describing a query, return a functor for performing the query (accepts component ...
Definition: Resource.cxx:97
smtk::common::typeName
std::string typeName()
Return the name of a class.
Definition: TypeName.h:276
smtk::resource::Resource
An abstract base class for SMTK resources.
Definition: Resource.h:60
smtk::resource::Lock
A read/write lock for resources.
Definition: Lock.h:42
smtk::resource::Resource::lock
Lock & lock(Key()) const
classes that are granted permission to the key may retrieve the resource's lock.
Definition: Resource.h:199
smtk::resource::Component
Component is the base class for records stored in an smtk::resource::Resource.
Definition: Component.h:43
smtk::resource::Resource::locked
LockType locked() const
Anyone can query whether or not the resource is locked.
Definition: Resource.h:202
smtk::resource::Manager
A resource Manager is responsible for tracking currently allocated resources, creating new resources ...
Definition: Manager.h:47
smtk::resource::PersistentObject
An abstract base class for SMTK resources and their components.
Definition: PersistentObject.h:39
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::resource::queryForObject
SMTKCORE_NO_EXPORT QueryType & queryForObject(const PersistentObject &object)
Given an object, return a query for interrogating it.
Definition: Resource.h:299
smtk::resource::ResourcePtr
smtk::shared_ptr< smtk::resource::Resource > ResourcePtr
Definition: PublicPointerDefs.h:295