Blender V5.3
imbuf/intern/metadata.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2024 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <cstdlib>
10#include <cstring>
11#include <utility>
12
15#include "BLI_listbase.hh"
16#include "BLI_string.hh"
17
18#include "BKE_idprop.hh"
19
20#include "DNA_ID.h" /* ID property definitions. */
21
22#include "IMB_imbuf_types.hh"
23
24#include "IMB_metadata.hh"
25
26namespace blender {
27
30 public:
32
37
38 private:
39 void delete_self_with_data() override
40 {
42 MEM_delete(this);
43 }
44};
45
47{
48 if (*metadata != nullptr) {
49 return;
50 }
51
52 *metadata = bke::idprop::create_group("metadata").release();
53}
54
56{
57 return this->metadata_ptr;
58}
59
61{
62 BLI_assert((this->metadata_ptr == nullptr) == (this->metadata_sharing_info == nullptr));
63
64 if (this->metadata_ptr == nullptr) {
65 /* Allocate on demand. */
66 this->metadata_ptr = bke::idprop::create_group("metadata").release();
68 MEM_new<MetaDataImplicitSharing>(__func__, this->metadata_ptr));
69 }
70 else if (!this->metadata_sharing_info->is_mutable()) {
71 /* Copy on write. */
74 MEM_new<MetaDataImplicitSharing>(__func__, this->metadata_ptr));
75 }
76
77 return this->metadata_ptr;
78}
79
81{
82 BLI_assert(metadata != nullptr);
83 BLI_assert(sharing_info.get() != nullptr);
84
85 this->metadata_ptr = const_cast<IDProperty *>(metadata);
86 this->metadata_sharing_info = std::move(sharing_info);
87}
88
90{
91 if (metadata == nullptr) {
92 return;
93 }
94
96}
97
99 const char *key,
100 char *value,
101 const size_t value_maxncpy)
102{
103 if (metadata == nullptr) {
104 return false;
105 }
106
108
109 if (prop && prop->type == IDP_STRING) {
110 BLI_strncpy(value, IDP_string_get(prop), value_maxncpy);
111 return true;
112 }
113 return false;
114}
115
116void IMB_metadata_copy(ImBuf *ibuf_dst, const ImBuf *ibuf_src)
117{
118 BLI_assert(ibuf_dst != ibuf_src);
119 ibuf_dst->metadata_ptr = ibuf_src->metadata_ptr;
120 ibuf_dst->metadata_sharing_info = ibuf_src->metadata_sharing_info;
121}
122
123void IMB_metadata_set_field(IDProperty *metadata, const char *key, const char *value)
124{
127
128 if (prop != nullptr && prop->type != IDP_STRING) {
130 prop = nullptr;
131 }
132
133 if (prop) {
134 IDP_AssignString(prop, value);
135 }
136 else {
137 prop = bke::idprop::create(key, value).release();
139 }
140}
141
142void IMB_metadata_foreach(const ImBuf *ibuf, IMBMetadataForeachCb callback, void *userdata)
143{
144 const IDProperty *metadata = ibuf->metadata();
145 if (metadata == nullptr) {
146 return;
147 }
148 for (const IDProperty &prop : metadata->data.group) {
149 callback(prop.name, IDP_string_get(&prop), userdata);
150 }
151}
152
153} // namespace blender
#define IDP_string_get(prop)
ID and Library types, which are fundamental for SDNA.
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.
std::unique_ptr< IDProperty, IDPropertyDeleter > create_group(StringRef prop_name, eIDPropertyFlag flags={})
Allocate a new IDProperty of type IDP_GROUP.
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
void IDP_FreeFromGroup(IDProperty *group, IDProperty *prop) ATTR_NONNULL()
Definition idprop.cc:812
void(*)(const char *field, const char *value, void *userdata) IMBMetadataForeachCb
bool IDP_AddToGroup(IDProperty *group, IDProperty *prop) ATTR_NONNULL()
Definition idprop.cc:788
IDProperty * IDP_CopyProperty(const IDProperty *prop) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition idprop.cc:986
IDProperty * IDP_GetPropertyFromGroup(const IDProperty *prop, StringRef name) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition idprop.cc:818
void IMB_metadata_ensure(IDProperty **metadata)
char * BLI_strncpy(char *__restrict dst, const char *__restrict src, size_t dst_maxncpy) ATTR_NONNULL(1
void IDP_AssignString(IDProperty *prop, const char *st) ATTR_NONNULL()
Definition idprop.cc:505
void IDP_FreeProperty(IDProperty *prop)
Definition idprop.cc:1403
void IMB_metadata_copy(ImBuf *ibuf_dst, const ImBuf *ibuf_src)
void IMB_metadata_foreach(const ImBuf *ibuf, IMBMetadataForeachCb callback, void *userdata)
bool IMB_metadata_get_field(const IDProperty *metadata, const char *key, char *value, size_t value_maxncpy)
void IMB_metadata_set_field(IDProperty *metadata, const char *key, const char *value)
void IMB_metadata_free(IDProperty *metadata)
eIDPropertyType type
Definition DNA_ID.h:145
IDProperty * metadata_ptr
IDProperty * metadata_for_write()
ImplicitSharingPtr metadata_sharing_info
const IDProperty * metadata() const
void assign_metadata(const IDProperty *metadata, ImplicitSharingPtr<> sharing_info)