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"
42 template<
typename BaseType,
typename... InputTypes>
57 : m_observers([this](
Observer& observer) {
58 for (
const auto& instance : m_instances)
68 void operator=(
const Instances&) =
delete;
71 using Superclass::unregisterType;
74 using Superclass::contains;
77 template<
typename Type,
typename... Args>
78 std::shared_ptr<Type>
create(Args&&... args)
80 std::shared_ptr<BaseType> instance = this->Superclass::createFromIndex(
81 typeid(Type).hash_code(), std::forward<Args>(args)...);
84 m_instances.insert(instance);
87 return std::static_pointer_cast<Type>(instance);
91 template<
typename... Args>
94 std::shared_ptr<BaseType> instance =
95 this->Superclass::createFromName(
typeName, std::forward<Args>(args)...);
98 m_instances.insert(instance);
105 template<
typename... Args>
106 std::shared_ptr<BaseType>
createFromIndex(
const std::size_t& typeIndex, Args&&... args)
108 std::shared_ptr<BaseType> instance =
109 this->Superclass::createFromIndex(typeIndex, std::forward<Args>(args)...);
112 m_instances.insert(instance);
123 bool manage(
const std::shared_ptr<BaseType>& instance)
129 bool didManage = m_instances.insert(instance).second;
142 bool unmanage(
const std::shared_ptr<BaseType>& instance)
148 auto it = m_instances.find(instance);
149 if (it == m_instances.end())
153 m_instances.erase(it);
159 bool contains(
const std::shared_ptr<BaseType>& instance)
161 return m_instances.find(instance) != m_instances.end();
167 for (
const auto& instance : m_instances)
182 for (
const auto& instance : m_instances)
193 std::size_t
size()
const {
return m_instances.size(); }
197 using Container = std::set<std::shared_ptr<BaseType>>;
199 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< BaseType > &)> Visitor
The signature used to visit managed instances of objects.
Definition: Instances.h:53
The Instances class is used to manage instances of objects which share an inherited base type (the Ba...
Definition: Instances.h:43
@ 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:92
@ 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
@ Modified
An instance has been marked modified (by an operation).
std::shared_ptr< Type > create(Args &&... args)
Create an instance of the given Type and manage it.
Definition: Instances.h:78
InstanceEvent
Events that an instance-manager can observe.
Definition: Instances.h:23
void clear()
Unmanage all instances.
Definition: Instances.h:165
std::string typeName()
Return the name of a class.
Definition: TypeName.h:289
smtk::common::Visit visit(Visitor visitor) const
Iterate over the collection of instances, invoking a visitor on each.
Definition: Instances.h:180
bool unmanage(const std::shared_ptr< BaseType > &instance)
Unmanage (drop the reference to) an instance.
Definition: Instances.h:142
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:106
std::size_t size() const
Return the number of instances being managed.
Definition: Instances.h:193
Observers & observers()
Return the set of observers of instances (so that you can insert an observer).
Definition: Instances.h:175
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< BaseType > &)> Observer
The signature of observers watching managed instance lifecycle events.
Definition: Instances.h:49
bool manage(const std::shared_ptr< BaseType > &instance)
Manage an already-created instance of a class.
Definition: Instances.h:123
Instances()
Construct a manager of object instances.
Definition: Instances.h:56
bool contains(const std::shared_ptr< BaseType > &instance)
Determine whether instance is managed or not.
Definition: Instances.h:159