38 #ifndef OPENVDB_TREE_NODEUNION_HAS_BEEN_INCLUDED
39 #define OPENVDB_TREE_NODEUNION_HAS_BEEN_INCLUDED
44 #include <type_traits>
51 #if OPENVDB_ABI_VERSION_NUMBER >= 4
59 template<
typename ValueT,
typename ChildT,
typename Enable =
void>
70 void setChild(ChildT* child) { mChild = child; }
72 const ValueT&
getValue()
const {
return mValue; }
74 void setValue(
const ValueT& val) { mValue = val; }
79 template<
typename ValueT,
typename ChildT>
80 class NodeUnion<ValueT, ChildT, typename
std::enable_if<std::is_pod<ValueT>::value>::type>
83 union { ChildT* mChild; ValueT
mValue; };
89 void setChild(ChildT* child) { mChild = child; }
91 const ValueT&
getValue()
const {
return mValue; }
93 void setValue(
const ValueT& val) { mValue = val; }
99 template<
typename ValueT,
typename ChildT>
100 class NodeUnion<ValueT, ChildT, typename
std::enable_if<CopyTraits<ValueT>::IsCopyable>::type>
103 union { ChildT* mChild; ValueT
mValue; };
108 { std::memcpy(
this, &other,
sizeof(*
this)); }
110 { std::memcpy(
this, &rhs,
sizeof(*
this));
return *
this; }
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; };
137 #else // OPENVDB_ABI_VERSION_NUMBER <= 3
143 template<
bool ValueIsClass,
class ValueT,
class ChildT>
class NodeUnionImpl;
148 template<
typename ValueT,
typename ChildT>
149 class NodeUnionImpl<false, ValueT, ChildT>
152 union { ChildT* child; ValueT value; } mUnion;
155 NodeUnionImpl() { mUnion.child =
nullptr; }
157 ChildT* getChild()
const {
return mUnion.child; }
158 void setChild(ChildT* child) { mUnion.child = child; }
160 const ValueT& getValue()
const {
return mUnion.value; }
161 ValueT& getValue() {
return mUnion.value; }
162 void setValue(
const ValueT& val) { mUnion.value = val; }
168 template<
typename ValueT,
typename ChildT>
169 class NodeUnionImpl<true, ValueT, ChildT>
172 union { ChildT* child; ValueT* value; } mUnion;
176 NodeUnionImpl() : mHasChild(true) { this->setChild(
nullptr); }
177 NodeUnionImpl(
const NodeUnionImpl& other) : mHasChild(true)
179 if (other.mHasChild) {
180 this->setChild(other.getChild());
182 this->setValue(other.getValue());
185 NodeUnionImpl& operator=(
const NodeUnionImpl& other)
187 if (other.mHasChild) {
188 this->setChild(other.getChild());
190 this->setValue(other.getValue());
194 ~NodeUnionImpl() { this->setChild(
nullptr); }
196 ChildT* getChild()
const {
return mHasChild ? mUnion.child :
nullptr; }
197 void setChild(ChildT* child)
199 if (!mHasChild)
delete mUnion.value;
200 mUnion.child = child;
204 const ValueT& getValue()
const {
return *mUnion.value; }
205 ValueT& getValue() {
return *mUnion.value; }
206 void setValue(
const ValueT& val)
208 if (!mHasChild)
delete mUnion.value;
209 mUnion.value =
new ValueT(val);
215 template<
typename ValueT,
typename ChildT>
216 struct NodeUnion:
public NodeUnionImpl<std::is_class<ValueT>::value, ValueT, ChildT>
227 #endif // OPENVDB_TREE_NODEUNION_HAS_BEEN_INCLUDED