OpenVDB  0.104.0
NodeUnion.h
Go to the documentation of this file.
1 
2 //
3 // Copyright (c) 2012 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 //
43 
44 #ifndef OPENVDB_TREE_NODEUNION_HAS_BEEN_INCLUDED
45 #define OPENVDB_TREE_NODEUNION_HAS_BEEN_INCLUDED
46 
47 #include <boost/type_traits/is_class.hpp>
48 #include <openvdb/version.h>
49 
50 namespace openvdb {
52 namespace OPENVDB_VERSION_NAME {
53 namespace tree {
54 
55 // Internal implementation of a union of a child node pointer and a fill value
56 template<bool ValueIsClass, class ValueT, class ChildT> class NodeUnionImpl;
57 
58 
59 // Partial specialization for fill values of non-class types
60 // (int, float, pointer, etc.) that stores elements by value
61 template<typename ValueT, typename ChildT>
62 class NodeUnionImpl</*ValueIsClass=*/false, ValueT, ChildT>
63 {
64 private:
65  union { ChildT* child; ValueT value; } mUnion;
66 
67 public:
68  NodeUnionImpl() { setChild(NULL); }
69 
70  ChildT* getChild() const { return mUnion.child; }
71  const ValueT& getValue() const { 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 fill 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) { setChild(NULL); }
89  {
90  if (other.mHasChild) setChild(other.getChild());
91  else setValue(other.getValue());
92  }
93  NodeUnionImpl& operator=(const NodeUnionImpl& other)
94  {
95  if (other.mHasChild) setChild(other.getChild());
96  else setValue(other.getValue());
97  }
98  ~NodeUnionImpl() { setChild(NULL); }
99 
100  ChildT* getChild() const
101  { return mHasChild ? mUnion.child : NULL; }
102  void setChild(ChildT* child)
103  {
104  if (!mHasChild) delete mUnion.value;
105  mUnion.child = child;
106  mHasChild = true;
107  }
108 
109  const ValueT& getValue() const { return *mUnion.value; }
111  void setValue(const ValueT& val)
112  {
115  if (!mHasChild) delete mUnion.value;
116  mUnion.value = new ValueT(val);
117  mHasChild = false;
118  }
119 };
120 
121 
122 template<typename ValueT, typename ChildT>
123 struct NodeUnion: public NodeUnionImpl<
124  boost::is_class<ValueT>::value, ValueT, ChildT>
125 {
127 };
128 
129 } // namespace tree
130 } // namespace OPENVDB_VERSION_NAME
131 } // namespace openvdb
132 
133 #endif // OPENVDB_TREE_NODEUNION_HAS_BEEN_INCLUDED
134 
135 // Copyright (c) 2012 DreamWorks Animation LLC
136 // All rights reserved. This software is distributed under the
137 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )