ImFusion C++ SDK 4.5.0
ImFusion::PolymorphicSubProperty< T > Class Template Reference

#include <ImFusion/Core/PolymorphicSubProperty.h>

This class extends SubProperty to represent a nested record of a polymorphic Configurable entity. More...

Inheritance diagram for ImFusion::PolymorphicSubProperty< T >:

Detailed Description

template<typename T>
class ImFusion::PolymorphicSubProperty< T >

This class extends SubProperty to represent a nested record of a polymorphic Configurable entity.

In contrast to a regular SubProperty (modeling value semantics for the nested entities), this class enables you to model polymorphic semantics where the template type of PolymorphicSubProperty defines a base interface but you can also store, serialize and deserialize derived classes. Therefore, you must meet the following requirements:

  • The base interface T must provide a method with the signature std::unique_ptr<T> clone() const to duplicate instances.
  • The base interface T must provide a method with the signature std::string id() const that returns a unique identifier (ID) for the concrete class specialization type of an runtime instance.
  • You must provide a factory function with the signature std::unique_ptr<T> (const std::string& id) to the PolymorphicSubProperty constructor that can create instances of the concrete classes based on the IDs returned by id(). This factory function will be used during deserialization.

Example usage:

// Base interface providing id() and clone() as pure virtual methods
struct Shape : public Configurable {
virtual ~Shape() = default;
virtual std::string id() const = 0;
virtual std::unique_ptr<Shape> clone() const = 0;
};
// Specializations of the base interface, each implementing id() and clone()
struct Circle : public Shape {
std::string id() const override { return "Circle"; }
std::unique_ptr<Shape> clone() const override {
return std::make_unique<Circle>(*this);
}
Parameter<double> radius = {"radius", 1.0, this};
};
struct Rectangle : public Shape {
std::string id() const override { return "Rectangle"; }
std::unique_ptr<Shape> clone() const override {
}
Parameter<double> width = {"width", 1.0, this};
Parameter<double> height = {"height", 1.0, this};
};
// Factory function to create Shape instances from their IDs
std::unique_ptr<Shape> createShapeFromId(const std::string& id) {
if (id == "Circle") return std::make_unique<Circle>();
if (id == "Rectangle") return std::make_unique<Rectangle>();
return nullptr;
}
// Configurable class containing a PolymorphicSubProperty of type Shape
struct Document : public Configurable {
PolymorphicSubProperty<Shape> p_shape;
Document()
: shape("shape", std::make_unique<Circle>(), *this, createShapeFromId)
{
}
};
Base interface for classes that support object serialization from/to Properties objects.
Definition Configurable.h:77
T make_unique(T... args)

The signal signalValueChanged is only emitted whenever the nested object is changed using setValue(), configure(), or if its Configurable::signalParametersChanged was emitted.

By convention, public SubProperties members use the p_ prefix for the variable name (instead of the common m_ prefix) in order to make their semantics easier to identify in code.

Note
Due to the way deserialization is implemented, you can not have multiple PolymorphicSubProperty instances with the same name next to each other.
Warning
The id <nullptr> is reserved by the system for representing null pointer values during serialization. Objects must not return <nullptr> from their id() method, as this will cause conflicts with the internal null handling mechanism. When a PolymorphicSubProperty contains a null pointer, it will be serialized with the special ID <nullptr> and properly restored as nullptr during deserialization.
Template Parameters
TUnderlying element type that will be wrapped in a std::unique_ptr. T must implement std::string id() const and std::unique_ptr<T> clone() const methods.
See also
Parameter, SubProperty, SubPropertyList

Public Types

using CreateFromId = std::function<std::unique_ptr<T>(const std::string&)>
using Base = SubProperty<std::unique_ptr<T>>

Public Member Functions

template<typename U>
 PolymorphicSubProperty (const std::string &name, U &&value, Configurable &parent, CreateFromId createFromId)
 Creates a PolymorphicSubProperty.
 PolymorphicSubProperty (const PolymorphicSubProperty &other)
 PolymorphicSubProperty (PolymorphicSubProperty &&)=default
PolymorphicSubPropertyoperator= (const PolymorphicSubProperty &other)
PolymorphicSubPropertyoperator= (PolymorphicSubProperty &&)=default
void configure (const Properties *p) override
 Configure this parameter/sub property by de-serializing the given Properties.
void configuration (Properties *p) const override
 Serialize the current parameter/sub property state into the given Properties object.
SubProperty< T > & operator= (const SubProperty< T > &other)
 Assigns the value and attributes from another SubProperty instance of the same type and emits signalValueChanged.
SubProperty< T > & operator= (SubProperty< T > &&other)
 Assigns the value and attributes from another SubProperty instance of the same type and emits signalValueChanged.
SubProperty< T > & operator= (U &&value)
 Assign a new value to this SubProperty, emits signalValueChanged.
void setValue (U &&value)
 Assign a new value to this SubProperty, emits signalValueChanged.
const T & value () const
 Access the underlying value.
T & value ()
Public Member Functions inherited from ImFusion::SubProperty< std::unique_ptr< T > >
 SubProperty (const std::string &name, U &&value)
 Creates a SubProperty that will be configured under the given name and with the given initial value.
 SubProperty (const std::string &name, U &&value, Configurable *parent)
 Create a SubProperty that is directly registered to the parent Configurable instance.
 SubProperty (const std::string &name, U &&value, Configurable &parent)
 SubProperty (const SubProperty< T > &other)
 SubProperty (SubProperty< T > &&other)
SubProperty< T > & operator= (const SubProperty< T > &other)
 Assigns the value and attributes from another SubProperty instance of the same type and emits signalValueChanged.
SubProperty< T > & operator= (SubProperty< T > &&other)
 Assigns the value and attributes from another SubProperty instance of the same type and emits signalValueChanged.
SubProperty< T > & operator= (U &&value)
 Assign a new value to this SubProperty, emits signalValueChanged.
void setValue (U &&value)
 Assign a new value to this SubProperty, emits signalValueChanged.
const T & value () const
 Access the underlying value.
T & value ()
const T * operator-> () const
 Access the underlying value.
T * operator-> ()
void configure (const Properties *p) override
 Configure this parameter/sub property by de-serializing the given Properties.
void configuration (Properties *p) const override
 Serialize the current parameter/sub property state into the given Properties object.
Public Member Functions inherited from ImFusion::ParameterBase
 ParameterBase (const std::string &name)
const Configurable * parent () const
 Return the parent Configurable instance that this parameter is registered with.
virtual const std::stringname () const
 Returns parameter name, can only be set during construction.
void setAttribute (const std::string &key, const std::string &value)
 Add the given attribute key-value pair to the set of parameter attributes.
void removeAttribute (const std::string &key)
 Remove the attribute with the given key.
const std::map< std::string, std::string > & attributes () const
 Get the attribute map.
void setLabel (const std::string &label)
 Sets an optional label for the Parameter if displayed in the UI.
void addDeprecatedName (const std::string &deprecatedName)
 Adds an optional alternative parameter name that should be used for param lookup for during configure() in case de-serialization from m_name was not successful.
const std::vector< std::string > & deprecatedNames () const
 Returns the list of deprecated parameter names that should be used for param lookup (see addDeprecatedName()).
Public Member Functions inherited from ImFusion::SignalReceiver
 SignalReceiver ()=default
 Default constructor.
 SignalReceiver (const SignalReceiver &other)
 Copy constructor, does not copy any existing signal connections from other.
SignalReceiveroperator= (SignalReceiver rhs)
 Assignment operator, disconnects all existing connections, does not copy any existing signal connections from rhs.
virtual ~SignalReceiver ()
 Virtual destructor disconnects from all connected signals.

Additional Inherited Members

Public Attributes inherited from ImFusion::ParameterBase
Signal signalValueChanged
 Signal gets emitted when the underlying value of the Parameter/SubProperty has changed (either through setValue() or configure()).
Protected Member Functions inherited from ImFusion::ParameterBase
void setParentFromCopy (const ParameterBase &other)
 Helper function to be called from a copy/move ctor in order to correctly set the parent relation ship to the new parent Configurable.
Protected Member Functions inherited from ImFusion::SignalReceiver
void disconnectAll ()
 Disconnects all existing connections.
Protected Attributes inherited from ImFusion::SubProperty< std::unique_ptr< T > >
m_value
Protected Attributes inherited from ImFusion::ParameterBase
Configurable * m_parent = nullptr
const std::string m_name
std::vector< std::stringm_deprecatedNames
std::map< std::string, std::stringm_attributes

Constructor & Destructor Documentation

◆ PolymorphicSubProperty()

template<typename T>
template<typename U>
ImFusion::PolymorphicSubProperty< T >::PolymorphicSubProperty ( const std::string & name,
U && value,
Configurable & parent,
CreateFromId createFromId )

Creates a PolymorphicSubProperty.

Parameters
nameThe name under which this property will be serialized
valueInitial value for the property
parentParent Configurable instance for registration
createFromIdFactory function that creates objects from id strings. Note: The id <nullptr> is handled automatically by the system and should not be handled by this factory function.

Member Function Documentation

◆ configure()

template<typename T>
void ImFusion::PolymorphicSubProperty< T >::configure ( const Properties * p)
overridevirtual

Configure this parameter/sub property by de-serializing the given Properties.

Implements ImFusion::ParameterBase.

◆ configuration()

template<typename T>
void ImFusion::PolymorphicSubProperty< T >::configuration ( Properties * p) const
overridevirtual

Serialize the current parameter/sub property state into the given Properties object.

Implements ImFusion::ParameterBase.

◆ operator=() [1/2]

template<typename T>
SubProperty< T > & ImFusion::SubProperty< T >::operator= ( const SubProperty< T > & other)

Assigns the value and attributes from another SubProperty instance of the same type and emits signalValueChanged.

Expects name and deprecated names to be the same and will not change the parent relationship. Use *this = other.value() if you want to assign only the value from other

◆ operator=() [2/2]

template<typename T>
SubProperty< T > & ImFusion::SubProperty< T >::operator= ( SubProperty< T > && other)

Assigns the value and attributes from another SubProperty instance of the same type and emits signalValueChanged.

Expects name and deprecated names to be the same and will not change the parent relationship. Use *this = other.value() if you want to assign only the value from other


The documentation for this class was generated from the following file:
  • ImFusion/Core/PolymorphicSubProperty.h
Search Tab / S to search, Esc to close