@@ -223,6 +223,58 @@ namespace XSwiftBus
223223 XPLMDataRef m_ref;
224224 };
225225
226+ /* !
227+ * Class providing a custom variable + dataref
228+ * \tparam DataRefTraits The trait class representing the dataref.
229+ */
230+ template <class DataRefTraits >
231+ class CustomDataRef
232+ {
233+ public:
234+ // ! Constructor
235+ CustomDataRef ()
236+ {
237+ if constexpr (std::is_same_v<typename DataRefTraits::type, int >)
238+ {
239+ m_ref = XPLMRegisterDataAccessor (DataRefTraits::name (), xplmType_Int, 0 , read, NULL , NULL , NULL , NULL ,
240+ NULL , NULL , NULL , NULL , NULL , NULL , NULL , this , NULL );
241+ }
242+ else { static_assert (false , " Unsupported type" ); }
243+ if (!m_ref)
244+ {
245+ XPLMDebugString (" Missing dataref:" );
246+ XPLMDebugString (DataRefTraits::name ());
247+ XPLMDebugString (" \n " );
248+ }
249+ }
250+
251+ CustomDataRef (const CustomDataRef &) = delete ;
252+ CustomDataRef &operator =(const CustomDataRef &) = delete ;
253+ CustomDataRef (CustomDataRef &&other) = default ;
254+ CustomDataRef &operator =(CustomDataRef &&other) = default ;
255+ ~CustomDataRef () { XPLMUnregisterDataAccessor (m_ref); }
256+
257+ static typename DataRefTraits::type read (void *refcon)
258+ {
259+ return reinterpret_cast <CustomDataRef *>(refcon)->get ();
260+ }
261+
262+ // ! True if the dataref exists
263+ bool isValid () const { return m_ref != nullptr ; }
264+
265+ // ! Set the value
266+ void set (typename DataRefTraits::type val)
267+ {
268+ m_datarefVal = val;
269+ }
270+
271+ // ! Get the value
272+ typename DataRefTraits::type get () const { return m_datarefVal; }
273+
274+ XPLMDataRef m_ref;
275+ typename DataRefTraits::type m_datarefVal;
276+ };
277+
226278 template <>
227279 inline void DataRefImpl::implSet<int >(int d)
228280 {
0 commit comments