OMNeT++ Simulation Library  6.0.3
cnedfunction.h
1 //==========================================================================
2 // CNEDFUNCTION.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_CNEDFUNCTION_H
17 #define __OMNETPP_CNEDFUNCTION_H
18 
19 #include <cstdarg>
20 #include "simkerneldefs.h"
21 #include "globals.h"
22 #include "cownedobject.h"
23 #include "cdynamicexpression.h"
24 
25 namespace omnetpp {
26 
27 
36 typedef cValue (*NedFunction)(cComponent *context, cValue argv[], int argc);
37 
46 typedef cValue (*NedFunctionExt)(cExpression::Context *context, cValue argv[], int argc);
47 
48 // NEDFunction was renamed NedFunction in OMNeT++ 5.3; typedef added for compatibility
49 typedef NedFunction NEDFunction;
50 
51 
52 
60 class SIM_API cNedFunction : public cNoncopyableOwnedObject
61 {
62  public:
63  enum Type { UNDEF=0, BOOL, INT, INTQ, DOUBLE, DOUBLEQ, STRING, OBJECT, XML, ANY };
64 
65  private:
66  std::string signature; // function signature, as passed to the ctor
67  std::vector<Type> argTypes; // argument types
68  bool hasVarargs_; // if true, signature contains "..." after the last typed arg
69  Type returnType; // return type
70  int minArgc, maxArgc; // minimum and maximum argument count
71  NedFunction f = nullptr; // implementation function
72  NedFunctionExt fext = nullptr; // alternative function that takes cExpression::Context as arg
73  std::string category; // category string; only used when listing all functions
74  std::string description; // optional documentation string
75 
76  protected:
77  void parseSignature(const char *signature);
78  void checkArgs(cValue argv[], int argc);
79 
80  public:
99  cNedFunction(NedFunction f, const char *signature, const char *category=nullptr, const char *description=nullptr);
100 
106  cNedFunction(NedFunctionExt f, const char *signature, const char *category=nullptr, const char *description=nullptr);
107 
111  virtual ~cNedFunction() {}
113 
119  virtual std::string str() const override;
121 
127  cValue invoke(cExpression::Context *context, cValue argv[], int argc);
128 
133  NedFunction getFunctionPointer() const {return f;}
134 
138  const char *getSignature() const {return signature.c_str();}
139 
143  Type getReturnType() const {return returnType;}
144 
148  Type getArgType(int k) const {return argTypes[k];}
149 
154  int getMinArgs() const {return minArgc;}
155 
161  int getMaxArgs() const {return maxArgc;}
162 
167  bool hasVarArgs() const {return hasVarargs_;}
168 
173  bool acceptsArgCount(int argCount) {return argCount >= minArgc && (hasVarargs_ || argCount <= maxArgc);}
174 
179  const char *getCategory() const {return category.c_str();}
180 
184  const char *getDescription() const {return description.c_str();}
186 
190  static cNedFunction *find(const char *name);
191 
196  static cNedFunction *find(const char *name, int argCount);
197 
201  static cNedFunction *get(const char *name);
202 
207  static cNedFunction *get(const char *name, int argCount);
208 
212  static cNedFunction *findByPointer(NedFunction f);
213 };
214 
215 // cNEDFunction was renamed cNedFunction in OMNeT++ 5.3; typedef added for compatibility
216 typedef cNedFunction cNEDFunction;
217 
218 } // namespace omnetpp
219 
220 
221 #endif
222 
223 
omnetpp::cValue
A variant-like value class used during evaluating NED expressions.
Definition: cvalue.h:47
omnetpp::cNedFunction::getCategory
const char * getCategory() const
Definition: cnedfunction.h:179
omnetpp::NedFunctionExt
cValue(* NedFunctionExt)(cExpression::Context *context, cValue argv[], int argc)
One of the two function signatures that can be used with cDynamicExpression. This one is more generic...
Definition: cnedfunction.h:46
omnetpp::cNedFunction::getFunctionPointer
NedFunction getFunctionPointer() const
Definition: cnedfunction.h:133
omnetpp::cNedFunction::getDescription
const char * getDescription() const
Definition: cnedfunction.h:184
omnetpp::cNedFunction::getMaxArgs
int getMaxArgs() const
Definition: cnedfunction.h:161
omnetpp::cNedFunction::getSignature
const char * getSignature() const
Definition: cnedfunction.h:138
omnetpp::cExpression::Context
Contextual information for evaluating the expression.
Definition: cexpression.h:39
omnetpp::cNedFunction
Registration class for extending NED with arbitrary new functions.
Definition: cnedfunction.h:60
omnetpp::cNedFunction::getReturnType
Type getReturnType() const
Definition: cnedfunction.h:143
omnetpp::cNedFunction::~cNedFunction
virtual ~cNedFunction()
Definition: cnedfunction.h:111
omnetpp::cNedFunction::getArgType
Type getArgType(int k) const
Definition: cnedfunction.h:148
omnetpp::cNedFunction::getMinArgs
int getMinArgs() const
Definition: cnedfunction.h:154
omnetpp::cComponent
Common base for module and channel classes.
Definition: ccomponent.h:49
omnetpp::NedFunction
cValue(* NedFunction)(cComponent *context, cValue argv[], int argc)
One of the two function signatures that can be used with cDynamicExpression. This one can be used wit...
Definition: cnedfunction.h:36
omnetpp::cNedFunction::acceptsArgCount
bool acceptsArgCount(int argCount)
Definition: cnedfunction.h:173
omnetpp::cNoncopyableOwnedObject
Base class for cOwnedObject-based classes that do not wish to support assignment and duplication.
Definition: cownedobject.h:242
omnetpp::cNedFunction::hasVarArgs
bool hasVarArgs() const
Definition: cnedfunction.h:167