TypeHandle

class TypeHandle

TypeHandle is the identifier used to differentiate C++ class types. Any C++ classes that inherit from some base class, and must be differentiated at run time, should store a static TypeHandle object that can be queried through a static member function named get_class_type(). Most of the time, it is also desirable to inherit from TypedObject, which provides some virtual functions to return the TypeHandle for a particular instance.

At its essence, a TypeHandle is simply a unique identifier that is assigned by the TypeRegistry. The TypeRegistry stores a tree of TypeHandles, so that ancestry of a particular type may be queried, and the type name may be retrieved for run-time display.

Inheritance diagram

Inheritance diagram of TypeHandle

enum MemoryClass
enumerator MC_singleton = 0
enumerator MC_array = 1
enumerator MC_deleted_chain_active = 2
enumerator MC_deleted_chain_inactive = 3
enumerator MC_limit = 4

Not a real value, just a placeholder for the maximum enum value.

TypeHandle(void) noexcept = default
TypeHandle(TypeHandle const&) = default

Private constructor for initializing a TypeHandle from an index, used by none() and by from_index().

int compare_to(TypeHandle const &other) const

Sorts TypeHandles arbitrarily (according to <, >, etc.). Returns a number less than 0 if this type sorts before the other one, greater than zero if it sorts after, 0 if they are equivalent.

void dec_memory_usage(TypeHandle::MemoryClass memory_class, std::size_t size)

Subtracts the indicated amount from the record for the total allocated memory for objects of this type.

int get_best_parent_from_Set(set<int> const &legal_vals) const

Return the Index of the BEst fit Classs from a set

TypeHandle get_child_class(int index) const

Returns the nth child class of this type. The index should be in the range 0 <= index < get_num_child_classes().

std::size_t get_hash(void) const

Returns a hash code suitable for phash_map.

int get_index(void) const

Returns the integer index associated with this TypeHandle. Each different TypeHandle will have a different index. However, you probably shouldn’t be using this method; you should just treat the TypeHandles as opaque classes. This is provided for the convenience of non-C++ scripting languages to build a hashtable of TypeHandles.

std::size_t get_memory_usage(TypeHandle::MemoryClass memory_class) const

Returns the total allocated memory used by objects of this type, for the indicated memory class. This is only updated if track-memory-usage is set true in your Config.prc file.

std::string get_name(TypedObject *object = nullptr) const

Returns the name of the type.

The “object” pointer is an optional pointer to the TypedObject class that owns this TypeHandle. It is only used in case the TypeHandle is inadvertantly undefined.

int get_num_child_classes(TypedObject *object = nullptr) const

Returns the number of child classes that this type is known to have. This may then be used to index into get_child_class().

The “object” pointer is an optional pointer to the TypedObject class that owns this TypeHandle. It is only used in case the TypeHandle is inadvertantly undefined.

int get_num_parent_classes(TypedObject *object = nullptr) const

Returns the number of parent classes that this type is known to have. This may then be used to index into get_parent_class(). The result will be 0 if this class does not inherit from any other classes, 1 if normal, single inheritance is in effect, or greater than one if multiple inheritance is in effect.

The “object” pointer is an optional pointer to the TypedObject class that owns this TypeHandle. It is only used in case the TypeHandle is inadvertantly undefined.

TypeHandle get_parent_class(int index) const

Returns the nth parent class of this type. The index should be in the range 0 <= index < get_num_parent_classes().

TypeHandle get_parent_towards(TypeHandle ancestor, TypedObject *object = nullptr) const

Returns the parent class that is in a direct line of inheritance to the indicated ancestor class. This is useful in the presence of multiple inheritance to try to determine what properties an unknown type may have.

The return value is TypeHandle::none() if the type does not inherit from the ancestor. If ancestor is the same as this type, the return value is this type.

The “object” pointer is an optional pointer to the TypedObject class that owns this TypeHandle. It is only used in case the TypeHandle is inadvertantly undefined.

void inc_memory_usage(TypeHandle::MemoryClass memory_class, std::size_t size)

Adds the indicated amount to the record for the total allocated memory for objects of this type.

bool is_derived_from(TypeHandle parent, TypedObject *object = nullptr) const

Returns true if this type is derived from the indicated type, false otherwise.

The “object” pointer is an optional pointer to the TypedObject class that owns this TypeHandle. It is only used in case the TypeHandle is inadvertantly undefined.

static TypeHandle make(PyTypeObject *classobj)
static constexpr TypeHandle none(void)
void output(std::ostream &out) const