ImFusion SDK 4.3
Serialization.cpp
#include <ImFusion/Core/Configurable.h>
#include <ImFusion/Core/Parameter.h>
#include <ImFusion/Core/Properties.h>
#include <ImFusion/Core/SubProperty.h>
using namespace ImFusion;
// Inherit from Configurable to make your class serializable
class ExampleClass : public Configurable
{
public:
// Serializes object state to the provided Properties instance
void configuration(Properties* p) const override
{
if (!p)
return;
// store a single primitive type with default value
p->setParam("count", m_count, 42);
// store a complex type in a nested subproperty
m_nestedClass.configuration(p->addSubProperties("NestedClass"));
}
// Restores object state from the provided Properties instance
void configure(const Properties* p) override
{
if (!p)
return;
// restore a single primitive type
p->param("count", m_count);
// restore a complex type from a nested property
m_nestedClass.configure(p->subProperties("NestedClass"));
}
private:
int m_count = 42;
SomeOtherConfigurableClass m_nestedClass;
};
// More terse implementation of the same logic as above
class ExampleClass : public Configurable
{
public:
// Here we use a Parameter<T> instead of a naked T.
// Initializing it in the header communicates its parameter key and the default value to the user.
// Passing this to the constructor will register it with the Configurable interface.
Parameter<int> p_count = {"count", 42, this};
// You can nest multiple Configurable instances using the SubProperty<T> type
SubProperty<SomeOtherConfigurableClass> p_nestedClass = {"NestedClass", SomeOtherConfigurableClass(), this};
}
Base interface for classes that support object serialization from/to Properties objects.
Definition Configurable.h:77
virtual void configure(const Properties *p)
Configure this object instance by de-serializing the given Properties.
virtual void configuration(Properties *p) const
Serialize the current object configuration into the given Properties object.
The Parameter class represents a single parameter of a Configurable entity.
Definition Parameter.h:53
Storage container for serialization of arbitrary types, internally backed by strings.
Definition Properties.h:50
const Properties * subProperties(const std::string &name) const
Return (first) sub-properties instance with the specified name.
void setParam(const std::string &name, const T &value, const T &defaultValue)
Set a parameter with arbitrary type and a default value.
Definition Properties.h:424
bool param(const std::string &name, T &value) const
Return the value of a parameter of arbitrary types.
Definition Properties.h:436
Properties * addSubProperties(const std::string &name)
Add sub-properties with specified name and return the created instance.
Namespace of the ImFusion SDK.
Definition Assert.h:7
Search Tab / S to search, Esc to close