OpenVDB 11.0.0

Loading...
Searching...
No Matches
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 {
20 OPENVDB_USE_VERSION_NAMESPACE
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 &&
50 !ValueTraits<ValueT>::IsMat &&
51 !ValueTraits<ValueT>::IsQuat &&
52 !std::is_same<ValueT, math::Coord>::value &&
53 !std::is_arithmetic<ValueT>::value,
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
openvdb::v11_0::tree::NodeUnion< ValueT, ChildT, typename std::enable_if< std::is_trivially_copyable< ValueT >::value >::type >::setChild
void setChild(ChildT *child)
Definition NodeUnion.h:70
openvdb::v11_0::tree::NodeUnion< ValueT, ChildT, typename std::enable_if< std::is_trivially_copyable< ValueT >::value >::type >::getValue
ValueT & getValue()
Definition NodeUnion.h:73
openvdb::v11_0::tree::NodeUnion< ValueT, ChildT, typename std::enable_if< std::is_trivially_copyable< ValueT >::value >::type >::getValue
const ValueT & getValue() const
Definition NodeUnion.h:72
openvdb::v11_0::tree::NodeUnion< ValueT, ChildT, typename std::enable_if< std::is_trivially_copyable< ValueT >::value >::type >::getChild
ChildT * getChild() const
Definition NodeUnion.h:69
openvdb::v11_0::tree::NodeUnion< ValueT, ChildT, typename std::enable_if< std::is_trivially_copyable< ValueT >::value >::type >::NodeUnion
NodeUnion()
Definition NodeUnion.h:67
openvdb::v11_0::tree::NodeUnion< ValueT, ChildT, typename std::enable_if< std::is_trivially_copyable< ValueT >::value >::type >::setValue
void setValue(const ValueT &val)
Definition NodeUnion.h:74
openvdb::v11_0::tree::NodeUnion< ValueT, ChildT, typename std::enable_if< std::is_trivially_copyable< ValueT >::value >::type >::mChild
ChildT * mChild
Definition NodeUnion.h:64
openvdb::v11_0::tree::NodeUnion
Default implementation of a NodeUnion that stores the child pointer and the value separately (i....
Definition NodeUnion.h:32
openvdb::v11_0::tree::NodeUnion::setChild
void setChild(ChildT *child)
Definition NodeUnion.h:41
openvdb::v11_0::tree::NodeUnion::getValue
ValueT & getValue()
Definition NodeUnion.h:44
openvdb::v11_0::tree::NodeUnion::getValue
const ValueT & getValue() const
Definition NodeUnion.h:43
openvdb::v11_0::tree::NodeUnion::getChild
ChildT * getChild() const
Definition NodeUnion.h:40
openvdb::v11_0::tree::NodeUnion::NodeUnion
NodeUnion()
Definition NodeUnion.h:38
openvdb::v11_0::tree::NodeUnion::setValue
void setValue(const ValueT &val)
Definition NodeUnion.h:45
openvdb
Definition Exceptions.h:13
std
Definition Coord.h:589
Types.h
openvdb::v11_0::ValueTraits
Definition Types.h:295
OPENVDB_VERSION_NAME
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition version.h.in:121
OPENVDB_USE_VERSION_NAMESPACE
#define OPENVDB_USE_VERSION_NAMESPACE
Definition version.h.in:212