OpenVDB 10.0.1
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
19namespace openvdb {
21namespace OPENVDB_VERSION_NAME {
22namespace 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
30template<typename ValueT, typename ChildT, typename Enable = void>
32{
33private:
34 ChildT* mChild;
35 ValueT mValue;
36
37public:
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 &&
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.)
59template<typename ValueT, typename ChildT>
60class NodeUnion<ValueT, ChildT,
61 typename std::enable_if<std::is_trivially_copyable<ValueT>::value>::type>
62{
63private:
64 union { ChildT* mChild; ValueT mValue; };
65
66public:
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
ChildT * child
Definition GridBuilder.h:1289
Default implementation of a NodeUnion that stores the child pointer and the value separately (i....
Definition NodeUnion.h:32
void setChild(ChildT *child)
Definition NodeUnion.h:41
ValueT & getValue()
Definition NodeUnion.h:44
const ValueT & getValue() const
Definition NodeUnion.h:43
ChildT * getChild() const
Definition NodeUnion.h:40
NodeUnion()
Definition NodeUnion.h:38
void setValue(const ValueT &val)
Definition NodeUnion.h:45
Definition Exceptions.h:13
Definition Coord.h:587
Definition Types.h:256
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition version.h.in:121
#define OPENVDB_USE_VERSION_NAMESPACE
Definition version.h.in:212