Blender V5.0
bmesh_operator_api_inline.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
10
11#pragma once
12
13#include "BLI_compiler_attrs.h"
14#include "BLI_compiler_compat.h"
15
16#include "bmesh_class.hh"
17
19
20struct BMOperator;
21
22/* Tool Flag API: Tool code must never put junk in header flags (#BMHeader.hflag)
23 * instead, use this API to set flags.
24 * If you need to store a value per element, use a #GHash or a mapping slot to do it. */
25
27 short _bmo_elem_flag_test(BMesh *bm, const BMFlagLayer *oflags, const short oflag)
28{
29 BLI_assert(bm->use_toolflags);
30 return oflags[bm->toolflag_index].f & oflag;
31}
32
34 bool _bmo_elem_flag_test_bool(BMesh *bm, const BMFlagLayer *oflags, const short oflag)
35{
36 BLI_assert(bm->use_toolflags);
37 return (oflags[bm->toolflag_index].f & oflag) != 0;
38}
39
40ATTR_NONNULL(1, 2)
42{
43 BLI_assert(bm->use_toolflags);
44 oflags[bm->toolflag_index].f |= oflag;
45}
46
47ATTR_NONNULL(1, 2)
49{
50 BLI_assert(bm->use_toolflags);
51 oflags[bm->toolflag_index].f &= (short)~oflag;
52}
53
54ATTR_NONNULL(1, 2)
56{
57 BLI_assert(bm->use_toolflags);
58 if (val) {
59 oflags[bm->toolflag_index].f |= oflag;
60 }
61 else {
62 oflags[bm->toolflag_index].f &= (short)~oflag;
63 }
64}
65
66ATTR_NONNULL(1, 2)
68{
69 BLI_assert(bm->use_toolflags);
70 oflags[bm->toolflag_index].f ^= oflag;
71}
72
73ATTR_NONNULL(1, 2)
75 BMOpSlot *slot,
76 void *element,
77 const int val)
78{
79 union {
80 void *ptr;
81 int val;
82 } t = {nullptr};
83 BLI_assert(slot->slot_subtype.map == BMO_OP_SLOT_SUBTYPE_MAP_INT);
84 BMO_slot_map_insert(op, slot, element, ((void)(t.val = val), t.ptr));
85}
86
87ATTR_NONNULL(1, 2)
89 BMOpSlot *slot,
90 void *element,
91 const bool val)
92{
93 union {
94 void *ptr;
95 bool val;
96 } t = {nullptr};
97 BLI_assert(slot->slot_subtype.map == BMO_OP_SLOT_SUBTYPE_MAP_BOOL);
98 BMO_slot_map_insert(op, slot, element, ((void)(t.val = val), t.ptr));
99}
100
101ATTR_NONNULL(1, 2)
103 BMOpSlot *slot,
104 void *element,
105 const float val)
106{
107 union {
108 void *ptr;
109 float val;
110 } t = {nullptr};
111 BLI_assert(slot->slot_subtype.map == BMO_OP_SLOT_SUBTYPE_MAP_FLT);
112 BMO_slot_map_insert(op, slot, element, ((void)(t.val = val), t.ptr));
113}
114
115/* pointer versions of BMO_slot_map_float_get and BMO_slot_map_float_insert.
116 *
117 * do NOT use these for non-operator-api-allocated memory! instead
118 * use BMO_slot_map_data_get and BMO_slot_map_insert, which copies the data. */
119
120ATTR_NONNULL(1, 2)
122 BMOpSlot *slot,
123 const void *element,
124 void *val)
125{
126 BLI_assert(slot->slot_subtype.map == BMO_OP_SLOT_SUBTYPE_MAP_INTERNAL);
127 BMO_slot_map_insert(op, slot, element, val);
128}
129
130ATTR_NONNULL(1, 2)
132 BMOpSlot *slot,
133 const void *element,
134 void *val)
135{
136 BLI_assert(slot->slot_subtype.map == BMO_OP_SLOT_SUBTYPE_MAP_ELEM);
137 BMO_slot_map_insert(op, slot, element, val);
138}
139
140/* no values */
141ATTR_NONNULL(1, 2)
143{
144 BLI_assert(slot->slot_subtype.map == BMO_OP_SLOT_SUBTYPE_MAP_EMPTY);
145 BMO_slot_map_insert(op, slot, element, nullptr);
146}
147
149 bool BMO_slot_map_contains(BMOpSlot *slot, const void *element)
150{
151 BLI_assert(slot->slot_type == BMO_OP_SLOT_MAPPING);
152 return BLI_ghash_haskey(slot->data.ghash, element);
153}
154
156 void **BMO_slot_map_data_get(BMOpSlot *slot, const void *element)
157{
158
159 return BLI_ghash_lookup_p(slot->data.ghash, element);
160}
161
163 float BMO_slot_map_float_get(BMOpSlot *slot, const void *element)
164{
165 void **data;
166 BLI_assert(slot->slot_subtype.map == BMO_OP_SLOT_SUBTYPE_MAP_FLT);
167
168 data = BMO_slot_map_data_get(slot, element);
169 if (data) {
170 return *(float *)data;
171 }
172 return 0.0f;
173}
174
176 int BMO_slot_map_int_get(BMOpSlot *slot, const void *element)
177{
178 void **data;
179 BLI_assert(slot->slot_subtype.map == BMO_OP_SLOT_SUBTYPE_MAP_INT);
180
181 data = BMO_slot_map_data_get(slot, element);
182 if (data) {
183 return *(int *)data;
184 }
185 return 0;
186}
187
189 bool BMO_slot_map_bool_get(BMOpSlot *slot, const void *element)
190{
191 void **data;
192 BLI_assert(slot->slot_subtype.map == BMO_OP_SLOT_SUBTYPE_MAP_BOOL);
193
194 data = BMO_slot_map_data_get(slot, element);
195 if (data) {
196 return *(bool *)data;
197 }
198 return false;
199}
200
202 void *BMO_slot_map_ptr_get(BMOpSlot *slot, const void *element)
203{
204 void **val = BMO_slot_map_data_get(slot, element);
205 BLI_assert(slot->slot_subtype.map == BMO_OP_SLOT_SUBTYPE_MAP_INTERNAL);
206 if (val) {
207 return *val;
208 }
209
210 return nullptr;
211}
212
214 void *BMO_slot_map_elem_get(BMOpSlot *slot, const void *element)
215{
216 void **val = (void **)BMO_slot_map_data_get(slot, element);
217 BLI_assert(slot->slot_subtype.map == BMO_OP_SLOT_SUBTYPE_MAP_ELEM);
218 if (val) {
219 return *val;
220 }
221
222 return nullptr;
223}
#define BLI_assert(a)
Definition BLI_assert.h:46
#define ATTR_WARN_UNUSED_RESULT
#define ATTR_NONNULL(...)
#define BLI_INLINE
void ** BLI_ghash_lookup_p(GHash *gh, const void *key) ATTR_WARN_UNUSED_RESULT
Definition BLI_ghash.cc:745
BMesh const char void * data
BMesh * bm
BLI_INLINE void _bmo_elem_flag_enable(BMesh *bm, BMFlagLayer *oflags, short oflag)
@ BMO_OP_SLOT_MAPPING
BLI_INLINE bool _bmo_elem_flag_test_bool(BMesh *bm, const BMFlagLayer *oflags, short oflag)
BLI_INLINE short _bmo_elem_flag_test(BMesh *bm, const BMFlagLayer *oflags, short oflag)
@ BMO_OP_SLOT_SUBTYPE_MAP_ELEM
@ BMO_OP_SLOT_SUBTYPE_MAP_BOOL
@ BMO_OP_SLOT_SUBTYPE_MAP_INTERNAL
@ BMO_OP_SLOT_SUBTYPE_MAP_INT
@ BMO_OP_SLOT_SUBTYPE_MAP_EMPTY
@ BMO_OP_SLOT_SUBTYPE_MAP_FLT
BLI_INLINE void _bmo_elem_flag_set(BMesh *bm, BMFlagLayer *oflags, short oflag, int val)
void BMO_slot_map_insert(BMOperator *op, BMOpSlot *slot, const void *element, const void *data)
BLI_INLINE void _bmo_elem_flag_disable(BMesh *bm, BMFlagLayer *oflags, short oflag)
BLI_INLINE void _bmo_elem_flag_toggle(BMesh *bm, BMFlagLayer *oflags, short oflag)
return BLI_ghash_haskey(slot->data.ghash, element)
BLI_INLINE void BMO_slot_map_int_insert(BMOperator *op, BMOpSlot *slot, void *element, const int val)
BLI_INLINE void BMO_slot_map_elem_insert(BMOperator *op, BMOpSlot *slot, const void *element, void *val)
ATTR_WARN_UNUSED_RESULT const void * element
BLI_INLINE void BMO_slot_map_float_insert(BMOperator *op, BMOpSlot *slot, void *element, const float val)
BLI_INLINE void BMO_slot_map_empty_insert(BMOperator *op, BMOpSlot *slot, const void *element)
ATTR_WARN_UNUSED_RESULT const BMFlagLayer const short oflag
BLI_INLINE void BMO_slot_map_bool_insert(BMOperator *op, BMOpSlot *slot, void *element, const bool val)
BLI_INLINE void BMO_slot_map_ptr_insert(BMOperator *op, BMOpSlot *slot, const void *element, void *val)
ATTR_WARN_UNUSED_RESULT const BMFlagLayer * oflags
PointerRNA * ptr
Definition wm_files.cc:4238