Public Types | Public Member Functions | List of all members
ime::PropertyContainer Class Reference

A container for ime::Property instances. More...

#include <PropertyContainer.h>

Public Types

template<typename... Args>
using Callback = std::function< void(Args...)>
 Event listener. More...
 

Public Member Functions

bool addProperty (const Property &Property)
 Add a property to the container. More...
 
bool addProperty (Property &&)
 
template<typename T >
void setValue (const std::string &name, T &&value)
 Change the value of a property. More...
 
template<typename T >
getValue (const std::string &name) const
 Get the value of a property. More...
 
bool removeProperty (const std::string &name)
 Remove a property from the container. More...
 
bool hasProperty (const std::string &name) const
 Check if the container has a given property. More...
 
bool propertyHasValue (const std::string &name) const
 Check if a property in the container has a value or not. More...
 
std::size_t getCount () const
 Get the number of properties in the container. More...
 
void forEachProperty (const Callback< Property & > &callback)
 Apply a callback to each property in the container. More...
 
int onValueChange (const std::string &name, const Callback< Property *const > &callback)
 Subscribe a callback to a value change event. More...
 
bool unsubscribe (const std::string &name, int id)
 Remove a callback from a value change event. More...
 
void clear ()
 Remove all properties from the container. More...
 

Detailed Description

A container for ime::Property instances.

Usage example:

settings.addProperty(Property("masterVolume", 100.0f));
settings.addProperty({"musicVolume", 80.0f});
settings.addProperty({"sfxVolume"});
...
settings.setValue("sfxVolume", 20.0f);
A container for ime::Property instances.
bool addProperty(const Property &Property)
Add a property to the container.

Definition at line 38 of file PropertyContainer.h.

Member Typedef Documentation

◆ Callback

template<typename... Args>
using ime::PropertyContainer::Callback = std::function<void(Args...)>

Event listener.

Definition at line 41 of file PropertyContainer.h.

Member Function Documentation

◆ addProperty()

bool ime::PropertyContainer::addProperty ( const Property Property)

Add a property to the container.

Parameters
PropertyThe property to be added
Returns
True if the property was added or false if a property with the same name already exits in the container

◆ clear()

void ime::PropertyContainer::clear ( )

Remove all properties from the container.

◆ forEachProperty()

void ime::PropertyContainer::forEachProperty ( const Callback< Property & > &  callback)

Apply a callback to each property in the container.

Parameters
callbackThe function to be applied

◆ getCount()

std::size_t ime::PropertyContainer::getCount ( ) const

Get the number of properties in the container.

Returns
The number of properties in the container

◆ getValue()

template<typename T >
T ime::PropertyContainer::getValue ( const std::string &  name) const

Get the value of a property.

Parameters
nameThe name of the property to get the value for
Exceptions
std::bad_any_castif the value stored by the property is not of type T
Returns
Value of a property

This function will throw an exception if the template argument T and the type of the stored value don't match. This means that the property must always have a value before calling this function

See also
setValue and propertyHasValue

◆ hasProperty()

bool ime::PropertyContainer::hasProperty ( const std::string &  name) const

Check if the container has a given property.

Parameters
nameThe name of the property to be checked
Returns
True if the property exists or false if the container does not have a property with the given name
See also
addProperty

◆ onValueChange()

int ime::PropertyContainer::onValueChange ( const std::string &  name,
const Callback< Property *const > &  callback 
)

Subscribe a callback to a value change event.

Parameters
nameThe name of the property to add the callback to
callbackThe function to be executed when the value changes
Returns
The unique identification of the callback

Since the value can be of any type, the callback is passed a const pointer to the property instead of the new value, so that you can retrieve it

See also
getValue and unsubscribe

◆ propertyHasValue()

bool ime::PropertyContainer::propertyHasValue ( const std::string &  name) const

Check if a property in the container has a value or not.

Parameters
nameThe name of the property to be checked
Returns
True if property has value or false if the property does not have a value or the property does not exist in the container
See also
hasProperty

◆ removeProperty()

bool ime::PropertyContainer::removeProperty ( const std::string &  name)

Remove a property from the container.

Parameters
nameThe name of the property to be removed
Returns
True if the property was removed or false if the specified property doesn't exist
See also
addProperty

◆ setValue()

template<typename T >
void ime::PropertyContainer::setValue ( const std::string &  name,
T &&  value 
)

Change the value of a property.

Parameters
nameThe name of the property to set a value for
valueThe new value of the property

Note that the type of the value of the property need not match the previous value type

See also
getValue

◆ unsubscribe()

bool ime::PropertyContainer::unsubscribe ( const std::string &  name,
int  id 
)

Remove a callback from a value change event.

Parameters
nameThe name of the property to remove the callback from
idThe unique identifier of the callback
Returns
True if the callback was removed or false a callback with the given id does not exist
See also
onValueChange

The documentation for this class was generated from the following file: