Statistics
| Branch: | Revision:

root / src / envir / resultfilter.cc @ fbe00e73

History | View | Annotate | Download (5.38 KB)

1
//==========================================================================
2
//  RESULTFILTER.CC - part of
3
//                     OMNeT++/OMNEST
4
//            Discrete System Simulation in C++
5
//
6
//  Author: Andras Varga
7
//
8
//==========================================================================
9

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

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

    
18
#include "commonutil.h"
19
#include "resultfilter.h"
20

    
21
cGlobalRegistrationList resultFilters("resultFilters");
22

    
23

    
24
ResultFilter::ResultFilter()
25
{
26
    delegates = new ResultListener*[1];
27
    delegates[0] = NULL;
28
}
29

    
30
void ResultFilter::addDelegate(ResultListener *delegate)
31
{
32
    // reallocate each time
33
    Assert(delegates!=NULL);
34
    int n = getNumDelegates();
35
    ResultListener **v = new ResultListener*[n+2];
36
    memcpy(v, delegates, n*sizeof(ResultListener*));
37
    v[n] = delegate;
38
    v[n+1] = NULL;
39
    delete [] delegates;
40
    delegates = v;
41
    delegate->subscribecount++;
42
    delegate->subscribedTo(this);
43
}
44

    
45
int ResultFilter::getNumDelegates() const
46
{
47
    int k = 0;
48
    while (delegates[k])
49
        k++;
50
    return k;
51
}
52

    
53
std::vector<ResultListener*> ResultFilter::getDelegates() const
54
{
55
    std::vector<ResultListener*> result;
56
    for (int i = 0; delegates[i]; i++)
57
        result.push_back(delegates[i]);
58
    return result;
59
}
60

    
61
ResultFilter::~ResultFilter()
62
{
63
    for (int i=0; delegates[i]; i++) {
64
        delegates[i]->subscribecount--;
65
        delegates[i]->unsubscribedFrom(this);
66
    }
67
    delete [] delegates;
68
}
69

    
70
void ResultFilter::fire(ResultFilter *prev, simtime_t_cref t, long l)
71
{
72
    for (int i=0; delegates[i]; i++)
73
        delegates[i]->receiveSignal(this, t, l);
74
}
75

    
76
void ResultFilter::fire(ResultFilter *prev, simtime_t_cref t, unsigned long l)
77
{
78
    for (int i=0; delegates[i]; i++)
79
        delegates[i]->receiveSignal(this, t, l);
80
}
81

    
82
void ResultFilter::fire(ResultFilter *prev, simtime_t_cref t, double d)
83
{
84
    for (int i=0; delegates[i]; i++)
85
        delegates[i]->receiveSignal(this, t, d);
86
}
87

    
88
void ResultFilter::fire(ResultFilter *prev, simtime_t_cref t, const SimTime& v)
89
{
90
    for (int i=0; delegates[i]; i++)
91
        delegates[i]->receiveSignal(this, t, v);
92
}
93

    
94
void ResultFilter::fire(ResultFilter *prev, simtime_t_cref t, const char *s)
95
{
96
    for (int i=0; delegates[i]; i++)
97
        delegates[i]->receiveSignal(this, t, s);
98
}
99

    
100
void ResultFilter::fire(ResultFilter *prev, simtime_t_cref t, cObject *obj)
101
{
102
    for (int i=0; delegates[i]; i++)
103
        delegates[i]->receiveSignal(this, t, obj);
104
}
105

    
106
void ResultFilter::finish(ResultFilter *prev)
107
{
108
    for (int i=0; delegates[i]; i++)
109
        delegates[i]->finish(this);
110
}
111

    
112
//---
113

    
114
void NumericResultFilter::receiveSignal(ResultFilter *prev, simtime_t_cref t, long l)
115
{
116
    simtime_t tt = t;
117
    double d = l;
118
    if (process(tt, d))
119
        fire(this, tt, d);
120
}
121

    
122
void NumericResultFilter::receiveSignal(ResultFilter *prev, simtime_t_cref t, unsigned long l)
123
{
124
    simtime_t tt = t;
125
    double d = l;
126
    if (process(tt, d))
127
        fire(this, tt, d);
128
}
129

    
130
void NumericResultFilter::receiveSignal(ResultFilter *prev, simtime_t_cref t, double d)
131
{
132
    simtime_t tt = t;
133
    if (process(tt, d))
134
        fire(this, tt, d);
135
}
136

    
137
void NumericResultFilter::receiveSignal(ResultFilter *prev, simtime_t_cref t, const SimTime& v)
138
{
139
    simtime_t tt = t;
140
    double d = v.dbl();
141
    if (process(tt, d))
142
        fire(this, tt, d);
143
}
144

    
145
void NumericResultFilter::receiveSignal(ResultFilter *prev, simtime_t_cref t, const char *s)
146
{
147
    throw cRuntimeError("%s: Cannot convert const char * to double", getClassName());
148
}
149

    
150
void NumericResultFilter::receiveSignal(ResultFilter *prev, simtime_t_cref t, cObject *obj)
151
{
152
    // note: cITimestampedValue stuff was already dispatched to (simtime_t,double) method in base class
153
    throw cRuntimeError("%s: Cannot convert cObject * to double", getClassName());
154
}
155

    
156
//---
157

    
158
#define THROW(t)  throw opp_runtime_error("%s: received data with wrong type (%s): object expected", getClassName(), t);
159

    
160
void ObjectResultFilter::receiveSignal(ResultFilter *prev, simtime_t_cref t, long l)
161
{
162
    THROW("long");
163
}
164

    
165
void ObjectResultFilter::receiveSignal(ResultFilter *prev, simtime_t_cref t, unsigned long l)
166
{
167
    THROW("unsigned long");
168
}
169

    
170
void ObjectResultFilter::receiveSignal(ResultFilter *prev, simtime_t_cref t, double d)
171
{
172
    THROW("double");
173
}
174

    
175
void ObjectResultFilter::receiveSignal(ResultFilter *prev, simtime_t_cref t, const SimTime& v)
176
{
177
    THROW("simtime_t");
178
}
179

    
180
void ObjectResultFilter::receiveSignal(ResultFilter *prev, simtime_t_cref t, const char *s)
181
{
182
    THROW("const char *");
183
}
184

    
185
#undef THROW
186

    
187
//---
188

    
189
ResultFilterDescriptor::ResultFilterDescriptor(const char *name, ResultFilter *(*f)())
190
  : cNoncopyableOwnedObject(name, false)
191
{
192
    creatorfunc = f;
193
}
194

    
195
ResultFilterDescriptor *ResultFilterDescriptor::find(const char *name)
196
{
197
    return dynamic_cast<ResultFilterDescriptor *>(resultFilters.getInstance()->lookup(name));
198
}
199

    
200
ResultFilterDescriptor *ResultFilterDescriptor::get(const char *name)
201
{
202
    ResultFilterDescriptor *p = find(name);
203
    if (!p)
204
        throw cRuntimeError("Result filter \"%s\" not found -- perhaps the name is wrong, "
205
                            "or the filter was not registered with Register_ResultFilter()", name);
206
    return p;
207
}
208

    
209