cnedfunction.h Source File

API: cnedfunction.h Source File
API
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
25namespace omnetpp {
26
27
36typedef cValue (*NedFunction)(cComponent *context, cValue argv[], int argc);
37
46typedef cValue (*NedFunctionExt)(cExpression::Context *context, cValue argv[], int argc);
47
48// NEDFunction was renamed NedFunction in OMNeT++ 5.3; typedef added for compatibility
49typedef NedFunction NEDFunction;
50
51
52
60class 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:
83
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
116
119 virtual std::string str() const override;
121
124
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
213};
214
215// cNEDFunction was renamed cNedFunction in OMNeT++ 5.3; typedef added for compatibility
216typedef cNedFunction cNEDFunction;
217
218} // namespace omnetpp
219
220
221#endif
222
223
Common base for module and channel classes.
Definition ccomponent.h:51
Contextual information for evaluating the expression.
Definition cexpression.h:40
Registration class for extending NED with arbitrary new functions.
Definition cnedfunction.h:61
static cNedFunction * find(const char *name, int argCount)
bool hasVarArgs() const
Definition cnedfunction.h:167
cNedFunction(NedFunction f, const char *signature, const char *category=nullptr, const char *description=nullptr)
static cNedFunction * findByPointer(NedFunction f)
const char * getCategory() const
Definition cnedfunction.h:179
static cNedFunction * get(const char *name)
static cNedFunction * find(const char *name)
bool acceptsArgCount(int argCount)
Definition cnedfunction.h:173
const char * getSignature() const
Definition cnedfunction.h:138
Type getReturnType() const
Definition cnedfunction.h:143
int getMinArgs() const
Definition cnedfunction.h:154
virtual ~cNedFunction()
Definition cnedfunction.h:111
const char * getDescription() const
Definition cnedfunction.h:184
NedFunction getFunctionPointer() const
Definition cnedfunction.h:133
virtual std::string str() const override
int getMaxArgs() const
Definition cnedfunction.h:161
Type getArgType(int k) const
Definition cnedfunction.h:148
cNedFunction(NedFunctionExt f, const char *signature, const char *category=nullptr, const char *description=nullptr)
cValue invoke(cExpression::Context *context, cValue argv[], int argc)
static cNedFunction * get(const char *name, int argCount)
A variant-like value class used during evaluating NED expressions.
Definition cvalue.h:48
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
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