source: xplra/src/xplra/GetDataRefTask.h@ 5:94fdd791268f

Last change on this file since 5:94fdd791268f was 4:48b5811d4b46, checked in by István Váradi <ivaradi@…>, 12 years ago

Added all the dataref retrieving tasks

File size: 20.6 KB
RevLine 
[3]1// Copyright (c) 2013 by István Váradi
2
3// This file is part of XPLRA, a remote-access plugin for X-Plane
4
5// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions are met:
7
8// 1. Redistributions of source code must retain the above copyright notice, this
9// list of conditions and the following disclaimer.
10// 2. Redistributions in binary form must reproduce the above copyright notice,
11// this list of conditions and the following disclaimer in the documentation
12// and/or other materials provided with the distribution.
13
14// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
15// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
18// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
23// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24
25// The views and conclusions contained in the software and documentation are those
26// of the authors and should not be interpreted as representing official policies,
27// either expressed or implied, of the FreeBSD Project.
28
29#ifndef XPLRA_GETDATAREFTASK_H
30#define XPLRA_GETDATAREFTASK_H
31//------------------------------------------------------------------------------
32
33#include "DataRefTask.h"
34
35//------------------------------------------------------------------------------
36
37namespace xplra {
38
39//------------------------------------------------------------------------------
40
41/**
[4]42 * Base class for tasks that query the value of some dataref.
[3]43 */
[4]44class GetDataRefTask : public DataRefTask
45{
46public:
47 /**
48 * Construct the task for the dataref with the given name.
49 */
50 GetDataRefTask(const std::string& name);
51
52 /**
53 * Construct the task for the given dataref.
54 */
55 GetDataRefTask(XPLMDataRef dataRef);
56};
57
58//------------------------------------------------------------------------------
59
60/**
61 * Base class for dataref querying tasks that query some scalar value.
62 *
63 * ConcreteClass is the actual child class. It should have a function
64 * called queryData, which wraps the corresponding function in XPLM.
65 */
66template <typename T, class ConcreteClass>
67class GetScalarDataRefTask : public GetDataRefTask
[3]68{
69private:
70 /**
[4]71 * The value retrieved.
72 */
73 T value;
74
75public:
76 /**
77 * Construct the task for the dataref with the given name.
78 */
79 GetScalarDataRefTask(const std::string& name);
80
81 /**
82 * Construct the task for the given dataref.
83 */
84 GetScalarDataRefTask(XPLMDataRef dataRef);
85
86 /**
87 * Get the value
[3]88 */
[4]89 T getValue() const;
90
91protected:
92 /**
93 * Perform the actual operation.
94 */
95 virtual void process();
96};
97
98//------------------------------------------------------------------------------
99//------------------------------------------------------------------------------
100
101/**
102 * A dataref task which retrieves the value of an integer.
103 */
104class GetIntDataRefTask :
105 public GetScalarDataRefTask<int, GetIntDataRefTask>
106{
107public:
108 /**
109 * Query the value from XPLM
110 */
111 static int queryData(XPLMDataRef dataRef);
112
113 /**
114 * Construct the task for the dataref with the given name.
115 */
116 GetIntDataRefTask(const std::string& name);
[3]117
118 /**
[4]119 * Construct the task for the given dataref.
120 */
121 GetIntDataRefTask(XPLMDataRef dataRef);
122};
123
124//------------------------------------------------------------------------------
125//------------------------------------------------------------------------------
126
127/**
128 * A dataref task which retrieves the value of a single-precision
129 * floating point data.
130 */
131class GetFloatDataRefTask :
132 public GetScalarDataRefTask<float, GetFloatDataRefTask>
133{
134public:
135 /**
136 * Query the value from XPLM
137 */
138 static float queryData(XPLMDataRef dataRef);
139
140 /**
141 * Construct the task for the dataref with the given name.
142 */
143 GetFloatDataRefTask(const std::string& name);
144
145 /**
146 * Construct the task for the given dataref.
147 */
148 GetFloatDataRefTask(XPLMDataRef dataRef);
149};
150
151//------------------------------------------------------------------------------
152//------------------------------------------------------------------------------
153
154/**
155 * A dataref task which retrieves the value of a double-precision
156 * floating point data.
157 */
158class GetDoubleDataRefTask :
159 public GetScalarDataRefTask<double, GetDoubleDataRefTask>
160{
161public:
162 /**
163 * Query the value from XPLM
164 */
165 static double queryData(XPLMDataRef dataRef);
166
167 /**
168 * Construct the task for the dataref with the given name.
169 */
170 GetDoubleDataRefTask(const std::string& name);
171
172 /**
173 * Construct the task for the given dataref.
174 */
175 GetDoubleDataRefTask(XPLMDataRef dataRef);
176};
177
178//------------------------------------------------------------------------------
179//------------------------------------------------------------------------------
180
181/**
182 * Base class for dataref querying tasks that query some array.
183 *
184 * ConcreteClass is the actual child class. It should have a function
185 * called queryData, which wraps the corresponding function in XPLM.
186 */
187template <typename T, class ConcreteClass>
188class GetArrayDataRefTask : public GetDataRefTask
189{
190private:
191 /**
192 * The maximal number of data items returned.
193 */
194 int maxCount;
195
196 /**
197 * The offset within the dataref's value to start the query from.
[3]198 */
199 int offset;
200
201 /**
[4]202 * The data.
[3]203 */
[4]204 T* data;
[3]205
206 /**
207 * The actual length of the data retrieved.
208 */
209 int length;
210
[4]211protected:
212 /**
213 * Construct the task for the dataref with the given name.
214 *
215 * @param maxCount the maximal number of items to retrieve. If <0,
216 * the function will perform an extra query to retrieve the size of
217 * of the dataref's data, which it will store in the task, so
218 * later on it can be reused.
219 */
220 GetArrayDataRefTask(const std::string& name,
221 int maxCount = -1, int offset = 0);
222
223 /**
224 * Construct the task for the given dataref
225 *
226 * @param maxCount the maximal number of items to retrieve. If <0,
227 * the function will perform an extra query to retrieve the size
228 * of the dataref's data, which it will store in the task, so
229 * later on it can be reused.
230 */
231 GetArrayDataRefTask(XPLMDataRef dataRef,
232 int maxCount = -1, int offset = 0);
233
234public:
235 /**
236 * Destroy the object.
237 */
238 virtual ~GetArrayDataRefTask();
239
240 /**
241 * Get the data.
242 */
243 const T* getData() const;
244
245 /**
246 * Get the length of the data.
247 */
248 int getLength() const;
249
250protected:
251 /**
252 * Process the dataref by querying its value.
253 */
254 virtual void process();
255};
256
257//------------------------------------------------------------------------------
258//------------------------------------------------------------------------------
259
260/**
261 * A dataref task which retrieves the value of a float array.
262 */
263class GetFloatArrayDataRefTask :
264 public GetArrayDataRefTask<float, GetFloatArrayDataRefTask>
265{
266public:
267 /**
268 * Get the data via XPLM
269 */
270 static int queryData(XPLMDataRef dataRef, float* dest,
271 int offset, int count);
272
273public:
274 /**
275 * Construct the task for the dataref with the given name.
276 *
277 * @param maxCount the maximal number of data items to
278 * retrieve. If <0, the function will perform an extra query to
279 * retrieve the size of the dataref's data, which it will store in
280 * the task, so later on it can be reused.
281 */
282 GetFloatArrayDataRefTask(const std::string& name,
283 int maxCount = -1, int offset = 0);
284
285 /**
286 * Construct the task for the given dataref
287 *
288 * @param maxBytes the maximal number of data items to
289 * retrieve. If <0, the function will perform an extra query to
290 * retrieve the size of the dataref's data, which it will store in
291 * the task, so later on it can be reused.
292 */
293 GetFloatArrayDataRefTask(XPLMDataRef dataRef,
294 int maxCount = -1, int offset = 0);
295};
296
297//------------------------------------------------------------------------------
298//------------------------------------------------------------------------------
299
300/**
301 * A dataref task which retrieves the value of an integer array.
302 */
303class GetIntArrayDataRefTask :
304 public GetArrayDataRefTask<int, GetIntArrayDataRefTask>
305{
306public:
307 /**
308 * Get the data via XPLM
309 */
310 static int queryData(XPLMDataRef dataRef, int* dest,
311 int offset, int count);
312
313public:
314 /**
315 * Construct the task for the dataref with the given name.
316 *
317 * @param maxCount the maximal number of data items to
318 * retrieve. If <0, the function will perform an extra query to
319 * retrieve the size of the dataref's data, which it will store in
320 * the task, so later on it can be reused.
321 */
322 GetIntArrayDataRefTask(const std::string& name,
323 int maxCount = -1, int offset = 0);
324
325 /**
326 * Construct the task for the given dataref
327 *
328 * @param maxBytes the maximal number of data items to
329 * retrieve. If <0, the function will perform an extra query to
330 * retrieve the size of the dataref's data, which it will store in
331 * the task, so later on it can be reused.
332 */
333 GetIntArrayDataRefTask(XPLMDataRef dataRef,
334 int maxCount = -1, int offset = 0);
335};
336
337//------------------------------------------------------------------------------
338//------------------------------------------------------------------------------
339
340/**
341 * A dataref task which retrieves the value of a byte array.
342 */
343class GetByteArrayDataRefTask :
344 public GetArrayDataRefTask<unsigned char, GetByteArrayDataRefTask>
345{
346public:
347 /**
348 * Get the data via XPLM
349 */
350 static int queryData(XPLMDataRef dataRef, void* dest,
351 int offset, int count);
352
[3]353public:
354 /**
355 * Construct the task for the dataref with the given name.
356 *
357 * @param maxBytes the maximal number of bytes to retrieve. If <0,
358 * the function will perform an extra query to retrieve the size
359 * of the dataref's data, which it will store in the task, so
360 * later on it can be reused.
361 */
362 GetByteArrayDataRefTask(const std::string& name,
363 int maxBytes = -1, int offset = 0);
364
365 /**
366 * Construct the task for the given dataref
367 *
368 * @param maxBytes the maximal number of bytes to retrieve. If <0,
369 * the function will perform an extra query to retrieve the size
370 * of the dataref's data, which it will store in the task, so
371 * later on it can be reused.
372 */
373 GetByteArrayDataRefTask(XPLMDataRef dataRef,
374 int maxBytes = -1, int offset = 0);
375};
376
377//------------------------------------------------------------------------------
[4]378// Template definitions
379//------------------------------------------------------------------------------
380
381template <typename T, class ConcreteClass>
382inline GetScalarDataRefTask<T, ConcreteClass>::
383GetScalarDataRefTask(const std::string& name) :
384 GetDataRefTask(name),
385 value(0)
386{
387}
388
389//------------------------------------------------------------------------------
390
391template <typename T, class ConcreteClass>
392inline GetScalarDataRefTask<T, ConcreteClass>::
393GetScalarDataRefTask(XPLMDataRef dataRef) :
394 GetDataRefTask(dataRef),
395 value(0)
396{
397}
398
399//------------------------------------------------------------------------------
400
401template <typename T, class ConcreteClass>
402inline T GetScalarDataRefTask<T, ConcreteClass>::getValue() const
403{
404 return value;
405}
406
[3]407//------------------------------------------------------------------------------
408
[4]409template <typename T, class ConcreteClass>
410void GetScalarDataRefTask<T, ConcreteClass>::process()
411{
412 value = ConcreteClass::queryData(getDataRef());
413}
414
415//------------------------------------------------------------------------------
416//------------------------------------------------------------------------------
417
418template <typename T, class ConcreteClass>
419inline GetArrayDataRefTask<T, ConcreteClass>::
420GetArrayDataRefTask(const std::string& name, int maxCount, int offset) :
421 GetDataRefTask(name),
422 maxCount(maxCount),
423 offset(offset),
424 data( (maxCount>0) ? new T[maxCount] : 0 ),
425 length(-1)
426{
427}
428
429//------------------------------------------------------------------------------
430
431template <typename T, class ConcreteClass>
432inline GetArrayDataRefTask<T, ConcreteClass>::
433GetArrayDataRefTask(XPLMDataRef dataRef, int maxCount, int offset) :
434 GetDataRefTask(dataRef),
435 maxCount(maxCount),
436 offset(offset),
437 data( (maxCount>0) ? new T[maxCount] : 0),
438 length(-1)
439{
440}
441
442//------------------------------------------------------------------------------
443
444template <typename T, class ConcreteClass>
445inline GetArrayDataRefTask<T, ConcreteClass>::~GetArrayDataRefTask()
446{
447 delete [] data;
448}
449
450//------------------------------------------------------------------------------
451
452template <typename T, class ConcreteClass>
453inline const T* GetArrayDataRefTask<T, ConcreteClass>::getData() const
[3]454{
455 return data;
456}
457
458//------------------------------------------------------------------------------
459
[4]460template <typename T, class ConcreteClass>
461inline int GetArrayDataRefTask<T, ConcreteClass>::getLength() const
[3]462{
463 return length;
464}
465
466//------------------------------------------------------------------------------
467
[4]468template <typename T, class ConcreteClass>
469void GetArrayDataRefTask<T, ConcreteClass>::process()
470{
471 if (maxCount<0) {
472 maxCount = ConcreteClass::queryData(getDataRef(), 0, 0, 0);
473
474 if (maxCount>0) {
475 maxCount -= offset;
476 }
477
478 if (maxCount>0) {
479 data = new T[maxCount];
480 } else {
481 maxCount = 0;
482 }
483 }
484
485 // Util::debug("GetByteArrayDataRefTask::process: dataRef=%p, maxBytes=%d, data=%p, offset\n",
486 // getDataRef(), maxBytes, data, offset);
487 if (maxCount>0) {
488 length = ConcreteClass::queryData(getDataRef(), data, offset, maxCount);
489 } else {
490 length = maxCount;
491 }
492}
493
494//------------------------------------------------------------------------------
495// Inline definitions
496//------------------------------------------------------------------------------
497
498inline GetDataRefTask::GetDataRefTask(const std::string& name) :
499 DataRefTask(name)
500{
501}
502
503//------------------------------------------------------------------------------
504
505inline GetDataRefTask::GetDataRefTask(XPLMDataRef dataRef) :
506 DataRefTask(dataRef)
507{
508}
509
510//------------------------------------------------------------------------------
511//------------------------------------------------------------------------------
512
513inline int GetIntDataRefTask::queryData(XPLMDataRef dataRef)
514{
515 return XPLMGetDatai(dataRef);
516}
517
518//------------------------------------------------------------------------------
519
520inline GetIntDataRefTask::GetIntDataRefTask(const std::string& name) :
521 GetScalarDataRefTask<int, GetIntDataRefTask>(name)
522{
523}
524
525//------------------------------------------------------------------------------
526
527inline GetIntDataRefTask::GetIntDataRefTask(XPLMDataRef dataRef) :
528 GetScalarDataRefTask<int, GetIntDataRefTask>(dataRef)
529{
530}
531
532//------------------------------------------------------------------------------
533//------------------------------------------------------------------------------
534
535inline float GetFloatDataRefTask::queryData(XPLMDataRef dataRef)
536{
537 return XPLMGetDataf(dataRef);
538}
539
540//------------------------------------------------------------------------------
541
542inline GetFloatDataRefTask::GetFloatDataRefTask(const std::string& name) :
543 GetScalarDataRefTask<float, GetFloatDataRefTask>(name)
544{
545}
546
547//------------------------------------------------------------------------------
548
549inline GetFloatDataRefTask::GetFloatDataRefTask(XPLMDataRef dataRef) :
550 GetScalarDataRefTask<float, GetFloatDataRefTask>(dataRef)
551{
552}
553
554//------------------------------------------------------------------------------
555//------------------------------------------------------------------------------
556
557inline double GetDoubleDataRefTask::queryData(XPLMDataRef dataRef)
558{
559 return XPLMGetDatad(dataRef);
560}
561
562//------------------------------------------------------------------------------
563
564inline GetDoubleDataRefTask::GetDoubleDataRefTask(const std::string& name) :
565 GetScalarDataRefTask<double, GetDoubleDataRefTask>(name)
566{
567}
568
569//------------------------------------------------------------------------------
570
571inline GetDoubleDataRefTask::GetDoubleDataRefTask(XPLMDataRef dataRef) :
572 GetScalarDataRefTask<double, GetDoubleDataRefTask>(dataRef)
573{
574}
575
576//------------------------------------------------------------------------------
577//------------------------------------------------------------------------------
578
579inline int GetFloatArrayDataRefTask::queryData(XPLMDataRef dataRef, float* dest,
580 int offset, int count)
581{
582 return XPLMGetDatavf(dataRef, dest, offset, count);
583}
584
585//------------------------------------------------------------------------------
586
587inline
588GetFloatArrayDataRefTask::GetFloatArrayDataRefTask(const std::string& name,
589 int maxCount, int offset) :
590 GetArrayDataRefTask<float, GetFloatArrayDataRefTask>(name, maxCount, offset)
591{
592}
593
594//------------------------------------------------------------------------------
595
596inline
597GetFloatArrayDataRefTask::GetFloatArrayDataRefTask(XPLMDataRef dataRef,
598 int maxCount, int offset) :
599 GetArrayDataRefTask<float, GetFloatArrayDataRefTask>(dataRef,
600 maxCount, offset)
601{
602}
603
604//------------------------------------------------------------------------------
605//------------------------------------------------------------------------------
606
607inline int GetIntArrayDataRefTask::queryData(XPLMDataRef dataRef, int* dest,
608 int offset, int count)
609{
610 return XPLMGetDatavi(dataRef, dest, offset, count);
611}
612
613//------------------------------------------------------------------------------
614
615inline
616GetIntArrayDataRefTask::GetIntArrayDataRefTask(const std::string& name,
617 int maxCount, int offset) :
618 GetArrayDataRefTask<int, GetIntArrayDataRefTask>(name, maxCount, offset)
619{
620}
621
622//------------------------------------------------------------------------------
623
624inline
625GetIntArrayDataRefTask::GetIntArrayDataRefTask(XPLMDataRef dataRef,
626 int maxCount, int offset) :
627 GetArrayDataRefTask<int, GetIntArrayDataRefTask>(dataRef,
628 maxCount, offset)
629{
630}
631
632//------------------------------------------------------------------------------
633//------------------------------------------------------------------------------
634
635inline int GetByteArrayDataRefTask::queryData(XPLMDataRef dataRef, void* dest,
636 int offset, int count)
637{
638 return XPLMGetDatab(dataRef, dest, offset, count);
639}
640
641//------------------------------------------------------------------------------
642
643inline
644GetByteArrayDataRefTask::GetByteArrayDataRefTask(const std::string& name,
645 int maxBytes, int offset) :
646 GetArrayDataRefTask<unsigned char, GetByteArrayDataRefTask>(name, maxBytes,
647 offset)
648{
649}
650
651//------------------------------------------------------------------------------
652
653inline
654GetByteArrayDataRefTask::GetByteArrayDataRefTask(XPLMDataRef dataRef,
655 int maxBytes, int offset) :
656 GetArrayDataRefTask<unsigned char, GetByteArrayDataRefTask>(dataRef,
657 maxBytes,
658 offset)
659{
660}
661
662//------------------------------------------------------------------------------
663
[3]664} /* namespace xplra */
665
666//------------------------------------------------------------------------------
667#endif // XPLRA_GETDATAREFTASK_H
668
669// Local Variables:
670// mode: C++
671// c-basic-offset: 4
672// indent-tabs-mode: nil
673// End:
Note: See TracBrowser for help on using the repository browser.