Main Page   Modules   Class Hierarchy   Compound List   File List   Compound Members  

cobject.h

00001 //==========================================================================
00002 //   COBJECT.H - header for
00003 //                             OMNeT++
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-2003 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 "util.h"
00024 #include "cexception.h"
00025 
00026 using std::ostream;
00027 
00028 
00029 #define FULLPATHBUF_SIZE  1024
00030 
00031 //=== classes declared here
00032 class  cObject;
00033 class  cStaticFlag;
00034 
00035 //=== classes mentioned
00036 class  cIterator;
00037 class  cHead;
00038 
00039 //=== Global objects:
00040 SIM_API extern cHead networks;            
00041 SIM_API extern cHead modinterfaces;       
00042 SIM_API extern cHead modtypes;            
00043 SIM_API extern cHead linktypes;           
00044 SIM_API extern cHead functions;           
00045 SIM_API extern cHead classes;             
00046 SIM_API extern cHead enums;               
00047 
00048 
00058 typedef int (*CompareFunc)(cObject *a, cObject *b);
00059 
00065 typedef bool (*ForeachFunc)(cObject *,bool);
00066 
00067 
00110 /* comment to integrate:
00111    What cObject does:
00112       - owner of a new object can be explicitly given, if omitted,
00113         defaultOwner() will will be used.
00114       - an object created thru the copy constructor:
00115           - will have the same owner as original;
00116           - does not dup() or take objects owned by the original.
00117       - destructor calls discard() for owned objects (see later).
00118    Objects contained as data members:
00119       the enclosing object should own them.
00120    What container objects derived from cObject should do:
00121       - they use the functions: take(obj), drop(obj), discard(obj)
00122       - when an object is inserted, if takeOwnership() is true, should
00123         take ownership of object by calling take(obj).
00124         TAKEOWNERSHIP() DEFAULTS TO true.
00125       - when an object is removed, they should call drop(obj) for it if
00126         they were the owner.
00127       - copy constructor copies should dup() and take ownership of objects
00128         that were owned by the original.
00129       - destructor doesn't need not call discard() for objects: this will be
00130         done in cObject's destructor.
00131 */
00132 class SIM_API cObject
00133 {
00134     friend class cHead;
00135     friend class cIterator;
00136     friend class const_cIterator;
00137     friend class cStaticFlag;
00138 
00139   protected:
00140     char *namestr;                    // name string
00141     char stor;                        // storage: Static/Auto/Dynamic ('S'/'A'/'D')
00142 
00143     bool tkownership;                 // for derived containers: take ownership of objects?
00144     cObject *ownerp, *prevp, *nextp;  // ptr to owner; linked list ptrs
00145     cObject *firstchildp;             // list of owned objects
00146 
00147     static int staticflag;            // to determine 'storage' (1 while in main())
00148     static int heapflag;              // to determine 'storage' (1 immediately after 'new')
00149     static char fullpathbuf[FULLPATHBUF_SIZE]; // buffer for fullPath()
00150 
00151   protected:
00159 
00170     void take(cObject *object)
00171         {object->setOwner( this );}
00172 
00185     void drop(cObject *object)
00186         {object->setOwner( object->defaultOwner() );}
00187 
00204     void discard(cObject *object)
00205         {if(object->storage()=='D') delete object; else object->setOwner(NULL);}
00207 
00210 
00217     void copyNotSupported() const;
00219 
00220   public:
00223 
00228     cObject(const cObject& obj);
00229 
00246     cObject();
00247 
00252     explicit cObject(const char *name);
00253 
00263     virtual ~cObject();
00264 
00271     virtual cObject *dup() const    {return new cObject(*this);}
00272 
00278     void destruct() {this->~cObject();}
00279 
00290     cObject& operator=(const cObject& o);
00292 
00299 
00304     void setName(const char *s)  {delete namestr; namestr=opp_strdup(s);}
00305 
00310     const char *name() const     {return namestr ? namestr : "";}
00311 
00316     bool isName(const char *s) const {return !opp_strcmp(namestr,s);}
00317 
00323     virtual const char *fullName() const
00324         {return name();}
00325 
00331     virtual const char *fullPath() const;
00332 
00337     virtual const char *fullPath(char *buffer, int buffersize) const;
00339 
00342 
00346     cObject *owner() const {return ownerp;}
00347 
00354     void setOwner(cObject *newowner);
00355 
00360     virtual cObject *defaultOwner() const;
00362 
00370 
00376     void takeOwnership(bool tk) {tkownership=tk;}
00377 
00383     bool takeOwnership() const   {return tkownership;}
00385 
00388 
00394     virtual const char *className() const;
00395 
00401     virtual void info(char *buf);
00402 
00409     virtual void writeTo(ostream& os);
00410 
00418     virtual void writeContents(ostream& os);
00420 
00429 
00434     virtual int netPack();
00435 
00440     virtual int netUnpack();
00442 
00445 
00484     virtual void forEach(ForeachFunc f);
00485 
00492     char storage() const         {return stor;}
00493 
00501     void *operator new(size_t m);
00502 
00512     cObject *findObject(const char *name, bool deep=true);
00513 
00518     static int cmpbyname(cObject *one, cObject *other);
00520 };
00521 
00522 
00523 //
00524 // Internal helper class for cObject.
00525 //
00526 class cStaticFlag
00527 {
00528   public:
00529     cStaticFlag()  {cObject::staticflag = 1;}
00530     ~cStaticFlag() {cObject::staticflag = 0;}
00531     static bool isSet() {return cObject::staticflag;}
00532 };
00533 
00534 
00552 template<class T>
00553 T check_and_cast(cObject *p)
00554 {
00555     if (!p)
00556         throw new cException("check_and_cast(): cannot cast NULL pointer to type '%s'",opp_typename(typeid(T)));
00557     T ret = dynamic_cast<T>(p);
00558     if (!ret)
00559         throw new cException("check_and_cast(): cannot cast (%s *)%s to type '%s'",p->className(),p->fullPath(),opp_typename(typeid(T)));
00560     return ret;
00561 }
00562 
00563 #endif
00564 

Generated at Mon Jun 16 23:37:31 2003 for OMNeT++ by doxygen1.2.8.1 written by Dimitri van Heesch, © 1997-2001