OpenVDB: NodeUnion.h Source File

OpenVDB 3.2.0

NodeUnion.h
Go to the documentation of this file.
1 //
3 // Copyright (c) 2012-2016 DreamWorks Animation LLC
4 //
5 // All rights reserved. This software is distributed under the
6 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
7 //
8 // Redistributions of source code must retain the above copyright
9 // and license notice and the following restrictions and disclaimer.
10 //
11 // * Neither the name of DreamWorks Animation nor the names of
12 // its contributors may be used to endorse or promote products derived
13 // from this software without specific prior written permission.
14 //
15 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY INDIRECT, INCIDENTAL,
20 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 // IN NO EVENT SHALL THE COPYRIGHT HOLDERS' AND CONTRIBUTORS' AGGREGATE
27 // LIABILITY FOR ALL CLAIMS REGARDLESS OF THEIR BASIS EXCEED US$250.00.
28 //
30 //
42
43 #ifndef OPENVDB_TREE_NODEUNION_HAS_BEEN_INCLUDED
44 #define OPENVDB_TREE_NODEUNION_HAS_BEEN_INCLUDED
45
46 #include <boost/type_traits/is_class.hpp>
47 #include <openvdb/version.h>
48
49 namespace openvdb {
50 OPENVDB_USE_VERSION_NAMESPACE
51 namespace OPENVDB_VERSION_NAME {
52 namespace tree {
53
54 // Internal implementation of a union of a child node pointer and a value
55 template<bool ValueIsClass, class ValueT, class ChildT> class NodeUnionImpl;
56
57
58 // Partial specialization for values of non-class types
59 // (int, float, pointer, etc.) that stores elements by value
60 template<typename ValueT, typename ChildT>
61 class NodeUnionImpl</*ValueIsClass=*/false, ValueT, ChildT>
62 {
63 private:
64 union { ChildT* child; ValueT value; } mUnion;
65
66 public:
67 NodeUnionImpl() { mUnion.child = NULL; }
68
69 ChildT* getChild() const { return mUnion.child; }
70 const ValueT& getValue() const { return mUnion.value; }
71 ValueT& getValue() { return mUnion.value; }
72 void setChild(ChildT* child) { mUnion.child = child; }
73 void setValue(const ValueT& val) { mUnion.value = val; }
74 };
75
76
77 // Partial specialization for values of class types (std::string,
78 // math::Vec, etc.) that stores elements by pointer
79 template<typename ValueT, typename ChildT>
80 class NodeUnionImpl</*ValueIsClass=*/true, ValueT, ChildT>
81 {
82 private:
83 union { ChildT* child; ValueT* value; } mUnion;
84 bool mHasChild;
85
86 public:
87 NodeUnionImpl() : mHasChild(true) { this->setChild(NULL); }
88 NodeUnionImpl(const NodeUnionImpl& other) : mHasChild(true)
89 {
90 if (other.mHasChild) {
91 this->setChild(other.getChild());
92 } else {
93 this->setValue(other.getValue());
94 }
95 }
96 NodeUnionImpl& operator=(const NodeUnionImpl& other)
97 {
98 if (other.mHasChild) {
99 this->setChild(other.getChild());
100 } else {
101 this->setValue(other.getValue());
102 }
103 return *this;
104 }
105 ~NodeUnionImpl() { this->setChild(NULL); }
106
107 ChildT* getChild() const { return mHasChild ? mUnion.child : NULL; }
108 void setChild(ChildT* child)
109 {
110 if (!mHasChild) delete mUnion.value;
111 mUnion.child = child;
112 mHasChild = true;
113 }
114
115 const ValueT& getValue() const { return *mUnion.value; }
116 ValueT& getValue() { return *mUnion.value; }
117 void setValue(const ValueT& val)
118 {
121 if (!mHasChild) delete mUnion.value;
122 mUnion.value = new ValueT(val);
123 mHasChild = false;
124 }
125 };
126
127
128 template<typename ValueT, typename ChildT>
129 struct NodeUnion: public NodeUnionImpl<
130 boost::is_class<ValueT>::value, ValueT, ChildT>
131 {
132 NodeUnion() {}
133 };
134
135 } // namespace tree
136 } // namespace OPENVDB_VERSION_NAME
137 } // namespace openvdb
138
139 #endif // OPENVDB_TREE_NODEUNION_HAS_BEEN_INCLUDED
140
141 // Copyright (c) 2012-2016 DreamWorks Animation LLC
142 // All rights reserved. This software is distributed under the
143 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
openvdb::v3_2_0::tree::NodeUnion::NodeUnion
NodeUnion()
Definition: NodeUnion.h:132
openvdb::v3_2_0::tree::NodeUnionImpl< false, ValueT, ChildT >::getValue
ValueT & getValue()
Definition: NodeUnion.h:71
openvdb::v3_2_0::tree::NodeUnionImpl< true, ValueT, ChildT >::NodeUnionImpl
NodeUnionImpl()
Definition: NodeUnion.h:87
openvdb::v3_2_0::tree::NodeUnionImpl< false, ValueT, ChildT >::setChild
void setChild(ChildT *child)
Definition: NodeUnion.h:72
openvdb::v3_2_0::tree::NodeUnionImpl< false, ValueT, ChildT >::getValue
const ValueT & getValue() const
Definition: NodeUnion.h:70
openvdb::v3_2_0::tree::NodeUnion
Definition: NodeUnion.h:129
openvdb::v3_2_0::tree::NodeUnionImpl< false, ValueT, ChildT >::value
ValueT value
Definition: NodeUnion.h:64
openvdb::v3_2_0::tree::NodeUnionImpl< true, ValueT, ChildT >::setValue
void setValue(const ValueT &val)
Definition: NodeUnion.h:117
openvdb::v3_2_0::tree::NodeUnionImpl< false, ValueT, ChildT >::setValue
void setValue(const ValueT &val)
Definition: NodeUnion.h:73
openvdb::v3_2_0::tree::NodeUnionImpl< true, ValueT, ChildT >::getChild
ChildT * getChild() const
Definition: NodeUnion.h:107
OPENVDB_VERSION_NAME
#define OPENVDB_VERSION_NAME
Definition: version.h:43
openvdb::v3_2_0::tree::NodeUnionImpl< true, ValueT, ChildT >::~NodeUnionImpl
~NodeUnionImpl()
Definition: NodeUnion.h:105
openvdb::v3_2_0::tree::NodeUnionImpl< false, ValueT, ChildT >::NodeUnionImpl
NodeUnionImpl()
Definition: NodeUnion.h:67
openvdb::v3_2_0::tree::NodeUnionImpl< true, ValueT, ChildT >::NodeUnionImpl
NodeUnionImpl(const NodeUnionImpl &other)
Definition: NodeUnion.h:88
openvdb::v3_2_0::tree::NodeUnionImpl< true, ValueT, ChildT >::setChild
void setChild(ChildT *child)
Definition: NodeUnion.h:108
openvdb
Definition: Exceptions.h:39
openvdb::v3_2_0::tree::NodeUnionImpl
Definition: NodeUnion.h:55
version.h
openvdb::v3_2_0::tree::NodeUnionImpl< false, ValueT, ChildT >::getChild
ChildT * getChild() const
Definition: NodeUnion.h:69
openvdb::v3_2_0::tree::NodeUnionImpl< true, ValueT, ChildT >::operator=
NodeUnionImpl & operator=(const NodeUnionImpl &other)
Definition: NodeUnion.h:96
OPENVDB_USE_VERSION_NAMESPACE
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:71
openvdb::v3_2_0::tree::NodeUnionImpl< true, ValueT, ChildT >::getValue
ValueT & getValue()
Definition: NodeUnion.h:116
openvdb::v3_2_0::tree::NodeUnionImpl< true, ValueT, ChildT >::getValue
const ValueT & getValue() const
Definition: NodeUnion.h:115
openvdb::v3_2_0::tree::NodeUnionImpl< true, ValueT, ChildT >::value
ValueT * value
Definition: NodeUnion.h:83