source: xplra/src/plugin/src/xplra/SetDataRefTask.h@ 13:42fd631176b7

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

Reorganized code to be able to handle the client library as a normal libtool library

File size: 19.0 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_SETDATAREFTASK_H
30#define XPLRA_SETDATAREFTASK_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 dataref tasks that set the value of some data
45 * referenced by a dataref.
46 *
47 * Since XPLM provides no feedback on whether the setting is
48 * successful, we detect only if the dataref is valid or not. So the
49 * isValid() function provides the only feedback if the task executed
50 * successfully or not. (We could use XPLMCanWriteDataRef, but for
51 * efficiency reasons we refrain from it. A separate task may be
52 * created for this purpose.)
53 */
54class SetDataRefTask : public DataRefTask
55{
56public:
57 /**
58 * Read a dataref setting specification and create the appropriate
59 * SetDataRefTask instance.
60 *
61 * @param result will contain the result of the operation. It
62 * signifies an error only, if some wrong data is read. If the
63 * stream fails, it will indicate RESULT_OK.
64 *
65 * @return the new instance or 0 on error.
66 */
67 static SetDataRefTask* create(uint8_t& result, hu::varadiistvan::scpl::io::DataStream& stream);
68
69 /**
70 * Construct the task for the dataref with the given name.
71 */
72 SetDataRefTask(const std::string& name);
73
74 /**
75 * Construct the task for the given dataref.
76 */
77 SetDataRefTask(XPLMDataRef dataRef);
78
79 /**
80 * Read and set the value to update from the given stream
81 */
82 virtual void readValue(hu::varadiistvan::scpl::io::DataStream& stream) = 0;
83};
84
85//------------------------------------------------------------------------------
86//------------------------------------------------------------------------------
87
88/**
89 * Base class for dataref setting tasks that set a single scalar
90 * value.
91 *
92 * ConcreteClass is the actual child class. It should have a function
93 * name setData() which performs the actual setting.
94 */
95template <typename T, class ConcreteClass>
96class SetScalarDataRefTask : public SetDataRefTask
97{
98protected:
99 /**
100 * The value to set.
101 */
102 T value;
103
104public:
105 /**
106 * Construct the task for setting the data referenced by the
107 * dataref with the given name.
108 */
109 SetScalarDataRefTask(const std::string& name);
110
111 /**
112 * Construct the task for setting the data referenced by the given
113 * dataref.
114 */
115 SetScalarDataRefTask(XPLMDataRef dataRef);
116
117protected:
118 /**
119 * Perform the actual operation.
120 */
121 virtual void process();
122};
123
124//------------------------------------------------------------------------------
125//------------------------------------------------------------------------------
126
127/**
128 * Task to set the value of an integer data.
129 */
130class SetIntDataRefTask :
131 public SetScalarDataRefTask<int, SetIntDataRefTask>
132{
133public:
134 /**
135 * Set the given dataref's value to the given value.
136 */
137 static void setData(XPLMDataRef dataRef, int value);
138
139 /**
140 * Construct the task for the dataref with the given name.
141 */
142 SetIntDataRefTask(const std::string& name);
143
144 /**
145 * Construct the task for the given dataref.
146 */
147 SetIntDataRefTask(XPLMDataRef dataRef);
148
149 /**
150 * Read and set the value to update from the given stream
151 */
152 virtual void readValue(hu::varadiistvan::scpl::io::DataStream& stream);
153};
154
155//------------------------------------------------------------------------------
156//------------------------------------------------------------------------------
157
158/**
159 * Task to set the value of a single-precision floating point data.
160 */
161class SetFloatDataRefTask :
162 public SetScalarDataRefTask<float, SetFloatDataRefTask>
163{
164public:
165 /**
166 * Set the given dataref's value to the given value.
167 */
168 static void setData(XPLMDataRef dataRef, float value);
169
170 /**
171 * Construct the task for the dataref with the given name.
172 */
173 SetFloatDataRefTask(const std::string& name);
174
175 /**
176 * Construct the task for the given dataref.
177 */
178 SetFloatDataRefTask(XPLMDataRef dataRef);
179
180 /**
181 * Read and set the value to update from the given stream
182 */
183 virtual void readValue(hu::varadiistvan::scpl::io::DataStream& stream);
184};
185
186//------------------------------------------------------------------------------
187//------------------------------------------------------------------------------
188
189/**
190 * Task to set the value of a double-precision floating point data.
191 */
192class SetDoubleDataRefTask :
193 public SetScalarDataRefTask<double, SetDoubleDataRefTask>
194{
195public:
196 /**
197 * Set the given dataref's value to the given value.
198 */
199 static void setData(XPLMDataRef dataRef, double value);
200
201 /**
202 * Construct the task for the dataref with the given name.
203 */
204 SetDoubleDataRefTask(const std::string& name);
205
206 /**
207 * Construct the task for the given dataref.
208 */
209 SetDoubleDataRefTask(XPLMDataRef dataRef);
210
211 /**
212 * Read and set the value to update from the given stream
213 */
214 virtual void readValue(hu::varadiistvan::scpl::io::DataStream& stream);
215};
216
217//------------------------------------------------------------------------------
218//------------------------------------------------------------------------------
219
220/**
221 * Base class for dataref setting tasks that set the values of an
222 * array.
223 *
224 * ConcreteClass is the actual child class. It should have a function
225 * name setData() which performs the actual setting.
226 */
227template <typename T, class ConcreteClass>
228class SetArrayDataRefTask : public SetDataRefTask
229{
230private:
231 /**
232 * The number of data items in the value.
233 */
234 int count;
235
236 /**
237 * The offset starting with which the data should be set.
238 */
239 int offset;
240
241 /**
242 * The value to set.
243 */
244 T* value;
245
246public:
247 /**
248 * Construct the data setting task for the dataref with the given
249 * name.
250 *
251 * The value array will be allocated, but not filled with
252 * data. Thus count should be positive!
253 */
254 SetArrayDataRefTask(const std::string& name, int count, int offset = 0);
255
256 /**
257 * Construct the data setting task for the given dataref.
258 *
259 * The value array will be allocated, but not filled with
260 * data. Thus count should be positive!
261 */
262 SetArrayDataRefTask(XPLMDataRef dataRef, int count, int offset = 0);
263
264 /**
265 * Destroy the task.
266 */
267 virtual ~SetArrayDataRefTask();
268
269 /**
270 * Read and set the value to update from the given stream
271 */
272 virtual void readValue(hu::varadiistvan::scpl::io::DataStream& stream);
273
274protected:
275 /**
276 * Perform the actual operation.
277 */
278 virtual void process();
279};
280
281//------------------------------------------------------------------------------
282//------------------------------------------------------------------------------
283
284/**
285 * A dataref setting task which sets an array of integers.
286 */
287class SetIntArrayDataRefTask :
288 public SetArrayDataRefTask<int, SetIntArrayDataRefTask>
289{
290public:
291 /**
292 * Perform the actual setting.
293 */
294 static void setData(XPLMDataRef dataRef, int* value, int count, int offset);
295
296 /**
297 * Construct the task for the dataref with the given name.
298 */
299 SetIntArrayDataRefTask(const std::string& name, int count, int offset = 0);
300
301 /**
302 * Construct the task for the given dataref.
303 */
304 SetIntArrayDataRefTask(XPLMDataRef dataRef, int count, int offset = 0);
305};
306
307//------------------------------------------------------------------------------
308//------------------------------------------------------------------------------
309
310/**
311 * A dataref setting task which sets an array of single-precision
312 * floating-point values.
313 */
314class SetFloatArrayDataRefTask :
315 public SetArrayDataRefTask<float, SetFloatArrayDataRefTask>
316{
317public:
318 /**
319 * Perform the actual setting.
320 */
321 static void setData(XPLMDataRef dataRef, float* value, int count, int offset);
322
323 /**
324 * Construct the task for the dataref with the given name.
325 */
326 SetFloatArrayDataRefTask(const std::string& name, int count, int offset = 0);
327
328 /**
329 * Construct the task for the given dataref.
330 */
331 SetFloatArrayDataRefTask(XPLMDataRef dataRef, int count, int offset = 0);
332};
333
334//------------------------------------------------------------------------------
335//------------------------------------------------------------------------------
336
337/**
338 * A dataref setting task which sets an array of bytes.
339 */
340class SetByteArrayDataRefTask :
341 public SetArrayDataRefTask<unsigned char, SetByteArrayDataRefTask>
342{
343public:
344 /**
345 * Perform the actual setting.
346 */
347 static void setData(XPLMDataRef dataRef, unsigned char* value,
348 int count, int offset);
349
350 /**
351 * Construct the task for the dataref with the given name.
352 */
353 SetByteArrayDataRefTask(const std::string& name, int count, int offset = 0);
354
355 /**
356 * Construct the task for the given dataref.
357 */
358 SetByteArrayDataRefTask(XPLMDataRef dataRef, int count, int offset = 0);
359};
360
361//------------------------------------------------------------------------------
362// Template definitions
363//------------------------------------------------------------------------------
364
365template <typename T, class ConcreteClass>
366inline SetScalarDataRefTask<T, ConcreteClass>::
367SetScalarDataRefTask(const std::string& name) :
368 SetDataRefTask(name)
369{
370}
371
372//------------------------------------------------------------------------------
373
374template <typename T, class ConcreteClass>
375inline SetScalarDataRefTask<T, ConcreteClass>::
376SetScalarDataRefTask(XPLMDataRef dataRef) :
377 SetDataRefTask(dataRef)
378{
379}
380
381//------------------------------------------------------------------------------
382
383template <typename T, class ConcreteClass>
384void SetScalarDataRefTask<T, ConcreteClass>::process()
385{
386 ConcreteClass::setData(getDataRef(), value);
387}
388
389//------------------------------------------------------------------------------
390//------------------------------------------------------------------------------
391
392template <typename T, class ConcreteClass>
393inline SetArrayDataRefTask<T, ConcreteClass>::
394SetArrayDataRefTask(const std::string& name, int count, int offset) :
395 SetDataRefTask(name),
396 count(count),
397 offset(offset),
398 value(new T[count])
399{
400}
401
402//------------------------------------------------------------------------------
403
404template <typename T, class ConcreteClass>
405inline SetArrayDataRefTask<T, ConcreteClass>::
406SetArrayDataRefTask(XPLMDataRef dataRef, int count, int offset) :
407 SetDataRefTask(dataRef),
408 count(count),
409 offset(offset),
410 value(new T[count])
411{
412}
413
414//------------------------------------------------------------------------------
415
416template <typename T, class ConcreteClass>
417inline SetArrayDataRefTask<T, ConcreteClass>::~SetArrayDataRefTask()
418{
419 delete [] value;
420}
421
422//------------------------------------------------------------------------------
423
424template <typename T, class ConcreteClass>
425void SetArrayDataRefTask<T, ConcreteClass>::
426readValue(hu::varadiistvan::scpl::io::DataStream& stream)
427{
428 stream.read(value, count * sizeof(T));
429}
430
431//------------------------------------------------------------------------------
432
433template <typename T, class ConcreteClass>
434void SetArrayDataRefTask<T, ConcreteClass>::process()
435{
436 ConcreteClass::setData(getDataRef(), value, count, offset);
437}
438
439//------------------------------------------------------------------------------
440// Inline definitions
441//------------------------------------------------------------------------------
442
443inline SetDataRefTask::SetDataRefTask(const std::string& name) :
444 DataRefTask(name)
445{
446}
447
448//------------------------------------------------------------------------------
449
450inline SetDataRefTask::SetDataRefTask(XPLMDataRef dataRef) :
451 DataRefTask(dataRef)
452{
453}
454
455//------------------------------------------------------------------------------
456//------------------------------------------------------------------------------
457
458inline void SetIntDataRefTask::setData(XPLMDataRef dataRef, int value)
459{
460 XPLMSetDatai(dataRef, value);
461}
462
463//------------------------------------------------------------------------------
464
465inline SetIntDataRefTask::SetIntDataRefTask(const std::string& name) :
466 SetScalarDataRefTask<int, SetIntDataRefTask>(name)
467{
468}
469
470//------------------------------------------------------------------------------
471
472inline SetIntDataRefTask::SetIntDataRefTask(XPLMDataRef dataRef) :
473 SetScalarDataRefTask<int, SetIntDataRefTask>(dataRef)
474{
475}
476
477//------------------------------------------------------------------------------
478//------------------------------------------------------------------------------
479
480inline void SetFloatDataRefTask::setData(XPLMDataRef dataRef, float value)
481{
482 XPLMSetDatai(dataRef, value);
483}
484
485//------------------------------------------------------------------------------
486
487inline SetFloatDataRefTask::SetFloatDataRefTask(const std::string& name) :
488 SetScalarDataRefTask<float, SetFloatDataRefTask>(name)
489{
490}
491
492//------------------------------------------------------------------------------
493
494inline SetFloatDataRefTask::SetFloatDataRefTask(XPLMDataRef dataRef) :
495 SetScalarDataRefTask<float, SetFloatDataRefTask>(dataRef)
496{
497}
498
499//------------------------------------------------------------------------------
500//------------------------------------------------------------------------------
501
502inline void SetDoubleDataRefTask::setData(XPLMDataRef dataRef, double value)
503{
504 XPLMSetDatai(dataRef, value);
505}
506
507//------------------------------------------------------------------------------
508
509inline SetDoubleDataRefTask::SetDoubleDataRefTask(const std::string& name) :
510 SetScalarDataRefTask<double, SetDoubleDataRefTask>(name)
511{
512}
513
514//------------------------------------------------------------------------------
515
516inline SetDoubleDataRefTask::SetDoubleDataRefTask(XPLMDataRef dataRef) :
517 SetScalarDataRefTask<double, SetDoubleDataRefTask>(dataRef)
518{
519}
520
521//------------------------------------------------------------------------------
522//------------------------------------------------------------------------------
523
524inline void SetIntArrayDataRefTask::setData(XPLMDataRef dataRef, int* value,
525 int count, int offset)
526{
527 XPLMSetDatavi(dataRef, value, offset, count);
528}
529
530//------------------------------------------------------------------------------
531
532inline SetIntArrayDataRefTask::SetIntArrayDataRefTask(const std::string& name,
533 int count, int offset) :
534 SetArrayDataRefTask<int, SetIntArrayDataRefTask>(name, count, offset)
535{
536}
537
538//------------------------------------------------------------------------------
539
540inline SetIntArrayDataRefTask::SetIntArrayDataRefTask(XPLMDataRef dataRef,
541 int count, int offset) :
542 SetArrayDataRefTask<int, SetIntArrayDataRefTask>(dataRef, count, offset)
543{
544}
545
546//------------------------------------------------------------------------------
547//------------------------------------------------------------------------------
548
549inline void SetFloatArrayDataRefTask::setData(XPLMDataRef dataRef,
550 float* value,
551 int count, int offset)
552{
553 XPLMSetDatavf(dataRef, value, offset, count);
554}
555
556//------------------------------------------------------------------------------
557
558inline
559SetFloatArrayDataRefTask::SetFloatArrayDataRefTask(const std::string& name,
560 int count, int offset) :
561 SetArrayDataRefTask<float, SetFloatArrayDataRefTask>(name, count, offset)
562{
563}
564
565//------------------------------------------------------------------------------
566
567inline
568SetFloatArrayDataRefTask::SetFloatArrayDataRefTask(XPLMDataRef dataRef,
569 int count, int offset) :
570 SetArrayDataRefTask<float, SetFloatArrayDataRefTask>(dataRef, count, offset)
571{
572}
573
574//------------------------------------------------------------------------------
575//------------------------------------------------------------------------------
576
577inline void SetByteArrayDataRefTask::setData(XPLMDataRef dataRef,
578 unsigned char* value,
579 int count, int offset)
580{
581 XPLMSetDatab(dataRef, value, offset, count);
582}
583
584//------------------------------------------------------------------------------
585
586inline
587SetByteArrayDataRefTask::SetByteArrayDataRefTask(const std::string& name,
588 int count, int offset) :
589 SetArrayDataRefTask<unsigned char, SetByteArrayDataRefTask>(name,
590 count, offset)
591{
592}
593
594//------------------------------------------------------------------------------
595
596inline
597SetByteArrayDataRefTask::SetByteArrayDataRefTask(XPLMDataRef dataRef,
598 int count, int offset) :
599 SetArrayDataRefTask<unsigned char, SetByteArrayDataRefTask>(dataRef,
600 count, offset)
601{
602}
603
604//------------------------------------------------------------------------------
605
606} /* namespace xplra */
607
608//------------------------------------------------------------------------------
609#endif // XPLRA_SETDATAREFTASK_H
610
611// Local Variables:
612// mode: C++
613// c-basic-offset: 4
614// indent-tabs-mode: nil
615// End:
Note: See TracBrowser for help on using the repository browser.