Blender V4.3
DNA_defs.h
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
11#pragma once
12
13/* `makesdna` ignores. */
14#ifdef DNA_DEPRECATED_ALLOW
15/* allow use of deprecated items */
16# define DNA_DEPRECATED
17#else
18# ifndef DNA_DEPRECATED
19# ifdef __GNUC__
20# define DNA_DEPRECATED __attribute__((deprecated))
21# else
22/* TODO: MSVC & others. */
23# define DNA_DEPRECATED
24# endif
25# endif
26#endif
27
28#ifdef __GNUC__
29# define DNA_PRIVATE_ATTR __attribute__((deprecated))
30#else
31# define DNA_PRIVATE_ATTR
32#endif
33
34/* poison pragma */
35#ifdef DNA_DEPRECATED_ALLOW
36# define DNA_DEPRECATED_GCC_POISON 0
37#else
38/* enable the pragma if we can */
39# ifdef __GNUC__
40# define DNA_DEPRECATED_GCC_POISON 1
41# else
42# define DNA_DEPRECATED_GCC_POISON 0
43# endif
44#endif
45
46/* hrmf, we need a better include then this */
47#include "../blenlib/BLI_sys_types.h" /* needed for int64_t only! */
48
49/* non-id name variables should use this length */
50#define MAX_NAME 64
51
52/* #DNA_DEFINE_CXX_METHODS is used to define C++ methods which are needed for proper/safe resource
53 * management, making unsafe (from an ownership perspective: i.e. pointers which sometimes needs to
54 * be set to nullptr on copy, sometimes needs to be dupalloc-ed) operations explicit, and taking
55 * care of compiler specific warnings when dealing with members marked with DNA_DEPRECATED.
56 *
57 * The `class_name` argument is to match the structure name the macro is used from.
58 *
59 * Typical usage example:
60 *
61 * typedef struct Object {
62 * DNA_DEFINE_CXX_METHODS(Object)
63 * } Object;
64 */
65#ifndef __cplusplus
66# define DNA_DEFINE_CXX_METHODS(class_name)
67#else
68
69/* Forward-declared here since there is no simple header file to be pulled for this functionality.
70 * Avoids pulling `string.h` from this header to get access to #memcpy. */
71extern "C" void _DNA_internal_memcpy(void *dst, const void *src, size_t size);
72extern "C" void _DNA_internal_memzero(void *dst, size_t size);
73extern "C" void _DNA_internal_swap(void *a, void *b, size_t size);
74
75namespace blender::dna::internal {
76
77template<class T> class ShallowDataConstRef {
78 public:
79 constexpr explicit ShallowDataConstRef(const T &ref) : ref_(ref) {}
80
81 inline const T *get_pointer() const
82 {
83 return &ref_;
84 }
85
86 private:
87 const T &ref_;
88};
89
90class ShallowZeroInitializeTag {};
91
92} // namespace blender::dna::internal
93
94# define DNA_DEFINE_CXX_METHODS(class_name) \
95 class_name() = default; \
96 ~class_name() = default; \
97 /* Delete copy and assignment, which are not safe for resource ownership. */ \
98 class_name(const class_name &other) = delete; \
99 class_name(class_name &&other) noexcept = delete; \
100 class_name &operator=(const class_name &other) = delete; \
101 class_name &operator=(class_name &&other) = delete; \
102 /* Support for shallow copy. */ \
103 /* NOTE: Calling the default constructor works-around deprecated warning generated by GCC. */ \
104 class_name(const blender::dna::internal::ShallowDataConstRef<class_name> ref) : class_name() \
105 { \
106 _DNA_internal_memcpy(this, ref.get_pointer(), sizeof(class_name)); \
107 } \
108 class_name &operator=(const blender::dna::internal::ShallowDataConstRef<class_name> ref) \
109 { \
110 if (this != ref.get_pointer()) { \
111 _DNA_internal_memcpy(this, ref.get_pointer(), sizeof(class_name)); \
112 } \
113 return *this; \
114 } \
115 /* Create object which memory is filled with zeros. */ \
116 class_name(const blender::dna::internal::ShallowZeroInitializeTag /*tag*/) : class_name() \
117 { \
118 _DNA_internal_memzero(this, sizeof(class_name)); \
119 } \
120 class_name &operator=(const blender::dna::internal::ShallowZeroInitializeTag /*tag*/) \
121 { \
122 _DNA_internal_memzero(this, sizeof(class_name)); \
123 return *this; \
124 }
125
126namespace blender::dna {
127
128/* Creates shallow copy of the given object.
129 * The entire object is copied as-is using memory copy.
130 *
131 * Typical usage:
132 * Object temp_object = blender::dna::shallow_copy(*input_object);
133 *
134 * From the implementation detail go via copy constructor/assign operator defined in the structure.
135 */
136template<typename T>
137[[nodiscard]] inline internal::ShallowDataConstRef<T> shallow_copy(const T &other)
138{
139 return internal::ShallowDataConstRef(other);
140}
141
142template<typename T> inline void shallow_copy_array(T *dst, const T *src, const int64_t size)
143{
144 _DNA_internal_memcpy(dst, src, sizeof(T) * size_t(size));
145}
146
147/* DNA object initializer which leads to an object which underlying memory is filled with zeroes.
148 */
149[[nodiscard]] inline internal::ShallowZeroInitializeTag shallow_zero_initialize()
150{
151 return internal::ShallowZeroInitializeTag();
152}
153
154template<typename T> inline void shallow_swap(T &a, T &b)
155{
156 _DNA_internal_swap(&a, &b, sizeof(T));
157}
158
159} // namespace blender::dna
160
161#endif
local_group_size(16, 16) .push_constant(Type b
void _DNA_internal_memzero(void *dst, size_t size)
Definition dna_utils.cc:338
void _DNA_internal_swap(void *a, void *b, size_t size)
Definition dna_utils.cc:344
void _DNA_internal_memcpy(void *dst, const void *src, size_t size)
Definition dna_utils.cc:332
__int64 int64_t
Definition stdint.h:89