Instrument Neutral Distributed Interface INDI 2.2.5
indipropertybasic.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2021 by Pawel Soja <[email protected]>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with this library; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17*/
18
19#include "indipropertybasic.h"
20#include "indipropertybasic_p.h"
21#include <cassert>
22
23namespace INDI
24{
25
26template <typename T>
28#ifdef INDI_PROPERTY_RAW_CAST
30{
31 *new PropertyView<T>()
32}
33, PropertyPrivate(&this->typedProperty)
34, raw{false}
35#else
36 :
37 PropertyPrivate(&property)
38#endif
39, widgets(count)
40{
41 this->typedProperty.setWidgets(widgets.data(), widgets.size());
42}
43
44#ifdef INDI_PROPERTY_RAW_CAST
45template <typename T>
48{
52, raw{true}
53{ }
54#endif
56template <typename T>
59#ifdef INDI_PROPERTY_RAW_CAST
60 if (!raw)
61 {
62 // Null out 'property' before deleting to prevent the base class
63 // PropertyPrivate::~PropertyPrivate() from doing a double-free
64 // when dynamic == true. Both 'property' and &this->typedProperty
65 // point to the same heap-allocated PropertyView<T>.
66 property = nullptr;
67 delete &this->typedProperty;
68 }
69#endif
72template <typename T>
76template <typename T>
79{ }
80
81template <typename T>
82PropertyBasic<T>::PropertyBasic(const std::shared_ptr<PropertyBasicPrivate> &dd)
83 : Property(std::static_pointer_cast<PropertyPrivate>(dd))
84{ }
85
86template <typename T>
87void PropertyBasic<T>::setDeviceName(const char *name)
88{
90 d->typedProperty.setDeviceName(name);
91}
92
93template <typename T>
94void PropertyBasic<T>::setDeviceName(const std::string &name)
95{
96 D_PTR(PropertyBasic);
97 d->typedProperty.setDeviceName(name);
98}
99
100template <typename T>
101void PropertyBasic<T>::setName(const char *name)
102{
104 d->typedProperty.setName(name);
105}
106
107template <typename T>
108void PropertyBasic<T>::setName(const std::string &name)
109{
111 d->typedProperty.setName(name);
114template <typename T>
115void PropertyBasic<T>::setLabel(const char *label)
117 D_PTR(PropertyBasic);
118 d->typedProperty.setLabel(label);
120
121template <typename T>
122void PropertyBasic<T>::setLabel(const std::string &label)
123{
124 D_PTR(PropertyBasic);
125 d->typedProperty.setLabel(label);
126}
127
128template <typename T>
129void PropertyBasic<T>::setGroupName(const char *name)
130{
131 D_PTR(PropertyBasic);
132 d->typedProperty.setGroupName(name);
134
135template <typename T>
136void PropertyBasic<T>::setGroupName(const std::string &name)
139 d->typedProperty.setGroupName(name);
141
142template <typename T>
144{
146 d->typedProperty.setPermission(permission);
149template <typename T>
151{
152 D_PTR(PropertyBasic);
153 d->typedProperty.setTimeout(timeout);
154}
155
156template <typename T>
158{
159 D_PTR(PropertyBasic);
160 d->typedProperty.setState(state);
161}
162
163template <typename T>
164void PropertyBasic<T>::setTimestamp(const char *timestamp)
165{
166 D_PTR(PropertyBasic);
167 d->typedProperty.setTimestamp(timestamp);
169
170template <typename T>
171void PropertyBasic<T>::setTimestamp(const std::string &timestamp)
172{
174 d->typedProperty.setTimestamp(timestamp);
175}
177template <typename T>
179{
180 D_PTR(const PropertyBasic);
181 return d->typedProperty.getDeviceName();
182}
183
184template <typename T>
185const char *PropertyBasic<T>::getName() const
186{
187 D_PTR(const PropertyBasic);
188 return d->typedProperty.getName();
189}
190
191template <typename T>
192const char *PropertyBasic<T>::getLabel() const
193{
194 D_PTR(const PropertyBasic);
195 return d->typedProperty.getLabel();
196}
197
198template <typename T>
200{
201 D_PTR(const PropertyBasic);
202 return d->typedProperty.getGroupName();
203}
204
205template <typename T>
207{
208 D_PTR(const PropertyBasic);
209 return d->typedProperty.getPermission();
210}
211
212template <typename T>
214{
215 D_PTR(const PropertyBasic);
216 return d->typedProperty.getPermissionAsString();
217}
218
219template <typename T>
221{
222 D_PTR(const PropertyBasic);
223 return d->typedProperty.getTimeout();
224}
225
226template <typename T>
228{
229 D_PTR(const PropertyBasic);
230 return d->typedProperty.getState();
231}
232
233template <typename T>
235{
236 D_PTR(const PropertyBasic);
237 return d->typedProperty.getStateAsString();
238}
239
240template <typename T>
242{
243 D_PTR(const PropertyBasic);
244 return d->typedProperty.getTimestamp();
245}
246
247template <typename T>
249{
250 D_PTR(const PropertyBasic);
251 return d->typedProperty.isEmpty();
252}
253
254template <typename T>
255bool PropertyBasic<T>::isNameMatch(const char *otherName) const
256{
257 D_PTR(const PropertyBasic);
258 return d->typedProperty.isNameMatch(otherName);
259}
260
261template <typename T>
262bool PropertyBasic<T>::isNameMatch(const std::string &otherName) const
263{
264 D_PTR(const PropertyBasic);
265 return d->typedProperty.isNameMatch(otherName);
266}
267
268template <typename T>
269bool PropertyBasic<T>::isLabelMatch(const char *otherLabel) const
270{
271 D_PTR(const PropertyBasic);
272 return d->typedProperty.isLabelMatch(otherLabel);
273}
274
275template <typename T>
276bool PropertyBasic<T>::isLabelMatch(const std::string &otherLabel) const
277{
278 D_PTR(const PropertyBasic);
279 return d->typedProperty.isLabelMatch(otherLabel);
280}
281
282template <typename T>
284{
285 D_PTR(const PropertyBasic);
286 return d->typedProperty.load();
287}
288
289template <typename T>
290void PropertyBasic<T>::save(FILE *f) const
291{
292 D_PTR(const PropertyBasic);
293 d->typedProperty.save(f);
294}
295
296template <typename T>
298{
299 D_PTR(const PropertyBasic);
300 return d->typedProperty.snoop(root);
301}
302
303template <typename T>
304void PropertyBasic<T>::vapply(const char *format, va_list args) const
305{
306 D_PTR(const PropertyBasic);
307 d->typedProperty.vapply(format, args);
308}
309
310template <typename T>
311void PropertyBasic<T>::vdefine(const char *format, va_list args) const
312{
313 D_PTR(const PropertyBasic);
314 d->typedProperty.vdefine(format, args);
315}
316
317template <typename T>
318void PropertyBasic<T>::apply(const char *format, ...) const
319{
320 D_PTR(const PropertyBasic);
321 va_list ap;
322 va_start(ap, format);
323 d->typedProperty.vapply(format, ap);
324 va_end(ap);
325}
326
327template <typename T>
328void PropertyBasic<T>::define(const char *format, ...) const
329{
330 D_PTR(const PropertyBasic);
331 va_list ap;
332 va_start(ap, format);
333 d->typedProperty.vdefine(format, ap);
334 va_end(ap);
335}
336
337template <typename T>
339{
340 D_PTR(const PropertyBasic);
341 d->typedProperty.apply();
342}
343
344template <typename T>
346{
347 D_PTR(const PropertyBasic);
348 d->typedProperty.define();
349}
350
351template <typename T>
353{
354 D_PTR(const PropertyBasic);
355 return d->typedProperty.findWidgetByName(name);
356}
357
358template <typename T>
360{
361 auto it = findWidgetByName(name);
362 return int(it == nullptr ? -1 : it - begin());
363}
364
365template <typename T>
367{
368 D_PTR(const PropertyBasic);
369 return d->typedProperty.count();
370}
371
372template <typename T>
374{
375 D_PTR(PropertyBasic);
376#ifdef INDI_PROPERTY_RAW_CAST
377 assert(d->raw == false);
378#endif
379 d->widgets.resize(size);
380 d->typedProperty.setWidgets(d->widgets.data(), d->widgets.size());
381}
382
383template <typename T>
385{
386 D_PTR(PropertyBasic);
387#ifdef INDI_PROPERTY_RAW_CAST
388 assert(d->raw == false);
389#endif
390 d->widgets.reserve(size);
391 d->typedProperty.setWidgets(d->widgets.data(), d->widgets.size());
392}
393
394template <typename T>
396{
397 D_PTR(PropertyBasic);
398#ifdef INDI_PROPERTY_RAW_CAST
399 assert(d->raw == false);
400#endif
401 d->widgets.shrink_to_fit();
402 d->typedProperty.setWidgets(d->widgets.data(), d->widgets.size());
403}
404
405template <typename T>
407{
408 D_PTR(PropertyBasic);
409#ifdef INDI_PROPERTY_RAW_CAST
410 assert(d->raw == false);
411#endif
412 item.setParent(&d->typedProperty);
413 d->widgets.push_back(std::move(item));
414 d->typedProperty.setWidgets(d->widgets.data(), d->widgets.size());
415}
416
417template <typename T>
419{
420 push(std::move(WidgetView<T>(item)));
421}
422
423template <typename T>
424const WidgetView<T> *PropertyBasic<T>::at(size_t index) const
425{
426 D_PTR(const PropertyBasic);
427 return d->typedProperty.at(index);
428}
429
430template <typename T>
432{
433 D_PTR(const PropertyBasic);
434 assert(index >= 0);
435 return *d->typedProperty.at(index);
436}
437
438template <typename T>
440{
441 D_PTR(PropertyBasic);
442 return d->typedProperty.begin();
443}
444
445template <typename T>
447{
448 D_PTR(PropertyBasic);
449 return d->typedProperty.end();
450}
451
452template <typename T>
454{
455 D_PTR(const PropertyBasic);
456 return d->typedProperty.begin();
457}
458
459template <typename T>
461{
462 D_PTR(const PropertyBasic);
463 return d->typedProperty.end();
464}
465
466template <typename T>
468{
469 D_PTR(PropertyBasic);
470 return &d->typedProperty;
471}
472
473#ifdef INDI_PROPERTY_BACKWARD_COMPATIBILE
474template <typename T>
476{
477 D_PTR(PropertyBasic);
478 return static_cast<PropertyView<T> *>(static_cast<INDI::PropertyPrivate*>(d)->property);
479}
480
481template <typename T>
483{
484 D_PTR(PropertyBasic);
485 return *static_cast<PropertyView<T> *>(static_cast<INDI::PropertyPrivate*>(d)->property);
486}
487#endif
488
494
495template class PropertyBasic<IText>;
496template class PropertyBasic<INumber>;
497template class PropertyBasic<ISwitch>;
498template class PropertyBasic<ILight>;
499template class PropertyBasic<IBLOB>;
500
501}
typename WidgetTraits< T >::PropertyType RawPropertyType
void setDeviceName(const char *name)
void push(WidgetView< T > &&item)
void vapply(const char *format, va_list args) const
void setState(IPState state)
bool isLabelMatch(const char *otherLabel) const
const char * getPermissionAsString() const
WidgetView< T > * findWidgetByName(const char *name) const
const char * getDeviceName() const
void setName(const char *name)
bool load()
load Attempt to load property values from configuration file.
WidgetView< T > * begin()
void save(FILE *f) const
save Save property to configuration file.
WidgetView< T > * end()
void vdefine(const char *format, va_list args) const
void resize(size_t size)
PropertyView< T > * operator&()
void setLabel(const char *label)
const char * getName() const
bool isNameMatch(const char *otherName) const
bool snoop(XMLEle *root)
Inspect the root XML and snoop the property if it exists in the root element.
INDI::PropertyView< T > * operator->()
const char * getGroupName() const
const char * getStateAsString() const
const char * getLabel() const
void setPermission(IPerm permission)
void void void apply() const
WidgetView< T > & operator[](int index) const
int findWidgetIndexByName(const char *name) const
PropertyBasic(PropertyBasicPrivate &dd)
const WidgetView< T > * at(size_t index) const
const char * getTimestamp() const
void setTimestamp(const char *timestamp)
void setGroupName(const char *name)
void reserve(size_t size)
void setTimeout(double timeout)
INDI::PropertyView< T > operator*()
Provides generic container for INDI properties.
IPerm
Permission hint, with respect to client.
Definition indiapi.h:181
IPState
Property state.
Definition indiapi.h:158
Namespace to encapsulate INDI client, drivers, and mediator classes.
Provides decorator for Low-Level IXXXVectorProperty/IXXX.
PropertyType * cast()