Changeset View
Changeset View
Standalone View
Standalone View
source/blender/blenlib/BLI_serialize.hh
| Show All 30 Lines | |||||
| * There are specific classes that builds up the data structure that | * There are specific classes that builds up the data structure that | ||||
| * can be (de)serialized. | * can be (de)serialized. | ||||
| * | * | ||||
| * - StringValue: for strings | * - StringValue: for strings | ||||
| * - IntValue: for integer values | * - IntValue: for integer values | ||||
| * - DoubleValue: for double precision floating point numbers | * - DoubleValue: for double precision floating point numbers | ||||
| * - BooleanValue: for boolean values | * - BooleanValue: for boolean values | ||||
| * - ArrayValue: An array of any supported value. | * - ArrayValue: An array of any supported value. | ||||
| * - ObjectValue: A key value pair where keys are std::string. | * - DictionaryValue: A key value pair where keys are std::string. | ||||
| * - NullValue: for null values. | * - NullValue: for null values. | ||||
| * | * | ||||
| * # Basic usage | * # Basic usage | ||||
| * | * | ||||
| * ## Serializing | * ## Serializing | ||||
| * | * | ||||
| * - Construct a structure that needs to be serialized using the `*Value` classes. | * - Construct a structure that needs to be serialized using the `*Value` classes. | ||||
| * - Construct the formatter you want to use | * - Construct the formatter you want to use | ||||
| Show All 39 Lines | |||||
| */ | */ | ||||
| enum class eValueType { | enum class eValueType { | ||||
| String, | String, | ||||
| Int, | Int, | ||||
| Array, | Array, | ||||
| Null, | Null, | ||||
| Boolean, | Boolean, | ||||
| Double, | Double, | ||||
| Object, | Dictionary, | ||||
| }; | }; | ||||
| class Value; | class Value; | ||||
| class StringValue; | class StringValue; | ||||
| class ObjectValue; | class DictionaryValue; | ||||
| template<typename T, eValueType V> class PrimitiveValue; | template<typename T, eValueType V> class PrimitiveValue; | ||||
| using IntValue = PrimitiveValue<int64_t, eValueType::Int>; | using IntValue = PrimitiveValue<int64_t, eValueType::Int>; | ||||
| using DoubleValue = PrimitiveValue<double, eValueType::Double>; | using DoubleValue = PrimitiveValue<double, eValueType::Double>; | ||||
| using BooleanValue = PrimitiveValue<bool, eValueType::Boolean>; | using BooleanValue = PrimitiveValue<bool, eValueType::Boolean>; | ||||
| template<typename Container, eValueType V, typename ContainerItem = typename Container::value_type> | template<typename Container, eValueType V, typename ContainerItem = typename Container::value_type> | ||||
| class ContainerValue; | class ContainerValue; | ||||
| /* ArrayValue stores its items as shared pointer as it shares data with a lookup table that can | /* ArrayValue stores its items as shared pointer as it shares data with a lookup table that can | ||||
| * be created by calling `create_lookup`. */ | * be created by calling `create_lookup`. */ | ||||
| using ArrayValue = ContainerValue<Vector<std::shared_ptr<Value>>, eValueType::Array>; | using ArrayValue = ContainerValue<Vector<std::shared_ptr<Value>>, eValueType::Array>; | ||||
| /** | /** | ||||
| * Class containing a (de)serializable value. | * Class containing a (de)serializable value. | ||||
| * | * | ||||
| * To serialize from or to a specific format the Value will be used as an intermediate container | * To serialize from or to a specific format the Value will be used as an intermediate container | ||||
| * holding the values. Value class is abstract. There are concrete classes to for different data | * holding the values. Value class is abstract. There are concrete classes to for different data | ||||
| * types. | * types. | ||||
| * | * | ||||
| * - `StringValue`: contains a string. | * - `StringValue`: contains a string. | ||||
| * - `IntValue`: contains an integer. | * - `IntValue`: contains an integer. | ||||
| * - `ArrayValue`: contains an array of elements. Elements don't need to be the same type. | * - `ArrayValue`: contains an array of elements. Elements don't need to be the same type. | ||||
| * - `NullValue`: represents nothing (null pointer or optional). | * - `NullValue`: represents nothing (null pointer or optional). | ||||
| * - `BooleanValue`: contains a boolean (true/false). | * - `BooleanValue`: contains a boolean (true/false). | ||||
| * - `DoubleValue`: contains a double precision floating point number. | * - `DoubleValue`: contains a double precision floating point number. | ||||
| * - `ObjectValue`: represents an object (key value pairs where keys are strings and values can be | * - `DictionaryValue`: represents an object (key value pairs where keys are strings and values can | ||||
| * of different types. | * be of different types. | ||||
| * | * | ||||
| */ | */ | ||||
| class Value { | class Value { | ||||
| private: | private: | ||||
| eValueType type_; | eValueType type_; | ||||
| protected: | protected: | ||||
| Value() = delete; | Value() = delete; | ||||
| Show All 34 Lines | public: | ||||
| /** | /** | ||||
| * Casts to an ArrayValue. | * Casts to an ArrayValue. | ||||
| * Will return nullptr when it is a different type. | * Will return nullptr when it is a different type. | ||||
| */ | */ | ||||
| const ArrayValue *as_array_value() const; | const ArrayValue *as_array_value() const; | ||||
| /** | /** | ||||
| * Casts to an ObjectValue. | * Casts to an DictionaryValue. | ||||
| * Will return nullptr when it is a different type. | * Will return nullptr when it is a different type. | ||||
| */ | */ | ||||
| const ObjectValue *as_object_value() const; | const DictionaryValue *as_dictionary_value() const; | ||||
| }; | }; | ||||
| /** | /** | ||||
| * For generating value types that represent types that are typically known processor data types. | * For generating value types that represent types that are typically known processor data types. | ||||
| */ | */ | ||||
| template< | template< | ||||
| /** Wrapped c/cpp data type that is used to store the value. */ | /** Wrapped c/cpp data type that is used to store the value. */ | ||||
| typename T, | typename T, | ||||
| Show All 34 Lines | public: | ||||
| { | { | ||||
| return string_; | return string_; | ||||
| } | } | ||||
| }; | }; | ||||
| /** | /** | ||||
| * Template for arrays and objects. | * Template for arrays and objects. | ||||
| * | * | ||||
| * Both ArrayValue and ObjectValue store their values in an array. | * Both ArrayValue and DictionaryValue store their values in an array. | ||||
| */ | */ | ||||
| template< | template< | ||||
| /** The container type where the elements are stored in. */ | /** The container type where the elements are stored in. */ | ||||
| typename Container, | typename Container, | ||||
| /** ValueType representing the value (object/array). */ | /** ValueType representing the value (object/array). */ | ||||
| eValueType V, | eValueType V, | ||||
| Show All 19 Lines | public: | ||||
| Container &elements() | Container &elements() | ||||
| { | { | ||||
| return inner_value_; | return inner_value_; | ||||
| } | } | ||||
| }; | }; | ||||
| /** | /** | ||||
| * Internal storage type for ObjectValue. | * Internal storage type for DictionaryValue. | ||||
| * | * | ||||
| * The elements are stored as an key value pair. The value is a shared pointer so it can be shared | * The elements are stored as an key value pair. The value is a shared pointer so it can be shared | ||||
| * when using `ObjectValue::create_lookup`. | * when using `DictionaryValue::create_lookup`. | ||||
| */ | */ | ||||
| using ObjectElementType = std::pair<std::string, std::shared_ptr<Value>>; | using DictionaryElementType = std::pair<std::string, std::shared_ptr<Value>>; | ||||
| /** | /** | ||||
| * Object is a key-value container where the key must be a std::string. | * Object is a key-value container where the key must be a std::string. | ||||
| * Internally it is stored in a blender::Vector to ensure the order of keys. | * Internally it is stored in a blender::Vector to ensure the order of keys. | ||||
| */ | */ | ||||
| class ObjectValue : public ContainerValue<Vector<ObjectElementType>, eValueType::Object> { | class DictionaryValue | ||||
| : public ContainerValue<Vector<DictionaryElementType>, eValueType::Dictionary> { | |||||
| public: | public: | ||||
| using LookupValue = std::shared_ptr<Value>; | using LookupValue = std::shared_ptr<Value>; | ||||
| using Lookup = Map<std::string, LookupValue>; | using Lookup = Map<std::string, LookupValue>; | ||||
| /** | /** | ||||
| * Return a lookup map to quickly lookup by key. | * Return a lookup map to quickly lookup by key. | ||||
| * | * | ||||
| * The lookup is owned by the caller. | * The lookup is owned by the caller. | ||||
| ▲ Show 20 Lines • Show All 42 Lines • Show Last 20 Lines | |||||