HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
TfType Class Reference

#include <type.h>

Classes

struct  Bases
 
class  FactoryBase
 Base class of all factory types. More...
 

Public Types

enum  LegacyFlags { ABSTRACT = 0x01, CONCRETE = 0x02, MANUFACTURABLE = 0x08 }
 
using DefinitionCallback = void(*)(TfType)
 Callback invoked when a declared type needs to be defined. More...
 

Public Member Functions

TF_API TfType ()
 
bool operator== (const TfType &t) const
 
bool operator!= (const TfType &t) const
 
bool operator< (const TfType &t) const
 Comparison operator. More...
 
bool operator> (const TfType &t) const
 
bool operator<= (const TfType &t) const
 
bool operator>= (const TfType &t) const
 
Pointer casts
TF_API voidCastToAncestor (TfType ancestor, void *addr) const
 
const voidCastToAncestor (TfType ancestor, const void *addr) const
 
TF_API voidCastFromAncestor (TfType ancestor, void *addr) const
 
const voidCastFromAncestor (TfType ancestor, const void *addr) const
 
Instantiation / Manufacturing
TF_API void SetFactory (std::unique_ptr< FactoryBase > factory) const
 
template<class T >
void SetFactory (std::unique_ptr< T > &factory) const
 
template<class T >
void SetFactory () const
 
const TfTypeFactory (std::unique_ptr< FactoryBase > factory) const
 
template<class T >
const TfTypeFactory (std::unique_ptr< T > &factory) const
 
template<class T >
const TfTypeFactory () const
 
template<class T >
TGetFactory () const
 

Static Public Member Functions

static TF_API TfType const & GetUnknownType ()
 

Friends

template<typename TypeVector >
struct Tf_AddBases
 
struct _TypeInfo
 
class Tf_TypeRegistry
 
template<class HashState >
void TfHashAppend (HashState &h, TfType const &type)
 

Finding types

TF_API TfType const & FindDerivedByName (const std::string &name) const
 
template<typename T >
static TfType const & Find ()
 
template<typename T >
static TfType const & Find (const T &obj)
 
static TfType const & Find (const std::type_info &t)
 
static TfType const & FindByTypeid (const std::type_info &t)
 
static TF_API TfType const & FindByName (const std::string &name)
 
template<typename BASE >
static TfType const & FindDerivedByName (const std::string &name)
 

Type queries

typedef TfType::_TypeInfo
*TfType::* 
UnspecifiedBoolType
 
TF_API const std::stringGetTypeName () const
 
TF_API const std::type_info & GetTypeid () const
 
TF_API std::vector< std::stringGetAliases (TfType derivedType) const
 
TF_API std::vector< TfTypeGetBaseTypes () const
 
TF_API size_t GetNBaseTypes (TfType *out, size_t maxBases) const
 
TF_API std::vector< TfTypeGetDirectlyDerivedTypes () const
 
TF_API TfType const & GetCanonicalType () const
 Return the canonical type for this type. More...
 
TF_API void GetAllDerivedTypes (std::set< TfType > *result) const
 
TF_API void GetAllAncestorTypes (std::vector< TfType > *result) const
 
TF_API bool IsA (TfType queryType) const
 
template<typename T >
bool IsA () const
 
bool IsUnknown () const
 
 operator UnspecifiedBoolType () const
 
bool operator! () const
 
bool IsRoot () const
 
TF_API bool IsEnumType () const
 
TF_API bool IsPlainOldDataType () const
 
TF_API size_t GetSizeof () const
 
static TF_API TfType const & GetRoot ()
 
static TF_API std::string GetCanonicalTypeName (const std::type_info &)
 

Registering new types

TF_API void AddAlias (TfType base, const std::string &name) const
 
const TfTypeAlias (TfType base, const std::string &name) const
 
static TF_API TfType const & Declare (const std::string &typeName)
 
static TF_API TfType const & Declare (const std::string &typeName, const std::vector< TfType > &bases, DefinitionCallback definitionCallback=nullptr)
 
template<typename T , typename BaseTypes = TfType::Bases<>>
static TfType const & Declare ()
 
template<typename T , typename B >
static TfType const & Define ()
 
template<typename T >
static TfType const & Define ()
 
template<typename Base , typename Derived >
static void AddAlias (const std::string &name)
 

Detailed Description

TfType represents a dynamic runtime type.

TfTypes are created and discovered at runtime, rather than compile time.

Features:

  • unique typename
  • safe across DSO boundaries
  • can represent C++ types, pure Python types, or Python subclasses of wrapped C++ types
  • lightweight value semantics – you can copy and default construct TfType, unlike std::type_info.
  • totally ordered – can use as a std::map key

Definition at line 64 of file type.h.

Member Typedef Documentation

Callback invoked when a declared type needs to be defined.

Definition at line 70 of file type.h.

Return the machine-independent name for this type. This name is specified when the TfType is declared.

See Also
Declare()

Definition at line 390 of file type.h.

Member Enumeration Documentation

Enumerator
ABSTRACT 

Abstract (unmanufacturable and unclonable)

CONCRETE 

Not abstract.

MANUFACTURABLE 

Manufacturable type (implies concrete)

Definition at line 80 of file type.h.

Constructor & Destructor Documentation

TF_API TfType::TfType ( )

Construct an TfType representing an unknown type.

To actually register a new type with the TfType system, see TfType::Declare().

Note that this always holds true:

TfType().IsUnknown() == true

Member Function Documentation

template<typename Base , typename Derived >
static void TfType::AddAlias ( const std::string name)
inlinestatic

Add an alias for DERIVED beneath BASE.

This is a convenience method, that declares both DERIVED and BASE as TfTypes before adding the alias.

Definition at line 505 of file type.h.

TF_API void TfType::AddAlias ( TfType  base,
const std::string name 
) const

Add an alias name for this type under the given base type.

Aliases are similar to typedefs in C++: they provide an alternate name for a type. The alias is defined with respect to the given base type. Aliases must be unique with respect to both other aliases beneath that base type and names of derived types of that base.

const TfType& TfType::Alias ( TfType  base,
const std::string name 
) const
inline

Convenience method to add an alias and return *this.

See Also
AddAlias()

Definition at line 524 of file type.h.

TF_API void* TfType::CastFromAncestor ( TfType  ancestor,
void addr 
) const

Cast addr, which pointed to the ancestor type ancestor, to the type of *this.

This function is the opposite of CastToAncestor(); the assumption is that addr was a pointer to the type corresponding to ancestor, and was then reinterpret-cast to void*, but now you wish to turn cast the pointer to the type corresponding to *this. While the fact that addr was a pointer of type ancestor is taken on faith, a runtime check is performed to verify that the underlying object pointed to by addr is of type *this (or derived from *this).

Warning
Again, this is dangerous territory, and there's probably something much better than using this function.
const void* TfType::CastFromAncestor ( TfType  ancestor,
const void addr 
) const
inline

Cast addr to the address corresponding to the type ancestor.

(This is a dangerous function; there's probably a much better way to do whatever it is you're trying to do.)

With multiple inheritance, you can't do a reinterpret_cast back to an ancestor type; this function figures out how to cast addr to the address corresponding to the type ancestor if in fact ancestor is really an ancestor of the type corresponding to *this.

In order for this function to work correctly, addr must have been a pointer of type corresponding to *this, which was cast to void; and of course the type of ancestor must be an ancestor of the type of *this.

Warning
You are warned: this is deadly dangerous stuff, and you shouldn't be doing it!

Definition at line 577 of file type.h.

TF_API void* TfType::CastToAncestor ( TfType  ancestor,
void addr 
) const

Cast addr to the address corresponding to the type ancestor.

(This is a dangerous function; there's probably a much better way to do whatever it is you're trying to do.)

With multiple inheritance, you can't do a reinterpret_cast back to an ancestor type; this function figures out how to cast addr to the address corresponding to the type ancestor if in fact ancestor is really an ancestor of the type corresponding to *this.

In order for this function to work correctly, addr must have been a pointer of type corresponding to *this, which was cast to void; and of course the type of ancestor must be an ancestor of the type of *this.

Warning
You are warned: this is deadly dangerous stuff, and you shouldn't be doing it!
const void* TfType::CastToAncestor ( TfType  ancestor,
const void addr 
) const
inline

Cast addr to the address corresponding to the type ancestor.

(This is a dangerous function; there's probably a much better way to do whatever it is you're trying to do.)

With multiple inheritance, you can't do a reinterpret_cast back to an ancestor type; this function figures out how to cast addr to the address corresponding to the type ancestor if in fact ancestor is really an ancestor of the type corresponding to *this.

In order for this function to work correctly, addr must have been a pointer of type corresponding to *this, which was cast to void; and of course the type of ancestor must be an ancestor of the type of *this.

Warning
You are warned: this is deadly dangerous stuff, and you shouldn't be doing it!

Definition at line 555 of file type.h.

static TF_API TfType const& TfType::Declare ( const std::string typeName)
static

Declare a TfType with the given typeName, but no base type information. This just establishes the minimal stub for the type to exist, prior to it being fleshed out with more declarations (specifying base types) or a definition.

static TF_API TfType const& TfType::Declare ( const std::string typeName,
const std::vector< TfType > &  bases,
DefinitionCallback  definitionCallback = nullptr 
)
static

Declare a TfType with the given typeName and bases. If the bases vector is empty, the type will be marked as deriving from the root TfType (see TfType::GetRootType()). The definitionCallback, if given, will be invoked later to define the type when needed.

It is ok to redeclare a type that has already been declared. The given bases will supplement any existing bases. An example use of this is the Plugin system, where only a single base may be known in the plugin metadata, but when the code is loaded later, a full set of bases is specified.

It is an error to redeclare a type's definitionCallback.

template<class T , class BaseTypes >
TfType const & TfType::Declare ( )
static

Declares a TfType with the given C++ type T and C++ base types Bases. Each of the base types will be declared (but not defined) as TfTypes if they have not already been. See the other Declare() methods for more details.

Definition at line 65 of file type_Impl.h.

template<typename T , typename BaseTypes >
TfType const & TfType::Define ( )
static

Define a TfType with the given C++ type T and C++ base types B. Each of the base types will be declared (but not defined) as TfTypes if they have not already been.

The typeName of the created TfType will be the canonical demangled RTTI type name, as defined by GetCanonicalTypeName().

It is an error to attempt to define a type that has already been defined.

Definition at line 77 of file type_Impl.h.

template<typename T >
TfType const & TfType::Define ( )
static

Define a TfType with the given C++ type T and no bases. See the other Define() template for more details.

Note
C++ does not allow default template arguments for function templates, so we provide this separate definition for the case of no bases.

Definition at line 102 of file type_Impl.h.

const TfType& TfType::Factory ( std::unique_ptr< FactoryBase factory) const
inline

Sets the factory object for this type. A type's factory typically has methods to instantiate the type given various arguments and must inherit from FactoryBase. The factory cannot be changed once set.

Definition at line 609 of file type.h.

template<class T >
const TfType& TfType::Factory ( std::unique_ptr< T > &  factory) const
inline

Sets the factory object for this type. A type's factory typically has methods to instantiate the type given various arguments and must inherit from FactoryBase. The factory cannot be changed once set.

Definition at line 618 of file type.h.

template<class T >
const TfType& TfType::Factory ( ) const
inline

Sets the factory object for this type to be a T. The factory cannot be changed once set.

Definition at line 627 of file type.h.

template<typename T >
static TfType const& TfType::Find ( )
inlinestatic

Retrieve the TfType corresponding to type T.

The type T must have been declared or defined in the type system or the TfType corresponding to an unknown type is returned.

See Also
IsUnknown()

Definition at line 153 of file type.h.

template<typename T >
static TfType const& TfType::Find ( const T obj)
inlinestatic

Retrieve the TfType corresponding to obj.

The TfType corresponding to the actual object represented by obj is returned; this may not be the object returned by TfType::Find<T>() if T is a polymorphic type.

This works for Python subclasses of the C++ type T as well, as long as T has been wrapped using TfPyPolymorphic.

Of course, the object's type must have been declared or defined in the type system or the TfType corresponding to an unknown type is returned.

See Also
IsUnknown()

Definition at line 173 of file type.h.

static TfType const& TfType::Find ( const std::type_info &  t)
inlinestatic

Retrieve the TfType corresponding to an obj with the given type_info.

Definition at line 186 of file type.h.

static TF_API TfType const& TfType::FindByName ( const std::string name)
static

Retrieve the TfType corresponding to the given name.

Every type defined in the TfType system has a unique, implementation independent name. In addition, aliases can be added to identify a type underneath a specific base type; see TfType::AddAlias(). The given name will first be tried as an alias under the root type, and subsequently as a typename.

This method is equivalent to:

For any object obj,

Find(obj) == FindByName( Find(obj).GetTypeName() )
static TfType const& TfType::FindByTypeid ( const std::type_info &  t)
inlinestatic

Retrieve the TfType corresponding to an obj with the given type_info.

Definition at line 193 of file type.h.

TF_API TfType const& TfType::FindDerivedByName ( const std::string name) const

Retrieve the TfType that derives from this type and has the given alias or typename.

See Also
AddAlias
template<typename BASE >
static TfType const& TfType::FindDerivedByName ( const std::string name)
inlinestatic

Retrieve the TfType that derives from BASE and has the given alias or typename.

This is a convenience method, and is equivalent to:

TfType::Find<BASE>().FindDerivedByName(name)

Definition at line 235 of file type.h.

TF_API std::vector<std::string> TfType::GetAliases ( TfType  derivedType) const

Returns a vector of the aliases registered for the derivedType under this, the base type.

See Also
AddAlias()
TF_API void TfType::GetAllAncestorTypes ( std::vector< TfType > *  result) const

Build a vector of all ancestor types inherited by this type. The starting type is itself included, as the first element of the results vector.

Types are given in "C3" resolution order, as used for new-style classes starting in Python 2.3. This algorithm is more complicated than a simple depth-first traversal of base classes, in order to prevent some subtle errors with multiple-inheritance. See the references below for more background.

Note
This can be expensive; consider caching the results. TfType does not cache this itself since it is not needed internally.
See Also
Guido van Rossum. "Unifying types and classes in Python 2.2: Method resolution order." http://www.python.org/download/releases/2.2.2/descrintro/#mro
Barrett, Cassels, Haahr, Moon, Playford, Withington. "A Monotonic Superclass Linearization for Dylan." OOPSLA 96. http://www.webcom.com/haahr/dylan/linearization-oopsla96.html
TF_API void TfType::GetAllDerivedTypes ( std::set< TfType > *  result) const

Return the set of all types derived (directly or indirectly) from this type.

TF_API std::vector<TfType> TfType::GetBaseTypes ( ) const

Return a vector of types from which this type was derived.

TF_API TfType const& TfType::GetCanonicalType ( ) const

Return the canonical type for this type.

static TF_API std::string TfType::GetCanonicalTypeName ( const std::type_info &  )
static

Return the canonical typeName used for a given std::type_info.

Exactly how the canonical name is generated is left undefined, but in practice it is likely to be the demangled RTTI name of the type_info, stripped of namespaces. The real answer is implemented by this method.

TF_API std::vector<TfType> TfType::GetDirectlyDerivedTypes ( ) const

Return a vector of types derived directly from this type.

template<class T >
T* TfType::GetFactory ( ) const
inline

Returns the factory object for this type as a T*, or NULL if there is no factory or the factory is not or is not derived from T. Clients can check if a factory is set using GetFactory<TfType::FactoryBase>().

Definition at line 637 of file type.h.

TF_API size_t TfType::GetNBaseTypes ( TfType out,
size_t  maxBases 
) const

Copy the first maxBases base types of this type to out, or all the base types if this type has maxBases or fewer base types. Return this type's number of base types.

Note that it is supported to change a TfType to its first base type by calling this function. For example:

TfType t = ...;
t.GetNBaseTypes(&t, 1);
static TF_API TfType const& TfType::GetRoot ( )
static

Return the root type of the type hierarchy.

All known types derive (directly or indirectly) from the root. If a type is specified with no bases, it is implicitly considered to derive from the root type.

TF_API size_t TfType::GetSizeof ( ) const

Return the size required to hold an instance of this type on the stack (does not include any heap allocated memory the instance uses).

This is what the C++ sizeof operator returns for the type, so this value is not very useful for Python types (it will always be sizeof(hboost::python::object)).

TF_API const std::type_info& TfType::GetTypeid ( ) const

Return a C++ RTTI type_info for this type.

If this type is unknown or has not yet had a C++ type defined, typeid(void) will be returned.

See Also
Define()
TF_API const std::string& TfType::GetTypeName ( ) const

Return the machine-independent name for this type. This name is specified when the TfType is declared.

See Also
Declare()
static TF_API TfType const& TfType::GetUnknownType ( )
static

Return an empty TfType, representing the unknown type.

This is equivalento the default constructor, TfType(). This form exists as a clearer way to express intent in code explicitly dealing with unknown types.

See Also
IsUnknown()
TF_API bool TfType::IsA ( TfType  queryType) const

Return true if this type is the same as or derived from queryType. If queryType is unknown, this always returns false.

template<typename T >
bool TfType::IsA ( ) const
inline

Return true if this type is the same as or derived from T. This is equivalent to:

IsA(Find<T>())

Definition at line 380 of file type.h.

TF_API bool TfType::IsEnumType ( ) const

Return true if this is an enum type.

TF_API bool TfType::IsPlainOldDataType ( ) const

Return true if this is a plain old data type, as defined by C++.

bool TfType::IsRoot ( ) const
inline

Return true if this is the root type.

Definition at line 404 of file type.h.

bool TfType::IsUnknown ( ) const
inline

Return true if this is the unknown type, representing a type unknown to the TfType system.

The unknown type does not derive from the root type, or any other type.

Definition at line 388 of file type.h.

TfType::operator UnspecifiedBoolType ( ) const
inline

Convert to bool – return true if this type is not unknown, false otherwise.

Definition at line 394 of file type.h.

bool TfType::operator! ( void  ) const
inline

Boolean not operator – return true if this type is unknown, false otherwise.

Definition at line 400 of file type.h.

bool TfType::operator!= ( const TfType t) const
inline

Definition at line 133 of file type.h.

bool TfType::operator< ( const TfType t) const
inline

Comparison operator.

Definition at line 136 of file type.h.

bool TfType::operator<= ( const TfType t) const
inline

Definition at line 138 of file type.h.

bool TfType::operator== ( const TfType t) const
inline

Equality operator.

Note
All unknown types (see IsUnknown()) are considered equal. This is so all unknown types will only occupy one key when used in an associative map.

Definition at line 132 of file type.h.

bool TfType::operator> ( const TfType t) const
inline

Definition at line 137 of file type.h.

bool TfType::operator>= ( const TfType t) const
inline

Definition at line 139 of file type.h.

TF_API void TfType::SetFactory ( std::unique_ptr< FactoryBase factory) const

Sets the factory object for this type. A type's factory typically has methods to instantiate the type given various arguments and must inherit from FactoryBase. The factory cannot be changed once set.

template<class T >
void TfType::SetFactory ( std::unique_ptr< T > &  factory) const
inline

Sets the factory object for this type. A type's factory typically has methods to instantiate the type given various arguments and must inherit from FactoryBase. The factory cannot be changed once set.

Definition at line 597 of file type.h.

template<class T >
void TfType::SetFactory ( ) const
inline

Sets the factory object for this type to be a T. The factory cannot be changed once set.

Definition at line 604 of file type.h.

Friends And Related Function Documentation

friend struct _TypeInfo
friend

Definition at line 687 of file type.h.

template<typename TypeVector >
friend struct Tf_AddBases
friend

Definition at line 686 of file type.h.

friend class Tf_TypeRegistry
friend

Definition at line 688 of file type.h.

template<class HashState >
void TfHashAppend ( HashState &  h,
TfType const &  type 
)
friend

Definition at line 693 of file type.h.


The documentation for this class was generated from the following files: