Darwin Neuroevolution Framework
Public Member Functions | List of all members
core::PropertySetVariant< TAG > Class Template Reference

A variant type (tagged-union) with PropertySet fields. More...

#include <properties.h>

Inheritance diagram for core::PropertySetVariant< TAG >:
core::NonCopyable

Public Member Functions

TAG tag () const
 The tag value indicating the active PropertySet case.
 
void selectCase (TAG tag)
 Selects the active PropertySet case.
 
const PropertySet * activeCase () const
 Returns the active PropertySet.
 
PropertySet * activeCase ()
 Returns the active PropertySet.
 
json toJson () const
 Serialize to a json object. More...
 
void fromJson (const json &json_obj)
 Deserialize from a json object. More...
 
void copyFrom (const PropertySetVariant &src)
 Transfer values between two property set variants.
 

Detailed Description

template<class TAG>
class core::PropertySetVariant< TAG >

A variant type (tagged-union) with PropertySet fields.

The PropertySetVariant offers a simple solution for grouping a set of mutually exclusive property sets. The tag type (normally an enum) tracks which field (case) is active.

Defining variants

First, we need a scalar tag type:

enum class VariantTag {
Empty,
Basic,
};
// the tag type must support core::toString() and core::fromString()
inline auto customStringify(core::TypeTag<VariantTag>) {
{ VariantTag::Empty, "empty" },
{ VariantTag::Basic, "basic" },
{ VariantTag::Extra, "extra" },
};
return stringify;
}

The variant type must derive from PropertySetVariant, and the CASE(tag, name, type) macro is used to declare the variant fields (cases):

struct VariantType : public core::PropertySetVariant<VariantTag> {
CASE(VariantTag::Empty, empty, EmptyProperties);
CASE(VariantTag::Basic, basic, BasicProperties);
CASE(VariantTag::Extra, extra, ExtraProperties);
};

Finally, the variant type is intended to be used inside a PropertySet, using the VARIANT(name, type, default_case, description) macro to declare variant fields.

struct Properties : public core::PropertySet {
...
VARIANT(name, VariantType, VariantTag::Basic, "A variant property");
...
};
See also
PropertySet

Member Function Documentation

◆ fromJson()

template<class TAG>
void core::PropertySetVariant< TAG >::fromJson ( const json &  json_obj)
inline

Deserialize from a json object.

The deserialization is not strict:

  • Extra cases are ignored
  • Missing cases will be set to the default values

◆ toJson()

template<class TAG>
json core::PropertySetVariant< TAG >::toJson ( ) const
inline

Serialize to a json object.

Note
All the PropertySet cases are serialized, not just the active one

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