source: xplra/src/xplra/GetDataRefTask.h@ 11:215163a309ee

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

Updated to the new, separated VSCPL and XPLCOMMON libraries

File size: 22.1 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 maxCount the maximal number of items to retrieve. If <0,
249 * the function will perform an extra query to retrieve the size of
250 * of the dataref's data, which it will store in the task, so
251 * later on it can be reused.
252 */
253 GetArrayDataRefTask(const std::string& name,
254 int maxCount = -1, int offset = 0);
255
256 /**
257 * Construct the task for the given dataref
258 *
259 * @param maxCount the maximal number of items to retrieve. If <0,
260 * the function will perform an extra query to retrieve the size
261 * of the dataref's data, which it will store in the task, so
262 * later on it can be reused.
263 */
264 GetArrayDataRefTask(XPLMDataRef dataRef,
265 int maxCount = -1, int offset = 0);
266
267public:
268 /**
269 * Destroy the object.
270 */
271 virtual ~GetArrayDataRefTask();
272
273 /**
274 * Get the data.
275 */
276 const T* getData() const;
277
278 /**
279 * Get the length of the data.
280 */
281 int getLength() const;
282
283protected:
284 /**
285 * Process the dataref by querying its value.
286 */
287 virtual void process();
288
289 /**
290 * Write the value into the given stream.
291 */
292 virtual void writeValue(hu::varadiistvan::scpl::io::DataStream& stream);
293};
294
295//------------------------------------------------------------------------------
296//------------------------------------------------------------------------------
297
298/**
299 * A dataref task which retrieves the value of a float array.
300 */
301class GetFloatArrayDataRefTask :
302 public GetArrayDataRefTask<float, GetFloatArrayDataRefTask>
303{
304public:
305 /**
306 * Get the data via XPLM
307 */
308 static int queryData(XPLMDataRef dataRef, float* dest,
309 int offset, int count);
310
311public:
312 /**
313 * Construct the task for the dataref with the given name.
314 *
315 * @param maxCount the maximal number of data items to
316 * retrieve. If <0, the function will perform an extra query to
317 * retrieve the size of the dataref's data, which it will store in
318 * the task, so later on it can be reused.
319 */
320 GetFloatArrayDataRefTask(const std::string& name,
321 int maxCount = -1, int offset = 0);
322
323 /**
324 * Construct the task for the given dataref
325 *
326 * @param maxBytes the maximal number of data items to
327 * retrieve. If <0, the function will perform an extra query to
328 * retrieve the size of the dataref's data, which it will store in
329 * the task, so later on it can be reused.
330 */
331 GetFloatArrayDataRefTask(XPLMDataRef dataRef,
332 int maxCount = -1, int offset = 0);
333};
334
335//------------------------------------------------------------------------------
336//------------------------------------------------------------------------------
337
338/**
339 * A dataref task which retrieves the value of an integer array.
340 */
341class GetIntArrayDataRefTask :
342 public GetArrayDataRefTask<int, GetIntArrayDataRefTask>
343{
344public:
345 /**
346 * Get the data via XPLM
347 */
348 static int queryData(XPLMDataRef dataRef, int* dest,
349 int offset, int count);
350
351public:
352 /**
353 * Construct the task for the dataref with the given name.
354 *
355 * @param maxCount the maximal number of data items to
356 * retrieve. If <0, the function will perform an extra query to
357 * retrieve the size of the dataref's data, which it will store in
358 * the task, so later on it can be reused.
359 */
360 GetIntArrayDataRefTask(const std::string& name,
361 int maxCount = -1, int offset = 0);
362
363 /**
364 * Construct the task for the given dataref
365 *
366 * @param maxBytes the maximal number of data items to
367 * retrieve. If <0, the function will perform an extra query to
368 * retrieve the size of the dataref's data, which it will store in
369 * the task, so later on it can be reused.
370 */
371 GetIntArrayDataRefTask(XPLMDataRef dataRef,
372 int maxCount = -1, int offset = 0);
373};
374
375//------------------------------------------------------------------------------
376//------------------------------------------------------------------------------
377
378/**
379 * A dataref task which retrieves the value of a byte array.
380 */
381class GetByteArrayDataRefTask :
382 public GetArrayDataRefTask<unsigned char, GetByteArrayDataRefTask>
383{
384public:
385 /**
386 * Get the data via XPLM
387 */
388 static int queryData(XPLMDataRef dataRef, void* dest,
389 int offset, int count);
390
391public:
392 /**
393 * Construct the task for the dataref with the given name.
394 *
395 * @param maxBytes the maximal number of bytes to retrieve. If <0,
396 * the function will perform an extra query to retrieve the size
397 * of the dataref's data, which it will store in the task, so
398 * later on it can be reused.
399 */
400 GetByteArrayDataRefTask(const std::string& name,
401 int maxBytes = -1, int offset = 0);
402
403 /**
404 * Construct the task for the given dataref
405 *
406 * @param maxBytes the maximal number of bytes to retrieve. If <0,
407 * the function will perform an extra query to retrieve the size
408 * of the dataref's data, which it will store in the task, so
409 * later on it can be reused.
410 */
411 GetByteArrayDataRefTask(XPLMDataRef dataRef,
412 int maxBytes = -1, int offset = 0);
413};
414
415//------------------------------------------------------------------------------
416// Template definitions
417//------------------------------------------------------------------------------
418
419template <typename T, class ConcreteClass>
420inline GetScalarDataRefTask<T, ConcreteClass>::
421GetScalarDataRefTask(const std::string& name) :
422 GetDataRefTask(name),
423 value(0)
424{
425}
426
427//------------------------------------------------------------------------------
428
429template <typename T, class ConcreteClass>
430inline GetScalarDataRefTask<T, ConcreteClass>::
431GetScalarDataRefTask(XPLMDataRef dataRef) :
432 GetDataRefTask(dataRef),
433 value(0)
434{
435}
436
437//------------------------------------------------------------------------------
438
439template <typename T, class ConcreteClass>
440inline T GetScalarDataRefTask<T, ConcreteClass>::getValue() const
441{
442 return value;
443}
444
445//------------------------------------------------------------------------------
446
447template <typename T, class ConcreteClass>
448void GetScalarDataRefTask<T, ConcreteClass>::process()
449{
450 value = ConcreteClass::queryData(getDataRef());
451}
452
453//------------------------------------------------------------------------------
454//------------------------------------------------------------------------------
455
456template <typename T, class ConcreteClass>
457inline GetArrayDataRefTask<T, ConcreteClass>::
458GetArrayDataRefTask(const std::string& name, int maxCount, int offset) :
459 GetDataRefTask(name),
460 maxCount(maxCount),
461 offset(offset),
462 data( (maxCount>0) ? new T[maxCount] : 0 ),
463 length(-1)
464{
465}
466
467//------------------------------------------------------------------------------
468
469template <typename T, class ConcreteClass>
470inline GetArrayDataRefTask<T, ConcreteClass>::
471GetArrayDataRefTask(XPLMDataRef dataRef, int maxCount, int offset) :
472 GetDataRefTask(dataRef),
473 maxCount(maxCount),
474 offset(offset),
475 data( (maxCount>0) ? new T[maxCount] : 0),
476 length(-1)
477{
478}
479
480//------------------------------------------------------------------------------
481
482template <typename T, class ConcreteClass>
483inline GetArrayDataRefTask<T, ConcreteClass>::~GetArrayDataRefTask()
484{
485 delete [] data;
486}
487
488//------------------------------------------------------------------------------
489
490template <typename T, class ConcreteClass>
491inline const T* GetArrayDataRefTask<T, ConcreteClass>::getData() const
492{
493 return data;
494}
495
496//------------------------------------------------------------------------------
497
498template <typename T, class ConcreteClass>
499inline int GetArrayDataRefTask<T, ConcreteClass>::getLength() const
500{
501 return length;
502}
503
504//------------------------------------------------------------------------------
505
506template <typename T, class ConcreteClass>
507void GetArrayDataRefTask<T, ConcreteClass>::process()
508{
509 if (maxCount<0) {
510 maxCount = ConcreteClass::queryData(getDataRef(), 0, 0, 0);
511
512 if (maxCount>0) {
513 maxCount -= offset;
514 }
515
516 if (maxCount>0) {
517 data = new T[maxCount];
518 } else {
519 maxCount = 0;
520 }
521 }
522
523 // Util::debug("GetByteArrayDataRefTask::process: dataRef=%p, maxBytes=%d, data=%p, offset\n",
524 // getDataRef(), maxBytes, data, offset);
525 if (maxCount>0) {
526 length = ConcreteClass::queryData(getDataRef(), data, offset, maxCount);
527 } else {
528 length = maxCount;
529 }
530}
531
532//------------------------------------------------------------------------------
533
534template <typename T, class ConcreteClass>
535void GetArrayDataRefTask<T, ConcreteClass>::
536writeValue(hu::varadiistvan::scpl::io::DataStream& stream)
537{
538 stream.writeS32(length);
539 if (length>0) {
540 stream.write(data, length * sizeof(T));
541 }
542}
543
544//------------------------------------------------------------------------------
545// Inline definitions
546//------------------------------------------------------------------------------
547
548inline GetDataRefTask::GetDataRefTask(const std::string& name) :
549 DataRefTask(name)
550{
551}
552
553//------------------------------------------------------------------------------
554
555inline GetDataRefTask::GetDataRefTask(XPLMDataRef dataRef) :
556 DataRefTask(dataRef)
557{
558}
559
560//------------------------------------------------------------------------------
561//------------------------------------------------------------------------------
562
563inline int GetIntDataRefTask::queryData(XPLMDataRef dataRef)
564{
565 return XPLMGetDatai(dataRef);
566}
567
568//------------------------------------------------------------------------------
569
570inline GetIntDataRefTask::GetIntDataRefTask(const std::string& name) :
571 GetScalarDataRefTask<int, GetIntDataRefTask>(name)
572{
573}
574
575//------------------------------------------------------------------------------
576
577inline GetIntDataRefTask::GetIntDataRefTask(XPLMDataRef dataRef) :
578 GetScalarDataRefTask<int, GetIntDataRefTask>(dataRef)
579{
580}
581
582//------------------------------------------------------------------------------
583//------------------------------------------------------------------------------
584
585inline float GetFloatDataRefTask::queryData(XPLMDataRef dataRef)
586{
587 return XPLMGetDataf(dataRef);
588}
589
590//------------------------------------------------------------------------------
591
592inline GetFloatDataRefTask::GetFloatDataRefTask(const std::string& name) :
593 GetScalarDataRefTask<float, GetFloatDataRefTask>(name)
594{
595}
596
597//------------------------------------------------------------------------------
598
599inline GetFloatDataRefTask::GetFloatDataRefTask(XPLMDataRef dataRef) :
600 GetScalarDataRefTask<float, GetFloatDataRefTask>(dataRef)
601{
602}
603
604//------------------------------------------------------------------------------
605//------------------------------------------------------------------------------
606
607inline double GetDoubleDataRefTask::queryData(XPLMDataRef dataRef)
608{
609 return XPLMGetDatad(dataRef);
610}
611
612//------------------------------------------------------------------------------
613
614inline GetDoubleDataRefTask::GetDoubleDataRefTask(const std::string& name) :
615 GetScalarDataRefTask<double, GetDoubleDataRefTask>(name)
616{
617}
618
619//------------------------------------------------------------------------------
620
621inline GetDoubleDataRefTask::GetDoubleDataRefTask(XPLMDataRef dataRef) :
622 GetScalarDataRefTask<double, GetDoubleDataRefTask>(dataRef)
623{
624}
625
626//------------------------------------------------------------------------------
627//------------------------------------------------------------------------------
628
629inline int GetFloatArrayDataRefTask::queryData(XPLMDataRef dataRef, float* dest,
630 int offset, int count)
631{
632 return XPLMGetDatavf(dataRef, dest, offset, count);
633}
634
635//------------------------------------------------------------------------------
636
637inline
638GetFloatArrayDataRefTask::GetFloatArrayDataRefTask(const std::string& name,
639 int maxCount, int offset) :
640 GetArrayDataRefTask<float, GetFloatArrayDataRefTask>(name, maxCount, offset)
641{
642}
643
644//------------------------------------------------------------------------------
645
646inline
647GetFloatArrayDataRefTask::GetFloatArrayDataRefTask(XPLMDataRef dataRef,
648 int maxCount, int offset) :
649 GetArrayDataRefTask<float, GetFloatArrayDataRefTask>(dataRef,
650 maxCount, offset)
651{
652}
653
654//------------------------------------------------------------------------------
655//------------------------------------------------------------------------------
656
657inline int GetIntArrayDataRefTask::queryData(XPLMDataRef dataRef, int* dest,
658 int offset, int count)
659{
660 return XPLMGetDatavi(dataRef, dest, offset, count);
661}
662
663//------------------------------------------------------------------------------
664
665inline
666GetIntArrayDataRefTask::GetIntArrayDataRefTask(const std::string& name,
667 int maxCount, int offset) :
668 GetArrayDataRefTask<int, GetIntArrayDataRefTask>(name, maxCount, offset)
669{
670}
671
672//------------------------------------------------------------------------------
673
674inline
675GetIntArrayDataRefTask::GetIntArrayDataRefTask(XPLMDataRef dataRef,
676 int maxCount, int offset) :
677 GetArrayDataRefTask<int, GetIntArrayDataRefTask>(dataRef,
678 maxCount, offset)
679{
680}
681
682//------------------------------------------------------------------------------
683//------------------------------------------------------------------------------
684
685inline int GetByteArrayDataRefTask::queryData(XPLMDataRef dataRef, void* dest,
686 int offset, int count)
687{
688 return XPLMGetDatab(dataRef, dest, offset, count);
689}
690
691//------------------------------------------------------------------------------
692
693inline
694GetByteArrayDataRefTask::GetByteArrayDataRefTask(const std::string& name,
695 int maxBytes, int offset) :
696 GetArrayDataRefTask<unsigned char, GetByteArrayDataRefTask>(name, maxBytes,
697 offset)
698{
699}
700
701//------------------------------------------------------------------------------
702
703inline
704GetByteArrayDataRefTask::GetByteArrayDataRefTask(XPLMDataRef dataRef,
705 int maxBytes, int offset) :
706 GetArrayDataRefTask<unsigned char, GetByteArrayDataRefTask>(dataRef,
707 maxBytes,
708 offset)
709{
710}
711
712//------------------------------------------------------------------------------
713
714} /* namespace xplra */
715
716//------------------------------------------------------------------------------
717#endif // XPLRA_GETDATAREFTASK_H
718
719// Local Variables:
720// mode: C++
721// c-basic-offset: 4
722// indent-tabs-mode: nil
723// End:
Note: See TracBrowser for help on using the repository browser.