![]() |
ImFusion SDK 4.3
|
#include <ImFusion/Core/Properties.h>
Storage container for serialization of arbitrary types, internally backed by strings. More...
Storage container for serialization of arbitrary types, internally backed by strings.
Properties storage is hierarchical in the sense that a Properties instance can have any number of nested subproperties. Parameters of subproperties can be set and retrieved by a path, for example param("Views/window", val) will retrieve the value for window in the subproperties Views. The same works with setParam(), which will always create all subproperties on the way. The path separator is always /, which must not be used in the naming of parameters or subproperties.
The following types are supported for parameter serialization:
void and long doubleconst char*vecX, vecXf, vecXi, matX, matXf in sizes 2-6)If you want to store a custom type in Properties make it implement the Configurable interface, implement the pair of configure()/configuration() functions and then serialize the type into a subproperty.
Public Types | |
| enum class | Comparison { ValuesOnly = 0 , Order = 1 << 0 , Defaults = 1 << 1 , Types = 1 << 2 , Attributes = 1 << 3 , SubProperties = 1 << 4 , All = 0xFF } |
| Bitflag enumeration to define which aspects to consider during comparison of two Properties objects. More... | |
| enum class | CopyOptions { Default = 0 , ReplaceSubProperties = 1 << 0 , KeepType = 1 << 1 , KeepAttributes = 1 << 2 , ExistingParamsOnly = 1 << 3 } |
| Bitflag enumeration to configure behavior of copyFrom(). More... | |
| using | ParamType = PropertiesDetail::ParamType |
| using | Attribute = PropertiesDetail::Attribute |
| using | Param = PropertiesDetail::Param |
| using | StringAccepter = PropertiesDetail::StringAccepter |
Public Member Functions | |
| Properties (const std::string &name) | |
| Instantiates a new empty Properties instance. | |
| Properties (std::initializer_list< std::pair< std::string, StringAccepter > > list) | |
| Convenience constructor enabling you to set parameters directly using an initializer list. | |
| Properties (const Properties &p) | |
| Properties (Properties &&rhs) noexcept | |
| Properties & | operator= (const Properties &rhs) |
| Properties & | operator= (Properties &&rhs) noexcept |
| const std::string & | name () const |
| Return the name of the property. | |
| void | setName (const std::string &name) |
| Set the name of the property. | |
| bool | contains (const std::string &name) const |
| Return whether the param with the given name is part of the properties or not. | |
| bool | isEmpty () const |
| Return true if there are neither parameters nor sub-properties. | |
| void | clearParamsOnly () |
| Clear all parameters. | |
| void | clear () |
| Clear all parameters and sub properties. | |
| void | removeParam (const std::string &name) |
| Remove a single parameter. | |
| void | removeDefault (const std::string &name) |
| Remove the default for a single parameter. | |
| void | removeDefaultParams () |
| Remove all parameters which current value is their default value. | |
| void | renameParam (const std::string &oldName, const std::string &newName) |
| Rename a parameter. | |
| void | setToDefaultParams () |
| Set all parameters that have a default value to the default value. | |
Setting parameters | |
| template<class T, PROHIBIT_ENUM(T)> | |
| void | setParam (const std::string &name, const T &value, const T &defaultValue) |
| Set a parameter with arbitrary type and a default value. | |
| template<class T, REQUIRE_ENUM(T)> | |
| void | setParam (const std::string &name, const T &value, const T &defaultValue) |
| Set a parameter with arbitrary enum type and a default value. Internally uses int. | |
| template<class T, PROHIBIT_ENUM(T)> | |
| void | setParam (const std::string &name, const T &value) |
| Set a parameter with arbitrary type without default value. | |
| template<class T, REQUIRE_ENUM(T)> | |
| void | setParam (const std::string &name, const T &value) |
| Set a parameter with arbitrary enum type and a default value. Internally uses int. | |
| void | setParam (const std::string &name, const char *value, const char *defaultValue) |
| Set a parameter as C string with default value. | |
| void | setParam (const std::string &name, const std::string &value, const char *defaultValue) |
| Set a parameter only the defaultValue given as C string. | |
| void | setParam (const std::string &name, const char *value) |
| Set a parameter as C string. | |
| void | setParam (const std::string &name, const std::string &value, const std::optional< std::string > &defaultValue, ParamType type) |
| Set a parameter as string, specifying the type, passing std::nullopt as defaultValue resets the default. | |
| template<class ForwardIterator> | |
| void | setParamRange (const std::string &name, ForwardIterator first, ForwardIterator last) |
| Set a parameter to a sequence of elements. | |
Querying parameters | |
| template<class T, PROHIBIT_ENUM(T)> | |
| bool | param (const std::string &name, T &value) const |
| Return the value of a parameter of arbitrary types. | |
| template<class T, REQUIRE_ENUM(T)> | |
| bool | param (const std::string &name, T &value) const |
| Return the value of a parameter, enum version. internally uses int for representing enums. | |
| template<class T> | |
| std::optional< T > | param (const std::string &name) const |
| Return the value of a parameter of arbitrary types. | |
| template<class ForwardIterator> | |
| bool | paramRange (const std::string &name, ForwardIterator first, ForwardIterator last) const |
| Return sequence of values from a parameter into an iterator. | |
| const std::map< std::string, Param > & | paramMap () const |
| Return the map with name-value pairs for all parameters. | |
| const std::vector< std::string > & | params () const |
| Return all parameters in the order as they were inserted. | |
| std::vector< std::string > | paramsRecursive () const |
| Recursively return all parameter names and those of the sub-properties. | |
Configure global and per-parameter attributes, used for instance by default-generated GUI | |
| ParamType | paramType (const std::string &name) const |
| Return the type of a parameter. | |
| void | setParamType (const std::string &name, ParamType type) |
| Overrides the ParamType set by setParam. | |
| void | setAttributes (const std::vector< Attribute > &attributes) |
| Set global attributes for this Properties instance. | |
| const std::vector< Attribute > & | attributes () const |
| Return the list of global attributes assigned to this Properties. | |
| std::optional< std::string > | attribute (const std::string &key) const |
| Return the value of a global attribute if it exists. | |
| bool | setParamAttributes (const std::string &name, const std::vector< Attribute > &attributes) |
| Set special attributes to a single parameter. | |
| bool | setParamAttributes (const std::string &name, const std::string &attributes) |
| Set special attributes to a single parameter. | |
| std::vector< Attribute > | paramAttributes (const std::string &name) const |
| Returns a list of attributes for the parameters type. | |
| std::optional< std::string > | paramAttribute (const std::string &name, const std::string &key) const |
| Return the value of a parameter attribute if it exists. | |
| void | setParamSchema (const std::string &name, ParamType type, const std::vector< Attribute > &attributes, const std::optional< std::string > &defaultValue) |
| Convenience function to set parameter type, attributes, and default value at once. | |
Dealing with sub-properties | |
| Properties * | addSubProperties (const std::string &name) |
| Add sub-properties with specified name and return the created instance. | |
| void | addSubProperties (std::unique_ptr< Properties > p) |
| Add the provided properties instance. | |
| void | insertSubProperties (std::unique_ptr< Properties > p, int location) |
| Insert the provided properties instance at a given location. | |
| const Properties * | subProperties (const std::string &name) const |
| Return (first) sub-properties instance with the specified name. | |
| Properties * | subProperties (const std::string &name, bool createIfDoesntExist=false) |
| Return (first) sub-properties instance with the specified name and optionally creates one if it doesn't exist. | |
| PropertyList | subPropertiesAll (const std::string &name) const |
| Return all properties instances with the given name in the correct order. | |
| PropertyList | subProperties () const |
| Return a list of all sub-properties. | |
| const std::map< std::string, PropertyList > & | propertyMap () const |
| Return the map with for all sub-properties group by their name. | |
| void | deleteSubProperty (Properties *p) |
| Delete a sub property. | |
| bool | removeSubProperties (const std::string &name) |
| Remove (all) sub-properties with the specified name. | |
| bool | removeEmptySubProperties () |
| Recursively removes all empty sub properties. | |
Utility functions | |
| int | fillPlaceHolders (const std::map< std::string, std::string > &placeholders) |
Replace all occurrences of placeholders of the form %(key). | |
| std::vector< std::string > | getPlaceHolders () const |
Return list of all placeholders of the form %(key) in the properties values that have not been replaced yet. | |
| void | iterate (std::function< void(const Properties *properties, const std::string &pathAndName)> visitorFunction) const |
| Performs a recursive breadth-first iteration over this instance and all of its sub properties. | |
| void | iterate (std::function< void(Properties *properties, const std::string &pathAndName)> visitorFunction) |
| bool | compareWith (const Properties &other, Flags< Comparison > compareFlags=Comparison::All) const |
| Compares the other Properties instance with this one. | |
| void | copyFrom (const Properties &src, Flags< CopyOptions > copyOptions=CopyOptions::Default, std::function< bool(const std::string &name, const Param &value)> predicate=nullptr) |
| Copy properties from another instance You can optionally pass a predicate function, which gets evaluated for every parameter and every sub property. | |
| void | sortParams (const std::function< bool(const std::string &, const std::string &)> &lessThan=std::less< std::string >()) |
| Sort parameters by name, default is lexicographical. | |
| static bool | compare (const std::optional< Properties > &lhs, const std::optional< Properties > &rhs, Flags< Comparison > compareFlags) noexcept |
| Convenience function to compare std::optional<Properties> instances. | |
|
strong |
Bitflag enumeration to define which aspects to consider during comparison of two Properties objects.
| Enumerator | |
|---|---|
| ValuesOnly | Compare only the param values (will always be compared) |
| Order | Compare the order of the parameters. |
| Defaults | Compare the optional default values of the parameters. |
| Types | Compare the parameter types. |
| Attributes | Compare the optional parameter attributes. |
| SubProperties | Recursively compare sub properties. |
| All | Compare all aspects of the Properties object. |
|
strong |
Bitflag enumeration to configure behavior of copyFrom().
| Properties | ( | std::initializer_list< std::pair< std::string, StringAccepter > > | list | ) |
Convenience constructor enabling you to set parameters directly using an initializer list.
| void setParam | ( | const std::string & | name, |
| const T & | value, | ||
| const T & | defaultValue ) |
Set a parameter with arbitrary type and a default value.
Also works with Eigen types, standard vectors, and vectors of Eigen types.
| void setParam | ( | const std::string & | name, |
| const T & | value ) |
Set a parameter with arbitrary type without default value.
Also works with Eigen types, standard vectors, and vectors of Eigen types.
| bool param | ( | const std::string & | name, |
| T & | value ) const |
Return the value of a parameter of arbitrary types.
Also works with Eigen types, standard vectors, vectors of Eigen types, and standard (unordered) sets (of basic types only). Returns true on success, and false on failure (e.g. param not present or not of compatible type).
| std::optional< T > param | ( | const std::string & | name | ) | const |
Return the value of a parameter of arbitrary types.
Also works with Eigen types, standard vectors, vectors of Eigen types, and standard (unordered) sets (of basic types only). Returns std::nullopt on failure (e.g. param not present or not of compatible type).
|
inline |
Return the map with name-value pairs for all parameters.
This offers fast lookup for string value compared to the param() method. Be aware that the order in the map is alphabetically which is not necessarily the actual order of parameters!
| std::vector< std::string > paramsRecursive | ( | ) | const |
Recursively return all parameter names and those of the sub-properties.
The parameter names of the sub-properties are prefix with the name of the sub-property, e.g. sub/par.
| void setAttributes | ( | const std::vector< Attribute > & | attributes | ) |
Set global attributes for this Properties instance.
Currently supported attributes are:
label (string): Text to use for the label instead of the name when displaying this property as sub property.collapsible (bool): Flag whether to show this sub property in a collapsible group box.hidden (bool): If set the PropertiesWidget will not show controls for this sub property.tight (bool): If set, the PropertiesWidget will not show left/right margins for this sub property. | bool setParamAttributes | ( | const std::string & | name, |
| const std::vector< Attribute > & | attributes ) |
Set special attributes to a single parameter.
The attributes are specified in the form: <attribute>: <value>, <attribute>: <value>. Whitespace in between the elements (e.g. around the :) is automatically trimmed. Attributes must not contain whitespace. The value can be either a number or a string. Strings must be quoted with single quotes (') and are allowed to contain ,.
The supported attributes depend on the type of the parameter. Currently supported attributes are:
label (string): text to use for the label when displaying this property in a default PropertiesWidget.useroption ('r', 'w', or 'rw'): if this is set for a parameter of an IoAlgorithm, the parameter will be displayed in the global settings dialog.read-only (bool): if set, the user cannot change the valuetooltip (string): tooltip that describes the parameterhidden (bool): if set, the PropertiesWidget will not show a control for this parametermin (double or int): minimum allowed valuemax (double or int): maximum allowed valuestep (double or int): step by which the value is increasedprefix (string): prefix displayed before the value (e.g. a unit)suffix (string): suffix displayed after the value (e.g. a unit)withSlider (bool): add a slider in the user interfacedecimals (int): number of decimals displayed. Setting this might alter min/max value (see https://doc.qt.io/qt-5/qdoublespinbox.html#decimals-prop)values (comma separated string, with or without whitespace around comma): String representation of enumerators.keys (comma separated string, with or without whitespace around comma): Integer values assigned to enumerators. If not specified, integer values 0,1,2,3,... are assumed.type: specifies the dialog type and must be one of: OpenDirectory, OpenFile or SaveFile. The default is OpenFile.caption: sets the title of the dialog (the default is a generic caption depending on the type)filter: string of wildcard patterns to filter available files, separated by ;;, e.g.: Text files (*.txt);;XML files (*.xml) (see also QFileDialog)dontUseNativeDialog: when set to true use the Qt dialog instead of the native dialog. The default is false.textBox (bool): if set, uses a text box instead of a line edit, making it more user-friendly for long or multi-line text. The default is false.type: specifies the dialog type and must be one of: normal, reduced, in-place.Returns true if the given string could be parsed or false otherwise.
| bool setParamAttributes | ( | const std::string & | name, |
| const std::string & | attributes ) |
Set special attributes to a single parameter.
The attributes are specified in the form: <attribute>: <value>, <attribute>: <value>. Whitespace in between the elements (e.g. around the :) is automatically trimmed. Attributes must not contain whitespace. The value can be either a number or a string. Strings must be quoted with single quotes (') and are allowed to contain ,.
The supported attributes depend on the type of the parameter. Currently supported attributes are:
label (string): text to use for the label when displaying this property in a default PropertiesWidget.useroption ('r', 'w', or 'rw'): if this is set for a parameter of an IoAlgorithm, the parameter will be displayed in the global settings dialog.read-only (bool): if set, the user cannot change the valuetooltip (string): tooltip that describes the parameterhidden (bool): if set, the PropertiesWidget will not show a control for this parametermin (double or int): minimum allowed valuemax (double or int): maximum allowed valuestep (double or int): step by which the value is increasedprefix (string): prefix displayed before the value (e.g. a unit)suffix (string): suffix displayed after the value (e.g. a unit)withSlider (bool): add a slider in the user interfacedecimals (int): number of decimals displayed. Setting this might alter min/max value (see https://doc.qt.io/qt-5/qdoublespinbox.html#decimals-prop)values (comma separated string, with or without whitespace around comma): String representation of enumerators.keys (comma separated string, with or without whitespace around comma): Integer values assigned to enumerators. If not specified, integer values 0,1,2,3,... are assumed.type: specifies the dialog type and must be one of: OpenDirectory, OpenFile or SaveFile. The default is OpenFile.caption: sets the title of the dialog (the default is a generic caption depending on the type)filter: string of wildcard patterns to filter available files, separated by ;;, e.g.: Text files (*.txt);;XML files (*.xml) (see also QFileDialog)dontUseNativeDialog: when set to true use the Qt dialog instead of the native dialog. The default is false.textBox (bool): if set, uses a text box instead of a line edit, making it more user-friendly for long or multi-line text. The default is false.type: specifies the dialog type and must be one of: normal, reduced, in-place.Returns true if the given string could be parsed or false otherwise.
| void setParamSchema | ( | const std::string & | name, |
| ParamType | type, | ||
| const std::vector< Attribute > & | attributes, | ||
| const std::optional< std::string > & | defaultValue ) |
Convenience function to set parameter type, attributes, and default value at once.
This function will not change the param value. Will create the param with empty value if it does not yet exist.
| Properties * addSubProperties | ( | const std::string & | name | ) |
Add sub-properties with specified name and return the created instance.
If the name is a path, all intermediate sub-properties will be created as well but only the last one is returned. E.g. foo/bar/test would create 3 sub-properties and return a pointer to test.
| const Properties * subProperties | ( | const std::string & | name | ) | const |
Return (first) sub-properties instance with the specified name.
name can also be a path of sub-properties in which case always the first sub-properties on each level is used.
| Properties * subProperties | ( | const std::string & | name, |
| bool | createIfDoesntExist = false ) |
Return (first) sub-properties instance with the specified name and optionally creates one if it doesn't exist.
If the name is a path of sub-properties all missing intermediate sub-properties will be created as well.
|
inline |
Return the map with for all sub-properties group by their name.
Be aware that the order in the map is alphabetically which is not necessarily the actual order of the sub-properties!
| bool removeSubProperties | ( | const std::string & | name | ) |
Remove (all) sub-properties with the specified name.
name can also be a path of sub-properties.
| bool removeEmptySubProperties | ( | ) |
Recursively removes all empty sub properties.
| int fillPlaceHolders | ( | const std::map< std::string, std::string > & | placeholders | ) |
Replace all occurrences of placeholders of the form %(key).
The method returns the number of replaced items.
| placeholders | Map of placeholder keys (as returned by getPlaceHolders(), i.e. including parenthesis) and their replacements |
| void iterate | ( | std::function< void(const Properties *properties, const std::string &pathAndName)> | visitorFunction | ) | const |
Performs a recursive breadth-first iteration over this instance and all of its sub properties.
The visitorFunction is called for this and each of the sub properties and receives a pointer to the instance and the absolute path (e.g. SubPropName/SubSubPropName/) to it as arguments.
| bool compareWith | ( | const Properties & | other, |
| Flags< Comparison > | compareFlags = Comparison::All ) const |
Compares the other Properties instance with this one.
Only those parts are compared that are listed in compareFlags, which can be useful if defaults/attributes are not configured in one of them.
|
staticnoexcept |
Convenience function to compare std::optional<Properties> instances.
Only those parts are compared that are listed in compareFlags, which can be useful if defaults/attributes are not configured in one of them. Returns false if any or both optionals are empty
| void copyFrom | ( | const Properties & | src, |
| Flags< CopyOptions > | copyOptions = CopyOptions::Default, | ||
| std::function< bool(const std::string &name, const Param &value)> | predicate = nullptr ) |
Copy properties from another instance You can optionally pass a predicate function, which gets evaluated for every parameter and every sub property.
Only those parts of src are copied for which predicate yields true. The predicate function gets the parameter/sub property name as well as its Param record as arguments.