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