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 friend class cMessage;
00141
00142 union
00143 {
00144 char *p;
00145 char chars[MAX_INTERNAL_NAME+1];
00146
00147 } nameunion;
00148
00149 cObject *ownerp;
00150 int pos;
00151
00152
00153
00154 static cDefaultList *defaultowner;
00155
00156
00157 static long total_objs;
00158 static long live_objs;
00159
00160 protected:
00161 static char fullpathbuf[MAX_OBJECTFULLPATH];
00162
00163 private:
00164
00165 virtual void ownedObjectDeleted(cObject *obj);
00166
00167
00168 virtual void yieldOwnership(cObject *obj, cObject *to);
00169
00170 public:
00171
00172 virtual void removeFromOwnershipTree();
00173
00174
00175 static void setDefaultOwner(cDefaultList *list);
00176
00177
00178 virtual void writeTo(std::ostream& os);
00179
00180 protected:
00188
00196 virtual void take(cObject *obj);
00197
00207 virtual void drop(cObject *obj);
00208
00222 void dropAndDelete(cObject *obj);
00224
00227
00234 void copyNotSupported() const;
00236
00237 public:
00240
00245 cObject(const cObject& obj);
00246
00254 cObject();
00255
00260 explicit cObject(const char *name);
00261
00274 virtual ~cObject();
00275
00282 virtual cPolymorphic *dup() const {return new cObject(*this);}
00283
00294 cObject& operator=(const cObject& o);
00296
00303
00308 virtual void setName(const char *s);
00309
00314 const char *name() const {return nameunion.chars[MAX_INTERNAL_NAME] ? nameunion.p : nameunion.chars;}
00315
00320 bool isName(const char *s) const {return !opp_strcmp(name(),s);}
00321
00327 virtual const char *fullName() const {return name();}
00328
00334 virtual std::string fullPath() const;
00335
00340 virtual const char *fullPath(char *buffer, int buffersize) const;
00342
00345
00349 cObject *owner() const {return ownerp;}
00350
00357 virtual bool isSoftOwner() {return false;}
00358
00364 static cDefaultList *defaultOwner();
00365
00367
00379 virtual void writeContents(std::ostream& os);
00381
00389
00393 virtual void netPack(cCommBuffer *buffer);
00394
00398 virtual void netUnpack(cCommBuffer *buffer);
00400
00403
00412 virtual void forEachChild(cVisitor *v);
00413
00425 cObject *findObject(const char *name, bool deep=true);
00426
00431 static int cmpbyname(cObject *one, cObject *other);
00433
00443 static long totalObjectCount() {return total_objs;}
00444
00451 static long liveObjectCount() {return live_objs;}
00452
00457 static void resetMessageCounters() {total_objs=live_objs=0L;}
00459 };
00460
00461
00462
00463
00464
00465 class cStaticFlag
00466 {
00467 private:
00468 static bool staticflag;
00469 public:
00470 cStaticFlag() {staticflag = true;}
00471 ~cStaticFlag() {staticflag = false;}
00472 static bool isSet() {return staticflag;}
00473 };
00474
00475 std::ostream& operator<< (std::ostream& os, const cObject *p);
00476 std::ostream& operator<< (std::ostream& os, const cObject& o);
00477
00478 inline std::ostream& operator<< (std::ostream& os, cObject *p) {
00479 return os << (const cObject *)p;
00480 }
00481
00482 inline std::ostream& operator<< (std::ostream& os, cObject& o) {
00483 return os << (const cObject&)o;
00484 }
00485
00486
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
00518 template<class T>
00519 const T check_and_cast(const cPolymorphic *p)
00520 {
00521 return check_and_cast<T>(const_cast<cPolymorphic *>(p));
00522 }
00523
00524 #endif
00525