11 #ifndef OPENVDB_TREE_NODEUNION_HAS_BEEN_INCLUDED
12 #define OPENVDB_TREE_NODEUNION_HAS_BEEN_INCLUDED
17 #include <type_traits>
24 #if OPENVDB_ABI_VERSION_NUMBER >= 4
32 template<
typename ValueT,
typename ChildT,
typename Enable =
void>
43 void setChild(ChildT* child) { mChild = child; }
45 const ValueT&
getValue()
const {
return mValue; }
47 void setValue(
const ValueT& val) { mValue = val; }
52 template<
typename ValueT,
typename ChildT>
53 class NodeUnion<ValueT, ChildT, typename
std::enable_if<std::is_pod<ValueT>::value>::type>
56 union { ChildT* mChild; ValueT
mValue; };
62 void setChild(ChildT* child) { mChild = child; }
64 const ValueT&
getValue()
const {
return mValue; }
66 void setValue(
const ValueT& val) { mValue = val; }
72 template<
typename ValueT,
typename ChildT>
73 class NodeUnion<ValueT, ChildT, typename
std::enable_if<CopyTraits<ValueT>::IsCopyable>::type>
76 union { ChildT* mChild; ValueT
mValue; };
81 { std::memcpy(
this, &other,
sizeof(*
this)); }
83 { std::memcpy(
this, &rhs,
sizeof(*
this));
return *
this; }
86 void setChild(ChildT* child) { mChild = child; }
88 const ValueT&
getValue()
const {
return mValue; }
90 void setValue(
const ValueT& val) { mValue = val; }
100 template<
typename T>
struct CopyTraits {
static const bool IsCopyable =
false; };
101 template<
typename T>
struct CopyTraits<math::
Vec2<T>> {
static const bool IsCopyable =
true; };
102 template<
typename T>
struct CopyTraits<math::
Vec3<T>> {
static const bool IsCopyable =
true; };
103 template<
typename T>
struct CopyTraits<math::
Vec4<T>> {
static const bool IsCopyable =
true; };
110 #else // OPENVDB_ABI_VERSION_NUMBER <= 3
116 template<
bool ValueIsClass,
class ValueT,
class ChildT>
class NodeUnionImpl;
121 template<
typename ValueT,
typename ChildT>
122 class NodeUnionImpl<false, ValueT, ChildT>
125 union { ChildT* child; ValueT value; } mUnion;
128 NodeUnionImpl() { mUnion.child =
nullptr; }
130 ChildT* getChild()
const {
return mUnion.child; }
131 void setChild(ChildT* child) { mUnion.child = child; }
133 const ValueT& getValue()
const {
return mUnion.value; }
134 ValueT& getValue() {
return mUnion.value; }
135 void setValue(
const ValueT& val) { mUnion.value = val; }
141 template<
typename ValueT,
typename ChildT>
142 class NodeUnionImpl<true, ValueT, ChildT>
145 union { ChildT* child; ValueT* value; } mUnion;
149 NodeUnionImpl() : mHasChild(true) { this->setChild(
nullptr); }
150 NodeUnionImpl(
const NodeUnionImpl& other) : mHasChild(true)
152 if (other.mHasChild) {
153 this->setChild(other.getChild());
155 this->setValue(other.getValue());
158 NodeUnionImpl& operator=(
const NodeUnionImpl& other)
160 if (other.mHasChild) {
161 this->setChild(other.getChild());
163 this->setValue(other.getValue());
167 ~NodeUnionImpl() { this->setChild(
nullptr); }
169 ChildT* getChild()
const {
return mHasChild ? mUnion.child :
nullptr; }
170 void setChild(ChildT* child)
172 if (!mHasChild)
delete mUnion.value;
173 mUnion.child = child;
177 const ValueT& getValue()
const {
return *mUnion.value; }
178 ValueT& getValue() {
return *mUnion.value; }
179 void setValue(
const ValueT& val)
181 if (!mHasChild)
delete mUnion.value;
182 mUnion.value =
new ValueT(val);
188 template<
typename ValueT,
typename ChildT>
189 struct NodeUnion:
public NodeUnionImpl<std::is_class<ValueT>::value, ValueT, ChildT>
200 #endif // OPENVDB_TREE_NODEUNION_HAS_BEEN_INCLUDED