Statistics
| Branch: | Revision:

root / include / cmathfunction.h @ 7f251033

History | View | Annotate | Download (5.5 KB)

1
//==========================================================================
2
//  CMATHFUNCTION.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 __CMATHFUNCTION_H
17
#define __CMATHFUNCTION_H
18

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

    
24
NAMESPACE_BEGIN
25

    
26

    
27
/**
28
 * Prototype for mathematical functions.
29
 *
30
 * @see cMathFunction, Define_Function().
31
 * @ingroup EnumsTypes
32
 */
33
typedef double (*MathFunc)(...);
34

    
35
/**
36
 * Prototype for mathematical functions taking no arguments.
37
 *
38
 * @see cMathFunction, Define_Function().
39
 * @ingroup EnumsTypes
40
 */
41
typedef double (*MathFuncNoArg)();
42

    
43
/**
44
 * Prototype for mathematical functions taking one argument.
45
 *
46
 * @see cMathFunction, Define_Function().
47
 * @ingroup EnumsTypes
48
 */
49
typedef double (*MathFunc1Arg)(double);
50

    
51
/**
52
 * Prototype for mathematical functions taking two arguments.
53
 *
54
 * @see cMathFunction, Define_Function().
55
 * @ingroup EnumsTypes
56
 */
57
typedef double (*MathFunc2Args)(double,double);
58

    
59
/**
60
 * Prototype for mathematical functions taking three arguments.
61
 *
62
 * @see cMathFunction, Define_Function().
63
 * @ingroup EnumsTypes
64
 */
65
typedef double (*MathFunc3Args)(double,double,double);
66

    
67
/**
68
 * Prototype for mathematical functions taking four arguments.
69
 *
70
 * @see cMathFunction, Define_Function().
71
 * @ingroup EnumsTypes
72
 */
73
typedef double (*MathFunc4Args)(double,double,double,double);
74

    
75

    
76
/**
77
 * Registration class for extending NED with new functions. Stores a pointer to
78
 * a function taking up to 4 doubles as args and returning a double.
79
 *
80
 * Objects of this class are usually created via the Define_Function() macro.
81
 *
82
 * @ingroup Internals
83
 */
84
class SIM_API cMathFunction : public cNoncopyableOwnedObject
85
{
86
  private:
87
    MathFunc f;            // pointer to the function
88
    int argc;              // argument count (up to 4 doubles)
89
    std::string categ;     // category string; only used when listing all functions
90
    std::string desc;      // optional documentation string
91

    
92
  public:
93
    /** @name Constructors, destructor, assignment */
94
    //@{
95
    /**
96
     * Constructor.
97
     */
98
    cMathFunction(const char *name, MathFuncNoArg f, int argc=-1, const char *category=NULL, const char *description=NULL);
99

    
100
    /**
101
     * Constructor.
102
     */
103
    cMathFunction(const char *name, MathFunc1Arg f, int argc=-1, const char *category=NULL, const char *description=NULL);
104

    
105
    /**
106
     * Constructor.
107
     */
108

    
109
    cMathFunction(const char *name, MathFunc2Args f, int argc=-1, const char *category=NULL, const char *description=NULL);
110

    
111
    /**
112
     * Constructor.
113
     */
114
    cMathFunction(const char *name, MathFunc3Args f, int argc=-1, const char *category=NULL, const char *description=NULL);
115

    
116
    /**
117
     * Constructor.
118
     */
119
    cMathFunction(const char *name, MathFunc4Args f, int argc=-1, const char *category=NULL, const char *description=NULL);
120

    
121
    /**
122
     * Destructor.
123
     */
124
    virtual ~cMathFunction() {}
125
    //@}
126

    
127
    /** @name Redefined cObject member functions. */
128
    //@{
129
    /**
130
     * Produces a one-line description of the object's contents.
131
     */
132
    virtual std::string info() const;
133
    //@}
134

    
135
    /** @name Member access. */
136
    //@{
137
    /**
138
     * Argument count to function.
139
     */
140
    int getNumArgs() const  {return argc;}
141

    
142
    /**
143
     * Returns function pointer as double function with unchecked arg list
144
     * (no type safety!)
145
     */
146
    MathFunc getMathFunc() const {return f;}
147

    
148
    /**
149
     * Returns function pointer as double function with no args.
150
     * Throws exception is actual arg count is different.
151
     */
152
    MathFuncNoArg getMathFuncNoArg() const;
153

    
154
    /**
155
     * Returns function pointer as double function with 1 double arg.
156
     * Throws exception is actual arg count is different.
157
     */
158
    MathFunc1Arg getMathFunc1Arg() const;
159

    
160
    /**
161
     * Returns function pointer as double function with 2 double args.
162
     * Throws exception is actual arg count is different.
163
     */
164
    MathFunc2Args getMathFunc2Args() const;
165

    
166
    /**
167
     * Returns function pointer as double function with 3 double args.
168
     * Throws exception is actual arg count is different.
169
     */
170
    MathFunc3Args getMathFunc3Args() const;
171

    
172
    /**
173
     * Returns function pointer as double function with 4 double args.
174
     * Throws exception is actual arg count is different.
175
     */
176
    MathFunc4Args getMathFunc4Args() const;
177

    
178
    /**
179
     * Returns a string that can be useful in classifying NED functions,
180
     * e.g. "trigonometric".
181
     */
182
    const char *getCategory() const  {return categ.c_str();}
183

    
184
    /**
185
     * Returns the function's documentation as a string.
186
     */
187
    const char *getDescription() const  {return desc.c_str();}
188
    //@}
189

    
190
    /**
191
     * Finds a registered function by name. Returns NULL if not found.
192
     */
193
    static cMathFunction *find(const char *name, int numArgs);
194

    
195
    /**
196
     * Finds a registered function by name. Throws an error if not found.
197
     */
198
    static cMathFunction *get(const char *name, int numArgs);
199

    
200
    /**
201
     * Finds a registered function by function pointer.
202
     */
203
    static cMathFunction *findByPointer(MathFunc f);
204

    
205
};
206

    
207
NAMESPACE_END
208

    
209

    
210
#endif
211

    
212