Blender V5.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
10
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# if defined(__GNUC__) || defined(__clang__) || defined(_MSC_VER)
19# define DNA_DEPRECATED [[deprecated]]
20# else
21# define DNA_DEPRECATED
22# endif
23#endif
24
25/* GCC considers default member initialization in constructors deprecated.
26 * This macro helps silence that while still warning in other cases */
27#if defined(__GNUC__) || defined(__clang__)
28/* clang-format off */
29# define DNA_DEPRECATED_IGNORE(statement) \
30 _Pragma("GCC diagnostic push") \
31 _Pragma("GCC diagnostic ignored \"-Wdeprecated-declarations\"") \
32 statement \
33 _Pragma("GCC diagnostic pop")
34/* clang-format on */
35#else
36# define DNA_DEPRECATED_IGNORE(statement) statement
37#endif
38
39#ifdef __GNUC__
40# define DNA_PRIVATE_ATTR __attribute__((deprecated))
41#else
42# define DNA_PRIVATE_ATTR
43#endif
44
45/* poison pragma */
46#ifdef DNA_DEPRECATED_ALLOW
47# define DNA_DEPRECATED_GCC_POISON 0
48#else
49/* enable the pragma if we can */
50# ifdef __GNUC__
51# define DNA_DEPRECATED_GCC_POISON 1
52# else
53# define DNA_DEPRECATED_GCC_POISON 0
54# endif
55#endif
56
57/* hrmf, we need a better include then this */
58#include "../blenlib/BLI_sys_types.hh" /* needed for int64_t only! */
59
60namespace blender {
61
62/* non-id name variables should use this length */
63#define MAX_NAME 64
64
65/* #DNA_DEFINE_CXX_METHODS is used to define C++ methods which are needed for proper/safe resource
66 * management, making unsafe (from an ownership perspective: i.e. pointers which sometimes needs to
67 * be set to nullptr on copy, sometimes needs to be dupalloc-ed) operations explicit, and taking
68 * care of compiler specific warnings when dealing with members marked with DNA_DEPRECATED.
69 *
70 * The `class_name` argument is to match the structure name the macro is used from.
71 *
72 * Typical usage example:
73 *
74 * struct Object {
75 * DNA_DEFINE_CXX_METHODS(Object)
76 * };
77 */
78
79/* Forward-declared here since there is no simple header file to be pulled for this functionality.
80 * Avoids pulling `string.h` from this header to get access to #memcpy. */
81void _DNA_internal_memcpy(void *dst, const void *src, size_t size);
82void _DNA_internal_memzero(void *dst, size_t size);
83void _DNA_internal_swap(void *a, void *b, size_t size);
84
85namespace dna::internal {
86
87template<class T> class ShallowDataConstRef {
88 public:
89 constexpr explicit ShallowDataConstRef(const T &ref) : ref_(ref) {}
90
91 const T *get_pointer() const
92 {
93 return &ref_;
94 }
95
96 private:
97 const T &ref_;
98};
99
101
102} // namespace dna::internal
103
104#define DNA_DEFINE_CXX_METHODS(class_name) \
105 DNA_DEPRECATED_IGNORE(class_name() = default;) \
106 ~class_name() = default; \
107 /* Delete copy and assignment, which are not safe for resource ownership. */ \
108 class_name(const class_name &other) = delete; \
109 class_name(class_name &&other) noexcept = delete; \
110 class_name &operator=(const class_name &other) = delete; \
111 class_name &operator=(class_name &&other) = delete; \
112 /* Support for shallow copy. */ \
113 /* NOTE: Calling the default constructor works-around deprecated warning generated by GCC. */ \
114 class_name(const dna::internal::ShallowDataConstRef<class_name> ref) : class_name() \
115 { \
116 _DNA_internal_memcpy(this, ref.get_pointer(), sizeof(class_name)); \
117 } \
118 class_name &operator=(const dna::internal::ShallowDataConstRef<class_name> ref) \
119 { \
120 if (this != ref.get_pointer()) { \
121 _DNA_internal_memcpy(this, ref.get_pointer(), sizeof(class_name)); \
122 } \
123 return *this; \
124 } \
125 /* Create object which memory is filled with zeros. */ \
126 class_name(const dna::internal::ShallowZeroInitializeTag /*tag*/) : class_name() \
127 { \
128 _DNA_internal_memzero(this, sizeof(class_name)); \
129 } \
130 class_name &operator=(const dna::internal::ShallowZeroInitializeTag /*tag*/) \
131 { \
132 _DNA_internal_memzero(this, sizeof(class_name)); \
133 return *this; \
134 }
135
136namespace dna {
137
138/* Creates shallow copy of the given object.
139 * The entire object is copied as-is using memory copy.
140 *
141 * Typical usage:
142 * Object temp_object = dna::shallow_copy(*input_object);
143 *
144 * From the implementation detail go via copy constructor/assign operator defined in the structure.
145 */
146template<typename T>
147[[nodiscard]] inline internal::ShallowDataConstRef<T> shallow_copy(const T &other)
148{
149 return internal::ShallowDataConstRef(other);
150}
151
152template<typename T> inline void shallow_copy_array(T *dst, const T *src, const int64_t size)
153{
154 _DNA_internal_memcpy(dst, src, sizeof(T) * size_t(size));
155}
156
157/* DNA object initializer which leads to an object which underlying memory is filled with zeroes.
158 */
163
164template<typename T> inline void shallow_swap(T &a, T &b)
165{
166 _DNA_internal_swap(&a, &b, sizeof(T));
167}
168
169} // namespace dna
170} // namespace blender
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
Definition btDbvt.cpp:52
constexpr ShallowDataConstRef(const T &ref)
Definition DNA_defs.h:89
#define T
void shallow_swap(T &a, T &b)
Definition DNA_defs.h:164
internal::ShallowZeroInitializeTag shallow_zero_initialize()
Definition DNA_defs.h:159
internal::ShallowDataConstRef< T > shallow_copy(const T &other)
Definition DNA_defs.h:147
void shallow_copy_array(T *dst, const T *src, const int64_t size)
Definition DNA_defs.h:152
BMesh Iterators.
__int64 int64_t
Definition BKE_text.h:90
void _DNA_internal_memcpy(void *dst, const void *src, size_t size)
Definition dna_utils.cc:266
void _DNA_internal_swap(void *a, void *b, size_t size)
Definition dna_utils.cc:276
void _DNA_internal_memzero(void *dst, size_t size)
Definition dna_utils.cc:271