Panda3D
 All Classes Functions Variables Enumerations
typedObject.h
1 // Filename: typedObject.h
2 // Created by: drose (11May01)
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 TYPEDOBJECT_H
16 #define TYPEDOBJECT_H
17 
18 #include "dtoolbase.h"
19 
20 #include "typeHandle.h"
21 #include "register_type.h"
22 #include "memoryBase.h"
23 
24 #include <set>
25 
26 ////////////////////////////////////////////////////////////////////
27 // Class : TypedObject
28 // Description : This is an abstract class that all classes which
29 // use TypeHandle, and also provide virtual functions to
30 // support polymorphism, should inherit from. Each
31 // derived class should define get_type(), which should
32 // return the specific type of the derived class.
33 // Inheriting from this automatically provides support
34 // for is_of_type() and is_exact_type().
35 //
36 // All classes that inherit directly or indirectly from
37 // TypedObject should redefine get_type() and
38 // force_init_type(), as shown below. Some classes that
39 // do not inherit from TypedObject may still declare
40 // TypeHandles for themselves by defining methods called
41 // get_class_type() and init_type(). Classes such as
42 // these may serve as base classes, but the dynamic type
43 // identification system will be limited. Classes that
44 // do not inherit from TypedObject need not define the
45 // virtual functions get_type() and force_init_type()
46 // (or any other virtual functions).
47 //
48 // There is a specific layout for defining the
49 // overrides from this class. Keeping the definitions
50 // formatted just like these examples will allow
51 // someone in the future to use a sed (or similar)
52 // script to make global changes, if necessary. Avoid
53 // rearranging the braces or the order of the functions
54 // unless you're ready to change them in every file all
55 // at once.
56 //
57 // What follows are some examples that can be used in
58 // new classes that you create.
59 //
60 // @par In the class definition (.h file):
61 // @code
62 // public:
63 // static TypeHandle get_class_type() {
64 // return _type_handle;
65 // }
66 // static void init_type() {
67 // <<<BaseClassOne>>>::init_type();
68 // <<<BaseClassTwo>>>::init_type();
69 // <<<BaseClassN>>>::init_type();
70 // register_type(_type_handle, "<<<ThisClassStringName>>>",
71 // <<<BaseClassOne>>>::get_class_type(),
72 // <<<BaseClassTwo>>>::get_class_type(),
73 // <<<BaseClassN>>>::get_class_type());
74 // }
75 // virtual TypeHandle get_type() const {
76 // return get_class_type();
77 // }
78 // virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
79 //
80 // private:
81 // static TypeHandle _type_handle;
82 // @endcode
83 //
84 // @par In the class .cxx file:
85 // @code
86 // TypeHandle <<<ThisClassStringName>>>::_type_handle;
87 // @endcode
88 //
89 // @par In the class config_<<<PackageName>>>.cxx file:
90 // @code
91 // ConfigureFn(config_<<<PackageName>>>) {
92 // <<<ClassOne>>>::init_type();
93 // <<<ClassTwo>>>::init_type();
94 // <<<ClassN>>>::init_type();
95 // }
96 // @endcode
97 ////////////////////////////////////////////////////////////////////
98 class EXPCL_DTOOL TypedObject : public MemoryBase {
99 public:
100  INLINE TypedObject();
101  INLINE TypedObject(const TypedObject &copy);
102  INLINE void operator = (const TypedObject &copy);
103 
104 PUBLISHED:
105  // A virtual destructor is just a good idea.
106  virtual ~TypedObject();
107 
108  // Derived classes should override this function to return
109  // get_class_type().
110  virtual TypeHandle get_type() const=0;
111 
112  INLINE int get_type_index() const;
113  INLINE bool is_of_type(TypeHandle handle) const;
114  INLINE bool is_exact_type(TypeHandle handle) const;
115 
116 public:
117  INLINE int get_best_parent_from_Set(const std::set<int> &) const;
118 
119  // Derived classes should override this function to call
120  // init_type(). It will only be called in error situations when the
121  // type was for some reason not properly initialized.
122  virtual TypeHandle force_init_type()=0;
123 
124  // This pair of methods exists mainly for the convenience of
125  // unambiguous upcasting. Interrogate generates code to call this
126  // method instead of making an explicit cast to (TypedObject *);
127  // this allows classes who multiply inherit from TypedObject to
128  // override these methods and disambiguate the cast. It doesn't
129  // have to be a virtual method, since this is just a static upcast.
130  INLINE TypedObject *as_typed_object();
131  INLINE const TypedObject *as_typed_object() const;
132 
133 public:
134  static TypeHandle get_class_type() {
135  return _type_handle;
136  }
137  static void init_type();
138 
139 private:
140  static TypeHandle _type_handle;
141 };
142 
143 #include "typedObject.I"
144 
145 #endif
This is an abstract class that all classes which use TypeHandle, and also provide virtual functions t...
Definition: typedObject.h:98
This class is intended to be the base class of all objects in Panda that might be allocated and delet...
Definition: memoryBase.h:73
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:85