Main Page | Modules | Class Hierarchy | Class List | File List | Class Members | Related Pages

cobject.h

00001 //==========================================================================
00002 //  COBJECT.H - part of
00003 //                     OMNeT++/OMNEST
00004 //            Discrete System Simulation in C++
00005 //
00006 //  Declaration of the following classes:
00007 //    cObject      : general base class
00008 //
00009 //==========================================================================
00010 
00011 /*--------------------------------------------------------------*
00012   Copyright (C) 1992-2005 Andras Varga
00013 
00014   This file is distributed WITHOUT ANY WARRANTY. See the file
00015   `license' for details on this and other legal matters.
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  /* should be 4n+3 */
00032 
00033 
00034 //=== classes declared here
00035 class cObject;
00036 class cStaticFlag;
00037 
00038 //=== classes mentioned
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         // last char: !=0 if p is used; =0 if chars[] is used (then also serves as EOS for 10-long strings)
00146     } nameunion;
00147 
00148     cObject *ownerp;       // owner pointer
00149     int pos;               // used if owner is a cDefaultList
00150 
00151     // list in which objects are accumulated if there's no simple module in context
00152     // (see also setDefaultOwner() and cSimulation::setContextModule())
00153     static cDefaultList *defaultowner;
00154 
00155     // global variables for statistics
00156     static long total_objs;
00157     static long live_objs;
00158 
00159   protected:
00160     static char fullpathbuf[MAX_OBJECTFULLPATH]; // buffer for fullPath()
00161 
00162   private:
00163     // internal
00164     virtual void ownedObjectDeleted(cObject *obj);
00165 
00166     // internal
00167     virtual void yieldOwnership(cObject *obj, cObject *to);
00168 
00169   public:
00170     // internal
00171     virtual void removeFromOwnershipTree();
00172 
00173     // internal
00174     static void setDefaultOwner(cDefaultList *list);
00175 
00176     // called internally by cSimpleModule::snapshot(), and in turn it calls writeContents()
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 // Internal helper class for cObject.
00463 //
00464 class cStaticFlag
00465 {
00466   private:
00467     static bool staticflag;  // set to true while in main()
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 // Note: this function cannot be put into utils.h (circular dependencies)
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 

Generated on Fri Apr 1 12:25:32 2005 for OMNeT++/OMNEST Simulation Library by  doxygen 1.3.9.1