HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
NodeUnion.h
Go to the documentation of this file.
1 // Copyright Contributors to the OpenVDB Project
2 // SPDX-License-Identifier: MPL-2.0
3 
4 /// @file NodeUnion.h
5 ///
6 /// @details NodeUnion is a templated helper class that controls access to either
7 /// the child node pointer or the value for a particular element of a root
8 /// or internal node. For space efficiency, the child pointer and the value
9 /// are unioned when possible, since the two are never in use simultaneously.
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 
93 /// @details A type T is copyable if
94 /// # T stores member values by value (vs. by pointer or reference)
95 /// and T's true byte size is given by sizeof(T).
96 /// # T has a trivial destructor
97 /// # T has a default constructor
98 /// # T has an assignment operator
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 
106 ////////////////////////////////////////
107 
108 
109 } // namespace tree
110 } // namespace OPENVDB_VERSION_NAME
111 } // namespace openvdb
112 
113 #endif // OPENVDB_TREE_NODEUNION_HAS_BEEN_INCLUDED
GLuint const GLfloat * val
Definition: glew.h:2794
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:166
GLuint GLuint GLsizei GLenum type
Definition: glew.h:1253
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h:112