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 /// @brief Default implementation of a NodeUnion that stores the child pointer
25 /// and the value separately (i.e., not in a union). Types which select this
26 /// specialization usually do not conform to the requirements of a union
27 /// member, that is that the type ValueT is not trivially copyable. This
28 /// implementation is thus NOT used for POD, math::Vec, math::Mat, math::Quat
29 /// or math::Coord types, but is used (for example) with std::string
30 template<typename ValueT, typename ChildT, typename Enable = void>
31 class NodeUnion
32 {
33 private:
34  ChildT* mChild;
35  ValueT mValue;
36 
37 public:
38  NodeUnion(): mChild(nullptr), mValue() {}
39 
40  ChildT* getChild() const { return mChild; }
41  void setChild(ChildT* child) { mChild = child; }
42 
43  const ValueT& getValue() const { return mValue; }
44  ValueT& getValue() { return mValue; }
45  void setValue(const ValueT& val) { mValue = val; }
46 
47  // Small check to ensure this class isn't
48  // selected for some expected types
49  static_assert(!ValueTraits<ValueT>::IsVec &&
54  "Unexpected instantiation of NodeUnion");
55 };
56 
57 /// @brief Template specialization of a NodeUnion that stores the child pointer
58 /// and the value together (int, float, pointer, etc.)
59 template<typename ValueT, typename ChildT>
60 class NodeUnion<ValueT, ChildT,
61  typename std::enable_if<std::is_trivially_copyable<ValueT>::value>::type>
62 {
63 private:
64  union { ChildT* mChild; ValueT mValue; };
65 
66 public:
67  NodeUnion(): mChild(nullptr) {}
68 
69  ChildT* getChild() const { return mChild; }
70  void setChild(ChildT* child) { mChild = child; }
71 
72  const ValueT& getValue() const { return mValue; }
73  ValueT& getValue() { return mValue; }
74  void setValue(const ValueT& val) { mValue = val; }
75 };
76 
77 
78 ////////////////////////////////////////
79 
80 
81 } // namespace tree
82 } // namespace OPENVDB_VERSION_NAME
83 } // namespace openvdb
84 
85 #endif // OPENVDB_TREE_NODEUNION_HAS_BEEN_INCLUDED
GLsizei const GLfloat * value
Definition: glcorearb.h:824
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:239
Default implementation of a NodeUnion that stores the child pointer and the value separately (i...
Definition: NodeUnion.h:31
Library and file format version numbers.
GLuint GLfloat * val
Definition: glcorearb.h:1608
type
Definition: core.h:1059
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h:119