HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
NodeUnion.h
Go to the documentation of this file.
1 ///////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (c) 2012-2018 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 //
29 ///////////////////////////////////////////////////////////////////////////
30 
31 /// @file NodeUnion.h
32 ///
33 /// @details NodeUnion is a templated helper class that controls access to either
34 /// the child node pointer or the value for a particular element of a root
35 /// or internal node. For space efficiency, the child pointer and the value
36 /// are unioned when possible, since the two are never in use simultaneously.
37 
38 #ifndef OPENVDB_TREE_NODEUNION_HAS_BEEN_INCLUDED
39 #define OPENVDB_TREE_NODEUNION_HAS_BEEN_INCLUDED
40 
41 #include <openvdb/version.h>
42 #include <openvdb/Types.h>
43 #include <cstring> // for std::memcpy()
44 #include <type_traits>
45 
46 namespace openvdb {
48 namespace OPENVDB_VERSION_NAME {
49 namespace tree {
50 
51 #if OPENVDB_ABI_VERSION_NUMBER >= 4
52 
53 // Forward declaration of traits class
54 template<typename T> struct CopyTraits;
55 
56 // Default implementation that stores the child pointer and the value separately
57 // (i.e., not in a union)
58 // This implementation is not used for POD, math::Vec or math::Coord value types.
59 template<typename ValueT, typename ChildT, typename Enable = void>
60 class NodeUnion
61 {
62 private:
63  ChildT* mChild;
64  ValueT mValue;
65 
66 public:
67  NodeUnion(): mChild(nullptr), mValue() {}
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 // Template specialization for values of POD types (int, float, pointer, etc.)
79 template<typename ValueT, typename ChildT>
80 class NodeUnion<ValueT, ChildT, typename std::enable_if<std::is_pod<ValueT>::value>::type>
81 {
82 private:
83  union { ChildT* mChild; ValueT mValue; };
84 
85 public:
86  NodeUnion(): mChild(nullptr) {}
87 
88  ChildT* getChild() const { return mChild; }
89  void setChild(ChildT* child) { mChild = child; }
90 
91  const ValueT& getValue() const { return mValue; }
92  ValueT& getValue() { return mValue; }
93  void setValue(const ValueT& val) { mValue = val; }
94 };
95 
96 
97 // Template specialization for values of types such as math::Vec3f and math::Coord
98 // for which CopyTraits<T>::IsCopyable is true
99 template<typename ValueT, typename ChildT>
100 class NodeUnion<ValueT, ChildT, typename std::enable_if<CopyTraits<ValueT>::IsCopyable>::type>
101 {
102 private:
103  union { ChildT* mChild; ValueT mValue; };
104 
105 public:
106  NodeUnion(): mChild(nullptr) {}
107  NodeUnion(const NodeUnion& other): mChild(nullptr)
108  { std::memcpy(this, &other, sizeof(*this)); }
109  NodeUnion& operator=(const NodeUnion& rhs)
110  { std::memcpy(this, &rhs, sizeof(*this)); return *this; }
111 
112  ChildT* getChild() const { return mChild; }
113  void setChild(ChildT* child) { mChild = child; }
114 
115  const ValueT& getValue() const { return mValue; }
116  ValueT& getValue() { return mValue; }
117  void setValue(const ValueT& val) { mValue = val; }
118 };
119 
120 
121 /// @details A type T is copyable if
122 /// # T stores member values by value (vs. by pointer or reference)
123 /// and T's true byte size is given by sizeof(T).
124 /// # T has a trivial destructor
125 /// # T has a default constructor
126 /// # T has an assignment operator
127 template<typename T> struct CopyTraits { static const bool IsCopyable = false; };
128 template<typename T> struct CopyTraits<math::Vec2<T>> { static const bool IsCopyable = true; };
129 template<typename T> struct CopyTraits<math::Vec3<T>> { static const bool IsCopyable = true; };
130 template<typename T> struct CopyTraits<math::Vec4<T>> { static const bool IsCopyable = true; };
131 template<> struct CopyTraits<math::Coord> { static const bool IsCopyable = true; };
132 
133 
134 ////////////////////////////////////////
135 
136 
137 #else // OPENVDB_ABI_VERSION_NUMBER <= 3
138 
139 // Prior to OpenVDB 4 and the introduction of C++11, values of non-POD types
140 // were heap-allocated and stored by pointer due to C++98 restrictions on unions.
141 
142 // Internal implementation of a union of a child node pointer and a value
143 template<bool ValueIsClass, class ValueT, class ChildT> class NodeUnionImpl;
144 
145 
146 // Partial specialization for values of non-class types
147 // (int, float, pointer, etc.) that stores elements by value
148 template<typename ValueT, typename ChildT>
149 class NodeUnionImpl</*ValueIsClass=*/false, ValueT, ChildT>
150 {
151 private:
152  union { ChildT* child; ValueT value; } mUnion;
153 
154 public:
155  NodeUnionImpl() { mUnion.child = nullptr; }
156 
157  ChildT* getChild() const { return mUnion.child; }
158  void setChild(ChildT* child) { mUnion.child = child; }
159 
160  const ValueT& getValue() const { return mUnion.value; }
161  ValueT& getValue() { return mUnion.value; }
162  void setValue(const ValueT& val) { mUnion.value = val; }
163 };
164 
165 
166 // Partial specialization for values of class types (std::string,
167 // math::Vec, etc.) that stores elements by pointer
168 template<typename ValueT, typename ChildT>
169 class NodeUnionImpl</*ValueIsClass=*/true, ValueT, ChildT>
170 {
171 private:
172  union { ChildT* child; ValueT* value; } mUnion;
173  bool mHasChild;
174 
175 public:
176  NodeUnionImpl() : mHasChild(true) { this->setChild(nullptr); }
177  NodeUnionImpl(const NodeUnionImpl& other) : mHasChild(true)
178  {
179  if (other.mHasChild) {
180  this->setChild(other.getChild());
181  } else {
182  this->setValue(other.getValue());
183  }
184  }
186  {
187  if (other.mHasChild) {
188  this->setChild(other.getChild());
189  } else {
190  this->setValue(other.getValue());
191  }
192  return *this;
193  }
194  ~NodeUnionImpl() { this->setChild(nullptr); }
195 
196  ChildT* getChild() const { return mHasChild ? mUnion.child : nullptr; }
197  void setChild(ChildT* child)
198  {
199  if (!mHasChild) delete mUnion.value;
200  mUnion.child = child;
201  mHasChild = true;
202  }
203 
204  const ValueT& getValue() const { return *mUnion.value; }
205  ValueT& getValue() { return *mUnion.value; }
206  void setValue(const ValueT& val)
207  {
208  if (!mHasChild) delete mUnion.value;
209  mUnion.value = new ValueT(val);
210  mHasChild = false;
211  }
212 };
213 
214 
215 template<typename ValueT, typename ChildT>
216 struct NodeUnion: public NodeUnionImpl<std::is_class<ValueT>::value, ValueT, ChildT>
217 {
219 };
220 
221 #endif
222 
223 } // namespace tree
224 } // namespace OPENVDB_VERSION_NAME
225 } // namespace openvdb
226 
227 #endif // OPENVDB_TREE_NODEUNION_HAS_BEEN_INCLUDED
228 
229 // Copyright (c) 2012-2018 DreamWorks Animation LLC
230 // All rights reserved. This software is distributed under the
231 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:189
GLuint GLfloat * val
Definition: glcorearb.h:1607
GLint GLint GLsizei GLint GLenum GLenum type
Definition: glcorearb.h:107
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h:135