19using namespace blender::io::serialize;
106 std::shared_ptr<DictionaryValue>
result = std::make_shared<DictionaryValue>();
131 if (std::optional<eIDPropertyType> subtype = get_id_property_type(
IDP_KEY_SUBTYPE)) {
179 return get_array_primitive<int32_t, IntValue>(
IDP_KEY_VALUE);
184 return get_array_primitive<float, DoubleValue>(
IDP_KEY_VALUE);
189 return get_array_primitive<double, DoubleValue>(
IDP_KEY_VALUE);
193 std::optional<std::string> get_string(
StringRef key)
const
196 if (value ==
nullptr) {
202 return value->get()->as_string_value()->value();
208 if (value ==
nullptr) {
211 if (value->get()->type() != eValueType::Array) {
214 return value->get()->as_array_value();
217 std::optional<bool> get_bool(StringRef key)
const
219 const std::shared_ptr<Value> *value =
lookup.lookup_ptr(key);
220 if (value ==
nullptr) {
223 if (value->get()->type() != eValueType::Boolean) {
226 return value->get()->as_boolean_value()->value();
229 std::optional<int32_t> get_int(StringRef key)
const
231 const std::shared_ptr<Value> *value =
lookup.lookup_ptr(key);
232 if (value ==
nullptr) {
235 if (value->get()->type() != eValueType::Int) {
238 return value->get()->as_int_value()->value();
241 std::optional<int32_t> get_enum(StringRef key)
const
243 const std::shared_ptr<Value> *value =
lookup.lookup_ptr(key);
244 if (value ==
nullptr) {
247 if (value->get()->type() != eValueType::Int) {
250 return value->get()->as_int_value()->value();
253 std::optional<double> get_double(StringRef key)
const
255 const std::shared_ptr<Value> *value =
lookup.lookup_ptr(key);
256 if (value ==
nullptr) {
259 if (value->get()->type() != eValueType::Double) {
262 return value->get()->as_double_value()->value();
265 std::optional<float> get_float(StringRef key)
const
267 return static_cast<std::optional<float>
>(get_double(key));
270 template<
typename PrimitiveType,
typename ValueType>
271 std::optional<Vector<PrimitiveType>> get_array_primitive(StringRef key)
const
273 const std::shared_ptr<Value> *value =
lookup.lookup_ptr(key);
274 if (value ==
nullptr) {
277 if (value->get()->type() != eValueType::Array) {
281 Vector<PrimitiveType>
result;
282 for (
const std::shared_ptr<Value> &
element : value->get()->as_array_value()->elements()) {
283 const ValueType *value_type =
static_cast<const ValueType *
>(
element.get());
285 result.append_as(primitive_value);
291 std::optional<eIDPropertyType> get_id_property_type(StringRef key)
const
293 std::optional<std::string> string_value = get_string(key);
294 if (!string_value.has_value()) {
297 const IDPropertySerializer &serializer =
serializer_for(*string_value);
298 return serializer.property_type();
329 std::optional<std::string> name = entry_reader.
get_name();
330 if (!name.has_value()) {
334 if (!string_value.has_value()) {
337 return create(name->c_str(), string_value->c_str());
368 std::optional<std::string> name = entry_reader.
get_name();
369 if (!name.has_value()) {
372 std::optional<bool> extracted_value = entry_reader.
get_bool_value();
373 if (!extracted_value.has_value()) {
376 return create_bool(name->c_str(), *extracted_value);
407 std::optional<std::string> name = entry_reader.
get_name();
408 if (!name.has_value()) {
411 std::optional<int32_t> extracted_value = entry_reader.
get_int_value();
412 if (!extracted_value.has_value()) {
415 return create(name->c_str(), *extracted_value);
446 std::optional<std::string> name = entry_reader.
get_name();
447 if (!name.has_value()) {
451 if (!extracted_value.has_value()) {
454 return create(name->c_str(), *extracted_value);
485 std::optional<std::string> name = entry_reader.
get_name();
486 if (!name.has_value()) {
490 if (!extracted_value.has_value()) {
493 return create(name->c_str(), *extracted_value);
556 std::optional<eIDPropertyType> property_subtype = entry_reader.
get_subtype();
557 if (!property_subtype.has_value()) {
561 switch (*property_subtype) {
563 return idprop_array_int_from_value(entry_reader);
565 return idprop_array_float_from_value(entry_reader);
567 return idprop_array_double_from_value(entry_reader);
589 for (
const IDProperty &id_property : values) {
592 if (!value_serializer.supports_serializing()) {
595 array.
append(value_serializer.idprop_to_dictionary(&id_property));
599 std::unique_ptr<IDProperty, IDPropertyDeleter> idprop_array_int_from_value(
600 DictionaryEntryParser &entry_reader)
const
604 std::optional<std::string> name = entry_reader.get_name();
605 if (!name.has_value()) {
608 std::optional<Vector<int32_t>> extracted_value = entry_reader.get_array_int_value();
609 if (!extracted_value.has_value()) {
612 return create(name->c_str(), *extracted_value);
615 std::unique_ptr<IDProperty, IDPropertyDeleter> idprop_array_float_from_value(
616 DictionaryEntryParser &entry_reader)
const
620 std::optional<std::string> name = entry_reader.get_name();
621 if (!name.has_value()) {
624 std::optional<Vector<float>> extracted_value = entry_reader.get_array_float_value();
625 if (!extracted_value.has_value()) {
628 return create(name->c_str(), *extracted_value);
631 std::unique_ptr<IDProperty, IDPropertyDeleter> idprop_array_double_from_value(
632 DictionaryEntryParser &entry_reader)
const
636 std::optional<std::string> name = entry_reader.get_name();
637 if (!name.has_value()) {
640 std::optional<Vector<double>> extracted_value = entry_reader.get_array_double_value();
641 if (!extracted_value.has_value()) {
644 return create(name->c_str(), *extracted_value);
668 std::shared_ptr<ArrayValue>
array = std::make_shared<ArrayValue>();
683 std::optional<std::string> name = entry_reader.
get_name();
684 if (!name.has_value()) {
689 if (
array ==
nullptr) {
694 for (
const std::shared_ptr<Value> &
element :
array->elements()) {
700 if (subproperty ==
nullptr) {
760 switch (property_type) {
783 BLI_assert_msg(
false,
"Trying to convert an unsupported/unknown property type to a string");
824 std::unique_ptr<ArrayValue>
result = std::make_unique<ArrayValue>();
825 const IDProperty *current_property = properties;
826 while (current_property !=
nullptr) {
832 current_property = current_property->
next;
847 std::optional<eIDPropertyType> property_type = entry_reader.
get_type();
848 if (!property_type.has_value()) {
867 if (last_created ==
nullptr) {
875 if (previous_added) {
876 previous_added->
next = last_created;
878 last_created->
prev = previous_added;
879 previous_added = last_created;
#define IDP_float_get(prop)
#define IDP_array_voidp_get(prop)
#define IDP_int_get(prop)
#define IDP_array_double_get(prop)
#define IDP_string_get(prop)
#define IDP_double_get(prop)
#define IDP_array_int_get(prop)
#define IDP_array_float_get(prop)
#define IDP_bool_get(prop)
ID and Library types, which are fundamental for SDNA.
#define BLI_assert_unreachable()
#define BLI_assert_msg(a, msg)
void append(const array< T > &from)
const Value * lookup_ptr(const Key &key) const
IDPSerializer for IDP_ARRAY.
std::string type_name() const override
return the type name for (de)serializing. Type name is stored in the type or subtype attribute of the...
constexpr IDPArraySerializer()=default
std::optional< eIDPropertyType > property_type() const override
return the IDPropertyType for (de)serializing.
std::shared_ptr< DictionaryValue > idprop_to_dictionary(const IDProperty *id_property) const override
create dictionary containing the given id_property.
std::unique_ptr< IDProperty, IDPropertyDeleter > entry_to_idprop(DictionaryEntryParser &entry_reader) const override
convert the entry to an id property.
IDPSerializer for IDP_INT.
std::optional< eIDPropertyType > property_type() const override
return the IDPropertyType for (de)serializing.
std::string type_name() const override
return the type name for (de)serializing. Type name is stored in the type or subtype attribute of the...
constexpr IDPBoolSerializer()=default
std::shared_ptr< DictionaryValue > idprop_to_dictionary(const IDProperty *id_property) const override
create dictionary containing the given id_property.
std::unique_ptr< IDProperty, IDPropertyDeleter > entry_to_idprop(DictionaryEntryParser &entry_reader) const override
convert the entry to an id property.
IDPSerializer for IDP_DOUBLE.
std::optional< eIDPropertyType > property_type() const override
return the IDPropertyType for (de)serializing.
constexpr IDPDoubleSerializer()=default
std::shared_ptr< DictionaryValue > idprop_to_dictionary(const IDProperty *id_property) const override
create dictionary containing the given id_property.
std::unique_ptr< IDProperty, IDPropertyDeleter > entry_to_idprop(DictionaryEntryParser &entry_reader) const override
convert the entry to an id property.
std::string type_name() const override
return the type name for (de)serializing. Type name is stored in the type or subtype attribute of the...
IDPSerializer for IDP_FLOAT.
std::shared_ptr< DictionaryValue > idprop_to_dictionary(const IDProperty *id_property) const override
create dictionary containing the given id_property.
constexpr IDPFloatSerializer()=default
std::unique_ptr< IDProperty, IDPropertyDeleter > entry_to_idprop(DictionaryEntryParser &entry_reader) const override
convert the entry to an id property.
std::optional< eIDPropertyType > property_type() const override
return the IDPropertyType for (de)serializing.
std::string type_name() const override
return the type name for (de)serializing. Type name is stored in the type or subtype attribute of the...
IDPSerializer for IDP_GROUP.
constexpr IDPGroupSerializer()=default
std::string type_name() const override
return the type name for (de)serializing. Type name is stored in the type or subtype attribute of the...
std::unique_ptr< IDProperty, IDPropertyDeleter > entry_to_idprop(DictionaryEntryParser &entry_reader) const override
convert the entry to an id property.
std::optional< eIDPropertyType > property_type() const override
return the IDPropertyType for (de)serializing.
std::shared_ptr< DictionaryValue > idprop_to_dictionary(const IDProperty *id_property) const override
create dictionary containing the given id_property.
IDPSerializer for IDP_INT.
std::string type_name() const override
return the type name for (de)serializing. Type name is stored in the type or subtype attribute of the...
constexpr IDPIntSerializer()=default
std::shared_ptr< DictionaryValue > idprop_to_dictionary(const IDProperty *id_property) const override
create dictionary containing the given id_property.
std::unique_ptr< IDProperty, IDPropertyDeleter > entry_to_idprop(DictionaryEntryParser &entry_reader) const override
convert the entry to an id property.
std::optional< eIDPropertyType > property_type() const override
return the IDPropertyType for (de)serializing.
IDPSerializer for IDP_STRING.
std::string type_name() const override
return the type name for (de)serializing. Type name is stored in the type or subtype attribute of the...
constexpr IDPStringSerializer()=default
std::optional< eIDPropertyType > property_type() const override
return the IDPropertyType for (de)serializing.
std::unique_ptr< IDProperty, IDPropertyDeleter > entry_to_idprop(DictionaryEntryParser &entry_reader) const override
convert the entry to an id property.
std::shared_ptr< DictionaryValue > idprop_to_dictionary(const IDProperty *id_property) const override
create dictionary containing the given id_property.
Dummy serializer for unknown and unsupported types.
std::unique_ptr< IDProperty, IDPropertyDeleter > entry_to_idprop(DictionaryEntryParser &) const override
convert the entry to an id property.
std::string type_name() const override
return the type name for (de)serializing. Type name is stored in the type or subtype attribute of the...
std::optional< eIDPropertyType > property_type() const override
return the IDPropertyType for (de)serializing.
bool supports_serializing() const override
Can the serializer be used?
constexpr IDPUnknownSerializer()=default
std::shared_ptr< DictionaryValue > idprop_to_dictionary(const IDProperty *) const override
create dictionary containing the given id_property.
Base class for (de)serializing IDProperties.
virtual std::shared_ptr< DictionaryValue > idprop_to_dictionary(const IDProperty *id_property) const =0
create dictionary containing the given id_property.
std::shared_ptr< DictionaryValue > create_dictionary(const IDProperty *id_property) const
Create a new DictionaryValue instance.
virtual std::string type_name() const =0
return the type name for (de)serializing. Type name is stored in the type or subtype attribute of the...
virtual std::optional< eIDPropertyType > property_type() const =0
return the IDPropertyType for (de)serializing.
virtual std::unique_ptr< IDProperty, IDPropertyDeleter > entry_to_idprop(DictionaryEntryParser &entry_reader) const =0
convert the entry to an id property.
constexpr IDPropertySerializer()=default
virtual bool supports_serializing() const
Can the serializer be used?
Span< std::shared_ptr< Value > > elements() const
Map< std::string, std::shared_ptr< Value > > Lookup
const ArrayValue * as_array_value() const
static constexpr StringRef IDP_KEY_SUBTYPE("subtype")
static constexpr StringRef IDP_KEY_ITEMTYPE("itemtype")
static constexpr StringRef IDP_PROPERTY_TYPENAME_FLOAT("IDP_FLOAT")
std::unique_ptr< IDProperty, IDPropertyDeleter > create_bool(StringRef prop_name, bool value, eIDPropertyFlag flags={})
Allocate a new IDProperty of type IDP_BOOLEAN, set its name and value.
static constexpr IDPUnknownSerializer IDP_SERIALIZER_UNKNOWN
static constexpr StringRef IDP_PROPERTY_TYPENAME_STRING("IDP_STRING")
static constexpr IDPStringSerializer IDP_SERIALIZER_STRING
static constexpr StringRef IDP_PROPERTY_TYPENAME_BOOL("IDP_BOOL")
static constexpr StringRef IDP_KEY_VALUE("value")
static const IDPropertySerializer & serializer_for(eIDPropertyType property_type)
get the serializer for the given property type.
static constexpr IDPArraySerializer IDP_SERIALIZER_ARRAY
std::unique_ptr< IDProperty, IDPropertyDeleter > create(StringRef prop_name, int32_t value, eIDPropertyFlag flags={})
Allocate a new IDProperty of type IDP_INT, set its name and value.
static constexpr StringRef IDP_PROPERTY_TYPENAME_DOUBLE("IDP_DOUBLE")
static constexpr IDPGroupSerializer IDP_SERIALIZER_GROUP
static constexpr StringRef IDP_PROPERTY_TYPENAME_UNKNOWN("IDP_UNKNOWN")
static constexpr StringRef IDP_PROPERTY_TYPENAME_GROUP("IDP_GROUP")
static IDProperty * idprop_from_value(const DictionaryValue &value)
static constexpr StringRef IDP_KEY_TYPE("type")
static constexpr StringRef IDP_PROPERTY_TYPENAME_INT("IDP_INT")
static constexpr IDPDoubleSerializer IDP_SERIALIZER_DOUBLE
static constexpr IDPFloatSerializer IDP_SERIALIZER_FLOAT
std::unique_ptr< IDProperty, IDPropertyDeleter > create_group(StringRef prop_name, eIDPropertyFlag flags={})
Allocate a new IDProperty of type IDP_GROUP.
IDProperty * convert_from_serialize_value(const io::serialize::Value &value)
Convert the given value (which must be an array) to an IDProperty.
static constexpr StringRef IDP_PROPERTY_TYPENAME_ARRAY("IDP_ARRAY")
std::unique_ptr< io::serialize::ArrayValue > convert_to_serialize_values(const IDProperty *properties)
Convert the given properties to Value objects for serialization.
static constexpr StringRef IDP_KEY_NAME("name")
static constexpr IDPBoolSerializer IDP_SERIALIZER_BOOL
static constexpr IDPIntSerializer IDP_SERIALIZER_INT
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
bool IDP_AddToGroup(IDProperty *group, IDProperty *prop) ATTR_NONNULL()
void IDP_FreeProperty(IDProperty *prop)
ATTR_WARN_UNUSED_RESULT const void * element
ListBaseT< struct IDProperty > group
Helper class for parsing DictionaryValues.
DictionaryEntryParser(const DictionaryValue &value)
std::optional< int > get_enum_value() const
std::optional< std::string > get_string_value() const
const ArrayValue * get_array_value() const
std::optional< std::string > get_name() const
std::optional< float > get_float_value() const
std::optional< Vector< double > > get_array_double_value() const
std::optional< eIDPropertyType > get_type() const
DictionaryValue::Lookup lookup
std::optional< int32_t > get_int_value() const
std::optional< Vector< float > > get_array_float_value() const
std::optional< double > get_double_value() const
std::optional< Vector< int32_t > > get_array_int_value() const
std::optional< eIDPropertyType > get_subtype() const
std::optional< bool > get_bool_value() const