X-Plane Remote Access Plugin and Client Library
GetDataRefTask.h
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 
39 namespace xplra {
40 
41 //------------------------------------------------------------------------------
42 
47 {
48 public:
57  static GetDataRefTask* create(uint8_t& result,
58  hu::varadiistvan::scpl::io::DataStream& stream);
59 
63  GetDataRefTask(const std::string& name);
64 
68  GetDataRefTask(XPLMDataRef dataRef);
69 
73  virtual void writeValue(hu::varadiistvan::scpl::io::DataStream& stream) = 0;
74 };
75 
76 //------------------------------------------------------------------------------
77 
84 template <typename T, class ConcreteClass>
86 {
87 private:
91  T value;
92 
93 public:
97  GetScalarDataRefTask(const std::string& name);
98 
103 
107  T getValue() const;
108 
109 protected:
113  virtual void process();
114 };
115 
116 //------------------------------------------------------------------------------
117 //------------------------------------------------------------------------------
118 
123  public GetScalarDataRefTask<int, GetIntDataRefTask>
124 {
125 public:
129  static int queryData(XPLMDataRef dataRef);
130 
134  GetIntDataRefTask(const std::string& name);
135 
139  GetIntDataRefTask(XPLMDataRef dataRef);
140 
144  virtual void writeValue(hu::varadiistvan::scpl::io::DataStream& stream);
145 };
146 
147 //------------------------------------------------------------------------------
148 //------------------------------------------------------------------------------
149 
155  public GetScalarDataRefTask<float, GetFloatDataRefTask>
156 {
157 public:
161  static float queryData(XPLMDataRef dataRef);
162 
166  GetFloatDataRefTask(const std::string& name);
167 
171  GetFloatDataRefTask(XPLMDataRef dataRef);
172 
176  virtual void writeValue(hu::varadiistvan::scpl::io::DataStream& stream);
177 };
178 
179 //------------------------------------------------------------------------------
180 //------------------------------------------------------------------------------
181 
187  public GetScalarDataRefTask<double, GetDoubleDataRefTask>
188 {
189 public:
193  static double queryData(XPLMDataRef dataRef);
194 
198  GetDoubleDataRefTask(const std::string& name);
199 
203  GetDoubleDataRefTask(XPLMDataRef dataRef);
204 
208  virtual void writeValue(hu::varadiistvan::scpl::io::DataStream& stream);
209 };
210 
211 //------------------------------------------------------------------------------
212 //------------------------------------------------------------------------------
213 
220 template <typename T, class ConcreteClass>
222 {
223 private:
227  int maxCount;
228 
232  int offset;
233 
237  T* data;
238 
242  int length;
243 
244 protected:
255  GetArrayDataRefTask(const std::string& name,
256  int maxCount = -1, int offset = 0);
257 
269  int maxCount = -1, int offset = 0);
270 
271 public:
276 
280  const T* getData() const;
281 
285  int getLength() const;
286 
287 protected:
291  virtual void process();
292 
296  virtual void writeValue(hu::varadiistvan::scpl::io::DataStream& stream);
297 };
298 
299 //------------------------------------------------------------------------------
300 //------------------------------------------------------------------------------
301 
306  public GetArrayDataRefTask<float, GetFloatArrayDataRefTask>
307 {
308 public:
312  static int queryData(XPLMDataRef dataRef, float* dest,
313  int offset, int count);
314 
315 public:
326  GetFloatArrayDataRefTask(const std::string& name,
327  int maxCount = -1, int offset = 0);
328 
339  GetFloatArrayDataRefTask(XPLMDataRef dataRef,
340  int maxCount = -1, int offset = 0);
341 };
342 
343 //------------------------------------------------------------------------------
344 //------------------------------------------------------------------------------
345 
350  public GetArrayDataRefTask<int, GetIntArrayDataRefTask>
351 {
352 public:
356  static int queryData(XPLMDataRef dataRef, int* dest,
357  int offset, int count);
358 
359 public:
370  GetIntArrayDataRefTask(const std::string& name,
371  int maxCount = -1, int offset = 0);
372 
383  GetIntArrayDataRefTask(XPLMDataRef dataRef,
384  int maxCount = -1, int offset = 0);
385 };
386 
387 //------------------------------------------------------------------------------
388 //------------------------------------------------------------------------------
389 
394  public GetArrayDataRefTask<unsigned char, GetByteArrayDataRefTask>
395 {
396 public:
400  static int queryData(XPLMDataRef dataRef, void* dest,
401  int offset, int count);
402 
403 public:
414  GetByteArrayDataRefTask(const std::string& name,
415  int maxBytes = -1, int offset = 0);
416 
427  GetByteArrayDataRefTask(XPLMDataRef dataRef,
428  int maxBytes = -1, int offset = 0);
429 };
430 
431 //------------------------------------------------------------------------------
432 // Template definitions
433 //------------------------------------------------------------------------------
434 
435 template <typename T, class ConcreteClass>
437 GetScalarDataRefTask(const std::string& name) :
438  GetDataRefTask(name),
439  value(0)
440 {
441 }
442 
443 //------------------------------------------------------------------------------
444 
445 template <typename T, class ConcreteClass>
447 GetScalarDataRefTask(XPLMDataRef dataRef) :
448  GetDataRefTask(dataRef),
449  value(0)
450 {
451 }
452 
453 //------------------------------------------------------------------------------
454 
455 template <typename T, class ConcreteClass>
457 {
458  return value;
459 }
460 
461 //------------------------------------------------------------------------------
462 
463 template <typename T, class ConcreteClass>
465 {
466  value = ConcreteClass::queryData(getDataRef());
467 }
468 
469 //------------------------------------------------------------------------------
470 //------------------------------------------------------------------------------
471 
472 template <typename T, class ConcreteClass>
474 GetArrayDataRefTask(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 
485 template <typename T, class ConcreteClass>
487 GetArrayDataRefTask(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 
498 template <typename T, class ConcreteClass>
500 {
501  delete [] data;
502 }
503 
504 //------------------------------------------------------------------------------
505 
506 template <typename T, class ConcreteClass>
508 {
509  return data;
510 }
511 
512 //------------------------------------------------------------------------------
513 
514 template <typename T, class ConcreteClass>
516 {
517  return length;
518 }
519 
520 //------------------------------------------------------------------------------
521 
522 template <typename T, class ConcreteClass>
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 
550 template <typename T, class ConcreteClass>
552 writeValue(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 
564 inline GetDataRefTask::GetDataRefTask(const std::string& name) :
565  DataRefTask(name)
566 {
567 }
568 
569 //------------------------------------------------------------------------------
570 
571 inline GetDataRefTask::GetDataRefTask(XPLMDataRef dataRef) :
572  DataRefTask(dataRef)
573 {
574 }
575 
576 //------------------------------------------------------------------------------
577 //------------------------------------------------------------------------------
578 
579 inline int GetIntDataRefTask::queryData(XPLMDataRef dataRef)
580 {
581  return XPLMGetDatai(dataRef);
582 }
583 
584 //------------------------------------------------------------------------------
585 
586 inline GetIntDataRefTask::GetIntDataRefTask(const std::string& name) :
588 {
589 }
590 
591 //------------------------------------------------------------------------------
592 
593 inline GetIntDataRefTask::GetIntDataRefTask(XPLMDataRef dataRef) :
595 {
596 }
597 
598 //------------------------------------------------------------------------------
599 //------------------------------------------------------------------------------
600 
601 inline float GetFloatDataRefTask::queryData(XPLMDataRef dataRef)
602 {
603  return XPLMGetDataf(dataRef);
604 }
605 
606 //------------------------------------------------------------------------------
607 
608 inline GetFloatDataRefTask::GetFloatDataRefTask(const std::string& name) :
610 {
611 }
612 
613 //------------------------------------------------------------------------------
614 
615 inline GetFloatDataRefTask::GetFloatDataRefTask(XPLMDataRef dataRef) :
617 {
618 }
619 
620 //------------------------------------------------------------------------------
621 //------------------------------------------------------------------------------
622 
623 inline double GetDoubleDataRefTask::queryData(XPLMDataRef dataRef)
624 {
625  return XPLMGetDatad(dataRef);
626 }
627 
628 //------------------------------------------------------------------------------
629 
630 inline GetDoubleDataRefTask::GetDoubleDataRefTask(const std::string& name) :
632 {
633 }
634 
635 //------------------------------------------------------------------------------
636 
637 inline GetDoubleDataRefTask::GetDoubleDataRefTask(XPLMDataRef dataRef) :
639 {
640 }
641 
642 //------------------------------------------------------------------------------
643 //------------------------------------------------------------------------------
644 
645 inline int GetFloatArrayDataRefTask::queryData(XPLMDataRef dataRef, float* dest,
646  int offset, int count)
647 {
648  return XPLMGetDatavf(dataRef, dest, offset, count);
649 }
650 
651 //------------------------------------------------------------------------------
652 
653 inline
655  int maxCount, int offset) :
656  GetArrayDataRefTask<float, GetFloatArrayDataRefTask>(name, maxCount, offset)
657 {
658 }
659 
660 //------------------------------------------------------------------------------
661 
662 inline
664  int maxCount, int offset) :
666  maxCount, offset)
667 {
668 }
669 
670 //------------------------------------------------------------------------------
671 //------------------------------------------------------------------------------
672 
673 inline int GetIntArrayDataRefTask::queryData(XPLMDataRef dataRef, int* dest,
674  int offset, int count)
675 {
676  return XPLMGetDatavi(dataRef, dest, offset, count);
677 }
678 
679 //------------------------------------------------------------------------------
680 
681 inline
683  int maxCount, int offset) :
684  GetArrayDataRefTask<int, GetIntArrayDataRefTask>(name, maxCount, offset)
685 {
686 }
687 
688 //------------------------------------------------------------------------------
689 
690 inline
692  int maxCount, int offset) :
694  maxCount, offset)
695 {
696 }
697 
698 //------------------------------------------------------------------------------
699 //------------------------------------------------------------------------------
700 
701 inline int GetByteArrayDataRefTask::queryData(XPLMDataRef dataRef, void* dest,
702  int offset, int count)
703 {
704  return XPLMGetDatab(dataRef, dest, offset, count);
705 }
706 
707 //------------------------------------------------------------------------------
708 
709 inline
711  int maxBytes, int offset) :
712  GetArrayDataRefTask<unsigned char, GetByteArrayDataRefTask>(name, maxBytes,
713  offset)
714 {
715 }
716 
717 //------------------------------------------------------------------------------
718 
719 inline
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:
XPLMDataRef dataRef
Definition: DataRefTask.h:61
std::string name
Definition: DataRefTask.h:56
GetArrayDataRefTask(const std::string &name, int maxCount=-1, int offset=0)
const T * getData() const
GetArrayDataRefTask(XPLMDataRef dataRef, int maxCount=-1, int offset=0)
virtual void writeValue(hu::varadiistvan::scpl::io::DataStream &stream)
static int queryData(XPLMDataRef dataRef, void *dest, int offset, int count)
GetByteArrayDataRefTask(const std::string &name, int maxBytes=-1, int offset=0)
virtual void writeValue(hu::varadiistvan::scpl::io::DataStream &stream)=0
static GetDataRefTask * create(uint8_t &result, hu::varadiistvan::scpl::io::DataStream &stream)
GetDataRefTask(const std::string &name)
virtual void writeValue(hu::varadiistvan::scpl::io::DataStream &stream)
static double queryData(XPLMDataRef dataRef)
GetDoubleDataRefTask(const std::string &name)
static int queryData(XPLMDataRef dataRef, float *dest, int offset, int count)
GetFloatArrayDataRefTask(const std::string &name, int maxCount=-1, int offset=0)
virtual void writeValue(hu::varadiistvan::scpl::io::DataStream &stream)
static float queryData(XPLMDataRef dataRef)
GetFloatDataRefTask(const std::string &name)
static int queryData(XPLMDataRef dataRef, int *dest, int offset, int count)
GetIntArrayDataRefTask(const std::string &name, int maxCount=-1, int offset=0)
GetIntDataRefTask(const std::string &name)
virtual void writeValue(hu::varadiistvan::scpl::io::DataStream &stream)
static int queryData(XPLMDataRef dataRef)
GetScalarDataRefTask(const std::string &name)
GetScalarDataRefTask(XPLMDataRef dataRef)
Python client module for the X-Plane Remote Access plugin.
Definition: DataRefTask.h:41