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-2008 Andras Varga
00013   Copyright (C) 2006-2008 OpenSim Ltd.
00014 
00015   This file is distributed WITHOUT ANY WARRANTY. See the file
00016   `license' for details on this and other legal matters.
00017 *--------------------------------------------------------------*/
00018 
00019 #ifndef __ONSTARTUP_H
00020 #define __ONSTARTUP_H
00021 
00022 #include <vector>
00023 #include <map>
00024 #include "simkerneldefs.h"
00025 #include "cownedobject.h"
00026 
00027 NAMESPACE_BEGIN
00028 
00029 
00030 // Generating identifiers unique for this file. See MSVC Help for __COUNTER__ for more info.
00031 #define __OPPCONCAT1(x,y) x##y
00032 #define __OPPCONCAT2(prefix,line) __OPPCONCAT1(prefix,line)
00033 #define MAKE_UNIQUE_WITHIN_FILE(prefix) __OPPCONCAT2(prefix,__LINE__)
00034 
00035 // helpers for EXECUTE_ON_STARTUP
00036 // IMPORTANT: if you change "__onstartup_func_" below, linkall.pl must also be updated!
00037 #define __ONSTARTUP_FUNC  MAKE_UNIQUE_WITHIN_FILE(__onstartup_func_)
00038 #define __ONSTARTUP_OBJ   MAKE_UNIQUE_WITHIN_FILE(__onstartup_obj_)
00039 
00040 // helper
00041 #define __FILEUNIQUENAME__  MAKE_UNIQUE_WITHIN_FILE(__uniquename_)
00042 
00052 #define EXECUTE_ON_STARTUP(CODE)  \
00053   namespace { \
00054     void __ONSTARTUP_FUNC() {CODE;} \
00055     static CodeFragments __ONSTARTUP_OBJ(__ONSTARTUP_FUNC, CodeFragments::STARTUP); \
00056   };
00057 
00066 //NOTE: implementation reuses some of the *startup* macros
00067 #define EXECUTE_ON_SHUTDOWN(CODE)  \
00068   namespace { \
00069     void __ONSTARTUP_FUNC() {CODE;} \
00070     static CodeFragments __ONSTARTUP_OBJ(__ONSTARTUP_FUNC, CodeFragments::SHUTDOWN); \
00071   };
00072 
00078 class SIM_API CodeFragments
00079 {
00080   public:
00081     enum Type {STARTUP, SHUTDOWN};
00082   private:
00083     Type type;
00084     void (*code)();
00085     CodeFragments *next;
00086     static CodeFragments *head;
00087   public:
00088     CodeFragments(void (*code)(), Type type);
00089     ~CodeFragments();
00090     static void executeAll(Type type);
00091 };
00092 
00093 NAMESPACE_END
00094 
00095 
00096 #endif
00097 
Generated on Tue Dec 2 11:16:27 2014 for OMNeT++ Simulation Library by  doxygen 1.6.3