source: xplra/src/plugin/src/xplra/GetDataRefTask.h@ 66:f7c8521991df

Last change on this file since 66:f7c8521991df was 66:f7c8521991df, checked in by István Váradi <ivaradi@…>, 11 years ago

Added the Doxygen configuration file and updated the documentation to eliminate the warnings

File size: 22.9 KB
Line 
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#include <hu/varadiistvan/scpl/io/DataStream.h>
36
37//------------------------------------------------------------------------------
38
39namespace xplra {
40
41//------------------------------------------------------------------------------
42
43/**
44 * Base class for tasks that query the value of some dataref.
45 */
46class GetDataRefTask : public DataRefTask
47{
48public:
49 /**
50 * Read a dataref query specification and create the appropriate
51 * GetDataRefTask instance.
52 *
53 * @return the new instance, or 0 on error. In that case the given
54 * result reference variable will be filled with the error code,
55 * unless the stream failed, in which case it remains RESULT_OK.
56 */
57 static GetDataRefTask* create(uint8_t& result,
58 hu::varadiistvan::scpl::io::DataStream& stream);
59
60 /**
61 * Construct the task for the dataref with the given name.
62 */
63 GetDataRefTask(const std::string& name);
64
65 /**
66 * Construct the task for the given dataref.
67 */
68 GetDataRefTask(XPLMDataRef dataRef);
69
70 /**
71 * Write the value into the given stream.
72 */
73 virtual void writeValue(hu::varadiistvan::scpl::io::DataStream& stream) = 0;
74};
75
76//------------------------------------------------------------------------------
77
78/**
79 * Base class for dataref querying tasks that query some scalar value.
80 *
81 * ConcreteClass is the actual child class. It should have a function
82 * called queryData, which wraps the corresponding function in XPLM.
83 */
84template <typename T, class ConcreteClass>
85class GetScalarDataRefTask : public GetDataRefTask
86{
87private:
88 /**
89 * The value retrieved.
90 */
91 T value;
92
93public:
94 /**
95 * Construct the task for the dataref with the given name.
96 */
97 GetScalarDataRefTask(const std::string& name);
98
99 /**
100 * Construct the task for the given dataref.
101 */
102 GetScalarDataRefTask(XPLMDataRef dataRef);
103
104 /**
105 * Get the value
106 */
107 T getValue() const;
108
109protected:
110 /**
111 * Perform the actual operation.
112 */
113 virtual void process();
114};
115
116//------------------------------------------------------------------------------
117//------------------------------------------------------------------------------
118
119/**
120 * A dataref task which retrieves the value of an integer.
121 */
122class GetIntDataRefTask :
123 public GetScalarDataRefTask<int, GetIntDataRefTask>
124{
125public:
126 /**
127 * Query the value from XPLM
128 */
129 static int queryData(XPLMDataRef dataRef);
130
131 /**
132 * Construct the task for the dataref with the given name.
133 */
134 GetIntDataRefTask(const std::string& name);
135
136 /**
137 * Construct the task for the given dataref.
138 */
139 GetIntDataRefTask(XPLMDataRef dataRef);
140
141 /**
142 * Write the value into the given stream.
143 */
144 virtual void writeValue(hu::varadiistvan::scpl::io::DataStream& stream);
145};
146
147//------------------------------------------------------------------------------
148//------------------------------------------------------------------------------
149
150/**
151 * A dataref task which retrieves the value of a single-precision
152 * floating point data.
153 */
154class GetFloatDataRefTask :
155 public GetScalarDataRefTask<float, GetFloatDataRefTask>
156{
157public:
158 /**
159 * Query the value from XPLM
160 */
161 static float queryData(XPLMDataRef dataRef);
162
163 /**
164 * Construct the task for the dataref with the given name.
165 */
166 GetFloatDataRefTask(const std::string& name);
167
168 /**
169 * Construct the task for the given dataref.
170 */
171 GetFloatDataRefTask(XPLMDataRef dataRef);
172
173 /**
174 * Write the value into the given stream.
175 */
176 virtual void writeValue(hu::varadiistvan::scpl::io::DataStream& stream);
177};
178
179//------------------------------------------------------------------------------
180//------------------------------------------------------------------------------
181
182/**
183 * A dataref task which retrieves the value of a double-precision
184 * floating point data.
185 */
186class GetDoubleDataRefTask :
187 public GetScalarDataRefTask<double, GetDoubleDataRefTask>
188{
189public:
190 /**
191 * Query the value from XPLM
192 */
193 static double queryData(XPLMDataRef dataRef);
194
195 /**
196 * Construct the task for the dataref with the given name.
197 */
198 GetDoubleDataRefTask(const std::string& name);
199
200 /**
201 * Construct the task for the given dataref.
202 */
203 GetDoubleDataRefTask(XPLMDataRef dataRef);
204
205 /**
206 * Write the value into the given stream.
207 */
208 virtual void writeValue(hu::varadiistvan::scpl::io::DataStream& stream);
209};
210
211//------------------------------------------------------------------------------
212//------------------------------------------------------------------------------
213
214/**
215 * Base class for dataref querying tasks that query some array.
216 *
217 * ConcreteClass is the actual child class. It should have a function
218 * called queryData, which wraps the corresponding function in XPLM.
219 */
220template <typename T, class ConcreteClass>
221class GetArrayDataRefTask : public GetDataRefTask
222{
223private:
224 /**
225 * The maximal number of data items returned.
226 */
227 int maxCount;
228
229 /**
230 * The offset within the dataref's value to start the query from.
231 */
232 int offset;
233
234 /**
235 * The data.
236 */
237 T* data;
238
239 /**
240 * The actual length of the data retrieved.
241 */
242 int length;
243
244protected:
245 /**
246 * Construct the task for the dataref with the given name.
247 *
248 * @param name the name of the dataref
249 * @param maxCount the maximal number of items to retrieve. If <0,
250 * the function will perform an extra query to retrieve the size of
251 * of the dataref's data, which it will store in the task, so
252 * later on it can be reused.
253 * @param offset the offset from which to query the array
254 */
255 GetArrayDataRefTask(const std::string& name,
256 int maxCount = -1, int offset = 0);
257
258 /**
259 * Construct the task for the given dataref
260 *
261 * @param dataRef the dataref
262 * @param maxCount the maximal number of items to retrieve. If <0,
263 * the function will perform an extra query to retrieve the size
264 * of the dataref's data, which it will store in the task, so
265 * later on it can be reused.
266 * @param offset the offset from which to query the array
267 */
268 GetArrayDataRefTask(XPLMDataRef dataRef,
269 int maxCount = -1, int offset = 0);
270
271public:
272 /**
273 * Destroy the object.
274 */
275 virtual ~GetArrayDataRefTask();
276
277 /**
278 * Get the data.
279 */
280 const T* getData() const;
281
282 /**
283 * Get the length of the data.
284 */
285 int getLength() const;
286
287protected:
288 /**
289 * Process the dataref by querying its value.
290 */
291 virtual void process();
292
293 /**
294 * Write the value into the given stream.
295 */
296 virtual void writeValue(hu::varadiistvan::scpl::io::DataStream& stream);
297};
298
299//------------------------------------------------------------------------------
300//------------------------------------------------------------------------------
301
302/**
303 * A dataref task which retrieves the value of a float array.
304 */
305class GetFloatArrayDataRefTask :
306 public GetArrayDataRefTask<float, GetFloatArrayDataRefTask>
307{
308public:
309 /**
310 * Get the data via XPLM
311 */
312 static int queryData(XPLMDataRef dataRef, float* dest,
313 int offset, int count);
314
315public:
316 /**
317 * Construct the task for the dataref with the given name.
318 *
319 * @param name the name of the dataref
320 * @param maxCount the maximal number of data items to
321 * retrieve. If <0, the function will perform an extra query to
322 * retrieve the size of the dataref's data, which it will store in
323 * the task, so later on it can be reused.
324 * @param offset the offset from which to query the array
325 */
326 GetFloatArrayDataRefTask(const std::string& name,
327 int maxCount = -1, int offset = 0);
328
329 /**
330 * Construct the task for the given dataref
331 *
332 * @param dataRef the dataref
333 * @param maxCount the maximal number of data items to
334 * retrieve. If <0, the function will perform an extra query to
335 * retrieve the size of the dataref's data, which it will store in
336 * the task, so later on it can be reused.
337 * @param offset the offset from which to query the array
338 */
339 GetFloatArrayDataRefTask(XPLMDataRef dataRef,
340 int maxCount = -1, int offset = 0);
341};
342
343//------------------------------------------------------------------------------
344//------------------------------------------------------------------------------
345
346/**
347 * A dataref task which retrieves the value of an integer array.
348 */
349class GetIntArrayDataRefTask :
350 public GetArrayDataRefTask<int, GetIntArrayDataRefTask>
351{
352public:
353 /**
354 * Get the data via XPLM
355 */
356 static int queryData(XPLMDataRef dataRef, int* dest,
357 int offset, int count);
358
359public:
360 /**
361 * Construct the task for the dataref with the given name.
362 *
363 * @param name the name of the dataref
364 * @param maxCount the maximal number of data items to
365 * retrieve. If <0, the function will perform an extra query to
366 * retrieve the size of the dataref's data, which it will store in
367 * the task, so later on it can be reused.
368 * @param offset the offset from which to query the array
369 */
370 GetIntArrayDataRefTask(const std::string& name,
371 int maxCount = -1, int offset = 0);
372
373 /**
374 * Construct the task for the given dataref
375 *
376 * @param dataRef the dataref
377 * @param maxCount the maximal number of data items to
378 * retrieve. If <0, the function will perform an extra query to
379 * retrieve the size of the dataref's data, which it will store in
380 * the task, so later on it can be reused.
381 * @param offset the offset from which to query the array
382 */
383 GetIntArrayDataRefTask(XPLMDataRef dataRef,
384 int maxCount = -1, int offset = 0);
385};
386
387//------------------------------------------------------------------------------
388//------------------------------------------------------------------------------
389
390/**
391 * A dataref task which retrieves the value of a byte array.
392 */
393class GetByteArrayDataRefTask :
394 public GetArrayDataRefTask<unsigned char, GetByteArrayDataRefTask>
395{
396public:
397 /**
398 * Get the data via XPLM
399 */
400 static int queryData(XPLMDataRef dataRef, void* dest,
401 int offset, int count);
402
403public:
404 /**
405 * Construct the task for the dataref with the given name.
406 *
407 * @param name the name of the dataref
408 * @param maxBytes the maximal number of bytes to retrieve. If <0,
409 * the function will perform an extra query to retrieve the size
410 * of the dataref's data, which it will store in the task, so
411 * later on it can be reused.
412 * @param offset the offset from which to query the array
413 */
414 GetByteArrayDataRefTask(const std::string& name,
415 int maxBytes = -1, int offset = 0);
416
417 /**
418 * Construct the task for the given dataref
419 *
420 * @param dataRef the dataref
421 * @param maxBytes the maximal number of bytes to retrieve. If <0,
422 * the function will perform an extra query to retrieve the size
423 * of the dataref's data, which it will store in the task, so
424 * later on it can be reused.
425 * @param offset the offset from which to query the array
426 */
427 GetByteArrayDataRefTask(XPLMDataRef dataRef,
428 int maxBytes = -1, int offset = 0);
429};
430
431//------------------------------------------------------------------------------
432// Template definitions
433//------------------------------------------------------------------------------
434
435template <typename T, class ConcreteClass>
436inline GetScalarDataRefTask<T, ConcreteClass>::
437GetScalarDataRefTask(const std::string& name) :
438 GetDataRefTask(name),
439 value(0)
440{
441}
442
443//------------------------------------------------------------------------------
444
445template <typename T, class ConcreteClass>
446inline GetScalarDataRefTask<T, ConcreteClass>::
447GetScalarDataRefTask(XPLMDataRef dataRef) :
448 GetDataRefTask(dataRef),
449 value(0)
450{
451}
452
453//------------------------------------------------------------------------------
454
455template <typename T, class ConcreteClass>
456inline T GetScalarDataRefTask<T, ConcreteClass>::getValue() const
457{
458 return value;
459}
460
461//------------------------------------------------------------------------------
462
463template <typename T, class ConcreteClass>
464void GetScalarDataRefTask<T, ConcreteClass>::process()
465{
466 value = ConcreteClass::queryData(getDataRef());
467}
468
469//------------------------------------------------------------------------------
470//------------------------------------------------------------------------------
471
472template <typename T, class ConcreteClass>
473inline GetArrayDataRefTask<T, ConcreteClass>::
474GetArrayDataRefTask(const std::string& name, int maxCount, int offset) :
475 GetDataRefTask(name),
476 maxCount(maxCount),
477 offset(offset),
478 data( (maxCount>0) ? new T[maxCount] : 0 ),
479 length(-1)
480{
481}
482
483//------------------------------------------------------------------------------
484
485template <typename T, class ConcreteClass>
486inline GetArrayDataRefTask<T, ConcreteClass>::
487GetArrayDataRefTask(XPLMDataRef dataRef, int maxCount, int offset) :
488 GetDataRefTask(dataRef),
489 maxCount(maxCount),
490 offset(offset),
491 data( (maxCount>0) ? new T[maxCount] : 0),
492 length(-1)
493{
494}
495
496//------------------------------------------------------------------------------
497
498template <typename T, class ConcreteClass>
499inline GetArrayDataRefTask<T, ConcreteClass>::~GetArrayDataRefTask()
500{
501 delete [] data;
502}
503
504//------------------------------------------------------------------------------
505
506template <typename T, class ConcreteClass>
507inline const T* GetArrayDataRefTask<T, ConcreteClass>::getData() const
508{
509 return data;
510}
511
512//------------------------------------------------------------------------------
513
514template <typename T, class ConcreteClass>
515inline int GetArrayDataRefTask<T, ConcreteClass>::getLength() const
516{
517 return length;
518}
519
520//------------------------------------------------------------------------------
521
522template <typename T, class ConcreteClass>
523void GetArrayDataRefTask<T, ConcreteClass>::process()
524{
525 if (maxCount<0) {
526 maxCount = ConcreteClass::queryData(getDataRef(), 0, 0, 0);
527
528 if (maxCount>0) {
529 maxCount -= offset;
530 }
531
532 if (maxCount>0) {
533 data = new T[maxCount];
534 } else {
535 maxCount = 0;
536 }
537 }
538
539 // Util::debug("GetByteArrayDataRefTask::process: dataRef=%p, maxBytes=%d, data=%p, offset\n",
540 // getDataRef(), maxBytes, data, offset);
541 if (maxCount>0) {
542 length = ConcreteClass::queryData(getDataRef(), data, offset, maxCount);
543 } else {
544 length = maxCount;
545 }
546}
547
548//------------------------------------------------------------------------------
549
550template <typename T, class ConcreteClass>
551void GetArrayDataRefTask<T, ConcreteClass>::
552writeValue(hu::varadiistvan::scpl::io::DataStream& stream)
553{
554 stream.writeS32(length);
555 if (length>0) {
556 stream.write(data, length * sizeof(T));
557 }
558}
559
560//------------------------------------------------------------------------------
561// Inline definitions
562//------------------------------------------------------------------------------
563
564inline GetDataRefTask::GetDataRefTask(const std::string& name) :
565 DataRefTask(name)
566{
567}
568
569//------------------------------------------------------------------------------
570
571inline GetDataRefTask::GetDataRefTask(XPLMDataRef dataRef) :
572 DataRefTask(dataRef)
573{
574}
575
576//------------------------------------------------------------------------------
577//------------------------------------------------------------------------------
578
579inline int GetIntDataRefTask::queryData(XPLMDataRef dataRef)
580{
581 return XPLMGetDatai(dataRef);
582}
583
584//------------------------------------------------------------------------------
585
586inline GetIntDataRefTask::GetIntDataRefTask(const std::string& name) :
587 GetScalarDataRefTask<int, GetIntDataRefTask>(name)
588{
589}
590
591//------------------------------------------------------------------------------
592
593inline GetIntDataRefTask::GetIntDataRefTask(XPLMDataRef dataRef) :
594 GetScalarDataRefTask<int, GetIntDataRefTask>(dataRef)
595{
596}
597
598//------------------------------------------------------------------------------
599//------------------------------------------------------------------------------
600
601inline float GetFloatDataRefTask::queryData(XPLMDataRef dataRef)
602{
603 return XPLMGetDataf(dataRef);
604}
605
606//------------------------------------------------------------------------------
607
608inline GetFloatDataRefTask::GetFloatDataRefTask(const std::string& name) :
609 GetScalarDataRefTask<float, GetFloatDataRefTask>(name)
610{
611}
612
613//------------------------------------------------------------------------------
614
615inline GetFloatDataRefTask::GetFloatDataRefTask(XPLMDataRef dataRef) :
616 GetScalarDataRefTask<float, GetFloatDataRefTask>(dataRef)
617{
618}
619
620//------------------------------------------------------------------------------
621//------------------------------------------------------------------------------
622
623inline double GetDoubleDataRefTask::queryData(XPLMDataRef dataRef)
624{
625 return XPLMGetDatad(dataRef);
626}
627
628//------------------------------------------------------------------------------
629
630inline GetDoubleDataRefTask::GetDoubleDataRefTask(const std::string& name) :
631 GetScalarDataRefTask<double, GetDoubleDataRefTask>(name)
632{
633}
634
635//------------------------------------------------------------------------------
636
637inline GetDoubleDataRefTask::GetDoubleDataRefTask(XPLMDataRef dataRef) :
638 GetScalarDataRefTask<double, GetDoubleDataRefTask>(dataRef)
639{
640}
641
642//------------------------------------------------------------------------------
643//------------------------------------------------------------------------------
644
645inline int GetFloatArrayDataRefTask::queryData(XPLMDataRef dataRef, float* dest,
646 int offset, int count)
647{
648 return XPLMGetDatavf(dataRef, dest, offset, count);
649}
650
651//------------------------------------------------------------------------------
652
653inline
654GetFloatArrayDataRefTask::GetFloatArrayDataRefTask(const std::string& name,
655 int maxCount, int offset) :
656 GetArrayDataRefTask<float, GetFloatArrayDataRefTask>(name, maxCount, offset)
657{
658}
659
660//------------------------------------------------------------------------------
661
662inline
663GetFloatArrayDataRefTask::GetFloatArrayDataRefTask(XPLMDataRef dataRef,
664 int maxCount, int offset) :
665 GetArrayDataRefTask<float, GetFloatArrayDataRefTask>(dataRef,
666 maxCount, offset)
667{
668}
669
670//------------------------------------------------------------------------------
671//------------------------------------------------------------------------------
672
673inline int GetIntArrayDataRefTask::queryData(XPLMDataRef dataRef, int* dest,
674 int offset, int count)
675{
676 return XPLMGetDatavi(dataRef, dest, offset, count);
677}
678
679//------------------------------------------------------------------------------
680
681inline
682GetIntArrayDataRefTask::GetIntArrayDataRefTask(const std::string& name,
683 int maxCount, int offset) :
684 GetArrayDataRefTask<int, GetIntArrayDataRefTask>(name, maxCount, offset)
685{
686}
687
688//------------------------------------------------------------------------------
689
690inline
691GetIntArrayDataRefTask::GetIntArrayDataRefTask(XPLMDataRef dataRef,
692 int maxCount, int offset) :
693 GetArrayDataRefTask<int, GetIntArrayDataRefTask>(dataRef,
694 maxCount, offset)
695{
696}
697
698//------------------------------------------------------------------------------
699//------------------------------------------------------------------------------
700
701inline int GetByteArrayDataRefTask::queryData(XPLMDataRef dataRef, void* dest,
702 int offset, int count)
703{
704 return XPLMGetDatab(dataRef, dest, offset, count);
705}
706
707//------------------------------------------------------------------------------
708
709inline
710GetByteArrayDataRefTask::GetByteArrayDataRefTask(const std::string& name,
711 int maxBytes, int offset) :
712 GetArrayDataRefTask<unsigned char, GetByteArrayDataRefTask>(name, maxBytes,
713 offset)
714{
715}
716
717//------------------------------------------------------------------------------
718
719inline
720GetByteArrayDataRefTask::GetByteArrayDataRefTask(XPLMDataRef dataRef,
721 int maxBytes, int offset) :
722 GetArrayDataRefTask<unsigned char, GetByteArrayDataRefTask>(dataRef,
723 maxBytes,
724 offset)
725{
726}
727
728//------------------------------------------------------------------------------
729
730} /* namespace xplra */
731
732//------------------------------------------------------------------------------
733#endif // XPLRA_GETDATAREFTASK_H
734
735// Local Variables:
736// mode: C++
737// c-basic-offset: 4
738// indent-tabs-mode: nil
739// End:
Note: See TracBrowser for help on using the repository browser.