SMTK
@SMTK_VERSION@
Simulation Modeling Tool Kit
|
11 #ifndef smtk_resource_Resource_h
12 #define smtk_resource_Resource_h
14 #include "smtk/CoreExports.h"
16 #include "smtk/common/Deprecation.h"
17 #include "smtk/common/UUID.h"
19 #include "smtk/resource/Component.h"
20 #include "smtk/resource/Lock.h"
21 #include "smtk/resource/PersistentObject.h"
22 #include "smtk/resource/ResourceLinks.h"
24 #include "smtk/resource/query/BadTypeError.h"
25 #include "smtk/resource/query/Queries.h"
29 #include <unordered_map>
53 template<
typename Self,
typename Parent>
63 typedef std::size_t Index;
71 template<
typename Child,
typename Parent>
74 static const Resource::Index type_index;
83 virtual Index
index()
const {
return std::type_index(
typeid(*
this)).hash_code(); }
87 template<
class ResourceType>
90 return this->isOfType(std::type_index(
typeid(ResourceType)).hash_code());
95 virtual bool isOfType(
const Index& index)
const;
99 virtual bool isOfType(
const std::string&
typeName)
const;
104 virtual int numberOfGenerationsFromBase(
const std::string&
typeName)
const;
109 const std::string& location()
const {
return m_location; }
112 bool setLocation(
const std::string& location);
119 std::string name()
const override;
120 bool setName(
const std::string& name);
121 bool isNameSet() {
return !m_name.empty(); }
132 virtual bool clean()
const {
return m_clean; }
133 void setClean(
bool state =
true);
156 template<
typename ComponentType>
159 return dynamic_cast<ComponentType*
>(this->component(uuid));
165 virtual std::function<bool(
const Component&)> queryOperation(
const std::string&)
const;
168 virtual void visit(std::function<
void(
const ComponentPtr&)>& v)
const = 0;
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;
179 template<
typename Collection>
180 SMTK_DEPRECATED_IN_21_11(
"Replaced by Resource::filterAs().")
181 Collection findAs(const std::
string& queryString)
const
183 return this->filterAs<Collection>(queryString);
185 template<
typename Collection>
186 Collection filterAs(
const std::string& queryString)
const;
188 Links& links()
override {
return m_links; }
189 const Links& links()
const override {
return m_links; }
191 Properties& properties()
override {
return m_properties; }
192 const Properties& properties()
const override {
return m_properties; }
194 const Queries& queries()
const {
return m_queries; }
195 Queries& queries() {
return m_queries; }
202 LockType
locked()
const {
return m_lock.state(); }
227 void operator()(
ResourcePtr& resource)
const { resource->m_id = m_id; }
240 SetLocation(
const std::string& url)
245 void operator()(
ResourcePtr& resource)
const { resource->m_location = m_url; }
247 std::string location()
const {
return m_url; }
249 const std::string& m_url;
253 std::string m_location;
259 Properties m_properties;
261 bool m_markedForRemoval =
false;
265 template<
typename Collection>
266 Collection Resource::filterAs(
const std::string& queryString)
const
279 std::dynamic_pointer_cast<typename Collection::value_type::element_type>(
component);
282 col.insert(col.end(), entry);
287 this->
visit(visitor);
298 template<
typename QueryType>
301 const auto* resource =
dynamic_cast<const Resource*
>(&object);
304 const auto*
const component =
dynamic_cast<const Component*
>(&object);
307 resource = component->
resource().get();
312 return resource->queries().get<QueryType>();
319 #endif // smtk_resource_Resource_h
The main namespace for the Simulation Modeling Tool Kit (SMTK).
Definition: doc.h:33
smtk::weak_ptr< smtk::resource::Manager > WeakManagerPtr
Definition: PublicPointerDefs.h:283
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
Links is a virtual class describing the API for connecting one resource/component to another resource...
Definition: Links.h:29
Derived resources are subclassed from smtk::resource::Resource.
Definition: DerivedFrom.h:32
virtual bool isMarkedForRemoval() const
Return whether the object is marked for removal.
Definition: Resource.h:140
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
ComponentType * componentAs(const smtk::common::UUID &uuid) const
A templated version of component() that casts its result to a type.
Definition: Resource.h:157
virtual Index index() const
index is a compile-time intrinsic of the derived resource; as such, it cannot be set.
Definition: Resource.h:83
An exception to throw when a type is requested that does not exist in a container.
Definition: BadTypeError.h:34
This specialization of smtk::resource::Properties completes aforementioned class's implementation by ...
Definition: Properties.h:518
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
The ResourceLinks class is a resource-specific API for manipulating unidirectional links from a Resou...
Definition: ResourceLinks.h:71
std::set< smtk::resource::ComponentPtr > ComponentSet
Definition: PublicPointerDefs.h:319
virtual void visit(std::function< void(const ComponentPtr &)> &v) const =0
visit all components in a resource.
A class for registering and accessing Query types.
Definition: Queries.h:30
ManagerPtr manager() const
Resources that are managed have a non-null pointer to their manager.
Definition: Resource.h:142
A utility for holding multiple lock-guards at once.
Definition: Lock.h:107
virtual bool clean() const
Indicate whether the resource is in sync with its location.
Definition: Resource.h:132
#define smtkSharedFromThisMacro(...)
A macro to help with derived classes whose bases enable shared_from_this().
Definition: SharedFromThis.h:190
Operations need the ability to lock and unlock resources, but no additional access privilege is requi...
Definition: Resource.h:46
Operation is a base class for all SMTK operations.
Definition: Operation.h:51
#define smtkTypeMacro(...)
Add typedefs to a class for identifcation.
Definition: SharedFromThis.h:51
virtual const ResourcePtr resource() const =0
Components are required to return their owning resource (if any).
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
#define smtkSuperclassMacro(...)
Add a typedef to the superclass of this class.
Definition: SharedFromThis.h:87
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
std::string typeName()
Return the name of a class.
Definition: TypeName.h:286
An abstract base class for SMTK resources.
Definition: Resource.h:60
A read/write lock for resources.
Definition: Lock.h:42
Lock & lock(Key()) const
classes that are granted permission to the key may retrieve the resource's lock.
Definition: Resource.h:199
Component is the base class for records stored in an smtk::resource::Resource.
Definition: Component.h:43
LockType locked() const
Anyone can query whether or not the resource is locked.
Definition: Resource.h:202
A resource Manager is responsible for tracking currently allocated resources, creating new resources ...
Definition: Manager.h:47
An abstract base class for SMTK resources and their components.
Definition: PersistentObject.h:39
smtk::shared_ptr< smtk::resource::Manager > ManagerPtr
Definition: PublicPointerDefs.h:281
smtk::shared_ptr< smtk::resource::Component > ComponentPtr
Definition: PublicPointerDefs.h:297
SMTKCORE_NO_EXPORT QueryType & queryForObject(const PersistentObject &object)
Given an object, return a query for interrogating it.
Definition: Resource.h:299
smtk::shared_ptr< smtk::resource::Resource > ResourcePtr
Definition: PublicPointerDefs.h:295