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