OpenVDB  7.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 {
21 namespace OPENVDB_VERSION_NAME {
22 namespace tree {
23 
24 
25 // Forward declaration of traits class
26 template<typename T> struct CopyTraits;
27 
28 // Default implementation that stores the child pointer and the value separately
29 // (i.e., not in a union)
30 // This implementation is not used for POD, math::Vec or math::Coord value types.
31 template<typename ValueT, typename ChildT, typename Enable = void>
32 class NodeUnion
33 {
34 private:
35  ChildT* mChild;
36  ValueT mValue;
37 
38 public:
39  NodeUnion(): mChild(nullptr), mValue() {}
40 
41  ChildT* getChild() const { return mChild; }
42  void setChild(ChildT* child) { mChild = child; }
43 
44  const ValueT& getValue() const { return mValue; }
45  ValueT& getValue() { return mValue; }
46  void setValue(const ValueT& val) { mValue = val; }
47 };
48 
49 
50 // Template specialization for values of POD types (int, float, pointer, etc.)
51 template<typename ValueT, typename ChildT>
52 class NodeUnion<ValueT, ChildT, typename std::enable_if<std::is_pod<ValueT>::value>::type>
53 {
54 private:
55  union { ChildT* mChild; ValueT mValue; };
56 
57 public:
58  NodeUnion(): mChild(nullptr) {}
59 
60  ChildT* getChild() const { return mChild; }
61  void setChild(ChildT* child) { mChild = child; }
62 
63  const ValueT& getValue() const { return mValue; }
64  ValueT& getValue() { return mValue; }
65  void setValue(const ValueT& val) { mValue = val; }
66 };
67 
68 
69 // Template specialization for values of types such as math::Vec3f and math::Coord
70 // for which CopyTraits<T>::IsCopyable is true
71 template<typename ValueT, typename ChildT>
72 class NodeUnion<ValueT, ChildT, typename std::enable_if<CopyTraits<ValueT>::IsCopyable>::type>
73 {
74 private:
75  union { ChildT* mChild; ValueT mValue; };
76 
77 public:
78  NodeUnion(): mChild(nullptr) {}
79  NodeUnion(const NodeUnion& other): mChild(nullptr)
80  { std::memcpy(this, &other, sizeof(*this)); }
82  { std::memcpy(this, &rhs, sizeof(*this)); return *this; }
83 
84  ChildT* getChild() const { return mChild; }
85  void setChild(ChildT* child) { mChild = child; }
86 
87  const ValueT& getValue() const { return mValue; }
88  ValueT& getValue() { return mValue; }
89  void setValue(const ValueT& val) { mValue = val; }
90 };
91 
92 
99 template<typename T> struct CopyTraits { static const bool IsCopyable = false; };
100 template<typename T> struct CopyTraits<math::Vec2<T>> { static const bool IsCopyable = true; };
101 template<typename T> struct CopyTraits<math::Vec3<T>> { static const bool IsCopyable = true; };
102 template<typename T> struct CopyTraits<math::Vec4<T>> { static const bool IsCopyable = true; };
103 template<> struct CopyTraits<math::Coord> { static const bool IsCopyable = true; };
104 
105 
107 
108 
109 } // namespace tree
110 } // namespace OPENVDB_VERSION_NAME
111 } // namespace openvdb
112 
113 #endif // OPENVDB_TREE_NODEUNION_HAS_BEEN_INCLUDED
openvdb::v7_1::math::Vec3
Definition: Vec3.h:24
openvdb::v7_1::tree::NodeUnion< ValueT, ChildT, typename std::enable_if< std::is_pod< ValueT >::value >::type >::getValue
const ValueT & getValue() const
Definition: NodeUnion.h:63
openvdb::v7_1::tree::NodeUnion::getChild
ChildT * getChild() const
Definition: NodeUnion.h:41
openvdb::v7_1::tree::NodeUnion< ValueT, ChildT, typename std::enable_if< std::is_pod< ValueT >::value >::type >::getChild
ChildT * getChild() const
Definition: NodeUnion.h:60
openvdb::v7_1::tree::CopyTraits
Definition: NodeUnion.h:99
Types.h
openvdb::v7_1::tree::NodeUnion::getValue
const ValueT & getValue() const
Definition: NodeUnion.h:44
version.h
Library and file format version numbers.
openvdb::v7_1::tree::NodeUnion< ValueT, ChildT, typename std::enable_if< std::is_pod< ValueT >::value >::type >::NodeUnion
NodeUnion()
Definition: NodeUnion.h:58
openvdb::v7_1::points::type
const Name const NamePair & type
Definition: PointAttribute.h:545
openvdb::v7_1::tree::NodeUnion< ValueT, ChildT, typename std::enable_if< std::is_pod< ValueT >::value >::type >::setValue
void setValue(const ValueT &val)
Definition: NodeUnion.h:65
openvdb::v7_1::tree::NodeUnion< ValueT, ChildT, typename std::enable_if< std::is_pod< ValueT >::value >::type >::setChild
void setChild(ChildT *child)
Definition: NodeUnion.h:61
openvdb::v7_1::math::Vec2
Definition: Vec2.h:24
openvdb::v7_1::tree::NodeUnion::setValue
void setValue(const ValueT &val)
Definition: NodeUnion.h:46
openvdb::v7_1::tree::NodeUnion< ValueT, ChildT, typename std::enable_if< std::is_pod< ValueT >::value >::type >::mValue
ValueT mValue
Definition: NodeUnion.h:55
openvdb::v7_1::tree::NodeUnion< ValueT, ChildT, typename std::enable_if< CopyTraits< ValueT >::IsCopyable >::type >::getChild
ChildT * getChild() const
Definition: NodeUnion.h:84
openvdb::v7_1::tree::NodeUnion< ValueT, ChildT, typename std::enable_if< CopyTraits< ValueT >::IsCopyable >::type >::setChild
void setChild(ChildT *child)
Definition: NodeUnion.h:85
openvdb::v7_1::tree::NodeUnion< ValueT, ChildT, typename std::enable_if< std::is_pod< ValueT >::value >::type >::getValue
ValueT & getValue()
Definition: NodeUnion.h:64
openvdb::v7_1::math::Coord
Signed (x, y, z) 32-bit integer coordinates.
Definition: Coord.h:26
openvdb::v7_1::tree::NodeUnion< ValueT, ChildT, typename std::enable_if< CopyTraits< ValueT >::IsCopyable >::type >::NodeUnion
NodeUnion(const NodeUnion &other)
Definition: NodeUnion.h:79
OPENVDB_USE_VERSION_NAMESPACE
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:146
openvdb::v7_1::tree::NodeUnion< ValueT, ChildT, typename std::enable_if< CopyTraits< ValueT >::IsCopyable >::type >::getValue
const ValueT & getValue() const
Definition: NodeUnion.h:87
openvdb::v7_1::tree::NodeUnion< ValueT, ChildT, typename std::enable_if< CopyTraits< ValueT >::IsCopyable >::type >::setValue
void setValue(const ValueT &val)
Definition: NodeUnion.h:89
std
Definition: Coord.h:587
openvdb::v7_1::math::Vec4
Definition: Vec4.h:25
openvdb::v7_1::tree::NodeUnion::setChild
void setChild(ChildT *child)
Definition: NodeUnion.h:42
openvdb::v7_1::tree::NodeUnion::NodeUnion
NodeUnion()
Definition: NodeUnion.h:39
OPENVDB_VERSION_NAME
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h:94
openvdb::v7_1::tree::NodeUnion::getValue
ValueT & getValue()
Definition: NodeUnion.h:45
openvdb::v7_1::tree::NodeUnion< ValueT, ChildT, typename std::enable_if< CopyTraits< ValueT >::IsCopyable >::type >::mValue
ValueT mValue
Definition: NodeUnion.h:75
openvdb::v7_1::tree::NodeUnion< ValueT, ChildT, typename std::enable_if< CopyTraits< ValueT >::IsCopyable >::type >::NodeUnion
NodeUnion()
Definition: NodeUnion.h:78
openvdb
Definition: Exceptions.h:13
openvdb::v7_1::tree::NodeUnion
Definition: NodeUnion.h:33
openvdb::v7_1::tree::NodeUnion< ValueT, ChildT, typename std::enable_if< CopyTraits< ValueT >::IsCopyable >::type >::operator=
NodeUnion & operator=(const NodeUnion &rhs)
Definition: NodeUnion.h:81
openvdb::v7_1::tree::NodeUnion< ValueT, ChildT, typename std::enable_if< CopyTraits< ValueT >::IsCopyable >::type >::getValue
ValueT & getValue()
Definition: NodeUnion.h:88