00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "typedObject.h"
00020 #include "pointerTo.h"
00021 #include "pointerToArray.h"
00022 #include "referenceCount.h"
00023 #include "dcast.h"
00024
00025 #include "notify.h"
00026
00027 class ThatThingie : public TypedObject, public ReferenceCount {
00028 public:
00029 ThatThingie(const string &name) : _name(name) {
00030 nout << "Constructing ThatThingie " << _name << "\n";
00031 }
00032 virtual ~ThatThingie() {
00033 nout << "Destructing ThatThingie " << _name << "\n";
00034 }
00035 static TypeHandle get_class_type() {
00036 return _type_handle;
00037 }
00038 static void init_type() {
00039 TypedObject::init_type();
00040 ReferenceCount::init_type();
00041 register_type(_type_handle, "ThatThingie",
00042 TypedObject::get_class_type(),
00043 ReferenceCount::get_class_type());
00044 }
00045 virtual TypeHandle get_type() const {
00046 return get_class_type();
00047 }
00048 virtual TypeHandle force_init_type(void) {
00049 init_type();
00050 return get_class_type();
00051 }
00052
00053 string _name;
00054
00055 private:
00056 static TypeHandle _type_handle;
00057 };
00058
00059 class ThisThingie : public ThatThingie {
00060 public:
00061 ThisThingie(const string &name) : ThatThingie(name) {
00062 nout << "Constructing ThisThingie " << _name << "\n";
00063 }
00064 virtual ~ThisThingie() {
00065 nout << "Destructing ThisThingie " << _name << "\n";
00066 }
00067 static TypeHandle get_class_type() {
00068 return _type_handle;
00069 }
00070 static void init_type() {
00071 ThatThingie::init_type();
00072 register_type(_type_handle, "ThisThingie", ThatThingie::get_class_type());
00073 }
00074 virtual TypeHandle get_type() const {
00075 return get_class_type();
00076 }
00077 virtual TypeHandle force_init_type(void) {
00078 init_type();
00079 return get_class_type();
00080 }
00081
00082 void const_hello() const {
00083 nout << "Const hello from " << _name << "\n";
00084 }
00085
00086 private:
00087 static TypeHandle _type_handle;
00088 };
00089
00090 class TheOtherThingie {
00091 public:
00092 TheOtherThingie(const string &name) : _name(name) {
00093 nout << "Constructing TheOtherThingie " << _name << "\n";
00094 }
00095 virtual ~TheOtherThingie() {
00096 nout << "Destructing TheOtherThingie " << _name << "\n";
00097 }
00098 static TypeHandle get_class_type() {
00099 return _type_handle;
00100 }
00101 static void init_type() {
00102 register_type(_type_handle, "TheOtherThingie");
00103 }
00104 virtual TypeHandle force_init_type(void) {
00105 init_type();
00106 return get_class_type();
00107 }
00108
00109 void hello() {
00110 nout << "Hello from " << _name << "\n";
00111 }
00112
00113 string _name;
00114
00115 private:
00116 static TypeHandle _type_handle;
00117 };
00118
00119 class WhatAThingie : public ThatThingie, public TheOtherThingie {
00120 public:
00121 WhatAThingie(const string &name) : ThatThingie(name), TheOtherThingie(name) {
00122 nout << "Constructing WhatAThingie " << ThatThingie::_name << "\n";
00123 }
00124 virtual ~WhatAThingie() {
00125 nout << "Destructing WhatAThingie " << ThatThingie::_name << "\n";
00126 }
00127 static TypeHandle get_class_type() {
00128 return _type_handle;
00129 }
00130 static void init_type() {
00131 ThatThingie::init_type();
00132 TheOtherThingie::init_type();
00133 register_type(_type_handle, "WhatAThingie",
00134 ThatThingie::get_class_type(),
00135 TheOtherThingie::get_class_type());
00136 }
00137 virtual TypeHandle get_type() const {
00138 return get_class_type();
00139 }
00140 virtual TypeHandle force_init_type(void) {
00141 init_type();
00142 return get_class_type();
00143 }
00144
00145 private:
00146 static TypeHandle _type_handle;
00147 };
00148
00149 TypeHandle ThatThingie::_type_handle;
00150 TypeHandle ThisThingie::_type_handle;
00151 TypeHandle TheOtherThingie::_type_handle;
00152 TypeHandle WhatAThingie::_type_handle;
00153
00154 void show_derivation(TypeHandle type) {
00155 nout << type << " derives from:";
00156 for (int i = 0; i < type.get_num_parent_classes(); i++) {
00157 nout << " " << type.get_parent_class(i);
00158 }
00159 nout << "\n";
00160 }
00161
00162 int
00163 main() {
00164
00165
00166 ThatThingie::init_type();
00167 ThisThingie::init_type();
00168 TheOtherThingie::init_type();
00169 WhatAThingie::init_type();
00170
00171 PointerTo<ThatThingie> thing_1 = new ThisThingie("thing_1");
00172 ConstPointerTo<ThatThingie> thing_2 = new ThatThingie("thing_2");
00173 PointerTo<ThatThingie> thing_3 = new WhatAThingie("thing_3");
00174
00175 nout << "\nthing_1 of type " << thing_1->get_type()
00176 << "\nthing_2 of type " << thing_2->get_type()
00177 << "\nthing_3 of type " << thing_3->get_type()
00178
00179 << "\n\nthing_1 is ThisThingie : "
00180 << thing_1->is_of_type(ThisThingie::get_class_type())
00181 << "\nthing_1 is ThatThingie : "
00182 << thing_1->is_of_type(ThatThingie::get_class_type())
00183 << "\nthing_1 is TheOtherThingie : "
00184 << thing_1->is_of_type(TheOtherThingie::get_class_type())
00185 << "\nthing_1 is WhatAThingie : "
00186 << thing_1->is_of_type(WhatAThingie::get_class_type())
00187
00188 << "\n\nthing_2 is ThisThingie : "
00189 << thing_2->is_of_type(ThisThingie::get_class_type())
00190 << "\nthing_2 is ThatThingie : "
00191 << thing_2->is_of_type(ThatThingie::get_class_type())
00192 << "\nthing_2 is TheOtherThingie : "
00193 << thing_2->is_of_type(TheOtherThingie::get_class_type())
00194 << "\nthing_2 is WhatAThingie : "
00195 << thing_2->is_of_type(WhatAThingie::get_class_type())
00196
00197 << "\n\nthing_3 is ThisThingie : "
00198 << thing_3->is_of_type(ThisThingie::get_class_type())
00199 << "\nthing_3 is ThatThingie : "
00200 << thing_3->is_of_type(ThatThingie::get_class_type())
00201 << "\nthing_3 is TheOtherThingie : "
00202 << thing_3->is_of_type(TheOtherThingie::get_class_type())
00203 << "\nthing_3 is WhatAThingie : "
00204 << thing_3->is_of_type(WhatAThingie::get_class_type())
00205 << "\n\n";
00206
00207 show_derivation(ThatThingie::get_class_type());
00208 show_derivation(ThisThingie::get_class_type());
00209 show_derivation(TheOtherThingie::get_class_type());
00210 show_derivation(WhatAThingie::get_class_type());
00211
00212 nout << "WhatAThingie::get_parent_towards(ReferenceCount) = "
00213 << WhatAThingie::get_class_type().get_parent_towards
00214 (ReferenceCount::get_class_type()) << "\n"
00215 << "WhatAThingie::get_parent_towards(ThatThingie) = "
00216 << WhatAThingie::get_class_type().get_parent_towards
00217 (ThatThingie::get_class_type()) << "\n"
00218 << "WhatAThingie::get_parent_towards(ThisThingie) = "
00219 << WhatAThingie::get_class_type().get_parent_towards
00220 (ThisThingie::get_class_type()) << "\n"
00221 << "WhatAThingie::get_parent_towards(TheOtherThingie) = "
00222 << WhatAThingie::get_class_type().get_parent_towards
00223 (TheOtherThingie::get_class_type()) << "\n"
00224 << "WhatAThingie::get_parent_towards(WhatAThingie) = "
00225 << WhatAThingie::get_class_type().get_parent_towards
00226 (WhatAThingie::get_class_type()) << "\n"
00227 << "\n";
00228
00229 {
00230 ConstPointerTo<ThatThingie> dup_thing_1 = thing_1;
00231 nout << "thing_1 is " << thing_1 << " and dup_thing_1 is "
00232 << dup_thing_1 << "\n";
00233 }
00234 nout << "thing_1 is now " << thing_1 << "\n";
00235
00236 nout << "\n";
00237
00238 {
00239 ConstPointerTo<ThatThingie> new_thing = new ThisThingie("new_thing");
00240 ((const ThisThingie *)new_thing.p())->const_hello();
00241 }
00242
00243 nout << "\n";
00244
00245 ((ThisThingie *)thing_1.p())->const_hello();
00246 ((WhatAThingie *)thing_3.p())->hello();
00247
00248 nout << "\n";
00249
00250
00251
00252 RefCountProxy<int> x = 10;
00253 x++;
00254 RefCountProxy<int> y;
00255 y = x;
00256 nout << "y is " << y << ", y's type is " << y.get_class_type() << "\n";
00257
00258
00259
00260
00261 PointerToArray<int> iarray(10);
00262 memset(iarray, 0, sizeof(int)*10);
00263
00264 for (int i = 0; i < 10; i++) {
00265 iarray[i] = i;
00266 }
00267
00268 ConstPointerToArray<int> jarray = iarray;
00269
00270
00271
00272 nout << "jarray[4] is " << jarray[4] << "\n";
00273
00274 cerr << "dcast thing_1: " << (void *)thing_1 << "\n";
00275 ThisThingie *tt1 = DCAST(ThisThingie, thing_1);
00276 cerr << "gives " << (void *)tt1 << "\n";
00277
00278 cerr << "dcast thing_2: " << (const void *)thing_2 << "\n";
00279 const ThisThingie *tt2 = DCAST(ThisThingie, thing_2);
00280 cerr << "gives " << (const void *)tt2 << "\n";
00281
00282 return 0;
00283 }