Statistics
| Branch: | Revision:

root / src / sim / cmathfunction.cc @ fbe00e73

History | View | Annotate | Download (5.77 KB)

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

    
9
/*--------------------------------------------------------------*
10
  Copyright (C) 1992-2008 Andras Varga
11
  Copyright (C) 2006-2008 OpenSim Ltd.
12

13
  This file is distributed WITHOUT ANY WARRANTY. See the file
14
  `license' for details on this and other legal matters.
15
*--------------------------------------------------------------*/
16

    
17
#include <math.h>
18
#include <string.h>
19
#include <stdarg.h>
20
#include "cmathfunction.h"
21
#include "globals.h"
22
#include "cenvir.h"
23
#include "cexception.h"
24
#include "random.h"
25
#include "stringutil.h"
26

    
27
#ifdef WITH_PARSIM
28
#include "ccommbuffer.h"
29
#include "parsim/cplaceholdermod.h"
30
#endif
31

    
32
USING_NAMESPACE
33

    
34
cMathFunction::cMathFunction(const char *name, MathFuncNoArg f, int ac, const char *category, const char *description) :
35
    cNoncopyableOwnedObject(name, false)
36
{
37
    this->categ = opp_nulltoempty(category);
38
    this->desc = opp_nulltoempty(description);
39
    this->f = (MathFunc)f;
40
    argc = 0;
41
    if (ac!=-1 && ac!=0)
42
        throw cRuntimeError("Register_Function() or cMathFunction: "
43
                            "attempt to register function \"%s\" with wrong "
44
                            "number of arguments %d, should be 0", name, ac);
45
}
46

    
47
cMathFunction::cMathFunction(const char *name, MathFunc1Arg f, int ac, const char *category, const char *description) :
48
    cNoncopyableOwnedObject(name, false)
49
{
50
    this->categ = opp_nulltoempty(category);
51
    this->desc = opp_nulltoempty(description);
52
    this->f = (MathFunc)f;
53
    argc = 1;
54
    if (ac!=-1 && ac!=1)
55
        throw cRuntimeError("Register_Function() or cMathFunction: "
56
                            "attempt to register function \"%s\" with wrong "
57
                            "number of arguments %d, should be 1", name, ac);
58
}
59

    
60
cMathFunction::cMathFunction(const char *name, MathFunc2Args f, int ac, const char *category, const char *description) :
61
    cNoncopyableOwnedObject(name, false)
62
{
63
    this->categ = opp_nulltoempty(category);
64
    this->desc = opp_nulltoempty(description);
65
    this->f = (MathFunc)f;
66
    argc = 2;
67
    if (ac!=-1 && ac!=2)
68
        throw cRuntimeError("Register_Function() or cMathFunction: "
69
                            "attempt to register function \"%s\" with wrong "
70
                            "number of arguments %d, should be 2", name, ac);
71
}
72

    
73
cMathFunction::cMathFunction(const char *name, MathFunc3Args f, int ac, const char *category, const char *description) :
74
    cNoncopyableOwnedObject(name, false)
75
{
76
    this->categ = opp_nulltoempty(category);
77
    this->desc = opp_nulltoempty(description);
78
    this->f = (MathFunc)f;
79
    argc = 3;
80
    if (ac!=-1 && ac!=3)
81
        throw cRuntimeError("Register_Function() or cMathFunction: "
82
                            "attempt to register function \"%s\" with wrong "
83
                            "number of arguments %d, should be 3", name, ac);
84
}
85

    
86
cMathFunction::cMathFunction(const char *name, MathFunc4Args f, int ac, const char *category, const char *description) :
87
    cNoncopyableOwnedObject(name, false)
88
{
89
    this->categ = opp_nulltoempty(category);
90
    this->desc = opp_nulltoempty(description);
91
    this->f = (MathFunc)f;
92
    argc = 4;
93
    if (ac!=-1 && ac!=4)
94
        throw cRuntimeError("Register_Function() or cMathFunction: "
95
                            "attempt to register function \"%s\" with wrong "
96
                            "number of arguments %d, should be 4", name, ac);
97
}
98

    
99
std::string cMathFunction::info() const
100
{
101
    std::stringstream out;
102
    out << "double " << getName() << "(";
103
    for (int i=0; i<argc; i++)
104
        out << (i?", ":"") << "double";
105
    out << ")";
106
    return out.str();
107
}
108

    
109
MathFuncNoArg cMathFunction::getMathFuncNoArg() const
110
{
111
    if (argc!=0)
112
        throw cRuntimeError(this,"getMathFuncNoArg(): arg count mismatch (argc=%d)",argc);
113
    return (MathFuncNoArg)f;
114
}
115

    
116
MathFunc1Arg cMathFunction::getMathFunc1Arg() const
117
{
118
    if (argc!=1)
119
        throw cRuntimeError(this,"getMathFunc1Arg(): arg count mismatch (argc=%d)",argc);
120
    return (MathFunc1Arg)f;
121
}
122

    
123
MathFunc2Args cMathFunction::getMathFunc2Args() const
124
{
125
    if (argc!=2)
126
        throw cRuntimeError(this,"getMathFunc2Args(): arg count mismatch (argc=%d)",argc);
127
    return (MathFunc2Args)f;
128
}
129

    
130
MathFunc3Args cMathFunction::getMathFunc3Args() const
131
{
132
    if (argc!=3)
133
        throw cRuntimeError(this,"getMathFunc3Args(): arg count mismatch (argc=%d)",argc);
134
    return (MathFunc3Args)f;
135
}
136

    
137
MathFunc4Args cMathFunction::getMathFunc4Args() const
138
{
139
    if (argc!=4)
140
        throw cRuntimeError(this,"getMathFunc4Args(): arg count mismatch (argc=%d)",argc);
141
    return (MathFunc4Args)f;
142
}
143

    
144
cMathFunction *cMathFunction::find(const char *name, int argcount)
145
{
146
    cRegistrationList *a = nedFunctions.getInstance();
147
    for (int i=0; i<a->size(); i++)
148
    {
149
        cMathFunction *f = dynamic_cast<cMathFunction *>(a->get(i));
150
        if (f && f->isName(name) && f->getNumArgs()==argcount)
151
            return f;
152
    }
153
    return NULL;
154
}
155

    
156
cMathFunction *cMathFunction::get(const char *name, int argcount)
157
{
158
    cMathFunction *p = find(name, argcount);
159
    if (!p)
160
        throw cRuntimeError("Math function \"%s\" with %d args not found -- perhaps it wasn't registered "
161
                            "with the Register_Function() macro, or its code is not linked in",
162
                            name, argcount);
163
    return p;
164
}
165

    
166
cMathFunction *cMathFunction::findByPointer(MathFunc f)
167
{
168
    cRegistrationList *a = nedFunctions.getInstance();
169
    for (int i=0; i<a->size(); i++)
170
    {
171
        cMathFunction *ff = dynamic_cast<cMathFunction *>(a->get(i));
172
        if (ff && ff->getMathFunc() == f)
173
            return ff;
174
    }
175
    return NULL;
176
}
177

    
178