OpenVDB: NodeUnion.h Source File

OpenVDB 8.1.0

NodeUnion.h
Go to the documentation of this file.
1 // Copyright Contributors to the OpenVDB Project
2 // SPDX-License-Identifier: MPL-2.0
3
10
11 #ifndef OPENVDB_TREE_NODEUNION_HAS_BEEN_INCLUDED
12 #define OPENVDB_TREE_NODEUNION_HAS_BEEN_INCLUDED
13
14 #include <openvdb/version.h>
15 #include <openvdb/Types.h>
16 #include <cstring> // for std::memcpy()
17 #include <type_traits>
18
19 namespace openvdb {
20 OPENVDB_USE_VERSION_NAMESPACE
21 namespace OPENVDB_VERSION_NAME {
22 namespace tree {
23
24 #if OPENVDB_ABI_VERSION_NUMBER >= 8
25
32 template<typename ValueT, typename ChildT, typename Enable = void>
33 class NodeUnion
34 {
35 private:
36 ChildT* mChild;
37 ValueT mValue;
38
39 public:
40 NodeUnion(): mChild(nullptr), mValue() {}
41
42 ChildT* getChild() const { return mChild; }
43 void setChild(ChildT* child) { mChild = child; }
44
45 const ValueT& getValue() const { return mValue; }
46 ValueT& getValue() { return mValue; }
47 void setValue(const ValueT& val) { mValue = val; }
48
49 // Small check to ensure this class isn't
50 // selected for some expected types
51 static_assert(!ValueTraits<ValueT>::IsVec &&
52 !ValueTraits<ValueT>::IsMat &&
53 !ValueTraits<ValueT>::IsQuat &&
54 !std::is_same<ValueT, math::Coord>::value &&
55 !std::is_arithmetic<ValueT>::value,
56 "Unexpected instantiation of NodeUnion");
57 };
58
61 template<typename ValueT, typename ChildT>
62 class NodeUnion<ValueT, ChildT,
63 typename std::enable_if<std::is_trivially_copyable<ValueT>::value>::type>
64 {
65 private:
66 union { ChildT* mChild; ValueT mValue; };
67
68 public:
69 NodeUnion(): mChild(nullptr) {}
70
71 ChildT* getChild() const { return mChild; }
72 void setChild(ChildT* child) { mChild = child; }
73
74 const ValueT& getValue() const { return mValue; }
75 ValueT& getValue() { return mValue; }
76 void setValue(const ValueT& val) { mValue = val; }
77 };
78
79 #else
80
81 // Forward declaration of traits class
82 template<typename T> struct CopyTraits;
83
84 // Default implementation that stores the child pointer and the value separately
85 // (i.e., not in a union)
86 // This implementation is not used for POD, math::Vec or math::Coord value types.
87 template<typename ValueT, typename ChildT, typename Enable = void>
88 class NodeUnion
89 {
90 private:
91 ChildT* mChild;
92 ValueT mValue;
93
94 public:
95 NodeUnion(): mChild(nullptr), mValue() {}
96
97 ChildT* getChild() const { return mChild; }
98 void setChild(ChildT* child) { mChild = child; }
99
100 const ValueT& getValue() const { return mValue; }
101 ValueT& getValue() { return mValue; }
102 void setValue(const ValueT& val) { mValue = val; }
103 };
104
105
106 // Template specialization for values of POD types (int, float, pointer, etc.)
107 template<typename ValueT, typename ChildT>
108 class NodeUnion<ValueT, ChildT, typename std::enable_if<std::is_pod<ValueT>::value>::type>
109 {
110 private:
111 union { ChildT* mChild; ValueT mValue; };
112
113 public:
114 NodeUnion(): mChild(nullptr) {}
115
116 ChildT* getChild() const { return mChild; }
117 void setChild(ChildT* child) { mChild = child; }
118
119 const ValueT& getValue() const { return mValue; }
120 ValueT& getValue() { return mValue; }
121 void setValue(const ValueT& val) { mValue = val; }
122 };
123
124
125 // Template specialization for values of types such as math::Vec3f and math::Coord
126 // for which CopyTraits<T>::IsCopyable is true
127 template<typename ValueT, typename ChildT>
128 class NodeUnion<ValueT, ChildT, typename std::enable_if<CopyTraits<ValueT>::IsCopyable>::type>
129 {
130 private:
131 union { ChildT* mChild; ValueT mValue; };
132
133 public:
134 NodeUnion(): mChild(nullptr) {}
135 NodeUnion(const NodeUnion& other): mChild(nullptr)
136 { std::memcpy(static_cast<void*>(this), &other, sizeof(*this)); }
137 NodeUnion& operator=(const NodeUnion& rhs)
138 { std::memcpy(static_cast<void*>(this), &rhs, sizeof(*this)); return *this; }
139
140 ChildT* getChild() const { return mChild; }
141 void setChild(ChildT* child) { mChild = child; }
142
143 const ValueT& getValue() const { return mValue; }
144 ValueT& getValue() { return mValue; }
145 void setValue(const ValueT& val) { mValue = val; }
146 };
147
148
155 template<typename T> struct CopyTraits { static const bool IsCopyable = false; };
156 template<typename T> struct CopyTraits<math::Vec2<T>> { static const bool IsCopyable = true; };
157 template<typename T> struct CopyTraits<math::Vec3<T>> { static const bool IsCopyable = true; };
158 template<typename T> struct CopyTraits<math::Vec4<T>> { static const bool IsCopyable = true; };
159 template<> struct CopyTraits<math::Coord> { static const bool IsCopyable = true; };
160
161 #endif
162
164
165
166 } // namespace tree
167 } // namespace OPENVDB_VERSION_NAME
168 } // namespace openvdb
169
170 #endif // OPENVDB_TREE_NODEUNION_HAS_BEEN_INCLUDED
openvdb::v8_1::tree::NodeUnion< ValueT, ChildT, typename std::enable_if< std::is_trivially_copyable< ValueT >::value >::type >::setChild
void setChild(ChildT *child)
Definition: NodeUnion.h:72
openvdb::v8_1::tree::NodeUnion< ValueT, ChildT, typename std::enable_if< std::is_trivially_copyable< ValueT >::value >::type >::getValue
const ValueT & getValue() const
Definition: NodeUnion.h:74
openvdb::v8_1::tree::NodeUnion< ValueT, ChildT, typename std::enable_if< std::is_trivially_copyable< ValueT >::value >::type >::getValue
ValueT & getValue()
Definition: NodeUnion.h:75
openvdb::v8_1::tree::NodeUnion< ValueT, ChildT, typename std::enable_if< std::is_trivially_copyable< ValueT >::value >::type >::getChild
ChildT * getChild() const
Definition: NodeUnion.h:71
openvdb::v8_1::tree::NodeUnion< ValueT, ChildT, typename std::enable_if< std::is_trivially_copyable< ValueT >::value >::type >::NodeUnion
NodeUnion()
Definition: NodeUnion.h:69
openvdb::v8_1::tree::NodeUnion< ValueT, ChildT, typename std::enable_if< std::is_trivially_copyable< ValueT >::value >::type >::setValue
void setValue(const ValueT &val)
Definition: NodeUnion.h:76
openvdb::v8_1::tree::NodeUnion< ValueT, ChildT, typename std::enable_if< std::is_trivially_copyable< ValueT >::value >::type >::mChild
ChildT * mChild
Definition: NodeUnion.h:66
openvdb::v8_1::tree::NodeUnion
Default implementation of a NodeUnion that stores the child pointer and the value separately (i....
Definition: NodeUnion.h:34
openvdb::v8_1::tree::NodeUnion::setChild
void setChild(ChildT *child)
Definition: NodeUnion.h:43
openvdb::v8_1::tree::NodeUnion::getValue
const ValueT & getValue() const
Definition: NodeUnion.h:45
openvdb::v8_1::tree::NodeUnion::getValue
ValueT & getValue()
Definition: NodeUnion.h:46
openvdb::v8_1::tree::NodeUnion::getChild
ChildT * getChild() const
Definition: NodeUnion.h:42
openvdb::v8_1::tree::NodeUnion::NodeUnion
NodeUnion()
Definition: NodeUnion.h:40
openvdb::v8_1::tree::NodeUnion::setValue
void setValue(const ValueT &val)
Definition: NodeUnion.h:47
openvdb
Definition: openvdb/Exceptions.h:13
std
Definition: Coord.h:587
Types.h
openvdb::v8_1::ValueTraits
Definition: openvdb/Types.h:252
OPENVDB_VERSION_NAME
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h.in:116
OPENVDB_USE_VERSION_NAMESPACE
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h.in:178