Loading...
Searching...
No Matches
NodeUnion.h
Go to the documentation of this file.
// Copyright Contributors to the OpenVDB Project
// SPDX-License-Identifier: MPL-2.0
/// @file NodeUnion.h
///
/// @details NodeUnion is a templated helper class that controls access to either
/// the child node pointer or the value for a particular element of a root
/// or internal node. For space efficiency, the child pointer and the value
/// are unioned when possible, since the two are never in use simultaneously.
#ifndef OPENVDB_TREE_NODEUNION_HAS_BEEN_INCLUDED
#define OPENVDB_TREE_NODEUNION_HAS_BEEN_INCLUDED
#include <openvdb/version.h>
#include <openvdb/Types.h>
#include <cstring> // for std::memcpy()
#include <type_traits>
namespace openvdb {
OPENVDB_USE_VERSION_NAMESPACE
namespace OPENVDB_VERSION_NAME {
namespace tree {
/// @brief Default implementation of a NodeUnion that stores the child pointer
/// and the value separately (i.e., not in a union). Types which select this
/// specialization usually do not conform to the requirements of a union
/// member, that is that the type ValueT is not trivially copyable. This
/// implementation is thus NOT used for POD, math::Vec, math::Mat, math::Quat
/// or math::Coord types, but is used (for example) with std::string
template<typename ValueT, typename ChildT, typename Enable = void>
class NodeUnion
{
private:
ChildT* mChild;
ValueT mValue;
public:
NodeUnion(): mChild(nullptr), mValue() {}
ChildT* getChild() const { return mChild; }
void setChild(ChildT* child) { mChild = child; }
const ValueT& getValue() const { return mValue; }
ValueT& getValue() { return mValue; }
void setValue(const ValueT& val) { mValue = val; }
// Small check to ensure this class isn't
// selected for some expected types
static_assert(!ValueTraits<ValueT>::IsVec &&
!ValueTraits<ValueT>::IsMat &&
!ValueTraits<ValueT>::IsQuat &&
!std::is_same<ValueT, math::Coord>::value &&
!std::is_arithmetic<ValueT>::value,
"Unexpected instantiation of NodeUnion");
};
/// @brief Template specialization of a NodeUnion that stores the child pointer
/// and the value together (int, float, pointer, etc.)
template<typename ValueT, typename ChildT>
class NodeUnion<ValueT, ChildT,
typename std::enable_if<std::is_trivially_copyable<ValueT>::value>::type>
{
private:
union { ChildT* mChild; ValueT mValue; };
public:
NodeUnion(): mChild(nullptr) {}
ChildT* getChild() const { return mChild; }
void setChild(ChildT* child) { mChild = child; }
const ValueT& getValue() const { return mValue; }
ValueT& getValue() { return mValue; }
void setValue(const ValueT& val) { mValue = val; }
};
////////////////////////////////////////
} // namespace tree
} // namespace OPENVDB_VERSION_NAME
} // namespace openvdb
#endif // OPENVDB_TREE_NODEUNION_HAS_BEEN_INCLUDED
Types.h
openvdb::v11_0::tree::NodeUnion< ValueT, ChildT, typename std::enable_if< std::is_trivially_copyable< ValueT >::value >::type >::setChild
void setChild(ChildT *child)
Definition NodeUnion.h:70
openvdb::v11_0::tree::NodeUnion< ValueT, ChildT, typename std::enable_if< std::is_trivially_copyable< ValueT >::value >::type >::getValue
ValueT & getValue()
Definition NodeUnion.h:73
openvdb::v11_0::tree::NodeUnion< ValueT, ChildT, typename std::enable_if< std::is_trivially_copyable< ValueT >::value >::type >::getValue
const ValueT & getValue() const
Definition NodeUnion.h:72
openvdb::v11_0::tree::NodeUnion< ValueT, ChildT, typename std::enable_if< std::is_trivially_copyable< ValueT >::value >::type >::getChild
ChildT * getChild() const
Definition NodeUnion.h:69
openvdb::v11_0::tree::NodeUnion< ValueT, ChildT, typename std::enable_if< std::is_trivially_copyable< ValueT >::value >::type >::NodeUnion
NodeUnion()
Definition NodeUnion.h:67
openvdb::v11_0::tree::NodeUnion< ValueT, ChildT, typename std::enable_if< std::is_trivially_copyable< ValueT >::value >::type >::setValue
void setValue(const ValueT &val)
Definition NodeUnion.h:74
openvdb::v11_0::tree::NodeUnion< ValueT, ChildT, typename std::enable_if< std::is_trivially_copyable< ValueT >::value >::type >::mChild
ChildT * mChild
Definition NodeUnion.h:64
openvdb::v11_0::tree::NodeUnion< ValueT, ChildT, typename std::enable_if< std::is_trivially_copyable< ValueT >::value >::type >::mValue
ValueT mValue
Definition NodeUnion.h:64
openvdb::v11_0::tree::NodeUnion::setChild
void setChild(ChildT *child)
Definition NodeUnion.h:41
openvdb::v11_0::tree::NodeUnion::getValue
ValueT & getValue()
Definition NodeUnion.h:44
openvdb::v11_0::tree::NodeUnion::getValue
const ValueT & getValue() const
Definition NodeUnion.h:43
openvdb::v11_0::tree::NodeUnion::getChild
ChildT * getChild() const
Definition NodeUnion.h:40
openvdb::v11_0::tree::NodeUnion::NodeUnion
NodeUnion()
Definition NodeUnion.h:38
openvdb::v11_0::tree::NodeUnion::setValue
void setValue(const ValueT &val)
Definition NodeUnion.h:45
openvdb
Definition Exceptions.h:13
std
Definition Coord.h:589
openvdb::v11_0::ValueTraits::IsVec
static const bool IsVec
Definition Types.h:296
openvdb::v11_0::ValueTraits::IsQuat
static const bool IsQuat
Definition Types.h:297
openvdb::v11_0::ValueTraits::IsMat
static const bool IsMat
Definition Types.h:298
OPENVDB_VERSION_NAME
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition version.h.in:121
OPENVDB_USE_VERSION_NAMESPACE
#define OPENVDB_USE_VERSION_NAMESPACE
Definition version.h.in:212