31 #include "MultiBuffer.h"
35 #include <xplra/Protocol.h>
37 #include <hu/varadiistvan/scpl/io/DataStream.h>
54 inline MultiBuffer::DataRef::DataRef(
const string& name, uint8_t type,
56 size_t length,
size_t offset) noexcept :
64 size_t alignment = getAlignment();
65 dataOffset = previous->dataOffset + previous->getSize();
66 dataOffset += alignment - 1;
67 dataOffset &= ~(alignment-1);
73 inline size_t MultiBuffer::DataRef::getSize() const noexcept
76 case Protocol::TYPE_INT:
77 return sizeof(int32_t);
78 case Protocol::TYPE_FLOAT:
80 case Protocol::TYPE_DOUBLE:
81 return sizeof(double);
82 case Protocol::TYPE_FLOAT_ARRAY:
83 return sizeof(float) * length;
84 case Protocol::TYPE_INT_ARRAY:
85 return sizeof(int32_t) * length;
86 case Protocol::TYPE_BYTE_ARRAY:
87 return sizeof(uint8_t) * length;
95 inline size_t MultiBuffer::DataRef::getAlignment() const noexcept
98 case Protocol::TYPE_INT:
99 case Protocol::TYPE_INT_ARRAY:
100 return sizeof(int32_t);
101 case Protocol::TYPE_FLOAT:
102 case Protocol::TYPE_FLOAT_ARRAY:
103 return sizeof(float);
104 case Protocol::TYPE_DOUBLE:
105 return sizeof(double);
106 case Protocol::TYPE_BYTE_ARRAY:
108 return sizeof(uint8_t);
114 bool MultiBuffer::DataRef::isArray() const noexcept
117 type==Protocol::TYPE_FLOAT_ARRAY ||
118 type==Protocol::TYPE_INT_ARRAY ||
119 type==Protocol::TYPE_BYTE_ARRAY;
125 MultiBuffer::DataRef::getCopyLength(
size_t userLength,
size_t userOffset)
129 (userOffset>length) ? 0 : (length - userOffset);
130 return (userLength==0) ? maxLength : min(userLength, maxLength);
136 inline void MultiBuffer::unfinalize() noexcept
185 template <
typename T, u
int8_t type>
size_t
187 size_t length,
size_t offset)
196 value, toCopy *
sizeof(T) );
204 template <
typename T, u
int8_t type>
206 size_t length,
size_t offset)
const
215 toCopy *
sizeof(T) );
223 template <
typename T, u
int8_t type>
228 return (offset<dataRef.
length) ?
230 offset *
sizeof(T)) : 0;
236 uint8_t unregisterCommand) noexcept :
239 registerCommand(registerCommand),
240 unregisterCommand(unregisterCommand),
258 dataRefs.push_back(
DataRef(name, Protocol::TYPE_INT, getLastDataRef()));
259 return dataRefs.size() - 1;
267 dataRefs.push_back(
DataRef(name, Protocol::TYPE_FLOAT, getLastDataRef()));
268 return dataRefs.size() - 1;
276 dataRefs.push_back(
DataRef(name, Protocol::TYPE_DOUBLE, getLastDataRef()));
277 return dataRefs.size() - 1;
283 size_t offset) noexcept
286 dataRefs.push_back(
DataRef(name, Protocol::TYPE_FLOAT_ARRAY,
287 getLastDataRef(), length, offset));
288 return dataRefs.size() - 1;
294 size_t offset) noexcept
297 dataRefs.push_back(
DataRef(name, Protocol::TYPE_INT_ARRAY,
298 getLastDataRef(), length, offset));
299 return dataRefs.size() - 1;
305 size_t offset) noexcept
308 dataRefs.push_back(
DataRef(name, Protocol::TYPE_BYTE_ARRAY,
309 getLastDataRef(), length, offset));
310 return dataRefs.size() - 1;
320 data =
new unsigned char[dataSize];
321 memset(
data, 0, dataSize);
392 *
reinterpret_cast<int32_t*
>(
getData(
id, Protocol::TYPE_INT)) = value;
399 return *
reinterpret_cast<const int32_t*
>(
getData(
id, Protocol::TYPE_INT));
406 return *
reinterpret_cast<const int32_t*
>(
getData(
id, Protocol::TYPE_INT));
413 return *
reinterpret_cast<int32_t*
>(
getData(
id, Protocol::TYPE_INT));
420 *
reinterpret_cast<float*
>(
getData(
id, Protocol::TYPE_FLOAT)) = value;
427 return *
reinterpret_cast<const float*
>(
getData(
id, Protocol::TYPE_FLOAT));
434 return *
reinterpret_cast<const float*
>(
getData(
id, Protocol::TYPE_FLOAT));
441 return *
reinterpret_cast<float*
>(
getData(
id, Protocol::TYPE_FLOAT));
448 *
reinterpret_cast<double*
>(
getData(
id, Protocol::TYPE_DOUBLE)) = value;
455 return *
reinterpret_cast<const double*
>(
getData(
id, Protocol::TYPE_DOUBLE));
462 return *
reinterpret_cast<const double*
>(
getData(
id, Protocol::TYPE_DOUBLE));
469 return *
reinterpret_cast<double*
>(
getData(
id, Protocol::TYPE_DOUBLE));
475 size_t length,
size_t offset)
477 return setArray<float, Protocol::TYPE_FLOAT_ARRAY>(
id, value, length, offset);
483 size_t length,
size_t offset)
const
485 return getArray<float, Protocol::TYPE_FLOAT_ARRAY>(
id, value, length, offset);
492 return getArray<float, Protocol::TYPE_FLOAT_ARRAY>(
id, offset);
498 size_t length,
size_t offset)
501 return setArray<int32_t, Protocol::TYPE_INT_ARRAY>(
id, value, length, offset);
507 size_t length,
size_t offset)
const
509 return getArray<int32_t, Protocol::TYPE_INT_ARRAY>(
id, value, length, offset);
516 return getArray<int32_t, Protocol::TYPE_INT_ARRAY>(
id, offset);
522 size_t length,
size_t offset)
524 return setArray<uint8_t, Protocol::TYPE_BYTE_ARRAY>(
id, value, length, offset);
530 size_t length,
size_t offset)
const
532 return getArray<uint8_t, Protocol::TYPE_BYTE_ARRAY>(
id, value, length, offset);
539 return getArray<uint8_t, Protocol::TYPE_BYTE_ARRAY>(
id, offset);
547 return setByteArray(
id,
reinterpret_cast<const uint8_t*
>(value.c_str()),
548 value.length(), offset);
563 while(length<toCopy && str[length]!=0) ++length;
566 return string(
reinterpret_cast<const char*
>(str), length);
573 return reinterpret_cast<const char*
>(
getByteArray(
id, offset));
583 for(vector<DataRef>::const_iterator i =
dataRefs.begin();
bool isArray() const noexcept
size_t getSize() const noexcept
size_t getCopyLength(size_t userLength, size_t userOffset) const noexcept
bool unregisterSafelyFromXPlane() noexcept
double getDouble(size_t id) const
size_t setIntArray(size_t id, const int32_t *value, size_t length=0, size_t offset=0)
const DataRef & getDataRef(size_t id, uint8_t type) const
void writeSpec(uint8_t command) const
std::vector< DataRef > dataRefs
size_t getByteArray(size_t id, uint8_t *value, size_t length=0, size_t offset=0) const
std::string getString(size_t id, size_t offset=0) const
const int32_t & getIntRef(size_t id) const
void * getData(size_t id, uint8_t type)
size_t addIntArray(const std::string &name, size_t length, size_t offset=0) noexcept
size_t addByteArray(const std::string &name, size_t length, size_t offset=0) noexcept
void reregisterInXPlane()
size_t setByteArray(size_t id, const uint8_t *value, size_t length=0, size_t offset=0)
void unregisterFromXPlane()
void setInt(size_t id, int value)
size_t addDouble(const std::string &name) noexcept
const char * getStringPtr(size_t id, size_t offset=0) const
const double & getDoubleRef(size_t id) const
size_t getIntArray(size_t id, int32_t *value, size_t length=0, size_t offset=0) const
void setDouble(size_t id, double value)
virtual void doExecute()=0
MultiBuffer(XPlane &xplane, uint8_t registerCommand, uint8_t unregisterCommand) noexcept
size_t getFloatArray(size_t id, float *value, size_t length=0, size_t offset=0) const
const float & getFloatRef(size_t id) const
const DataRef * getLastDataRef() const noexcept
size_t setArray(size_t id, const T *value, size_t length, size_t offset)
virtual ~MultiBuffer() noexcept
size_t addFloat(const std::string &name) noexcept
size_t setString(size_t id, const std::string &value, size_t offset=0)
int getInt(size_t id) const
size_t setFloatArray(size_t id, const float *value, size_t length=0, size_t offset=0)
size_t addInt(const std::string &name) noexcept
size_t addFloatArray(const std::string &name, size_t length, size_t offset=0) noexcept
size_t getArray(size_t id, T *value, size_t length, size_t offset) const
void forgetRegistration() noexcept
float getFloat(size_t id) const
virtual void doExecuteUnregistered()=0
uint8_t unregisterCommand
void setFloat(size_t id, float value)
scpl::io::DataStream * stream
void checkResult(uint8_t result, bool hasParameter=false, long parameter=0)