cownedobject.h

00001 //==========================================================================
00002 //  COWNEDOBJECT.H - part of
00003 //                     OMNeT++/OMNEST
00004 //            Discrete System Simulation in C++
00005 //
00006 //  Author: Andras Varga
00007 //
00008 //==========================================================================
00009 
00010 /*--------------------------------------------------------------*
00011   Copyright (C) 1992-2008 Andras Varga
00012   Copyright (C) 2006-2008 OpenSim Ltd.
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 __COWNEDOBJECT_H
00019 #define __COWNEDOBJECT_H
00020 
00021 #include <typeinfo>
00022 #include <iostream>
00023 #include "simkerneldefs.h"
00024 #include "simutil.h"
00025 #include "cnamedobject.h"
00026 #include "cexception.h"
00027 
00028 NAMESPACE_BEGIN
00029 
00030 
00031 class cOwnedObject;
00032 class cStaticFlag;
00033 class cSimulation;
00034 class cDefaultList;
00035 class cMessage;
00036 class cPacket;
00037 
00038 
00108 class SIM_API cOwnedObject : public cNamedObject
00109 {
00110     friend class cObject;
00111     friend class cDefaultList;
00112     friend class cSimulation;
00113     friend class cMessage;  // because of refcounting business
00114     friend class cPacket;   // because of refcounting business
00115 
00116   private:
00117     cObject *ownerp;   // owner pointer
00118     unsigned int pos;  // used only when owner is a cDefaultList
00119 
00120   private:
00121     // list in which objects are accumulated if there is no simple module in context
00122     // (see also setDefaultOwner() and cSimulation::setContextModule())
00123     static cDefaultList *defaultowner;
00124 
00125     // global variables for statistics
00126     static long total_objs;
00127     static long live_objs;
00128 
00129   private:
00130     void copy(const cOwnedObject& obj);
00131 
00132   public:
00133     // internal
00134     virtual void removeFromOwnershipTree();
00135 
00136     // internal
00137     static void setDefaultOwner(cDefaultList *list);
00138 
00139   public:
00146     cOwnedObject();
00147 
00152     explicit cOwnedObject(const char *name, bool namepooling=true);
00153 
00157     cOwnedObject(const cOwnedObject& obj);
00158 
00162     virtual ~cOwnedObject();
00163 
00174     cOwnedObject& operator=(const cOwnedObject& o);
00175 
00176     // Note: dup() is still the original cObject one, which throws an error
00177     // to indicate that dup() has to be redefined in each subclass.
00178 
00182     virtual void parsimPack(cCommBuffer *buffer);
00183 
00187     virtual void parsimUnpack(cCommBuffer *buffer);
00189 
00195     virtual cObject *getOwner() const {return ownerp;}
00196 
00200     virtual bool isOwnedObject() const {return true;}
00201 
00208     virtual bool isSoftOwner() const {return false;}
00209 
00215     static cDefaultList *getDefaultOwner();
00217 
00227     static long getTotalObjectCount() {return total_objs;}
00228 
00235     static long getLiveObjectCount() {return live_objs;}
00236 
00241     static void resetObjectCounters()  {total_objs=live_objs=0L;}
00243 };
00244 
00245 
00252 class SIM_API cNoncopyableOwnedObject : public cOwnedObject, noncopyable
00253 {
00254   public:
00258     explicit cNoncopyableOwnedObject(const char *name=NULL, bool namepooling=true) :
00259         cOwnedObject(name, namepooling) {}
00260 
00264     virtual cNoncopyableOwnedObject *dup() const;
00265 
00269     virtual void parsimPack(cCommBuffer *buffer);
00270 
00274     virtual void parsimUnpack(cCommBuffer *buffer);
00275 };
00276 
00277 
00278 //
00279 // Internal class: provides a flag that shows if control is in the main() function.
00280 //
00281 class SIM_API cStaticFlag
00282 {
00283   private:
00284     static bool staticflag;  // set to true while in main()
00285     static bool exitingflag; // set on getting a TERM or INT signal (Windows)
00286   public:
00287     cStaticFlag()  {staticflag = true;}
00288     ~cStaticFlag() {staticflag = false;}
00289     static void set(bool b) {staticflag = b;}
00290     static void setExiting() {exitingflag = true;}
00291     static bool isExiting() {return exitingflag;}
00292     static bool isSet() {return staticflag;}
00293 };
00294 
00295 SIM_API std::ostream& operator<< (std::ostream& os, const cOwnedObject *p);
00296 SIM_API std::ostream& operator<< (std::ostream& os, const cOwnedObject& o);
00297 
00298 inline std::ostream& operator<< (std::ostream& os, cOwnedObject *p) {
00299     return os << (const cOwnedObject *)p;
00300 }
00301 
00302 inline std::ostream& operator<< (std::ostream& os, cOwnedObject& o) {
00303     return os << (const cOwnedObject&)o;
00304 }
00305 
00306 
00323 // Note: this function cannot be put into utils.h (circular dependencies)
00324 template<class T>
00325 T check_and_cast(cObject *p)
00326 {
00327     if (!p)
00328         throw cRuntimeError("check_and_cast(): cannot cast NULL pointer to type '%s'",opp_typename(typeid(T)));
00329     T ret = dynamic_cast<T>(p);
00330     if (!ret)
00331         throw cRuntimeError("check_and_cast(): cannot cast (%s *)%s to type '%s'",p->getClassName(),p->getFullPath().c_str(),opp_typename(typeid(T)));
00332     return ret;
00333 }
00334 
00339 template<class T>
00340 const T check_and_cast(const cObject *p)
00341 {
00342     return check_and_cast<T>(const_cast<cObject *>(p));
00343 }
00344 
00345 NAMESPACE_END
00346 
00347 
00348 #endif
00349 
Generated on Tue Dec 2 11:16:27 2014 for OMNeT++ Simulation Library by  doxygen 1.6.3