Statistics
| Branch: | Revision:

root / include / cnedfunction.h @ master

History | View | Annotate | Download (4.68 KB)

1
//==========================================================================
2
//  CNEDFUNCTION.H - part of
3
//                     OMNeT++/OMNEST
4
//            Discrete System Simulation in C++
5
//
6
//==========================================================================
7

    
8
/*--------------------------------------------------------------*
9
  Copyright (C) 1992-2008 Andras Varga
10
  Copyright (C) 2006-2008 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 __CNEDFUNCTION_H
17
#define __CNEDFUNCTION_H
18

    
19
#include <stdarg.h>
20
#include "simkerneldefs.h"
21
#include "globals.h"
22
#include "cownedobject.h"
23
#include "cdynamicexpression.h"
24

    
25
NAMESPACE_BEGIN
26

    
27

    
28
/**
29
 * A function that can be used with cDynamicExpression.
30
 *
31
 * @see cNEDFunction, Define_NED_Function().
32
 * @ingroup EnumsTypes
33
 */
34
typedef cDynamicExpression::Value (*NEDFunction)(cComponent *context,
35
                                                 cDynamicExpression::Value argv[],
36
                                                 int argc);
37

    
38

    
39
/**
40
 * Registration class for extending NED with new functions.
41
 *
42
 * Objects of this class are usually created via the Define_NED_Function() macro.
43
 *
44
 * @ingroup Internals
45
 */
46
class SIM_API cNEDFunction : public cNoncopyableOwnedObject
47
{
48
  private:
49
    std::string sign;      // function signature, as passed to the ctor
50
    std::string argtypes;  // sequence of B,L,D,Q,S,X,*
51
    char rettype;          // one of B,L,D,Q,S,X,*
52
    int minargc, maxargc;  // minimum and maximum argument count
53
    NEDFunction f;         // function ptr
54
    std::string categ;     // category string; only used when listing all functions
55
    std::string desc;      // optional documentation string
56

    
57
  protected:
58
    void parseSignature(const char *signature);
59
    void checkArgs(cDynamicExpression::Value argv[], int argc);
60

    
61
  public:
62
    /** @name Constructors, destructor, assignment */
63
    //@{
64
    /**
65
     * Constructor. Signature is expected in the following syntax:
66
     *     returntype name(argtype argname,...),
67
     * where types can be bool, long, double, quantity, string, xml, any;
68
     * names of optional args end in '?'. The object name will be
69
     * the function name, as extracted from the signature string.
70
     *
71
     * Example: "quantity uniform(quantity a, quantity b, long rng?)"
72
     */
73
    cNEDFunction(NEDFunction f, const char *signature, const char *category=NULL, const char *description=NULL);
74

    
75
    /**
76
     * Destructor.
77
     */
78
    virtual ~cNEDFunction() {}
79
    //@}
80

    
81
    /** @name Redefined cObject member functions. */
82
    //@{
83
    /**
84
     * Produces a one-line description of the object's contents.
85
     */
86
    virtual std::string info() const;
87
    //@}
88

    
89
    /** @name Member access. */
90
    //@{
91
    /**
92
     * Performs argument type checking, and invokes the function.
93
     */
94
    cDynamicExpression::Value invoke(cComponent *context, cDynamicExpression::Value argv[], int argc);
95

    
96
    /**
97
     * Returns the function pointer. Do not call the function
98
     * directly, because that would bypass argument type validation.
99
     */
100
    NEDFunction getFunctionPointer() const  {return f;}
101

    
102
    /**
103
     * Returns the functions signature, as passed to the constructor
104
     */
105
    const char *getSignature() const {return sign.c_str();}
106

    
107
    /**
108
     * Returns the function return type, one of: B,L,D,Q,S,X,*
109
     */
110
    char getReturnType() const  {return rettype;}
111

    
112
    /**
113
     * Returns the type of the kth argument; result is
114
     * one of: B,L,D,Q,S,X,*
115
     */
116
    char getArgType(int k) const  {return argtypes[k];}
117

    
118
    /**
119
     * Returns the minimum number of arguments (i.e. the number
120
     * of mandatory arguments).
121
     */
122
    int getMinArgs() const  {return minargc;}
123

    
124
    /**
125
     * Returns the maximum number of arguments (i.e. the last max-min
126
     * args are optional).
127
     */
128
    int getMaxArgs() const  {return maxargc;}
129

    
130
    /**
131
     * Returns a string that can be useful in classifying NED functions,
132
     * e.g. "trigonometric".
133
     */
134
    const char *getCategory() const  {return categ.c_str();}
135

    
136
    /**
137
     * Returns the function's documentation as a string.
138
     */
139
    const char *getDescription() const  {return desc.c_str();}
140
    //@}
141

    
142
    /**
143
     * Finds a registered function by name. Returns NULL if not found.
144
     */
145
    static cNEDFunction *find(const char *name, int numArgs);
146

    
147
    /**
148
     * Finds a registered function by name. Throws an error if not found.
149
     */
150
    static cNEDFunction *get(const char *name, int numArgs);
151

    
152
    /**
153
     * Finds a registered function by function pointer.
154
     */
155
    static cNEDFunction *findByPointer(NEDFunction f);
156
};
157

    
158
NAMESPACE_END
159

    
160

    
161
#endif
162

    
163