Blender V5.3
BLO_read_write.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
30
31#pragma once
32
33#include <type_traits>
34
35#include "DNA_ID.h"
36#include "DNA_listBase.h"
37#include "DNA_sdna_type_ids.hh"
38
40#include "BLI_function_ref.hh"
42#include "BLI_map.hh"
43
44#include "BLO_core_bhead.hh"
45
46namespace blender {
47
50struct BlendLibReader;
51struct ID;
52struct ListBase;
53struct Main;
54struct WriteData;
55struct FileData;
57
58template<typename T> class BlendStructWriter;
59
68 protected:
73
74 public:
76 : wd_(&wd), struct_nr_(struct_nr), data_(data)
77 {
78 }
79
80 template<typename T> BlendStructWriter<T> typed();
81
87 void generated_ptr(int64_t offset);
88};
89
94template<typename T> class BlendStructWriter : public BlendStructWriterVoid {
95 public:
97
99 : BlendStructWriterVoid(writer), shallow_data(*reinterpret_cast<T *>(this->data_.data()))
100 {
101 }
102};
103
109
111
118template<typename T>
120 std::type_identity_t<FunctionRef<void(BlendStructWriter<T> &struct_writer)>>;
121
125#define blo_blend_struct_writer_fn_typed(T, fn) \
126 ((fn) ? BlendStructWriterFn([fn](BlendStructWriterVoid &struct_writer) { \
127 BlendStructWriter<T> typed_writer(struct_writer); \
128 fn(typed_writer); \
129 }) : \
130 nullptr)
131
133 WriteData *wd = nullptr;
134
139 bool is_undo() const;
140
144 StringRefNull filepath() const;
145
146 void write_struct_by_name(const char *struct_name,
147 const void *data,
148 BlendStructWriterFn fn = nullptr);
149 void write_struct_by_id(int struct_id, const void *data, BlendStructWriterFn fn = nullptr);
150 void write_struct_at_address_by_id(int struct_id,
151 const void *address,
152 const void *data,
153 BlendStructWriterFn fn = nullptr);
155 int struct_id,
156 const void *address,
157 const void *data,
158 BlendStructWriterFn fn = nullptr);
159 void write_struct_array_by_name(const char *struct_name,
160 int64_t array_size,
161 const void *data,
162 BlendStructWriterFn fn = nullptr);
163 void write_struct_array_by_id(int struct_id,
164 int64_t array_size,
165 const void *data,
166 BlendStructWriterFn fn = nullptr);
167 void write_struct_array_at_address_by_id(int struct_id,
168 int64_t array_size,
169 const void *address,
170 const void *data,
171 BlendStructWriterFn fn = nullptr);
172 void write_struct_list_by_name(const char *struct_name,
173 ListBase *list,
174 BlendStructWriterFn fn = nullptr);
175 void write_struct_list_by_id(int struct_id,
176 const ListBase *list,
177 BlendStructWriterFn fn = nullptr);
178
193 void write_raw(size_t size_in_bytes, const void *data);
194
196 void write_char_array(int64_t num, const char *data);
197 void write_int8_array(int64_t num, const int8_t *data);
202 void write_float_array(int64_t num, const float *data);
203 void write_double_array(int64_t num, const double *data);
204 void write_float3_array(int64_t num, const float *data);
205 void write_pointer_array(int64_t num, const void *data);
206
208 void write_string(const char *data);
209
219 void write_shared(const void *data,
220 size_t approximate_size_in_bytes,
221 const ImplicitSharingInfo *sharing_info,
222 FunctionRef<void()> write_fn);
223
224 int struct_id_by_name(const char *struct_name) const;
225
234 void generated_pointer_tag(const void *data);
235
236 template<typename T> void write_struct(const T *data, BlendStructWriterTypedFn<T> fn = nullptr)
237 {
238 this->write_struct_by_id(
240 }
241
242 template<typename T>
248
249 template<typename T>
250 void write_struct_at_address(const void *address,
251 const T *data,
253 {
256 }
257
258 template<typename T>
259 void write_struct_at_address_cast(const void *address,
260 const void *data,
262 {
265 }
266
267 template<typename T>
268 void write_struct_array(const int64_t array_size,
269 const T *data,
271 {
274 }
275
276 template<typename T>
277 void write_struct_array_cast(const int64_t array_size,
278 const void *data,
280 {
283 }
284
285 template<typename T>
287 const void *address,
288 const T *data,
290 {
292 array_size,
293 address,
294 data,
296 }
297
298 template<typename T>
304
305 template<typename T>
306 void write_id_struct(const void *id_address,
307 const T *id,
309 {
310 this->write_id_struct(GS(id_cast<const ID *>(id)->name), id_address, id, fn);
311 }
312
313 template<typename T>
314 void write_embedded_id_struct(const void *id_address,
315 const T *id,
317 {
318 this->write_id_struct(BLO_CODE_DATA, id_address, id, fn);
319 }
320
322 template<typename T>
323 void write_id_struct(const int filecode,
324 const void *id_address,
325 const T *id,
327 {
329 filecode,
331 id_address,
332 id,
333 [&](BlendStructWriterVoid &struct_writer) {
334 BlendStructWriter<T> typed_writer(struct_writer);
335 ID &shallow_id = *reinterpret_cast<ID *>(&typed_writer.shallow_data);
336 /* Clear runtime ID fields shared by every ID write path. */
337 if (this->is_undo()) {
338 shallow_id.tag &= ID_TAG_KEEP_ON_UNDO;
339 }
340 else {
341 shallow_id.tag = 0;
342 shallow_id.session_uid = 0;
343 shallow_id.recalc = 0;
344 shallow_id.recalc_up_to_undo_push = 0;
345 shallow_id.recalc_after_undo_push = 0;
346 }
347 shallow_id.us = 0;
348 shallow_id.icon_id = 0;
349 shallow_id.runtime = nullptr;
350 shallow_id.prev = nullptr;
351 shallow_id.next = nullptr;
352 shallow_id.orig_id = nullptr;
353 shallow_id.newid = nullptr;
354 shallow_id.py_instance = nullptr;
355 if (fn) {
356 fn(typed_writer);
357 }
358 });
359 }
360};
361
372
377
378/* -------------------------------------------------------------------- */
412
419 private:
420 static constexpr int static_size = 8192;
422
423 public:
424 BLO_Write_IDBuffer(ID &id, bool is_undo, bool is_placeholder);
425 BLO_Write_IDBuffer(ID &id, BlendWriter *writer);
426
428 {
429 return static_cast<ID *>(buffer_.buffer());
430 };
431};
432
434
435/* -------------------------------------------------------------------- */
465
466void *blo_read_raw_address_impl(BlendDataReader *reader, const void *old_address);
467#define BLO_read_raw_address(reader, ptr_p) \
468 *((void **)ptr_p) = blo_read_raw_address_impl((reader), *(ptr_p))
469
476void *blo_read_struct_impl(BlendDataReader *reader, const void *old_address, size_t expected_size);
477#define BLO_read_struct(reader, struct_name, ptr_p) \
478 (*((void **)ptr_p) = blo_read_struct_impl(reader, *((void **)ptr_p), sizeof(struct_name)))
479
484void *blo_read_struct_nonnull_impl(BlendDataReader *reader,
485 const void *old_address,
486 size_t expected_size);
487#define BLO_read_struct_nonnull(reader, struct_name, ptr_p) \
488 (*((void **)ptr_p) = blo_read_struct_nonnull_impl( \
489 reader, *((void **)ptr_p), sizeof(struct_name)))
490
500void *blo_read_struct_no_us_impl(BlendDataReader *reader,
501 const void *old_address,
502 size_t expected_size);
503
504#define BLO_read_struct_no_us(reader, struct_name, ptr_p) \
505 (*((void **)ptr_p) = blo_read_struct_no_us_impl(reader, *((void **)ptr_p), sizeof(struct_name)))
506
507#define BLO_read_struct_array_no_us(reader, struct_name, ptr_p, array_size) \
508 (*((void **)ptr_p) = blo_read_struct_no_us_impl( \
509 reader, *((void **)ptr_p), sizeof(struct_name) * size_t(array_size)))
510
514void *blo_read_struct_no_us_nonnull_impl(BlendDataReader *reader,
515 const void *old_address,
516 size_t expected_size);
517
518#define BLO_read_struct_no_us_nonnull(reader, struct_name, ptr_p) \
519 (*((void **)ptr_p) = blo_read_struct_no_us_nonnull_impl( \
520 reader, *((void **)ptr_p), sizeof(struct_name)))
521
528void *BLO_read_struct_by_name_array(BlendDataReader *reader,
529 StringRef struct_name,
530 int64_t items_num,
531 const void *old_address);
532
533/* Read all elements in list
534 *
535 * Updates all `->prev` and `->next` pointers of the list elements.
536 * Updates the `list->first` and `list->last` pointers.
537 */
538void BLO_read_struct_list_with_size(BlendDataReader *reader,
539 size_t expected_elem_size,
540 ListBase *list);
541
542#define BLO_read_struct_list(reader, struct_name, list) \
543 BLO_read_struct_list_with_size(reader, sizeof(struct_name), list)
544
550void BLO_read_struct_list_with_type_size(BlendDataReader *reader,
551 size_t base_size,
552 ListBase *list,
553 FunctionRef<size_t(const void *data)> type_size_fn);
554void *blo_read_struct_with_type_size_impl(BlendDataReader *reader,
555 const void *old_address,
556 size_t base_size,
557 FunctionRef<size_t(const void *data)> type_size_fn);
558
559template<typename T>
561 ListBaseT<T> *list,
562 FunctionRef<size_t(const T &data)> type_size_fn)
563{
565 reader, sizeof(T), reinterpret_cast<ListBase *>(list), [&](const void *data) {
566 return type_size_fn(*static_cast<const T *>(data));
567 });
568}
569
570template<typename T>
572 T **ptr_p,
573 FunctionRef<size_t(const T &data)> type_size_fn)
574{
575 *ptr_p = static_cast<T *>(
576 blo_read_struct_with_type_size_impl(reader, *ptr_p, sizeof(T), [&](const void *data) {
577 return type_size_fn(*static_cast<const T *>(data));
578 }));
579}
580
593[[nodiscard]] bool blo_read_array_impl(
594 BlendDataReader *reader, int64_t array_size, int elems, size_t elem_size, void **ptr_p);
595
596template<typename T, typename SizeT>
597 requires(!std::is_void_v<T> && !std::is_pointer_v<T>)
598[[nodiscard]] bool BLO_read_array(BlendDataReader *reader,
599 T **ptr_p,
600 const SizeT array_size,
601 const int elems = 1)
602{
603 return blo_read_array_impl(
604 reader, int64_t(array_size), elems, sizeof(T), reinterpret_cast<void **>(ptr_p));
605}
606
607template<typename T, typename SizeT>
608 requires(!std::is_void_v<T> && !std::is_pointer_v<T>)
610 T **ptr_p,
611 SizeT *array_size,
612 const int elems = 1)
613{
615 reader, int64_t(*array_size), elems, sizeof(T), reinterpret_cast<void **>(ptr_p)))
616 {
617 *array_size = 0;
618 }
619}
620
625[[nodiscard]] bool blo_read_pointer_array_impl(BlendDataReader *reader,
626 int64_t array_size,
627 void **ptr_p);
628
629template<typename T, typename SizeT>
630 requires(std::is_pointer_v<T> || std::is_void_v<T>)
631[[nodiscard]] bool BLO_read_pointer_array(BlendDataReader *reader,
632 T **ptr_p,
633 const SizeT array_size)
634{
635 return blo_read_pointer_array_impl(reader, array_size, reinterpret_cast<void **>(ptr_p));
636}
637
638template<typename T, typename SizeT>
639 requires(std::is_pointer_v<T> || std::is_void_v<T>)
641 T **ptr_p,
642 SizeT *array_size)
643{
644 if (!blo_read_pointer_array_impl(reader, *array_size, reinterpret_cast<void **>(ptr_p))) {
645 *array_size = 0;
646 }
647}
648
649/* Read null terminated string. */
650
651void BLO_read_string(BlendDataReader *reader, char **ptr_p);
652void BLO_read_string(BlendDataReader *reader, char *const *ptr_p);
653void BLO_read_string(BlendDataReader *reader, const char **ptr_p);
654
655/* Misc. */
656
658 BlendDataReader *reader,
659 const void **ptr_p,
660 FunctionRef<const ImplicitSharingInfo *()> read_fn);
661
666template<typename T>
668 T **data_ptr,
669 FunctionRef<const ImplicitSharingInfo *()> read_fn)
670{
672 reader, (const void **)data_ptr, read_fn);
673 /* Need const-cast here, because not all DNA members that reference potentially shared data are
674 * const yet. */
675 *data_ptr = const_cast<T *>(static_cast<const T *>(shared_data.data));
676 return shared_data.sharing_info;
677}
678
679int BLO_read_fileversion_get(BlendDataReader *reader);
680bool BLO_read_data_is_undo(BlendDataReader *reader);
681void BLO_read_data_globmap_add(BlendDataReader *reader, void *oldaddr, void *newaddr);
682void BLO_read_glob_list(BlendDataReader *reader, ListBase *list);
683BlendFileReadReport *BLO_read_data_reports(BlendDataReader *reader);
684void BLO_read_data_invalidate(BlendDataReader *reader, const char *message);
687
689
690/* -------------------------------------------------------------------- */
696
707 ID *self_id,
708 const bool is_linked_only,
709 ID *id) ATTR_NONNULL(2);
710
723 ATTR_NONNULL(1);
724
725/* Misc. */
726
730
732
733/* -------------------------------------------------------------------- */
736
744void BLO_reportf_wrap(BlendFileReadReport *reports, eReportType type, const char *format, ...)
745 ATTR_PRINTF_FORMAT(3, 4);
746
748
749} // namespace blender
#define blo_blend_struct_writer_fn_typed(T, fn)
ID and Library types, which are fundamental for SDNA.
#define ID_TAG_KEEP_ON_UNDO
Definition DNA_ID.h:1044
These structs are the foundation for all linked lists in the library system.
long long int int64_t
BlendStructWriterVoid(WriteData &wd, const int struct_nr, MutableSpan< char > data)
BlendStructWriter< T > typed()
void generated_ptr(int64_t offset)
Definition writefile.cc:986
BlendStructWriter(const BlendStructWriterVoid &writer)
#define T
int sdna_struct_id_get()
BMesh Iterators.
void BLO_read_struct_with_type_size(BlendDataReader *reader, T **ptr_p, FunctionRef< size_t(const T &data)> type_size_fn)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
bool blo_read_array_impl(BlendDataReader *reader, int64_t array_size, int elems, size_t elem_size, void **ptr_p)
Definition readfile.cc:6477
void BLO_read_glob_list(BlendDataReader *reader, ListBase *list)
Definition readfile.cc:6829
__int64 int64_t
Definition BKE_text.h:90
struct Library * BLO_read_data_current_library(BlendDataReader *reader)
const ImplicitSharingInfo * BLO_read_shared(BlendDataReader *reader, T **data_ptr, FunctionRef< const ImplicitSharingInfo *()> read_fn)
void BLO_read_struct_list_with_size(BlendDataReader *reader, size_t expected_elem_size, ListBase *list)
Definition readfile.cc:6551
void * blo_read_struct_no_us_impl(BlendDataReader *reader, const void *old_address, size_t expected_size)
Definition readfile.cc:6440
unsigned int uint
Definition BKE_text.h:55
BlendFileReadReport * BLO_read_data_reports(BlendDataReader *reader)
Definition readfile.cc:6834
void * BLO_read_struct_by_name_array(BlendDataReader *reader, StringRef struct_name, int64_t items_num, const void *old_address)
Definition readfile.cc:6521
void BLO_read_data_globmap_add(BlendDataReader *reader, void *oldaddr, void *newaddr)
Definition readfile.cc:6824
void BLO_read_pointer_array_and_validate_size(BlendDataReader *reader, T **ptr_p, SizeT *array_size)
ImplicitSharingInfoAndData blo_read_shared_impl(BlendDataReader *reader, const void **ptr_p, FunctionRef< const ImplicitSharingInfo *()> read_fn)
void BLO_read_data_invalidate(BlendDataReader *reader, const char *message)
Definition readfile.cc:6839
void * blo_read_raw_address_impl(BlendDataReader *reader, const void *old_address)
Definition readfile.cc:6419
unsigned short uint16_t
Definition BKE_text.h:80
bool BLO_read_data_is_undo(BlendDataReader *reader)
Definition readfile.cc:6819
unsigned int uint32_t
Definition BKE_text.h:81
std::type_identity_t< FunctionRef< void(BlendStructWriter< T > &struct_writer)> > BlendStructWriterTypedFn
void * blo_read_struct_nonnull_impl(BlendDataReader *reader, const void *old_address, size_t expected_size)
Definition readfile.cc:6461
BMesh const char void * data
void * blo_read_struct_with_type_size_impl(BlendDataReader *reader, const void *old_address, size_t base_size, FunctionRef< size_t(const void *data)> type_size_fn)
Definition readfile.cc:6633
unsigned char uint8_t
Definition BKE_text.h:79
bool BLO_read_pointer_array(BlendDataReader *reader, T **ptr_p, const SizeT array_size)
BlendFileReadReport * BLO_read_lib_reports(BlendLibReader *reader)
Definition readfile.cc:6858
void BLO_read_struct_list_with_type_size(BlendDataReader *reader, size_t base_size, ListBase *list, FunctionRef< size_t(const void *data)> type_size_fn)
Definition readfile.cc:6591
ID * BLO_read_get_new_id_address(BlendLibReader *reader, ID *self_id, const bool is_linked_only, ID *id) ATTR_NONNULL(2)
Definition readfile.cc:6533
bool blo_read_pointer_array_impl(BlendDataReader *reader, int64_t array_size, void **ptr_p)
Definition readfile.cc:6708
void BLO_read_data_set_need_preview_render_restart(BlendDataReader *reader)
Definition readfile.cc:6863
ATTR_WARN_UNUSED_RESULT const size_t num
void BLO_reportf_wrap(BlendFileReadReport *reports, eReportType type, const char *format,...) ATTR_PRINTF_FORMAT(3
Definition readfile.cc:237
bool BLO_read_array(BlendDataReader *reader, T **ptr_p, const SizeT array_size, const int elems=1)
void BLO_read_array_and_validate_size(BlendDataReader *reader, T **ptr_p, SizeT *array_size, const int elems=1)
Main * BLO_read_lib_get_main(BlendLibReader *reader)
Definition readfile.cc:6853
size_t ATTR_PRINTF_FORMAT(3, 4)
int BLO_read_fileversion_get(BlendDataReader *reader)
Definition readfile.cc:6546
ID_Type GS(const char *name)
Definition DNA_ID.h:410
signed int int32_t
Definition BKE_text.h:78
bool BLO_read_lib_is_undo(BlendLibReader *reader)
Definition readfile.cc:6848
void * blo_read_struct_no_us_nonnull_impl(BlendDataReader *reader, const void *old_address, size_t expected_size)
Definition readfile.cc:6469
FunctionRef< void(BlendStructWriterVoid &struct_writer)> BlendStructWriterFn
void ATTR_NONNULL(1, 2)
void * blo_read_struct_impl(BlendDataReader *reader, const void *old_address, size_t expected_size)
Definition readfile.cc:6424
ID * BLO_read_get_new_id_address_from_session_uid(BlendLibReader *reader, uint session_uid) ATTR_NONNULL(1)
Definition readfile.cc:6541
void BLO_read_string(BlendDataReader *reader, char **ptr_p)
Definition readfile.cc:6653
signed char int8_t
Definition BKE_text.h:76
signed short int16_t
Definition BKE_text.h:77
BLO_Write_IDBuffer(ID &id, bool is_undo, bool is_placeholder)
Map< uint64_t, ImplicitSharingInfoAndData > shared_data_by_stored_address
void write_struct_array_cast(const int64_t array_size, const void *data, BlendStructWriterTypedFn< T > fn=nullptr)
void write_pointer_array(int64_t num, const void *data)
void write_struct_by_id(int struct_id, const void *data, BlendStructWriterFn fn=nullptr)
void write_struct_array_by_id(int struct_id, int64_t array_size, const void *data, BlendStructWriterFn fn=nullptr)
void write_struct_array_by_name(const char *struct_name, int64_t array_size, const void *data, BlendStructWriterFn fn=nullptr)
int struct_id_by_name(const char *struct_name) const
void write_shared(const void *data, size_t approximate_size_in_bytes, const ImplicitSharingInfo *sharing_info, FunctionRef< void()> write_fn)
void generated_pointer_tag(const void *data)
void write_string(const char *data)
void write_struct_by_name(const char *struct_name, const void *data, BlendStructWriterFn fn=nullptr)
void write_embedded_id_struct(const void *id_address, const T *id, BlendStructWriterTypedFn< T > fn=nullptr)
void write_uint8_array(int64_t num, const uint8_t *data)
void write_struct_list_by_id(int struct_id, const ListBase *list, BlendStructWriterFn fn=nullptr)
void write_int8_array(int64_t num, const int8_t *data)
void write_struct_at_address_by_id(int struct_id, const void *address, const void *data, BlendStructWriterFn fn=nullptr)
void write_float_array(int64_t num, const float *data)
void write_struct_list_by_name(const char *struct_name, ListBase *list, BlendStructWriterFn fn=nullptr)
void write_struct_array_at_address_by_id(int struct_id, int64_t array_size, const void *address, const void *data, BlendStructWriterFn fn=nullptr)
void write_struct_at_address(const void *address, const T *data, BlendStructWriterTypedFn< T > fn=nullptr)
void write_id_struct(const int filecode, const void *id_address, const T *id, BlendStructWriterTypedFn< T > fn=nullptr)
void write_float3_array(int64_t num, const float *data)
void write_struct_at_address_cast(const void *address, const void *data, BlendStructWriterTypedFn< T > fn=nullptr)
void write_id_struct(const void *id_address, const T *id, BlendStructWriterTypedFn< T > fn=nullptr)
void write_char_array(int64_t num, const char *data)
void write_struct_list(const ListBaseT< T > *list, BlendStructWriterTypedFn< T > fn=nullptr)
void write_int16_array(int64_t num, const int16_t *data)
void write_struct_cast(const void *data, BlendStructWriterTypedFn< T > fn=nullptr)
void write_uint32_array(int64_t num, const uint32_t *data)
void write_struct(const T *data, BlendStructWriterTypedFn< T > fn=nullptr)
void write_double_array(int64_t num, const double *data)
bool is_undo() const
void write_struct_at_address_by_id_with_filecode(int filecode, int struct_id, const void *address, const void *data, BlendStructWriterFn fn=nullptr)
void write_raw(size_t size_in_bytes, const void *data)
void write_struct_array_at_address(const int64_t array_size, const void *address, const T *data, BlendStructWriterTypedFn< T > fn=nullptr)
void write_struct_array(const int64_t array_size, const T *data, BlendStructWriterTypedFn< T > fn=nullptr)
void write_int32_array(int64_t num, const int32_t *data)
StringRefNull filepath() const
unsigned int session_uid
Definition DNA_ID.h:463
bke::id::ID_Runtime * runtime
Definition DNA_ID.h:541
struct ID * newid
Definition DNA_ID.h:419
unsigned int recalc_up_to_undo_push
Definition DNA_ID.h:456
void * prev
Definition DNA_ID.h:418
void * next
Definition DNA_ID.h:418
int icon_id
Definition DNA_ID.h:445
unsigned int recalc_after_undo_push
Definition DNA_ID.h:457
unsigned int recalc
Definition DNA_ID.h:446
struct ID * orig_id
Definition DNA_ID.h:502
void * py_instance
Definition DNA_ID.h:519
const ImplicitSharingInfo * sharing_info