4 #ifndef SWIFT_SIM_XSWIFTBUS_DATAREFS_H
5 #define SWIFT_SIM_XSWIFTBUS_DATAREFS_H
9 #include <XPLM/XPLMDataAccess.h>
10 #include <XPLM/XPLMUtilities.h>
18 # include "datarefs.inc"
28 DataRefImpl(
char const *name) : m_ref(XPLMFindDataRef(name))
32 XPLMDebugString(
"Missing dataref:");
33 XPLMDebugString(name);
34 XPLMDebugString(
"\n");
38 bool isValid()
const {
return m_ref; }
51 class ArrayDataRefImpl
54 ArrayDataRefImpl(
char const *name,
int size) : m_ref(XPLMFindDataRef(name)), m_size(size)
58 XPLMDebugString(
"Missing dataref:");
59 XPLMDebugString(name);
60 XPLMDebugString(
"\n");
64 bool isValid()
const {
return m_ref; }
67 void implSetAll(T *
const);
70 void implGetAll(T *)
const;
73 void implSetAt(
int index, T);
76 T implGetAt(
int index)
const;
88 template <
class DataRefTraits>
91 static_assert(!DataRefTraits::is_array,
"this is an array dataref");
95 DataRef() : DataRefImpl(DataRefTraits::name()) {}
106 static_assert(DataRefTraits::writable,
"read-only dataref");
107 DataRefImpl::implSet(d);
119 using DataRefImpl::isValid;
127 template <
class DataRefTraits>
130 static_assert(DataRefTraits::is_array,
"not an array dataref");
134 ArrayDataRef() : ArrayDataRefImpl(DataRefTraits::name(), DataRefTraits::size) {}
146 void setAll(std::array<DataRefType, DataRefSize>
const &a)
148 static_assert(DataRefTraits::writable,
"read-only dataref");
149 ArrayDataRefImpl::implSetAll<DataRefType>(a.data());
153 std::array<DataRefType, DataRefSize>
getAll()
const
155 std::array<DataRefType, DataRefSize> result;
156 ArrayDataRefImpl::implGetAll<DataRefType>(result.data());
163 static_assert(DataRefTraits::writable,
"read-only dataref");
164 ArrayDataRefImpl::implSetAt(index, d);
168 DataRefType getAt(
int index)
const {
return ArrayDataRefImpl::implGetAt<DataRefType>(index); }
170 using ArrayDataRefImpl::isValid;
178 template <
class DataRefTraits>
181 static_assert(DataRefTraits::is_array,
"not an array dataref");
189 XPLMDebugString(
"Missing dataref:");
190 XPLMDebugString(DataRefTraits::name());
191 XPLMDebugString(
"\n");
207 static_assert(DataRefTraits::writable,
"read-only dataref");
208 assert((s.size() + 1) <= (DataRefTraits::size - offset));
209 XPLMSetDatab(m_ref, (
void *)s.c_str(), (
int)offset, (
int)s.size() + 1);
215 std::string s(size, 0);
216 XPLMGetDatab(m_ref, &s[0], (
int)offset, (
int)size);
217 size = s.find(
char(0));
218 if (size != std::string::npos) s.resize(size);
227 inline void DataRefImpl::implSet<int>(
int d)
229 XPLMSetDatai(m_ref, d);
232 inline void DataRefImpl::implSet<float>(
float d)
234 XPLMSetDataf(m_ref, d);
237 inline void DataRefImpl::implSet<double>(
double d)
239 XPLMSetDatad(m_ref, d);
242 inline int DataRefImpl::implGet<int>()
const
244 return XPLMGetDatai(m_ref);
247 inline float DataRefImpl::implGet<float>()
const
249 return XPLMGetDataf(m_ref);
252 inline double DataRefImpl::implGet<double>()
const
254 return XPLMGetDatad(m_ref);
258 inline void ArrayDataRefImpl::implSetAll(
int const *v)
260 XPLMSetDatavi(m_ref,
const_cast<int *
>(v), 0, m_size);
263 inline void ArrayDataRefImpl::implSetAll(
float const *v)
265 XPLMSetDatavf(m_ref,
const_cast<float *
>(v), 0, m_size);
268 inline void ArrayDataRefImpl::implGetAll(
int *v)
const
270 XPLMGetDatavi(m_ref, &v[0], 0, m_size);
273 inline void ArrayDataRefImpl::implGetAll(
float *v)
const
275 XPLMGetDatavf(m_ref, &v[0], 0, m_size);
279 inline void ArrayDataRefImpl::implSetAt<int>(
int i,
int d)
282 XPLMSetDatavi(m_ref, &d, i, 1);
285 inline void ArrayDataRefImpl::implSetAt<float>(
int i,
float d)
288 XPLMSetDatavf(m_ref, &d, i, 1);
291 inline int ArrayDataRefImpl::implGetAt<int>(
int i)
const
295 XPLMGetDatavi(m_ref, &d, i, 1);
299 inline float ArrayDataRefImpl::implGetAt<float>(
int i)
const
303 XPLMGetDatavf(m_ref, &d, i, 1);
Class providing access to a single X-Plane array dataref.
static constexpr auto DataRefSize
Array dataref size.
std::array< DataRefType, DataRefSize > getAll() const
Get the value of the whole array.
void setAt(int index, DataRefType d)
Set the value of a single element (if it is writable)
void setAll(std::array< DataRefType, DataRefSize > const &a)
Set the value of the whole array (if it is writable)
ArrayDataRef()
Constructor.
DataRefType getAt(int index) const
Get the value of a single element.
typename DataRefTraits::type DataRefType
Dataref type.
DataRefTraits TraitsType
Traits type.
Class providing access to a single X-Plane dataref.
void set(DataRefType d)
Set the value of the dataref (if it is writable)
void setAsDouble(double d)
Set as integer, avoids cast warnings such as "possible loss of data".
void setAsInt(int d)
Set as integer, avoids cast warnings such as "possible loss of data".
typename DataRefTraits::type DataRefType
Dataref type.
DataRefTraits TraitsType
Traits type.
DataRefType get() const
Get the value of the dataref.
Class providing access to a single X-Plane string dataref.
void set(std::string const &s)
Set the value of the whole string (if it is writable)
std::string getSubstr(size_t offset, size_t size) const
Get the value of part of the string.
StringDataRef()
Constructor.
bool isValid() const
True if the dataref exists.
void setSubstr(size_t offset, std::string const &s)
Set the value of part of the string (if it is writable)
std::string get() const
Get the value of the whole string.
Plugin loaded by X-Plane which publishes a DBus service.