OMNeT++ Simulation Library  6.0.3
ccontextswitcher.h
1 //==========================================================================
2 // CCONTEXTSWITCHER.H - part of
3 // OMNeT++/OMNEST
4 // Discrete System Simulation in C++
5 //
6 //==========================================================================
7 
8 /*--------------------------------------------------------------*
9  Copyright (C) 1992-2017 Andras Varga
10  Copyright (C) 2006-2017 OpenSim Ltd.
11 
12  This file is distributed WITHOUT ANY WARRANTY. See the file
13  `license' for details on this and other legal matters.
14 *--------------------------------------------------------------*/
15 
16 #ifndef __OMNETPP_CCONTEXTSWITCHER_H
17 #define __OMNETPP_CCONTEXTSWITCHER_H
18 
19 #include <type_traits> // is_integer
20 #include <limits> // numeric_limits
21 #include <cstring> // for strlen, etc.
22 #include <cstdarg> // for va_list
23 #include <cstdio> // for sprintf
24 #include <cstdlib> // for gcvt
25 #include <typeinfo> // for type_info
26 #include <string> // for std::string
27 #include "platdep/platmisc.h" // for gcvt, etc
28 #include "simkerneldefs.h"
29 #include "errmsg.h"
30 
31 namespace omnetpp {
32 
33 class cObject;
34 class cComponent;
35 
36 // logically belongs to csimulation.h but must be here because of declaration order
37 enum ContextType {CTX_NONE, CTX_BUILD, CTX_INITIALIZE, CTX_EVENT, CTX_REFRESHDISPLAY, CTX_FINISH, CTX_CLEANUP};
38 
39 
61 #define Enter_Method(...) omnetpp::cMethodCallContextSwitcher __ctx(this); __ctx.methodCall(__VA_ARGS__)
62 
82 #define Enter_Method_Silent(...) omnetpp::cMethodCallContextSwitcher __ctx(this); __ctx.methodCallSilent(__VA_ARGS__)
83 
91 class SIM_API cContextSwitcher
92 {
93  protected:
94  cComponent *callerContext;
95  public:
99  cContextSwitcher(const cComponent *newContext);
100 
104  ~cContextSwitcher();
105 
109  cComponent *getCallerContext() const { return callerContext; }
110 };
111 
117 {
118  private:
119  static int depth;
120 
121  public:
125  //TODO store previous frame, __FILE__, __LINE__, __FUNCTION__ too, at least in debug builds?
126  cMethodCallContextSwitcher(const cComponent *newContext);
127 
132 
137  _OPP_GNU_ATTRIBUTE(format(printf, 2, 3))
138  void methodCall(const char *methodFmt,...);
139  _OPP_GNU_ATTRIBUTE(format(printf, 2, 3))
140  void methodCallSilent(const char *methodFm,...);
141  void methodCallSilent();
142 
146  static int getDepth() {return depth;}
147 };
148 
156 class SIM_API cContextTypeSwitcher
157 {
158  private:
159  ContextType savedContextType;
160 
161  public:
165  cContextTypeSwitcher(ContextType contexttype);
166 
171 };
172 
173 
174 } // namespace omnetpp
175 
176 #endif
177 
178 
omnetpp::cMethodCallContextSwitcher
Internal class. May only be used via the Enter_Method() and Enter_Method_Silent() macros!
Definition: ccontextswitcher.h:116
omnetpp::cContextSwitcher
The constructor switches the context to the given component, and the destructor restores the original...
Definition: ccontextswitcher.h:91
omnetpp::cMethodCallContextSwitcher::getDepth
static int getDepth()
Definition: ccontextswitcher.h:146
omnetpp::cContextSwitcher::getCallerContext
cComponent * getCallerContext() const
Definition: ccontextswitcher.h:109
omnetpp::cContextTypeSwitcher
The constructor switches the context type, and the destructor restores the original context type.
Definition: ccontextswitcher.h:156
omnetpp::cComponent
Common base for module and channel classes.
Definition: ccomponent.h:49