00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #ifndef __COBJECT_H
00019 #define __COBJECT_H
00020
00021 #include <typeinfo>
00022 #include <iostream>
00023 #include "cenvir.h"
00024 #include "defs.h"
00025 #include "util.h"
00026 #include "cpolymorphic.h"
00027 #include "cexception.h"
00028 #include "cvisitor.h"
00029
00030
00031 #define MAX_INTERNAL_NAME 11
00032
00033
00034
00035 class cObject;
00036 class cStaticFlag;
00037
00038
00039 class cCommBuffer;
00040 class cArray;
00041 class cDefaultList;
00042
00043
00053 typedef int (*CompareFunc)(cObject *a, cObject *b);
00054
00055
00135 class SIM_API cObject : public cPolymorphic
00136 {
00137 private:
00138 friend class cDefaultList;
00139 friend class cSimulation;
00140
00141 union
00142 {
00143 char *p;
00144 char chars[MAX_INTERNAL_NAME+1];
00145
00146 } nameunion;
00147
00148 cObject *ownerp;
00149 int pos;
00150
00151
00152
00153 static cDefaultList *defaultowner;
00154
00155
00156 static long total_objs;
00157 static long live_objs;
00158
00159 protected:
00160 static char fullpathbuf[MAX_OBJECTFULLPATH];
00161
00162 private:
00163
00164 virtual void ownedObjectDeleted(cObject *obj);
00165
00166
00167 virtual void yieldOwnership(cObject *obj, cObject *to);
00168
00169 public:
00170
00171 virtual void removeFromOwnershipTree();
00172
00173
00174 static void setDefaultOwner(cDefaultList *list);
00175
00176
00177 virtual void writeTo(std::ostream& os);
00178
00179 protected:
00187
00195 virtual void take(cObject *obj);
00196
00206 virtual void drop(cObject *obj);
00207
00221 void dropAndDelete(cObject *obj);
00223
00226
00233 void copyNotSupported() const;
00235
00236 public:
00239
00244 cObject(const cObject& obj);
00245
00253 cObject();
00254
00259 explicit cObject(const char *name);
00260
00273 virtual ~cObject();
00274
00281 virtual cObject *dup() const {return new cObject(*this);}
00282
00293 cObject& operator=(const cObject& o);
00295
00302
00307 virtual void setName(const char *s);
00308
00313 const char *name() const {return nameunion.chars[MAX_INTERNAL_NAME] ? nameunion.p : nameunion.chars;}
00314
00319 bool isName(const char *s) const {return !opp_strcmp(name(),s);}
00320
00326 virtual const char *fullName() const {return name();}
00327
00333 virtual std::string fullPath() const;
00334
00339 virtual const char *fullPath(char *buffer, int buffersize) const;
00341
00344
00348 cObject *owner() const {return ownerp;}
00349
00356 virtual bool isSoftOwner() {return false;}
00357
00363 static cDefaultList *defaultOwner();
00364
00366
00378 virtual void writeContents(std::ostream& os);
00380
00388
00392 virtual void netPack(cCommBuffer *buffer);
00393
00397 virtual void netUnpack(cCommBuffer *buffer);
00399
00402
00411 virtual void forEachChild(cVisitor *v);
00412
00424 cObject *findObject(const char *name, bool deep=true);
00425
00430 static int cmpbyname(cObject *one, cObject *other);
00432
00442 static long totalObjectCount() {return total_objs;}
00443
00450 static long liveObjectCount() {return live_objs;}
00451
00456 static void resetMessageCounters() {total_objs=live_objs=0L;}
00458 };
00459
00460
00461
00462
00463
00464 class cStaticFlag
00465 {
00466 private:
00467 static bool staticflag;
00468 public:
00469 cStaticFlag() {staticflag = true;}
00470 ~cStaticFlag() {staticflag = false;}
00471 static bool isSet() {return staticflag;}
00472 };
00473
00474 std::ostream& operator<< (std::ostream& os, const cObject *p);
00475 std::ostream& operator<< (std::ostream& os, const cObject& o);
00476
00477 inline std::ostream& operator<< (std::ostream& os, cObject *p) {
00478 return os << (const cObject *)p;
00479 }
00480
00481 inline std::ostream& operator<< (std::ostream& os, cObject& o) {
00482 return os << (const cObject&)o;
00483 }
00484
00485
00503
00504 template<class T>
00505 T check_and_cast(cPolymorphic *p)
00506 {
00507 if (!p)
00508 throw new cRuntimeError("check_and_cast(): cannot cast NULL pointer to type '%s'",opp_typename(typeid(T)));
00509 T ret = dynamic_cast<T>(p);
00510 if (!ret)
00511 throw new cRuntimeError("check_and_cast(): cannot cast (%s *)%s to type '%s'",p->className(),p->fullPath().c_str(),opp_typename(typeid(T)));
00512 return ret;
00513 }
00514
00515 #endif
00516