Panda3D
 All Classes Functions Variables Enumerations
typeHandle.h
1 // Filename: typeHandle.h
2 // Created by: drose (23Oct98)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 #ifndef TYPEHANDLE_H
16 #define TYPEHANDLE_H
17 
18 #include "dtoolbase.h"
19 
20 #include <set>
21 
22 // The following illustrates the convention for declaring a type that
23 // uses TypeHandle. In this example, ThisThingie inherits from
24 // TypedObject, which automatically supplies some type-differentiation
25 // functions at the cost of one virtual function, get_type(); however,
26 // this inheritance is optional, and may be omitted to avoid the
27 // virtual function pointer overhead. (If you do use TypedObject, be
28 // sure to consider whether your destructor should also be virtual.)
29 
30 //
31 // class ThatThingie : public SimpleTypedObject {
32 // public:
33 // static TypeHandle get_class_type() {
34 // return _type_handle;
35 // }
36 // static void init_type() {
37 // register_type(_type_handle, "ThatThingie");
38 // }
39 //
40 // private:
41 // static TypeHandle _type_handle;
42 // };
43 //
44 // class ThisThingie : public ThatThingie, publid TypedObject {
45 // public:
46 // static TypeHandle get_class_type() {
47 // return _type_handle;
48 // }
49 // static void init_type() {
50 // ThatThingie::init_type();
51 // TypedObject::init_type();
52 // register_type(_type_handle, "ThisThingie",
53 // ThatThingie::get_class_type(),
54 // TypedObject::get_class_type());
55 // }
56 // virtual TypeHandle get_type() const {
57 // return get_class_type();
58 // }
59 //
60 // private:
61 // static TypeHandle _type_handle;
62 // };
63 //
64 
65 class TypedObject;
66 
67 ////////////////////////////////////////////////////////////////////
68 // Class : TypeHandle
69 // Description : TypeHandle is the identifier used to differentiate
70 // C++ class types. Any C++ classes that inherit from
71 // some base class, and must be differentiated at run
72 // time, should store a static TypeHandle object that
73 // can be queried through a static member function
74 // named get_class_type(). Most of the time, it is also
75 // desirable to inherit from TypedObject, which provides
76 // some virtual functions to return the TypeHandle for a
77 // particular instance.
78 //
79 // At its essence, a TypeHandle is simply a unique
80 // identifier that is assigned by the TypeRegistry. The
81 // TypeRegistry stores a tree of TypeHandles, so that
82 // ancestry of a particular type may be queried, and the
83 // type name may be retrieved for run-time display.
84 ////////////////////////////////////////////////////////////////////
85 class EXPCL_DTOOL TypeHandle {
86 PUBLISHED:
87  enum MemoryClass {
88  MC_singleton,
89  MC_array,
90  MC_deleted_chain_active,
91  MC_deleted_chain_inactive,
92 
93  MC_limit // Not a real value, just a placeholder for the maximum
94  // enum value.
95  };
96 
97  INLINE TypeHandle();
98  INLINE TypeHandle(const TypeHandle &copy);
99 
100  EXTENSION(static TypeHandle make(PyTypeObject *classobj));
101 
102  INLINE bool operator == (const TypeHandle &other) const;
103  INLINE bool operator != (const TypeHandle &other) const;
104  INLINE bool operator < (const TypeHandle &other) const;
105  INLINE bool operator <= (const TypeHandle &other) const;
106  INLINE bool operator > (const TypeHandle &other) const;
107  INLINE bool operator >= (const TypeHandle &other) const;
108  INLINE int compare_to(const TypeHandle &other) const;
109  INLINE size_t get_hash() const;
110 
111  INLINE string get_name(TypedObject *object = (TypedObject *)NULL) const;
112  INLINE bool is_derived_from(TypeHandle parent,
113  TypedObject *object = (TypedObject *)NULL) const;
114 
115  INLINE int get_num_parent_classes(TypedObject *object = (TypedObject *)NULL) const;
116  INLINE TypeHandle get_parent_class(int index) const;
117 
118  INLINE int get_num_child_classes(TypedObject *object = (TypedObject *)NULL) const;
119  INLINE TypeHandle get_child_class(int index) const;
120 
121  INLINE TypeHandle get_parent_towards(TypeHandle ancestor,
122  TypedObject *object = (TypedObject *)NULL) const;
123 
124  INLINE int get_best_parent_from_Set(const std::set< int > &legal_vals) const;
125 
126 #ifdef DO_MEMORY_USAGE
127  int get_memory_usage(MemoryClass memory_class) const;
128  void inc_memory_usage(MemoryClass memory_class, int size);
129  void dec_memory_usage(MemoryClass memory_class, int size);
130 #else
131  static CONSTEXPR int get_memory_usage(MemoryClass) { return 0; }
132  INLINE void inc_memory_usage(MemoryClass, int) { }
133  INLINE void dec_memory_usage(MemoryClass, int) { }
134 #endif // DO_MEMORY_USAGE
135 
136  INLINE int get_index() const;
137  INLINE void output(ostream &out) const;
138  INLINE static TypeHandle none();
139  INLINE operator bool () const;
140 
141 private:
142  int _index;
143  static TypeHandle _none;
144 
145  friend class TypeRegistry;
146 };
147 
148 
149 // It's handy to be able to output a TypeHandle directly, and see the
150 // type name.
151 INLINE ostream &operator << (ostream &out, TypeHandle type) {
152  type.output(out);
153  return out;
154 }
155 
156 EXPCL_DTOOL ostream &operator << (ostream &out, TypeHandle::MemoryClass mem_class);
157 
158 // We must include typeRegistry at this point so we can call it from
159 // our inline functions. This is a circular include that is
160 // strategically placed to do no harm.
161 /* okcircular */
162 #include "typeRegistry.h"
163 
164 #include "typeHandle.I"
165 
166 #endif
This is an abstract class that all classes which use TypeHandle, and also provide virtual functions t...
Definition: typedObject.h:98
An STL function object class, this is intended to be used on any ordered collection of class objects ...
Definition: stl_compares.h:79
The TypeRegistry class maintains all the assigned TypeHandles in a given system.
Definition: typeRegistry.h:39
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:85