X-Plane Remote Access Plugin and Client Library
MultiBuffer.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 HU_VARADIISTVAN_XPLRA_MULTIBUFFER_H
30 #define HU_VARADIISTVAN_XPLRA_MULTIBUFFER_H
31 //------------------------------------------------------------------------------
32 
33 #include "Exception.h"
34 
35 #include <string>
36 #include <vector>
37 
38 #include <inttypes.h>
39 
40 //------------------------------------------------------------------------------
41 
42 namespace hu { namespace varadiistvan { namespace xplra {
43 
44 //------------------------------------------------------------------------------
45 
46 class XPlane;
47 
48 //------------------------------------------------------------------------------
49 
56 {
57 protected:
61  class DataRef
62  {
63  public:
67  std::string name;
68 
72  uint8_t type;
73 
77  size_t length;
78 
82  size_t offset;
83 
87  size_t dataOffset;
88 
92  DataRef(const std::string& name, uint8_t type,
93  const DataRef* previous = 0,
94  size_t length = 0, size_t offset = 0) noexcept;
95 
99  size_t getSize() const noexcept;
100 
105  size_t getAlignment() const noexcept;
106 
110  bool isArray() const noexcept;
111 
116  size_t getCopyLength(size_t userLength, size_t userOffset) const noexcept;
117  };
118 
122  std::vector<DataRef> dataRefs;
123 
127  mutable unsigned char* data;
128 
133 
138 
143 
149 
150 protected:
155  uint8_t unregisterCommand) noexcept;
156 
161  virtual ~MultiBuffer() noexcept;
162 
163 public:
167  XPlane& getXPlane() const noexcept;
168 
175  size_t addInt(const std::string& name) noexcept;
176 
183  size_t addFloat(const std::string& name) noexcept;
184 
191  size_t addDouble(const std::string& name) noexcept;
192 
199  size_t addFloatArray(const std::string& name, size_t length,
200  size_t offset = 0) noexcept;
201 
208  size_t addIntArray(const std::string& name, size_t length,
209  size_t offset = 0) noexcept;
210 
217  size_t addByteArray(const std::string& name, size_t length,
218  size_t offset = 0) noexcept;
219 
225  bool finalize() const;
226 
232  void registerInXPlane();
233 
237  void unregisterFromXPlane();
238 
246  bool unregisterSafelyFromXPlane() noexcept;
247 
254  void execute();
255 
259  void setInt(size_t id, int value);
260 
264  int getInt(size_t id) const;
265 
269  const int32_t& getIntRef(size_t id) const;
270 
274  int32_t& getIntRef(size_t id);
275 
279  void setFloat(size_t id, float value);
280 
284  float getFloat(size_t id) const;
285 
289  const float& getFloatRef(size_t id) const;
290 
294  float& getFloatRef(size_t id);
295 
299  void setDouble(size_t id, double value);
300 
304  double getDouble(size_t id) const;
305 
309  const double& getDoubleRef(size_t id) const;
310 
314  double& getDoubleRef(size_t id);
315 
329  size_t setFloatArray(size_t id, const float* value,
330  size_t length = 0, size_t offset = 0);
331 
345  size_t getFloatArray(size_t id, float* value,
346  size_t length = 0, size_t offset = 0) const;
347 
356  const float* getFloatArray(size_t id, size_t offset = 0) const;
357 
371  size_t setIntArray(size_t id, const int32_t* value,
372  size_t length = 0, size_t offset = 0);
373 
387  size_t getIntArray(size_t id, int32_t* value,
388  size_t length = 0, size_t offset = 0) const;
389 
397  const int32_t* getIntArray(size_t id, size_t offset = 0) const;
398 
412  size_t setByteArray(size_t id, const uint8_t* value,
413  size_t length = 0, size_t offset = 0);
414 
428  size_t getByteArray(size_t id, uint8_t* value,
429  size_t length = 0, size_t offset = 0) const;
430 
438  const uint8_t* getByteArray(size_t id, size_t offset = 0) const;
439 
440 
448  size_t setString(size_t id, const std::string& value,
449  size_t offset = 0);
450 
455  std::string getString(size_t id, size_t offset = 0) const;
456 
461  const char* getStringPtr(size_t id, size_t offset = 0) const;
462 
463 protected:
468  virtual void doExecute() = 0;
469 
474  virtual void doExecuteUnregistered() = 0;
475 
480  void writeSpec(uint8_t command) const;
481 
493  void reregisterInXPlane();
494 
495 private:
499  void unfinalize() noexcept;
500 
507  void forgetRegistration() noexcept;
508 
512  const DataRef* getLastDataRef() const noexcept;
513 
518  const DataRef& getDataRef(size_t id, uint8_t type) const;
519 
524  void* getData(size_t id, uint8_t type);
525 
530  const void* getData(size_t id, uint8_t type) const;
531 
535  template <typename T, uint8_t type> size_t
536  setArray(size_t id, const T* value, size_t length, size_t offset);
537 
542  template <typename T, uint8_t type> size_t
543  getArray(size_t id, T* value, size_t length, size_t offset) const;
544 
549  template <typename T, uint8_t type> const T*
550  getArray(size_t id, size_t offset) const;
551 
552  friend class XPlane;
553 };
554 
555 //------------------------------------------------------------------------------
556 // Inline definitions
557 //------------------------------------------------------------------------------
558 
559 inline XPlane& MultiBuffer::getXPlane() const noexcept
560 {
561  return xplane;
562 }
563 
564 //------------------------------------------------------------------------------
565 
566 } /* namespace hu::varadiistvan::xplra */ } /* namespace hu::varadiistvan */ } /* namespace hu */
567 
568 //------------------------------------------------------------------------------
569 #endif // HU_VARADIISTVAN_XPLRA_MULTIBUFFER_H
570 
571 // Local Variables:
572 // mode: C++
573 // c-basic-offset: 4
574 // indent-tabs-mode: nil
575 // End:
DataRef(const std::string &name, uint8_t type, const DataRef *previous=0, size_t length=0, size_t offset=0) noexcept
Definition: MultiBuffer.cc:54
size_t getCopyLength(size_t userLength, size_t userOffset) const noexcept
Definition: MultiBuffer.cc:125
double getDouble(size_t id) const
Definition: MultiBuffer.cc:453
size_t setIntArray(size_t id, const int32_t *value, size_t length=0, size_t offset=0)
Definition: MultiBuffer.cc:497
const DataRef & getDataRef(size_t id, uint8_t type) const
Definition: MultiBuffer.cc:157
void writeSpec(uint8_t command) const
Definition: MultiBuffer.cc:578
std::vector< DataRef > dataRefs
Definition: MultiBuffer.h:122
size_t getByteArray(size_t id, uint8_t *value, size_t length=0, size_t offset=0) const
Definition: MultiBuffer.cc:529
std::string getString(size_t id, size_t offset=0) const
Definition: MultiBuffer.cc:553
const int32_t & getIntRef(size_t id) const
Definition: MultiBuffer.cc:404
void * getData(size_t id, uint8_t type)
Definition: MultiBuffer.cc:167
size_t addIntArray(const std::string &name, size_t length, size_t offset=0) noexcept
Definition: MultiBuffer.cc:293
size_t addByteArray(const std::string &name, size_t length, size_t offset=0) noexcept
Definition: MultiBuffer.cc:304
size_t setByteArray(size_t id, const uint8_t *value, size_t length=0, size_t offset=0)
Definition: MultiBuffer.cc:521
XPlane & getXPlane() const noexcept
Definition: MultiBuffer.h:559
void setInt(size_t id, int value)
Definition: MultiBuffer.cc:390
size_t addDouble(const std::string &name) noexcept
Definition: MultiBuffer.cc:273
const char * getStringPtr(size_t id, size_t offset=0) const
Definition: MultiBuffer.cc:571
const double & getDoubleRef(size_t id) const
Definition: MultiBuffer.cc:460
size_t getIntArray(size_t id, int32_t *value, size_t length=0, size_t offset=0) const
Definition: MultiBuffer.cc:506
void setDouble(size_t id, double value)
Definition: MultiBuffer.cc:446
size_t getFloatArray(size_t id, float *value, size_t length=0, size_t offset=0) const
Definition: MultiBuffer.cc:482
const float & getFloatRef(size_t id) const
Definition: MultiBuffer.cc:432
const DataRef * getLastDataRef() const noexcept
Definition: MultiBuffer.cc:150
size_t setArray(size_t id, const T *value, size_t length, size_t offset)
Definition: MultiBuffer.cc:186
size_t addFloat(const std::string &name) noexcept
Definition: MultiBuffer.cc:264
size_t setString(size_t id, const std::string &value, size_t offset=0)
Definition: MultiBuffer.cc:544
size_t setFloatArray(size_t id, const float *value, size_t length=0, size_t offset=0)
Definition: MultiBuffer.cc:474
size_t addInt(const std::string &name) noexcept
Definition: MultiBuffer.cc:255
size_t addFloatArray(const std::string &name, size_t length, size_t offset=0) noexcept
Definition: MultiBuffer.cc:282
size_t getArray(size_t id, T *value, size_t length, size_t offset) const
Definition: MultiBuffer.cc:205
float getFloat(size_t id) const
Definition: MultiBuffer.cc:425
void setFloat(size_t id, float value)
Definition: MultiBuffer.cc:418
Python client module for the X-Plane Remote Access plugin.
Definition: DataRefTask.h:41