24 #ifndef SDF_DECLAREHANDLES_H
25 #define SDF_DECLAREHANDLES_H
30 #include "pxr/usd/sdf/api.h"
31 #include "pxr/base/arch/demangle.h"
32 #include "pxr/base/arch/hints.h"
33 #include "pxr/base/tf/diagnostic.h"
34 #include "pxr/base/tf/weakPtrFacade.h"
35 #include "pxr/base/tf/declarePtrs.h"
39 #include <type_traits>
41 #include <boost/intrusive_ptr.hpp>
42 #include <boost/operators.hpp>
44 PXR_NAMESPACE_OPEN_SCOPE
54 typedef boost::intrusive_ptr<Sdf_Identity> Sdf_IdentityRefPtr;
63 class SdfHandle :
private boost::totally_ordered<SdfHandle<T> > {
68 typedef typename std::remove_const<SpecType>::type NonConstSpecType;
73 explicit SdfHandle(
const Sdf_IdentityRefPtr&
id) : _spec(
id) { }
74 SdfHandle(
const SpecType& spec) : _spec(spec) { }
81 const_cast<NonConstSpecType&
>(_spec) = x._spec;
88 const_cast<NonConstSpecType&
>(_spec) = x._spec;
96 if (ARCH_UNLIKELY(_spec.IsDormant())) {
101 return const_cast<SpecType*
>(&_spec);
104 const SpecType & GetSpec()
const
111 const_cast<SpecType&
>(_spec) = SpecType();
114 #if !defined(doxygen)
115 typedef SpecType This::*UnspecifiedBoolType;
120 operator UnspecifiedBoolType()
const
122 return _spec.IsDormant() ? 0 : &This::_spec;
128 return _spec.IsDormant();
135 return _spec == other._spec;
141 bool operator<(const SdfHandle<U>& other)
const
143 return _spec < other._spec;
154 template <
class U>
friend class SdfHandle;
161 return !x ? 0 : x.operator->();
164 PXR_NAMESPACE_CLOSE_SCOPE
168 using PXR_NS::get_pointer;
172 PXR_NAMESPACE_OPEN_SCOPE
178 typedef std::vector<Handle> Vector;
179 typedef std::vector<ConstHandle> ConstVector;
186 typedef std::vector<Handle> Vector;
187 typedef std::vector<ConstHandle> ConstVector;
190 template <
typename T>
192 SdfCreateHandle(T *p)
198 SDF_API SdfHandleTo<SdfLayer>::Handle
201 template <
typename T>
203 SdfCreateNonConstHandle(T
const *p)
205 return SdfCreateHandle(const_cast<T *>(p));
208 struct Sdf_CastAccess {
209 template<
class DST,
class SRC>
210 static DST CastSpec(
const SRC& spec) {
217 const SdfSpec& srcSpec,
const std::type_info& destType);
220 Sdf_CanCastToTypeCheckSchema(
221 const SdfSpec& srcSpec,
const std::type_info& destType);
223 template <
class DST,
class SRC>
224 struct Sdf_SpecTypesAreDirectlyRelated
225 : std::integral_constant<bool,
226 std::is_base_of<DST, SRC>::value ||
227 std::is_base_of<SRC, DST>::value>
240 template <
typename DST,
typename SRC>
245 typedef typename DST::SpecType Spec;
248 if (Sdf_CanCastToType(x.GetSpec(),
typeid(Spec))) {
249 return Handle(Sdf_CastAccess::CastSpec<Spec,SRC>(x.GetSpec()));
255 template <
typename DST,
typename SRC>
267 template <
typename DST,
typename SRC>
272 typedef typename DST::SpecType Spec;
274 static_assert(Sdf_SpecTypesAreDirectlyRelated<Spec, SRC>::value,
275 "Spec and SRC must be directly related.");
277 return Handle(Sdf_CastAccess::CastSpec<Spec,SRC>(x.GetSpec()));
280 template <
typename T>
292 template <
typename DST,
typename SRC>
297 typedef typename DST::SpecType Spec;
300 if (Sdf_CanCastToTypeCheckSchema(x.GetSpec(),
typeid(Spec))) {
301 return Handle(Sdf_CastAccess::CastSpec<Spec,SRC>(x.GetSpec()));
310 template <
typename DST,
typename SRC>
315 typedef typename DST::SpecType Spec;
317 return Handle(Sdf_CastAccess::CastSpec<Spec,SRC>(x.GetSpec()));
321 template <
typename DST_SPEC,
typename SRC_SPEC>
326 return Sdf_CastAccess::CastSpec<DST_SPEC,SRC_SPEC>(x);
330 typedef std::vector<TfRefPtr<SdfLayer> > SdfLayerRefPtrVector;
331 typedef std::set<SdfHandleTo<SdfLayer>::Handle> SdfLayerHandleSet;
333 #define SDF_DECLARE_HANDLES(cls) \
334 typedef SdfHandleTo<class cls>::Handle cls##Handle; \
335 typedef SdfHandleTo<class cls>::ConstHandle cls##ConstHandle; \
336 typedef SdfHandleTo<class cls>::Vector cls##HandleVector; \
337 typedef SdfHandleTo<class cls>::ConstVector cls##ConstHandleVector
339 PXR_NAMESPACE_CLOSE_SCOPE
341 #endif // SDF_DECLAREHANDLES_H
A unit of scene description that you combine with other units of scene description to form a shot...
TO TfSafeDynamic_cast(FROM *ptr)
Safely perform a dynamic cast.
bool operator!() const
Returns false in a boolean context if the object is valid, true otherwise.
SdfHandle< typename DST::SpecType > TfStatic_cast(const SdfHandle< SRC > &x)
Convert SdfHandle<SRC> x to an SdfHandle<DST>.
ARCH_API std::string ArchGetDemangled(const std::string &typeName)
Return demangled RTTI-generated type name.
Base class for all Sdf spec classes.
SdfHandle is a smart ptr that calls IsDormant() on the pointed-to object as an extra expiration check...
Pointer storage with deletion detection.
SdfHandle< typename DST::SpecType > TfDynamic_cast(const SdfHandle< SRC > &x)
Convert SdfHandle<SRC> x to an SdfHandle<DST>.
SdfHandle< typename DST::SpecType > SdfSpecDynamic_cast(const SdfHandle< SRC > &x)
Convert SdfHandle<SRC> x to an SdfHandle<DST>.
#define TF_FATAL_ERROR(fmt, args)
Issue a fatal error and end the program.
SpecType * operator->() const
Dereference.
bool operator==(const SdfHandle< U > &other) const
Compares handles for equality.
Reference-counted smart pointer utility class.
friend size_t hash_value(const This &x)
Hash.
SdfHandle< typename DST::SpecType > SdfSpecStatic_cast(const SdfHandle< SRC > &x)
Convert SdfHandle<SRC> x to an SdfHandle<DST>.