OpenVDB 5.1.0

NodeUnion.h
Go to the documentation of this file.
1 //
3 // Copyright (c) 2012-2018 DreamWorks Animation LLC
4 //
5 // All rights reserved. This software is distributed under the
6 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
7 //
8 // Redistributions of source code must retain the above copyright
9 // and license notice and the following restrictions and disclaimer.
10 //
11 // * Neither the name of DreamWorks Animation nor the names of
12 // its contributors may be used to endorse or promote products derived
13 // from this software without specific prior written permission.
14 //
15 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY INDIRECT, INCIDENTAL,
20 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 // IN NO EVENT SHALL THE COPYRIGHT HOLDERS' AND CONTRIBUTORS' AGGREGATE
27 // LIABILITY FOR ALL CLAIMS REGARDLESS OF THEIR BASIS EXCEED US$250.00.
28 //
30
37
38 #ifndef OPENVDB_TREE_NODEUNION_HAS_BEEN_INCLUDED
39 #define OPENVDB_TREE_NODEUNION_HAS_BEEN_INCLUDED
40
41 #include <openvdb/version.h>
42 #include <openvdb/Types.h>
43 #include <cstring> // for std::memcpy()
44 #include <type_traits>
45
46 namespace openvdb {
47 OPENVDB_USE_VERSION_NAMESPACE
48 namespace OPENVDB_VERSION_NAME {
49 namespace tree {
50
51 #if OPENVDB_ABI_VERSION_NUMBER >= 4
52
53 // Forward declaration of traits class
54 template<typename T> struct CopyTraits;
55
56 // Default implementation that stores the child pointer and the value separately
57 // (i.e., not in a union)
58 // This implementation is not used for POD, math::Vec or math::Coord value types.
59 template<typename ValueT, typename ChildT, typename Enable = void>
60 class NodeUnion
61 {
62 private:
63 ChildT* mChild;
64 ValueT mValue;
65
66 public:
67 NodeUnion(): mChild(nullptr), mValue() {}
68
69 ChildT* getChild() const { return mChild; }
70 void setChild(ChildT* child) { mChild = child; }
71
72 const ValueT& getValue() const { return mValue; }
73 ValueT& getValue() { return mValue; }
74 void setValue(const ValueT& val) { mValue = val; }
75 };
76
77
78 // Template specialization for values of POD types (int, float, pointer, etc.)
79 template<typename ValueT, typename ChildT>
80 class NodeUnion<ValueT, ChildT, typename std::enable_if<std::is_pod<ValueT>::value>::type>
81 {
82 private:
83 union { ChildT* mChild; ValueT mValue; };
84
85 public:
86 NodeUnion(): mChild(nullptr) {}
87
88 ChildT* getChild() const { return mChild; }
89 void setChild(ChildT* child) { mChild = child; }
90
91 const ValueT& getValue() const { return mValue; }
92 ValueT& getValue() { return mValue; }
93 void setValue(const ValueT& val) { mValue = val; }
94 };
95
96
97 // Template specialization for values of types such as math::Vec3f and math::Coord
98 // for which CopyTraits<T>::IsCopyable is true
99 template<typename ValueT, typename ChildT>
100 class NodeUnion<ValueT, ChildT, typename std::enable_if<CopyTraits<ValueT>::IsCopyable>::type>
101 {
102 private:
103 union { ChildT* mChild; ValueT mValue; };
104
105 public:
106 NodeUnion(): mChild(nullptr) {}
107 NodeUnion(const NodeUnion& other): mChild(nullptr)
108 { std::memcpy(this, &other, sizeof(*this)); }
109 NodeUnion& operator=(const NodeUnion& rhs)
110 { std::memcpy(this, &rhs, sizeof(*this)); return *this; }
111
112 ChildT* getChild() const { return mChild; }
113 void setChild(ChildT* child) { mChild = child; }
114
115 const ValueT& getValue() const { return mValue; }
116 ValueT& getValue() { return mValue; }
117 void setValue(const ValueT& val) { mValue = val; }
118 };
119
120
127 template<typename T> struct CopyTraits { static const bool IsCopyable = false; };
128 template<typename T> struct CopyTraits<math::Vec2<T>> { static const bool IsCopyable = true; };
129 template<typename T> struct CopyTraits<math::Vec3<T>> { static const bool IsCopyable = true; };
130 template<typename T> struct CopyTraits<math::Vec4<T>> { static const bool IsCopyable = true; };
131 template<> struct CopyTraits<math::Coord> { static const bool IsCopyable = true; };
132
133
135
136
137 #else // OPENVDB_ABI_VERSION_NUMBER <= 3
138
139 // Prior to OpenVDB 4 and the introduction of C++11, values of non-POD types
140 // were heap-allocated and stored by pointer due to C++98 restrictions on unions.
141
142 // Internal implementation of a union of a child node pointer and a value
143 template<bool ValueIsClass, class ValueT, class ChildT> class NodeUnionImpl;
144
145
146 // Partial specialization for values of non-class types
147 // (int, float, pointer, etc.) that stores elements by value
148 template<typename ValueT, typename ChildT>
149 class NodeUnionImpl</*ValueIsClass=*/false, ValueT, ChildT>
150 {
151 private:
152 union { ChildT* child; ValueT value; } mUnion;
153
154 public:
155 NodeUnionImpl() { mUnion.child = nullptr; }
156
157 ChildT* getChild() const { return mUnion.child; }
158 void setChild(ChildT* child) { mUnion.child = child; }
159
160 const ValueT& getValue() const { return mUnion.value; }
161 ValueT& getValue() { return mUnion.value; }
162 void setValue(const ValueT& val) { mUnion.value = val; }
163 };
164
165
166 // Partial specialization for values of class types (std::string,
167 // math::Vec, etc.) that stores elements by pointer
168 template<typename ValueT, typename ChildT>
169 class NodeUnionImpl</*ValueIsClass=*/true, ValueT, ChildT>
170 {
171 private:
172 union { ChildT* child; ValueT* value; } mUnion;
173 bool mHasChild;
174
175 public:
176 NodeUnionImpl() : mHasChild(true) { this->setChild(nullptr); }
177 NodeUnionImpl(const NodeUnionImpl& other) : mHasChild(true)
178 {
179 if (other.mHasChild) {
180 this->setChild(other.getChild());
181 } else {
182 this->setValue(other.getValue());
183 }
184 }
185 NodeUnionImpl& operator=(const NodeUnionImpl& other)
186 {
187 if (other.mHasChild) {
188 this->setChild(other.getChild());
189 } else {
190 this->setValue(other.getValue());
191 }
192 return *this;
193 }
194 ~NodeUnionImpl() { this->setChild(nullptr); }
195
196 ChildT* getChild() const { return mHasChild ? mUnion.child : nullptr; }
197 void setChild(ChildT* child)
198 {
199 if (!mHasChild) delete mUnion.value;
200 mUnion.child = child;
201 mHasChild = true;
202 }
203
204 const ValueT& getValue() const { return *mUnion.value; }
205 ValueT& getValue() { return *mUnion.value; }
206 void setValue(const ValueT& val)
207 {
208 if (!mHasChild) delete mUnion.value;
209 mUnion.value = new ValueT(val);
210 mHasChild = false;
211 }
212 };
213
214
215 template<typename ValueT, typename ChildT>
216 struct NodeUnion: public NodeUnionImpl<std::is_class<ValueT>::value, ValueT, ChildT>
217 {
218 NodeUnion() {}
219 };
220
221 #endif
222
223 } // namespace tree
224 } // namespace OPENVDB_VERSION_NAME
225 } // namespace openvdb
226
227 #endif // OPENVDB_TREE_NODEUNION_HAS_BEEN_INCLUDED
228
229 // Copyright (c) 2012-2018 DreamWorks Animation LLC
230 // All rights reserved. This software is distributed under the
231 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
openvdb::v5_1::tree::NodeUnion< ValueT, ChildT, typename std::enable_if< CopyTraits< ValueT >::IsCopyable >::type >::operator=
NodeUnion & operator=(const NodeUnion &rhs)
Definition: NodeUnion.h:109
openvdb::v5_1::tree::NodeUnion< ValueT, ChildT, typename std::enable_if< CopyTraits< ValueT >::IsCopyable >::type >::getValue
const ValueT & getValue() const
Definition: NodeUnion.h:115
openvdb::v5_1::tree::NodeUnion< ValueT, ChildT, typename std::enable_if< CopyTraits< ValueT >::IsCopyable >::type >::setValue
void setValue(const ValueT &val)
Definition: NodeUnion.h:117
openvdb::v5_1::tree::NodeUnion< ValueT, ChildT, typename std::enable_if< CopyTraits< ValueT >::IsCopyable >::type >::getChild
ChildT * getChild() const
Definition: NodeUnion.h:112
openvdb::v5_1::tree::NodeUnion< ValueT, ChildT, typename std::enable_if< std::is_pod< ValueT >::value >::type >::getValue
ValueT & getValue()
Definition: NodeUnion.h:92
openvdb::v5_1::math::Vec4
Definition: Mat4.h:51
Types.h
openvdb::v5_1::tree::NodeUnion< ValueT, ChildT, typename std::enable_if< CopyTraits< ValueT >::IsCopyable >::type >::NodeUnion
NodeUnion()
Definition: NodeUnion.h:106
openvdb::v5_1::tree::NodeUnion< ValueT, ChildT, typename std::enable_if< std::is_pod< ValueT >::value >::type >::getChild
ChildT * getChild() const
Definition: NodeUnion.h:88
openvdb::v5_1::tree::NodeUnion< ValueT, ChildT, typename std::enable_if< std::is_pod< ValueT >::value >::type >::setChild
void setChild(ChildT *child)
Definition: NodeUnion.h:89
openvdb::v5_1::tree::NodeUnion< ValueT, ChildT, typename std::enable_if< CopyTraits< ValueT >::IsCopyable >::type >::setChild
void setChild(ChildT *child)
Definition: NodeUnion.h:113
openvdb::v5_1::math::Vec3
Definition: Mat.h:197
openvdb::v5_1::tree::NodeUnion::getChild
ChildT * getChild() const
Definition: NodeUnion.h:69
openvdb::v5_1::tree::NodeUnion::getValue
ValueT & getValue()
Definition: NodeUnion.h:73
openvdb::v5_1::tree::NodeUnion::NodeUnion
NodeUnion()
Definition: NodeUnion.h:67
openvdb::v5_1::tree::NodeUnion::getValue
const ValueT & getValue() const
Definition: NodeUnion.h:72
openvdb::v5_1::tree::NodeUnion< ValueT, ChildT, typename std::enable_if< CopyTraits< ValueT >::IsCopyable >::type >::NodeUnion
NodeUnion(const NodeUnion &other)
Definition: NodeUnion.h:107
OPENVDB_VERSION_NAME
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h:136
openvdb::v5_1::tree::NodeUnion::setChild
void setChild(ChildT *child)
Definition: NodeUnion.h:70
openvdb::v5_1::tree::NodeUnion< ValueT, ChildT, typename std::enable_if< std::is_pod< ValueT >::value >::type >::mValue
ValueT mValue
Definition: NodeUnion.h:83
openvdb::v5_1::tree::NodeUnion< ValueT, ChildT, typename std::enable_if< std::is_pod< ValueT >::value >::type >::NodeUnion
NodeUnion()
Definition: NodeUnion.h:86
openvdb
Definition: Exceptions.h:40
version.h
Library and file format version numbers.
openvdb::v5_1::tree::NodeUnion::setValue
void setValue(const ValueT &val)
Definition: NodeUnion.h:74
openvdb::v5_1::tree::NodeUnion< ValueT, ChildT, typename std::enable_if< CopyTraits< ValueT >::IsCopyable >::type >::mValue
ValueT mValue
Definition: NodeUnion.h:103
openvdb::v5_1::math::Coord
Signed (x, y, z) 32-bit integer coordinates.
Definition: Coord.h:51
openvdb::v5_1::math::Vec2
Definition: Vec2.h:50
openvdb::v5_1::tree::NodeUnion
Definition: NodeUnion.h:60
openvdb::v5_1::tree::NodeUnion< ValueT, ChildT, typename std::enable_if< std::is_pod< ValueT >::value >::type >::getValue
const ValueT & getValue() const
Definition: NodeUnion.h:91
OPENVDB_USE_VERSION_NAMESPACE
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:188
openvdb::v5_1::tree::NodeUnion< ValueT, ChildT, typename std::enable_if< CopyTraits< ValueT >::IsCopyable >::type >::getValue
ValueT & getValue()
Definition: NodeUnion.h:116
openvdb::v5_1::tree::CopyTraits
Definition: NodeUnion.h:54
openvdb::v5_1::tree::NodeUnion< ValueT, ChildT, typename std::enable_if< std::is_pod< ValueT >::value >::type >::setValue
void setValue(const ValueT &val)
Definition: NodeUnion.h:93