SMTK
@SMTK_VERSION@
Simulation Modeling Tool Kit
|
10 #ifndef smtk_common_Instances_h
11 #define smtk_common_Instances_h
13 #include "smtk/common/Factory.h"
14 #include "smtk/common/Observers.h"
41 template<
typename BaseType,
typename... InputTypes>
56 : m_observers([this](
Observer& observer) {
57 for (
const auto& instance : m_instances)
67 void operator=(
const Instances&) =
delete;
70 using Superclass::unregisterType;
73 using Superclass::contains;
76 template<
typename Type,
typename... Args>
77 std::shared_ptr<Type>
create(Args&&... args)
79 std::shared_ptr<BaseType> instance = this->Superclass::createFromIndex(
80 typeid(Type).hash_code(), std::forward<Args>(args)...);
83 m_instances.insert(instance);
86 return std::static_pointer_cast<Type>(instance);
90 template<
typename... Args>
93 std::shared_ptr<BaseType> instance =
94 this->Superclass::createFromName(
typeName, std::forward<Args>(args)...);
97 m_instances.insert(instance);
104 template<
typename... Args>
105 std::shared_ptr<BaseType>
createFromIndex(
const std::size_t& typeIndex, Args&&... args)
107 std::shared_ptr<BaseType> instance =
108 this->Superclass::createFromIndex(typeIndex, std::forward<Args>(args)...);
111 m_instances.insert(instance);
122 bool manage(
const std::shared_ptr<BaseType>& instance)
128 bool didManage = m_instances.insert(instance).second;
141 bool unmanage(
const std::shared_ptr<BaseType>& instance)
147 auto it = m_instances.find(instance);
148 if (it == m_instances.end())
152 m_instances.erase(it);
158 bool contains(
const std::shared_ptr<BaseType>& instance)
160 return m_instances.find(instance) != m_instances.end();
166 for (
const auto& instance : m_instances)
181 for (
const auto& instance : m_instances)
192 std::size_t
size()
const {
return m_instances.size(); }
196 using Container = std::set<std::shared_ptr<BaseType>>;
198 Container m_instances;
The main namespace for the Simulation Modeling Tool Kit (SMTK).
Definition: doc.h:33
@ Halt
Stop visiting items immediately.
std::function< smtk::common::Visit(const std::shared_ptr< smtk::task::Task > &)> Visitor
The signature used to visit managed instances of objects.
Definition: Instances.h:52
The Instances class is used to manage instances of objects which share an inherited base type (the Ba...
Definition: Instances.h:42
@ Unmanaged
An instance of a managed class was removed from the instance manager.
Visit
Return values common to most visitor methods.
Definition: Visit.h:23
@ Continue
Continue to visit items.
std::shared_ptr< BaseType > createFromName(const std::string &typeName, Args &&... args)
Create and manage an instance of Type using its type-name.
Definition: Instances.h:91
@ Managed
An instance of a managed class was added to the instance manager.
A Factory is a class that constructs instances of registered classes which all inherit a common BaseT...
Definition: Factory.h:112
std::shared_ptr< Type > create(Args &&... args)
Create an instance of the given Type and manage it.
Definition: Instances.h:77
InstanceEvent
Events that an instance-manager can observe.
Definition: Instances.h:23
void clear()
Unmanage all instances.
Definition: Instances.h:164
std::string typeName()
Return the name of a class.
Definition: TypeName.h:286
smtk::common::Visit visit(Visitor visitor) const
Iterate over the collection of instances, invoking a visitor on each.
Definition: Instances.h:179
bool unmanage(const std::shared_ptr< BaseType > &instance)
Unmanage (drop the reference to) an instance.
Definition: Instances.h:141
std::shared_ptr< BaseType > createFromIndex(const std::size_t &typeIndex, Args &&... args)
Create and manage an instance of a Type using its type-index.
Definition: Instances.h:105
std::size_t size() const
Return the number of instances being managed.
Definition: Instances.h:192
Observers & observers()
Return the set of observers of instances (so that you can insert an observer).
Definition: Instances.h:174
smtk::common::Instances< smtk::task::Adaptor, void, std::tuple< smtk::task::Adaptor::Configuration & >, std::tuple< smtk::task::Adaptor::Configuration &, smtk::task::Task *, smtk::task::Task * > > Instances
Track smtk::task::Adaptor objects with smtk::common::Instances.
Definition: Instances.h:39
std::function< void(InstanceEvent, const std::shared_ptr< smtk::task::Task > &)> Observer
The signature of observers watching managed instance lifecycle events.
Definition: Instances.h:48
bool manage(const std::shared_ptr< BaseType > &instance)
Manage an already-created instance of a class.
Definition: Instances.h:122
Instances()
Construct a manager of object instances.
Definition: Instances.h:55
bool contains(const std::shared_ptr< BaseType > &instance)
Determine whether instance is managed or not.
Definition: Instances.h:158