Blender V5.3
idprop_serialize.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2021 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <optional>
10
11#include "DNA_ID.h"
12
13#include "BKE_idprop.hh"
14
15#include "BLI_listbase.hh"
16#include "BLI_serialize.hh"
17
18namespace blender::bke::idprop {
19using namespace blender::io::serialize;
20
21/* Forward declarations */
24static IDProperty *idprop_from_value(const DictionaryValue &value);
25static const IDPropertySerializer &serializer_for(eIDPropertyType property_type);
26static const IDPropertySerializer &serializer_for(StringRef idprop_typename);
27
28/* -------------------------------------------------------------------- */
31
32/* Definitions */
33static constexpr StringRef IDP_KEY_NAME("name");
34static constexpr StringRef IDP_KEY_TYPE("type");
35static constexpr StringRef IDP_KEY_SUBTYPE("subtype");
44static constexpr StringRef IDP_KEY_ITEMTYPE("itemtype");
45static constexpr StringRef IDP_KEY_VALUE("value");
46
47static constexpr StringRef IDP_PROPERTY_TYPENAME_STRING("IDP_STRING");
48static constexpr StringRef IDP_PROPERTY_TYPENAME_BOOL("IDP_BOOL");
49static constexpr StringRef IDP_PROPERTY_TYPENAME_INT("IDP_INT");
50static constexpr StringRef IDP_PROPERTY_TYPENAME_FLOAT("IDP_FLOAT");
51static constexpr StringRef IDP_PROPERTY_TYPENAME_DOUBLE("IDP_DOUBLE");
52static constexpr StringRef IDP_PROPERTY_TYPENAME_ARRAY("IDP_ARRAY");
53static constexpr StringRef IDP_PROPERTY_TYPENAME_GROUP("IDP_GROUP");
54static constexpr StringRef IDP_PROPERTY_TYPENAME_UNKNOWN("IDP_UNKNOWN");
55
62 public:
63 constexpr IDPropertySerializer() = default;
64
69 virtual std::string type_name() const = 0;
70
74 virtual std::optional<eIDPropertyType> property_type() const = 0;
75
79 virtual std::shared_ptr<DictionaryValue> idprop_to_dictionary(
80 const IDProperty *id_property) const = 0;
81
85 virtual std::unique_ptr<IDProperty, IDPropertyDeleter> entry_to_idprop(
86 DictionaryEntryParser &entry_reader) const = 0;
87
93 virtual bool supports_serializing() const
94 {
95 return true;
96 }
97
98 protected:
104 std::shared_ptr<DictionaryValue> create_dictionary(const IDProperty *id_property) const
105 {
106 std::shared_ptr<DictionaryValue> result = std::make_shared<DictionaryValue>();
107 result->append_str(IDP_KEY_NAME, id_property->name);
108 result->append_str(IDP_KEY_TYPE, this->type_name());
109 return result;
110 }
111};
112
118
119 public:
120 explicit DictionaryEntryParser(const DictionaryValue &value) : lookup(value.create_lookup()) {}
121
122 std::optional<eIDPropertyType> get_type() const
123 {
124 return get_id_property_type(IDP_KEY_TYPE);
125 }
126
127 std::optional<eIDPropertyType> get_subtype() const
128 {
129 /* The "subtype" key is the expected one, but in certain cases an "itemtype" key is used. See
130 * the documentation of #IDP_KEY_ITEMTYPE for more info. */
131 if (std::optional<eIDPropertyType> subtype = get_id_property_type(IDP_KEY_SUBTYPE)) {
132 return subtype;
133 }
134 return get_id_property_type(IDP_KEY_ITEMTYPE);
135 }
136
137 std::optional<std::string> get_name() const
138 {
139 return get_string(IDP_KEY_NAME);
140 }
141
142 std::optional<std::string> get_string_value() const
143 {
144 return get_string(IDP_KEY_VALUE);
145 }
146
147 std::optional<bool> get_bool_value() const
148 {
149 return get_bool(IDP_KEY_VALUE);
150 }
151
152 std::optional<int32_t> get_int_value() const
153 {
154 return get_int(IDP_KEY_VALUE);
155 }
156
157 std::optional<float> get_float_value() const
158 {
159 return get_float(IDP_KEY_VALUE);
160 }
161
162 std::optional<double> get_double_value() const
163 {
164 return get_double(IDP_KEY_VALUE);
165 }
166
167 std::optional<int> get_enum_value() const
168 {
169 return get_enum(IDP_KEY_VALUE);
170 }
171
173 {
174 return get_array(IDP_KEY_VALUE);
175 }
176
177 std::optional<Vector<int32_t>> get_array_int_value() const
178 {
179 return get_array_primitive<int32_t, IntValue>(IDP_KEY_VALUE);
180 }
181
182 std::optional<Vector<float>> get_array_float_value() const
183 {
184 return get_array_primitive<float, DoubleValue>(IDP_KEY_VALUE);
185 }
186
187 std::optional<Vector<double>> get_array_double_value() const
188 {
189 return get_array_primitive<double, DoubleValue>(IDP_KEY_VALUE);
190 }
191
192 private:
193 std::optional<std::string> get_string(StringRef key) const
194 {
195 const std::shared_ptr<Value> *value = lookup.lookup_ptr(key);
196 if (value == nullptr) {
197 return std::nullopt;
198 }
199 if (value->get()->type() != eValueType::String) {
200 return std::nullopt;
201 }
202 return value->get()->as_string_value()->value();
203 }
204
205 const ArrayValue *get_array(StringRef key) const
206 {
207 const std::shared_ptr<Value> *value = lookup.lookup_ptr(key);
208 if (value == nullptr) {
209 return nullptr;
210 }
211 if (value->get()->type() != eValueType::Array) {
212 return nullptr;
213 }
214 return value->get()->as_array_value();
215 }
216
217 std::optional<bool> get_bool(StringRef key) const
218 {
219 const std::shared_ptr<Value> *value = lookup.lookup_ptr(key);
220 if (value == nullptr) {
221 return std::nullopt;
222 }
223 if (value->get()->type() != eValueType::Boolean) {
224 return std::nullopt;
225 }
226 return value->get()->as_boolean_value()->value();
227 }
228
229 std::optional<int32_t> get_int(StringRef key) const
230 {
231 const std::shared_ptr<Value> *value = lookup.lookup_ptr(key);
232 if (value == nullptr) {
233 return std::nullopt;
234 }
235 if (value->get()->type() != eValueType::Int) {
236 return std::nullopt;
237 }
238 return value->get()->as_int_value()->value();
239 }
240
241 std::optional<int32_t> get_enum(StringRef key) const
242 {
243 const std::shared_ptr<Value> *value = lookup.lookup_ptr(key);
244 if (value == nullptr) {
245 return std::nullopt;
246 }
247 if (value->get()->type() != eValueType::Int) {
248 return std::nullopt;
249 }
250 return value->get()->as_int_value()->value();
251 }
252
253 std::optional<double> get_double(StringRef key) const
254 {
255 const std::shared_ptr<Value> *value = lookup.lookup_ptr(key);
256 if (value == nullptr) {
257 return std::nullopt;
258 }
259 if (value->get()->type() != eValueType::Double) {
260 return std::nullopt;
261 }
262 return value->get()->as_double_value()->value();
263 }
264
265 std::optional<float> get_float(StringRef key) const
266 {
267 return static_cast<std::optional<float>>(get_double(key));
268 }
269
270 template<typename PrimitiveType, typename ValueType>
271 std::optional<Vector<PrimitiveType>> get_array_primitive(StringRef key) const
272 {
273 const std::shared_ptr<Value> *value = lookup.lookup_ptr(key);
274 if (value == nullptr) {
275 return std::nullopt;
276 }
277 if (value->get()->type() != eValueType::Array) {
278 return std::nullopt;
279 }
280
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());
284 PrimitiveType primitive_value = value_type->value();
285 result.append_as(primitive_value);
286 }
287
288 return result;
289 }
290
291 std::optional<eIDPropertyType> get_id_property_type(StringRef key) const
292 {
293 std::optional<std::string> string_value = get_string(key);
294 if (!string_value.has_value()) {
295 return std::nullopt;
296 }
297 const IDPropertySerializer &serializer = serializer_for(*string_value);
298 return serializer.property_type();
299 }
300};
301
304 public:
305 constexpr IDPStringSerializer() = default;
306
307 std::string type_name() const override
308 {
310 }
311
312 std::optional<eIDPropertyType> property_type() const override
313 {
314 return IDP_STRING;
315 }
316
317 std::shared_ptr<DictionaryValue> idprop_to_dictionary(
318 const IDProperty *id_property) const override
319 {
320 std::shared_ptr<DictionaryValue> result = create_dictionary(id_property);
321 result->append_str(IDP_KEY_VALUE, IDP_string_get(id_property));
322 return result;
323 }
324
325 std::unique_ptr<IDProperty, IDPropertyDeleter> entry_to_idprop(
326 DictionaryEntryParser &entry_reader) const override
327 {
328 BLI_assert(*(entry_reader.get_type()) == IDP_STRING);
329 std::optional<std::string> name = entry_reader.get_name();
330 if (!name.has_value()) {
331 return nullptr;
332 }
333 std::optional<std::string> string_value = entry_reader.get_string_value();
334 if (!string_value.has_value()) {
335 return nullptr;
336 }
337 return create(name->c_str(), string_value->c_str());
338 }
339};
340
343 public:
344 constexpr IDPBoolSerializer() = default;
345
346 std::string type_name() const override
347 {
349 }
350
351 std::optional<eIDPropertyType> property_type() const override
352 {
353 return IDP_BOOLEAN;
354 }
355
356 std::shared_ptr<DictionaryValue> idprop_to_dictionary(
357 const IDProperty *id_property) const override
358 {
359 std::shared_ptr<DictionaryValue> result = create_dictionary(id_property);
360 result->append(IDP_KEY_VALUE, std::make_shared<BooleanValue>(IDP_bool_get(id_property) != 0));
361 return result;
362 }
363
364 std::unique_ptr<IDProperty, IDPropertyDeleter> entry_to_idprop(
365 DictionaryEntryParser &entry_reader) const override
366 {
367 BLI_assert(*(entry_reader.get_type()) == IDP_BOOLEAN);
368 std::optional<std::string> name = entry_reader.get_name();
369 if (!name.has_value()) {
370 return nullptr;
371 }
372 std::optional<bool> extracted_value = entry_reader.get_bool_value();
373 if (!extracted_value.has_value()) {
374 return nullptr;
375 }
376 return create_bool(name->c_str(), *extracted_value);
377 }
378};
379
382 public:
383 constexpr IDPIntSerializer() = default;
384
385 std::string type_name() const override
386 {
388 }
389
390 std::optional<eIDPropertyType> property_type() const override
391 {
392 return IDP_INT;
393 }
394
395 std::shared_ptr<DictionaryValue> idprop_to_dictionary(
396 const IDProperty *id_property) const override
397 {
398 std::shared_ptr<DictionaryValue> result = create_dictionary(id_property);
399 result->append_int(IDP_KEY_VALUE, IDP_int_get(id_property));
400 return result;
401 }
402
403 std::unique_ptr<IDProperty, IDPropertyDeleter> entry_to_idprop(
404 DictionaryEntryParser &entry_reader) const override
405 {
406 BLI_assert(*(entry_reader.get_type()) == IDP_INT);
407 std::optional<std::string> name = entry_reader.get_name();
408 if (!name.has_value()) {
409 return nullptr;
410 }
411 std::optional<int32_t> extracted_value = entry_reader.get_int_value();
412 if (!extracted_value.has_value()) {
413 return nullptr;
414 }
415 return create(name->c_str(), *extracted_value);
416 }
417};
418
421 public:
422 constexpr IDPFloatSerializer() = default;
423
424 std::string type_name() const override
425 {
427 }
428
429 std::optional<eIDPropertyType> property_type() const override
430 {
431 return IDP_FLOAT;
432 }
433
434 std::shared_ptr<DictionaryValue> idprop_to_dictionary(
435 const IDProperty *id_property) const override
436 {
437 std::shared_ptr<DictionaryValue> result = create_dictionary(id_property);
438 result->append_double(IDP_KEY_VALUE, IDP_float_get(id_property));
439 return result;
440 }
441
442 std::unique_ptr<IDProperty, IDPropertyDeleter> entry_to_idprop(
443 DictionaryEntryParser &entry_reader) const override
444 {
445 BLI_assert(*(entry_reader.get_type()) == IDP_FLOAT);
446 std::optional<std::string> name = entry_reader.get_name();
447 if (!name.has_value()) {
448 return nullptr;
449 }
450 std::optional<float> extracted_value = entry_reader.get_float_value();
451 if (!extracted_value.has_value()) {
452 return nullptr;
453 }
454 return create(name->c_str(), *extracted_value);
455 }
456};
457
460 public:
461 constexpr IDPDoubleSerializer() = default;
462
463 std::string type_name() const override
464 {
466 }
467
468 std::optional<eIDPropertyType> property_type() const override
469 {
470 return IDP_DOUBLE;
471 }
472
473 std::shared_ptr<DictionaryValue> idprop_to_dictionary(
474 const IDProperty *id_property) const override
475 {
476 std::shared_ptr<DictionaryValue> result = create_dictionary(id_property);
477 result->append_double(IDP_KEY_VALUE, IDP_double_get(id_property));
478 return result;
479 }
480
481 std::unique_ptr<IDProperty, IDPropertyDeleter> entry_to_idprop(
482 DictionaryEntryParser &entry_reader) const override
483 {
484 BLI_assert(*(entry_reader.get_type()) == IDP_DOUBLE);
485 std::optional<std::string> name = entry_reader.get_name();
486 if (!name.has_value()) {
487 return nullptr;
488 }
489 std::optional<double> extracted_value = entry_reader.get_double_value();
490 if (!extracted_value.has_value()) {
491 return nullptr;
492 }
493 return create(name->c_str(), *extracted_value);
494 }
495};
496
499 public:
500 constexpr IDPArraySerializer() = default;
501
502 std::string type_name() const override
503 {
505 }
506
507 std::optional<eIDPropertyType> property_type() const override
508 {
509 return IDP_ARRAY;
510 }
511
512 std::shared_ptr<DictionaryValue> idprop_to_dictionary(
513 const IDProperty *id_property) const override
514 {
515 std::shared_ptr<DictionaryValue> result = create_dictionary(id_property);
516 const IDPropertySerializer &subtype_serializer = serializer_for(
517 static_cast<eIDPropertyType>(id_property->subtype));
518 result->append_str(IDP_KEY_SUBTYPE, subtype_serializer.type_name());
519
520 ArrayValue &array = *result->append_array(IDP_KEY_VALUE);
521 switch (static_cast<eIDPropertyType>(id_property->subtype)) {
522 case IDP_INT: {
523 int32_t *values = IDP_array_int_get(id_property);
524 add_values<int32_t, IntValue>(array, Span<int32_t>(values, id_property->len));
525 break;
526 }
527 case IDP_FLOAT: {
528 float *values = IDP_array_float_get(id_property);
529 add_values<float, DoubleValue>(array, Span<float>(values, id_property->len));
530 break;
531 }
532 case IDP_DOUBLE: {
533 double *values = IDP_array_double_get(id_property);
534 add_values<double, DoubleValue>(array, Span<double>(values, id_property->len));
535 break;
536 }
537 case IDP_GROUP: {
538 IDProperty *values = static_cast<IDProperty *>(IDP_array_voidp_get(id_property));
539 add_values(array, Span<IDProperty>(values, id_property->len));
540 break;
541 }
542 default: {
543 /* IDP_ARRAY only supports IDP_INT, IDP_FLOAT, IDP_DOUBLE and IDP_GROUP. */
545 break;
546 }
547 }
548
549 return result;
550 }
551
552 std::unique_ptr<IDProperty, IDPropertyDeleter> entry_to_idprop(
553 DictionaryEntryParser &entry_reader) const override
554 {
555 BLI_assert(*(entry_reader.get_type()) == IDP_ARRAY);
556 std::optional<eIDPropertyType> property_subtype = entry_reader.get_subtype();
557 if (!property_subtype.has_value()) {
558 return nullptr;
559 }
560
561 switch (*property_subtype) {
562 case IDP_INT:
563 return idprop_array_int_from_value(entry_reader);
564 case IDP_FLOAT:
565 return idprop_array_float_from_value(entry_reader);
566 case IDP_DOUBLE:
567 return idprop_array_double_from_value(entry_reader);
568 default:
569 return nullptr;
570 }
571 }
572
573 private:
575 template</* C-primitive type of the values to add. Possible types are `float`, `int32_t` or
576 * `double`. */
577 typename PrimitiveType,
578 /* Type of value that can store the PrimitiveType in the Array. */
579 typename ValueType>
580 void add_values(ArrayValue &array, Span<PrimitiveType> values) const
581 {
582 for (PrimitiveType value : values) {
583 array.append(std::make_shared<ValueType>(value));
584 }
585 }
586
587 void add_values(ArrayValue &array, Span<IDProperty> values) const
588 {
589 for (const IDProperty &id_property : values) {
590 const IDPropertySerializer &value_serializer = serializer_for(
591 static_cast<eIDPropertyType>(id_property.type));
592 if (!value_serializer.supports_serializing()) {
593 continue;
594 }
595 array.append(value_serializer.idprop_to_dictionary(&id_property));
596 }
597 }
598
599 std::unique_ptr<IDProperty, IDPropertyDeleter> idprop_array_int_from_value(
600 DictionaryEntryParser &entry_reader) const
601 {
602 BLI_assert(*(entry_reader.get_type()) == IDP_ARRAY);
603 BLI_assert(*(entry_reader.get_subtype()) == IDP_INT);
604 std::optional<std::string> name = entry_reader.get_name();
605 if (!name.has_value()) {
606 return nullptr;
607 }
608 std::optional<Vector<int32_t>> extracted_value = entry_reader.get_array_int_value();
609 if (!extracted_value.has_value()) {
610 return nullptr;
611 }
612 return create(name->c_str(), *extracted_value);
613 }
614
615 std::unique_ptr<IDProperty, IDPropertyDeleter> idprop_array_float_from_value(
616 DictionaryEntryParser &entry_reader) const
617 {
618 BLI_assert(*(entry_reader.get_type()) == IDP_ARRAY);
619 BLI_assert(*(entry_reader.get_subtype()) == IDP_FLOAT);
620 std::optional<std::string> name = entry_reader.get_name();
621 if (!name.has_value()) {
622 return nullptr;
623 }
624 std::optional<Vector<float>> extracted_value = entry_reader.get_array_float_value();
625 if (!extracted_value.has_value()) {
626 return nullptr;
627 }
628 return create(name->c_str(), *extracted_value);
629 }
630
631 std::unique_ptr<IDProperty, IDPropertyDeleter> idprop_array_double_from_value(
632 DictionaryEntryParser &entry_reader) const
633 {
634 BLI_assert(*(entry_reader.get_type()) == IDP_ARRAY);
635 BLI_assert(*(entry_reader.get_subtype()) == IDP_DOUBLE);
636 std::optional<std::string> name = entry_reader.get_name();
637 if (!name.has_value()) {
638 return nullptr;
639 }
640 std::optional<Vector<double>> extracted_value = entry_reader.get_array_double_value();
641 if (!extracted_value.has_value()) {
642 return nullptr;
643 }
644 return create(name->c_str(), *extracted_value);
645 }
646};
647
650 public:
651 constexpr IDPGroupSerializer() = default;
652
653 std::string type_name() const override
654 {
656 }
657
658 std::optional<eIDPropertyType> property_type() const override
659 {
660 return IDP_GROUP;
661 }
662
663 std::shared_ptr<DictionaryValue> idprop_to_dictionary(
664 const IDProperty *id_property) const override
665 {
666 std::shared_ptr<DictionaryValue> result = create_dictionary(id_property);
667
668 std::shared_ptr<ArrayValue> array = std::make_shared<ArrayValue>();
669 for (IDProperty &sub_property : id_property->data.group) {
670 const IDPropertySerializer &sub_property_serializer = serializer_for(
671 static_cast<eIDPropertyType>(sub_property.type));
672 array->append(sub_property_serializer.idprop_to_dictionary(&sub_property));
673 }
674
675 result->append(IDP_KEY_VALUE, std::move(array));
676 return result;
677 }
678
679 std::unique_ptr<IDProperty, IDPropertyDeleter> entry_to_idprop(
680 DictionaryEntryParser &entry_reader) const override
681 {
682 BLI_assert(*(entry_reader.get_type()) == IDP_GROUP);
683 std::optional<std::string> name = entry_reader.get_name();
684 if (!name.has_value()) {
685 return nullptr;
686 }
687
688 const ArrayValue *array = entry_reader.get_array_value();
689 if (array == nullptr) {
690 return nullptr;
691 }
692
693 std::unique_ptr<IDProperty, IDPropertyDeleter> result = create_group(name->c_str());
694 for (const std::shared_ptr<Value> &element : array->elements()) {
695 if (element->type() != eValueType::Dictionary) {
696 continue;
697 }
698 const DictionaryValue *subobject = element->as_dictionary_value();
699 IDProperty *subproperty = idprop_from_value(*subobject);
700 if (subproperty == nullptr) {
701 continue;
702 }
703 if (!IDP_AddToGroup(result.get(), subproperty)) {
704 /* Duplicate name: the group did not take ownership. */
705 IDP_FreeProperty(subproperty);
706 }
707 }
708
709 return result;
710 }
711};
712
717 public:
718 constexpr IDPUnknownSerializer() = default;
719 std::string type_name() const override
720 {
722 }
723 std::optional<eIDPropertyType> property_type() const override
724 {
725 return std::nullopt;
726 }
727
728 std::shared_ptr<DictionaryValue> idprop_to_dictionary(
729 const IDProperty * /*id_property*/) const override
730 {
732 return nullptr;
733 }
734
735 bool supports_serializing() const override
736 {
737 return false;
738 }
739
740 std::unique_ptr<IDProperty, IDPropertyDeleter> entry_to_idprop(
741 DictionaryEntryParser & /*entry_reader*/) const override
742 {
743 return nullptr;
744 }
745};
746
747/* Serializers are constructed statically to remove construction/destruction. */
756
759{
760 switch (property_type) {
761 case IDP_STRING:
763
764 case IDP_BOOLEAN:
765 return IDP_SERIALIZER_BOOL;
766
767 case IDP_INT:
768 return IDP_SERIALIZER_INT;
769
770 case IDP_FLOAT:
772
773 case IDP_DOUBLE:
775
776 case IDP_ARRAY:
778
779 case IDP_GROUP:
781
782 default:
783 BLI_assert_msg(false, "Trying to convert an unsupported/unknown property type to a string");
785 }
786}
787
789static const IDPropertySerializer &serializer_for(StringRef idprop_typename)
790{
791 if (idprop_typename == IDP_PROPERTY_TYPENAME_STRING) {
793 }
794 if (idprop_typename == IDP_PROPERTY_TYPENAME_BOOL) {
795 return IDP_SERIALIZER_BOOL;
796 }
797 if (idprop_typename == IDP_PROPERTY_TYPENAME_INT) {
798 return IDP_SERIALIZER_INT;
799 }
800 if (idprop_typename == IDP_PROPERTY_TYPENAME_FLOAT) {
802 }
803 if (idprop_typename == IDP_PROPERTY_TYPENAME_DOUBLE) {
805 }
806 if (idprop_typename == IDP_PROPERTY_TYPENAME_ARRAY) {
808 }
809 if (idprop_typename == IDP_PROPERTY_TYPENAME_GROUP) {
811 }
813}
814
816
817/* -------------------------------------------------------------------- */
820
821std::unique_ptr<ArrayValue> convert_to_serialize_values(const IDProperty *properties)
822{
823 BLI_assert(properties != nullptr);
824 std::unique_ptr<ArrayValue> result = std::make_unique<ArrayValue>();
825 const IDProperty *current_property = properties;
826 while (current_property != nullptr) {
827 const IDPropertySerializer &serializer = serializer_for(
828 static_cast<eIDPropertyType>(current_property->type));
829 if (serializer.supports_serializing()) {
830 result->append(serializer.idprop_to_dictionary(current_property));
831 }
832 current_property = current_property->next;
833 }
834
835 return result;
836}
837
839
840/* -------------------------------------------------------------------- */
843
845{
846 DictionaryEntryParser entry_reader(value);
847 std::optional<eIDPropertyType> property_type = entry_reader.get_type();
848 if (!property_type.has_value()) {
849 return nullptr;
850 }
851
852 const IDPropertySerializer &serializer = serializer_for(*property_type);
853 return serializer.entry_to_idprop(entry_reader).release();
854}
855
857{
858 IDProperty *result = nullptr;
859 IDProperty *previous_added = nullptr;
860
861 for (const std::shared_ptr<Value> &element : value.elements()) {
862 if (element->type() != eValueType::Dictionary) {
863 continue;
864 }
865 const DictionaryValue *object_value = element->as_dictionary_value();
866 IDProperty *last_created = idprop_from_value(*object_value);
867 if (last_created == nullptr) {
868 continue;
869 }
870
871 if (result == nullptr) {
872 result = last_created;
873 }
874
875 if (previous_added) {
876 previous_added->next = last_created;
877 }
878 last_created->prev = previous_added;
879 previous_added = last_created;
880 }
881
882 return result;
883}
884
886{
887 if (value.type() != eValueType::Array) {
888 return nullptr;
889 }
890
891 return idprop_from_value(*value.as_array_value());
892}
893
895
896} // namespace blender::bke::idprop
#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(a)
#define BLI_assert_msg(a, msg)
void append(const array< T > &from)
const Value * lookup_ptr(const Key &key) const
Definition BLI_map.hh:508
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.
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.
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::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.
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...
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.
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::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.
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::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.
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.
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?
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.
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
Definition serialize.cc:56
PrimitiveType
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()
Definition idprop.cc:788
void IDP_FreeProperty(IDProperty *prop)
Definition idprop.cc:1403
signed int int32_t
Definition BKE_text.h:78
ATTR_WARN_UNUSED_RESULT const void * element
ListBaseT< struct IDProperty > group
Definition DNA_ID.h:133
eIDPropertyType type
Definition DNA_ID.h:145
struct IDProperty * prev
Definition DNA_ID.h:144
struct IDProperty * next
Definition DNA_ID.h:144
IDPropertyData data
Definition DNA_ID.h:157
Helper class for parsing DictionaryValues.
DictionaryEntryParser(const DictionaryValue &value)
std::optional< std::string > get_string_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
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