# onstartup.h

    00001 //==========================================================================
    00002 //  ONSTARTUP.H - part of
    00003 //                     OMNeT++/OMNEST
    00004 //            Discrete System Simulation in C++
    00005 //
    00006 //
    00007 //  EXECUTE_ON_STARTUP macro and supporting 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 __ONSTARTUP_H
    00019 #define __ONSTARTUP_H
    00020
    00021 #include "defs.h"
    00022
    00023
    00033 #define EXECUTE_ON_STARTUP(NAME, CODE)  \
    00034  static void __##NAME##_code() {CODE;} \
    00035  static ExecuteOnStartup __##NAME##_reg(__##NAME##_code);
    00036
    00037
    00038 //
    00039 // Example:
    00040 // EXECUTE_ON_STARTUP(EthernetModule, moduletypes->instance().add(new EthernetModuleType());)
    00041 //
    00042
    00048 class SIM_API ExecuteOnStartup
    00049 {
    00050      private:
    00051          void (*code_to_exec)();
    00052          ExecuteOnStartup *next;
    00053          static ExecuteOnStartup *head;
    00054      public:
    00055          ExecuteOnStartup(void (*code_to_exec)());
    00056          ~ExecuteOnStartup();
    00057          void execute();
    00058          static void executeAll();
    00059 };
    00060
    00061
    00067 template<class T> class cSingleton
    00068 {
    00069   private:
    00070     T *inst;
    00071     const char *tmpname;
    00072   public:
    00073     cSingleton(const char *name) {tmpname = name;}
    00074     ~cSingleton() {delete inst;}
    00075     T *instance() {if (!inst) {inst=new T(tmpname); inst->removeFromOwnershipTree();} return inst;}
    00076 };
    00077
    00078 #endif
    00079