-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdata.cpp
343 lines (307 loc) · 8.08 KB
/
data.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
#include <sstream>
#include "include/SDK/XPLMProcessing.h"
#include "include/SDK/XPLMDataAccess.h"
#include "include/data.h"
#include "include/mfdpage.h"
using std::set;
data_t::data_t(void)
{
}
data_t::~data_t(void)
{
for (set<object_t*>::iterator it = a_datasources.begin(); it != a_datasources.end(); ++it)
{
delete *it;
}
debug_out(debug, "data: deleted data connection (self: %p)", this);
}
const float& data_t::upd_interval(void)
{
return a_interval;
}
void data_t::connect(float interval)
{
a_interval = interval;
XPLMRegisterFlightLoopCallback(update, interval, this);
debug_out(debug, "data: engaged data connection (self: %p)", this);
}
void data_t::disconnect()
{
XPLMUnregisterFlightLoopCallback(update, this);
debug_out(debug, "data: deactivating data connection with x-plane (self: %p)", this);
}
void data_t::add_listener(listener_t* listener)
{
if (!listener) return;
if (a_listeners.insert(listener).second)
{
debug_out(verbose, "data: added listener: {%s} (self: %p)", listener->name().c_str(), listener);
}
}
void data_t::remove_listener(listener_t* listener)
{
if (!listener) return;
if (a_listeners.erase(listener))
{
debug_out(verbose, "data: removed listener: {%s} (self: %p)", listener->name().c_str(), listener);
}
}
object_t* data_t::add_datasource(const char* ref)
{
if (!ref) return 0;
datasource_t* source = 0;
source = new datasource_t(ref);
for (set<object_t*>::iterator it = a_datasources.begin(); it != a_datasources.end(); ++it) {
if ((*it)->name().c_str() == source->name()) { // check if the datasource has already been added
debug_out(verbose, "data: datasource '%s' already added", source->name().c_str());
return *it;
}
}
if (a_datasources.insert(source).second) {
debug_out(verbose, "data: added datasource '%s' (self: %p)", source->name().c_str(), source);
}
return source;
}
void data_t::remove_datasource(object_t* source)
{
if (!source) return;
if (a_datasources.erase(source))
{
debug_out(verbose, "data: removed datasource '%s' (self: %p)", source->name().c_str(), source);
delete source;
}
}
void data_t::remove_datasource(const char* ref)
{
if (!ref) return;
for (set<object_t*>::iterator it = a_datasources.begin(); it != a_datasources.end(); ++it)
{
if ((*it)->name() == ref)
remove_datasource(*it);
}
}
/* private members */
void data_t::refresh_datasources(void) // firstly called by xplane
{
a_wantsupdate.clear();
for (set<object_t*>::iterator it = a_datasources.begin(); it != a_datasources.end(); ++it)
{
if ((*it)->refresh()) // refresh each datasource value
{
debug_out(all, "data: '%s' type %d reported new data (%s)", (*it)->name().c_str(), (*it)->getType(),(const char*)*(*it));
refresh_listeners(*it); // refresh all the listeners with this datasource
}
}
for (set<listener_t*>::iterator it = a_wantsupdate.begin(); it != a_wantsupdate.end(); ++it) // populated by refresh_listeners
{
(*it)->final_refresh();
}
}
void data_t::refresh_listeners(object_t* updated_source)
{
int not_orphaned = 0;
for (set<listener_t*>::iterator it = a_listeners.begin(); it != a_listeners.end(); ++it) // for each page
{
if ((*it)->has_object(updated_source)) // if the page is expecting updates for this datasource
{
not_orphaned++;
if ((*it)->refresh(updated_source)) // mfdpage refresh
a_wantsupdate.insert(*it); // if returned true (visible), schedule it for the final_refresh
}
}
if (!not_orphaned) // nobody wants this datasource
remove_datasource(updated_source);
}
// static member, called by X-Plane
float data_t::update(float elapsed_lastcall, float elapsed_lastloop, int n_loop, void* arg)
{
data_t* me = reinterpret_cast<data_t*>(arg);
me->refresh_datasources();
return me->upd_interval();
}
/*
** datasource_t
*/
datasource_t::datasource_t(const char* ref) : a_name(ref), a_newdata(true)
{
a_ref = XPLMFindDataRef(ref);
if (!a_ref)
throw "data source not found";
a_reftype = XPLMGetDataRefTypes(a_ref);
if (!a_reftype)
throw "data type not applicable";
a_dint = 0;
a_dfloat = 0;
a_ddouble = 0;
a_dstring.clear();
}
datasource_t::~datasource_t(void)
{
debug_out(verbose, "data: deleted data source '%s' (self: %p)", a_name.c_str(), this);
}
const std::string& datasource_t::name(void)
{
return a_name;
}
bool datasource_t::newdata_available(void)
{
return a_newdata;
}
void datasource_t::mark_dirty(void)
{
a_isdirty = true;
}
// we use standard casts for now, i.e. no exact rounding
datasource_t::operator int(void)
{
switch (a_reftype)
{
case xplmType_Int:
return a_dint;
case xplmType_Float:
return a_dfloat;
case xplmType_Double:
return a_ddouble;
default:
return 0;
}
}
datasource_t::operator double(void)
{
switch (a_reftype)
{
case xplmType_Int:
return a_dint;
case xplmType_Float:
return a_dfloat;
case xplmType_Double:
return a_ddouble;
default:
return 0;
}
}
datasource_t::operator float(void)
{
switch (a_reftype)
{
case xplmType_Int:
return a_dint;
case xplmType_Float:
return a_dfloat;
case xplmType_Double:
return a_ddouble;
default:
return 0;
}
}
datasource_t::operator const std::string&(void)
{
std::ostringstream os;
switch (a_reftype)
{
case xplmType_Int:
os << a_dint;
break;
case xplmType_Float:
os << a_dfloat;
break;
case xplmType_Double:
os << a_ddouble;
break;
case xplmType_Data:
return a_dstring;
break;
}
a_dstring.assign(os.str());
return a_dstring;
}
datasource_t::operator const char*(void)
{
std::ostringstream os;
switch (a_reftype)
{
case xplmType_Int:
os << a_dint;
break;
case xplmType_Float:
os << a_dfloat;
break;
case xplmType_Double:
os << a_ddouble;
break;
case xplmType_Data:
return a_dstring.c_str();
break;
}
a_dstring.assign(os.str());
return a_dstring.c_str();
}
/* private members */
bool datasource_t::refresh(void)
{
int dint;
float dfloat;
double ddouble;
char tdstring[256];
std::string dstring;
a_newdata = false;
switch (a_reftype)
{
case xplmType_Int:
if (((dint = XPLMGetDatai(a_ref)) != a_dint) || a_isdirty)
{
a_dint = dint;
a_isdirty = false;
return (a_newdata = true);
}
else return false;
case xplmType_Float:
if (a_name == "sim/time/local_time_sec" || a_name == "sim/time/zulu_time_sec")
{
if (((dint = XPLMGetDataf(a_ref)) != (int)a_dfloat) || a_isdirty)
{
a_dfloat = dint;
a_isdirty = false;
return (a_newdata = true);
}
else return false;
}
else if (((dfloat = XPLMGetDataf(a_ref)) != a_dfloat) || a_isdirty)
{
a_dfloat = dfloat;
a_isdirty = false;
return (a_newdata = true);
}
else return false;
case xplmType_Double:
if (((ddouble = XPLMGetDatad(a_ref)) != a_ddouble) || a_isdirty)
{
a_ddouble = ddouble;
a_isdirty = false;
return (a_newdata = true);
}
else return false;
case xplmType_FloatArray:
return false;
case xplmType_IntArray:
return false;
case xplmType_Data:
XPLMGetDatab(a_ref, tdstring, 0, sizeof(tdstring)-1);
dstring.assign(tdstring);
if (dstring != a_dstring || a_isdirty)
{
a_dstring = dstring;
a_isdirty = false;
return (a_newdata = true);
}
else return false;
case xplmType_Unknown:
return false;
default:
return false;
}
}
int datasource_t::getType(void)
{
return a_reftype;
}