/* Copyright (C) 2021 by Pawel Soja This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ #pragma once #include "indiapi.h" #include "indidevapi.h" #include "indiwidgettraits.h" #include "indiwidgetview.h" #include #include #include #include #include namespace INDI { template struct WidgetView; template struct PropertyView; #define PROPERTYVIEW_BASE_ACCESS public // don't use direct access to low-level property //#define PROPERTYVIEW_BASE_ACCESS protected // future /** * \class INDI::PropertyView and INDI::WidgetView * \brief Provides decorator for Low-Level IXXXVectorProperty/IXXX * * INDI::PropertyView * * A class that will allow a easy transition to the new widget handling interface (future). * - Use PropertyView instead of ITextVectorProperty * - Use PropertyView instead of INumberVectorProperty * - Use PropertyView instead of ISwitchVectorProperty * - Use PropertyView instead of ILightVectorProperty * - Use PropertyView instead of IBLOBVectorProperty * * The PropertyView class is compatible with low-level IXXXVectorProperty structures. * * INDI::WidgetView * * A class that will allow a easy transition to the new widget handling interface (future). * - Use WidgetView instead of IText * - Use WidgetView instead of INumber * - Use WidgetView instead of ISwitch * - Use WidgetView instead of ILight * - Use WidgetView instead of IBLOB * * The WidgetView class is compatible with low-level IXXX structures. */ template struct PropertyView: PROPERTYVIEW_BASE_ACCESS WidgetTraits::PropertyType { using Type = T; using PropertyType = typename WidgetTraits::PropertyType; using WidgetType = WidgetView; friend class Property; friend class PropertyPrivate; friend class BaseDevice; friend class DefaultDevice; template friend struct WidgetView; template using enable_if_is_same_t = typename std::enable_if::value, bool>::type; public: PropertyView(); // #PS: do not delete items, they may be on the stack. //~PropertyView() { for(auto &p: *this) {p.clear();} free(widget()); } public: void setDeviceName(const char *name); /* outside implementation */ void setDeviceName(const std::string &name); /* outside implementation */ void setName(const char *name); /* outside implementation */ void setName(const std::string &name); /* outside implementation */ void setLabel(const char *label); /* outside implementation */ void setLabel(const std::string &label); /* outside implementation */ void setGroupName(const char *name); /* outside implementation */ void setGroupName(const std::string &name); /* outside implementation */ void setPermission(IPerm permission); /* outside implementation */ void setTimeout(double timeout); /* outside implementation */ void setState(IPState state); void setTimestamp(const char *timestamp); /* outside implementation */ void setTimestamp(const std::string ×tamp); /* outside implementation */ void setAux(void *user); /* outside implementation */ void setWidgets(WidgetType *w, size_t count); /* outside implementation */ template void setWidgets(WidgetType (&w)[N]); /* outside implementation */ public: // only for ISwitch void setRule(ISRule rule); /* outside implementation */ bool setRule(const std::string &rule); /* outside implementation */ template = true> void reset() { IUResetSwitch(this); } template = true> WidgetType * findOnSwitch() const { return static_cast(IUFindOnSwitch(this)); } template = true> int findOnSwitchIndex() const { return IUFindOnSwitchIndex(this); } public: // only for INumber template = true> void updateMinMax(); /* outside implementation - only driver side, see indipropertyview_driver.cpp */ public: //getters const char *getDeviceName() const { return this->device; } const char *getName() const { return this->name; } const char *getLabel() const { return this->label; } const char *getGroupName() const { return this->group; } IPerm getPermission() const; /* outside implementation */ const char *getPermissionAsString() const { return permStr(getPermission()); } ISRule getRule() const; /* outside implementation */ const char * getRuleAsString() const { return ruleStr(getRule()); } double getTimeout() const; /* outside implementation */ IPState getState() const { return this->s; } const char *getStateAsString() const { return pstateStr(getState()); } const char *getTimestamp() const { return this->timestamp; } void *getAux() const { return this->aux; } int count() const; /* outside implementation */ WidgetType *widget() const; /* outside implementation */ WidgetType *findWidgetByName(const char *name) const; /* outside implementation */ public: //tests bool isEmpty() const { return widget() == nullptr || count() == 0; } bool isNameMatch(const char *otherName) const { return !strcmp(getName(), otherName); } bool isNameMatch(const std::string &otherName) const { return getName() == otherName; } bool isLabelMatch(const char *otherLabel) const { return !strcmp(getLabel(), otherLabel); } bool isLabelMatch(const std::string &otherLabel) const { return getLabel() == otherLabel; } public: // only driver side void save(FILE *f) const; /* outside implementation */ void vapply(const char *format, va_list args) const; /* outside implementation - only driver side, see indipropertyview_driver.cpp */ void vdefine(const char *format, va_list args) const; /* outside implementation - only driver side, see indipropertyview_driver.cpp */ void apply(const char *format, ...) const ATTRIBUTE_FORMAT_PRINTF(2, 3); /* outside implementation - only driver side, see indipropertyview_driver.cpp */ void define(const char *format, ...) const ATTRIBUTE_FORMAT_PRINTF(2, 3); /* outside implementation - only driver side, see indipropertyview_driver.cpp */ void apply() const { apply(nullptr); } void define() const { define(nullptr); } public: template = true> void fill( const char *device, const char *name, const char *label, const char *group, IPerm permission, double timeout, IPState state ); /* outside implementation - only driver side, see indipropertyview_driver.cpp */ template = true> void fill( const char *device, const char *name, const char *label, const char *group, IPerm permission, double timeout, IPState state ); /* outside implementation - only driver side, see indipropertyview_driver.cpp */ template = true> void fill( const char *device, const char *name, const char *label, const char *group, IPerm permission, ISRule rule, double timeout, IPState state ); /* outside implementation - only driver side, see indipropertyview_driver.cpp */ template = true> void fill( const char *device, const char *name, const char *label, const char *group, IPState state ); /* outside implementation - only driver side, see indipropertyview_driver.cpp */ template = true> void fill( const char *device, const char *name, const char *label, const char *group, IPerm permission, double timeout, IPState state ); /* outside implementation - only driver side, see indipropertyview_driver.cpp */ public: template = true> bool update(const char * const texts[], const char * const names[], int n); /* outside implementation - only driver side, see indipropertyview_driver.cpp */ template = true> bool update(const double values[], const char * const names[], int n); /* outside implementation - only driver side, see indipropertyview_driver.cpp */ template = true> bool update(const ISState states[], const char * const names[], int n); /* outside implementation - only driver side, see indipropertyview_driver.cpp */ /* template = true> bool update(..., const char * const names[], int n); */ template = true> bool update( const int sizes[], const int blobsizes[], const char * const blobs[], const char * const formats[], const char * const names[], int n ); /* outside implementation - only driver side, see indipropertyview_driver.cpp */ public: WidgetType *begin() const { return widget(); } WidgetType *end() const { return widget() + count(); } WidgetType *at(size_t index) const { return widget() + index; } public: void clear() { for(auto &p : *this) { p.clear(); } //free(widget()); // #PS: do not delete items, they may be on the stack. memset(this, 0, sizeof(*this)); } }; template struct WidgetView; template <> struct WidgetView: PROPERTYVIEW_BASE_ACCESS IText { using Type = IText; template friend struct PropertyView; public: WidgetView() { memset(this, 0, sizeof(*this)); } WidgetView(const WidgetView &other): Type(other) { this->text = nullptr; setText(other.text); } WidgetView(WidgetView &&other): Type(other) { memset(static_cast(&other), 0, sizeof(other)); } WidgetView &operator=(const WidgetView &other) { return *this = WidgetView(other); } WidgetView &operator=(WidgetView &&other) { std::swap(static_cast(other), static_cast(*this)); return *this; } ~WidgetView() { free(this->text); } void clear() { free(this->text); memset(this, 0, sizeof(*this)); } // bool isNull() const { return reinterpret_cast(this) == nullptr; } public: // setters void setParent(ITextVectorProperty *parent) { this->tvp = parent; } void setParent(PropertyView *parent) { this->tvp = static_cast(parent); } void setName(const char *name) { INDI::strlcpy(this->name, name); } void setName(const std::string &name) { setName(name.data()); } void setLabel(const char *label) { INDI::strlcpy(this->label, label); } void setLabel(const std::string &label) { setLabel(label.data()); } //void setText(const char *text) { free(this->text); this->text = strndup(text, strlen(text)); } void setText(const char *text, size_t size) { INDI::strlcpy(this->text = static_cast(realloc(this->text, size + 1)), text, size + 1); } void setText(const char *text) { setText(text, strlen(text)); } void setText(const std::string &text) { setText(text.data(), text.size()); } void setAux(void *user) { this->aux0 = user; } // don't use any other aux! public: //getters const char *getName() const { return this->name; } const char *getLabel() const { return this->label; } const char *getText() const { return this->text ? this->text : ""; } void *getAux() const { return this->aux0; } public: //tests bool isNameMatch(const char *otherName) const { return !strcmp(getName(), otherName); } bool isNameMatch(const std::string &otherName) const { return getName() == otherName; } bool isLabelMatch(const char *otherLabel) const { return !strcmp(getLabel(), otherLabel); } bool isLabelMatch(const std::string &otherLabel) const { return getLabel() == otherLabel; } public: void fill(const char *name, const char *label, const char *initialText) ; /* outside implementation - only driver side, see indipropertyview_driver.cpp */ void fill(const std::string &name, const std::string &label, const std::string &initialText) { fill(name.c_str(), label.c_str(), initialText.c_str()); } }; template <> struct WidgetView: PROPERTYVIEW_BASE_ACCESS INumber { using Type = INumber; template friend struct PropertyView; public: WidgetView() { memset(this, 0, sizeof(*this)); } WidgetView(const WidgetView &other): Type(other) { } WidgetView(WidgetView &&other): Type(other) { memset(static_cast(&other), 0, sizeof(other)); } WidgetView &operator=(const WidgetView &other) { return *this = WidgetView(other); } WidgetView &operator=(WidgetView &&other) { std::swap(static_cast(other), static_cast(*this)); return *this; } ~WidgetView() { } void clear() { memset(this, 0, sizeof(*this)); } // bool isNull() const { return reinterpret_cast(this) == nullptr; } public: // setters void setParent(INumberVectorProperty *parent) { this->nvp = parent; } void setParent(PropertyView *parent) { this->nvp = static_cast(parent); } void setName(const char *name) { INDI::strlcpy(this->name, name); } void setName(const std::string &name) { setName(name.data()); } void setLabel(const char *label) { INDI::strlcpy(this->label, label); } void setLabel(const std::string &label) { setLabel(label.data()); } void setFormat(const char *format) { INDI::strlcpy(this->format, format); } void setFormat(const std::string &format) { setLabel(format.data()); } void setMin(double min) { this->min = min; } void setMax(double max) { this->max = max; } void setMinMax(double min, double max) { setMin(min); setMax(max); } void setStep(double step) { this->step = step; } void setValue(double value) { this->value = value; } void setAux(void *user) { this->aux0 = user; } // don't use any other aux! public: //getters const char *getName() const { return this->name; } const char *getLabel() const { return this->label; } const char *getFormat() const { return this->format; } double getMin() const { return this->min; } double getMax() const { return this->max; } double getStep() const { return this->step; } double getValue() const { return this->value; } void *getAux() const { return this->aux0; } public: //tests bool isNameMatch(const char *otherName) const { return !strcmp(getName(), otherName); } bool isNameMatch(const std::string &otherName) const { return getName() == otherName; } bool isLabelMatch(const char *otherLabel) const { return !strcmp(getLabel(), otherLabel); } bool isLabelMatch(const std::string &otherLabel) const { return getLabel() == otherLabel; } public: void fill(const char *name, const char *label, const char *format, double min, double max, double step, double value) ; /* outside implementation - only driver side, see indipropertyview_driver.cpp */ void fill(const std::string &name, const std::string &label, const std::string &format, double min, double max, double step, double value) { fill(name.c_str(), label.c_str(), format.c_str(), min, max, step, value); } }; template <> struct WidgetView: PROPERTYVIEW_BASE_ACCESS ISwitch { using Type = ISwitch; template friend struct PropertyView; public: WidgetView() { memset(this, 0, sizeof(*this)); } WidgetView(const WidgetView &other): Type(other) { } WidgetView(WidgetView &&other): Type(other) { memset(static_cast(&other), 0, sizeof(other)); } WidgetView &operator=(const WidgetView &other) { return *this = WidgetView(other); } WidgetView &operator=(WidgetView &&other) { std::swap(static_cast(other), static_cast(*this)); return *this; } ~WidgetView() { } void clear() { memset(this, 0, sizeof(*this)); } // bool isNull() const { return reinterpret_cast(this) == nullptr; } public: // setters void setParent(ISwitchVectorProperty *parent) { this->svp = parent; } void setParent(PropertyView *parent) { this->svp = static_cast(parent); } void setName(const char *name) { INDI::strlcpy(this->name, name); } void setName(const std::string &name) { setName(name.data()); } void setLabel(const char *label) { INDI::strlcpy(this->label, label); } void setLabel(const std::string &label) { setLabel(label.data()); } void setState(const ISState &state) { this->s = state; } bool setState(const std::string &state) { return crackISState(state.data(), &this->s) == 0; } void setAux(void *user) { this->aux = user; } // don't use any other aux! public: //getters const char *getName() const { return this->name; } const char *getLabel() const { return this->label; } ISState getState() const { return this->s; } const char *getStateAsString() const { return sstateStr(getState()); } void *getAux() const { return this->aux; } public: //tests bool isNameMatch(const char *otherName) const { return !strcmp(getName(), otherName); } bool isNameMatch(const std::string &otherName) const { return getName() == otherName; } bool isLabelMatch(const char *otherLabel) const { return !strcmp(getLabel(), otherLabel); } bool isLabelMatch(const std::string &otherLabel) const { return getLabel() == otherLabel; } public: void fill(const char *name, const char *label, ISState state = ISS_OFF) ; /* outside implementation - only driver side, see indipropertyview_driver.cpp */ void fill(const std::string &name, const std::string &label, ISState state = ISS_OFF) { fill(name.c_str(), label.c_str(), state); } }; template <> struct WidgetView: PROPERTYVIEW_BASE_ACCESS ILight { using Type = ILight; template friend struct PropertyView; public: WidgetView() { memset(this, 0, sizeof(*this)); } WidgetView(const WidgetView &other): Type(other) { } WidgetView(WidgetView &&other): Type(other) { memset(static_cast(&other), 0, sizeof(other)); } WidgetView &operator=(const WidgetView &other) { return *this = WidgetView(other); } WidgetView &operator=(WidgetView &&other) { std::swap(static_cast(other), static_cast(*this)); return *this; } ~WidgetView() { } void clear() { memset(this, 0, sizeof(*this)); } // bool isNull() const { return reinterpret_cast(this) == nullptr; } public: // setters void setParent(ILightVectorProperty *parent) { this->lvp = parent; } void setParent(PropertyView *parent) { this->lvp = static_cast(parent); } void setName(const char *name) { INDI::strlcpy(this->name, name); } void setName(const std::string &name) { setName(name.data()); } void setLabel(const char *label) { INDI::strlcpy(this->label, label); } void setLabel(const std::string &label) { setLabel(label.data()); } void setState(const IPState &state) { this->s = state; } bool setState(const std::string &state) { return crackIPState(state.data(), &this->s) == 0; } void setAux(void *user) { this->aux = user; } // don't use any other aux! public: //getters const char *getName() const { return this->name; } const char *getLabel() const { return this->label; } IPState getState() const { return this->s; } const char *getStateAsString() const { return pstateStr(getState()); } void *getAux() const { return this->aux; } public: //tests bool isNameMatch(const char *otherName) const { return !strcmp(getName(), otherName); } bool isNameMatch(const std::string &otherName) const { return getName() == otherName; } bool isLabelMatch(const char *otherLabel) const { return !strcmp(getLabel(), otherLabel); } bool isLabelMatch(const std::string &otherLabel) const { return getLabel() == otherLabel; } public: void fill(const char *name, const char *label, IPState state = IPS_OK) ; /* outside implementation - only driver side, see indipropertyview_driver.cpp */ void fill(const std::string &name, const std::string &&label, IPState state = IPS_OK) { fill(name.c_str(), label.c_str(), state); } }; template <> struct WidgetView: PROPERTYVIEW_BASE_ACCESS IBLOB { using Type = IBLOB; template friend struct PropertyView; public: WidgetView() { memset(this, 0, sizeof(*this)); } WidgetView(const WidgetView &other): Type(other) { } WidgetView(WidgetView &&other): Type(other) { memset(static_cast(&other), 0, sizeof(other)); } WidgetView &operator=(const WidgetView &other) { return *this = WidgetView(other); } WidgetView &operator=(WidgetView &&other) { std::swap(static_cast(other), static_cast(*this)); return *this; } ~WidgetView() { /* free(this->blob); */ } void clear() { /* free(this->blob); */ memset(this, 0, sizeof(*this)); } // bool isNull() const { return reinterpret_cast(this) == nullptr; } public: // setters void setParent(IBLOBVectorProperty *parent) { this->bvp = parent; } void setParent(PropertyView *parent) { this->bvp = static_cast(parent); } void setName(const char *name) { INDI::strlcpy(this->name, name); } void setName(const std::string &name) { setName(name.data()); } void setLabel(const char *label) { INDI::strlcpy(this->label, label); } void setLabel(const std::string &label) { setLabel(label.data()); } void setFormat(const char *format) { INDI::strlcpy(this->format, format); } void setFormat(const std::string &format) { setLabel(format.data()); } void setBlob(void *blob) { this->blob = blob; } void setBlobLen(int size) { this->bloblen = size; } void setSize(int size) { this->size = size; } void setAux(void *user) { this->aux0 = user; } // don't use any other aux! public: //getters const char *getName() const { return this->name; } const char *getLabel() const { return this->label; } const char *getFormat() const { return this->format; } void *getBlob() { return this->blob; } const void *getBlob() const { return this->blob; } std::string getBlobAsString() const { return std::string(static_cast(this->blob), this->bloblen); } int getBlobLen() const { return this->bloblen; } int getSize() const { return this->size; } void *getAux() const { return this->aux0; } public: //tests bool isNameMatch(const char *otherName) const { return !strcmp(getName(), otherName); } bool isNameMatch(const std::string &otherName) const { return getName() == otherName; } bool isLabelMatch(const char *otherLabel) const { return !strcmp(getLabel(), otherLabel); } bool isLabelMatch(const std::string &otherLabel) const { return getLabel() == otherLabel; } public: void fill(const char *name, const char *label, const char *format) ; /* outside implementation - only driver side, see indipropertyview_driver.cpp */ void fill(const std::string &name, const std::string &label, const std::string &format) { fill(name.c_str(), label.c_str(), format.c_str()); } }; /* outside implementation */ template inline PropertyView::PropertyView() { memset(this, 0, sizeof(*this)); } template inline void PropertyView::setDeviceName(const char *name) { INDI::strlcpy(this->device, name); } template inline void PropertyView::setDeviceName(const std::string &name) { setDeviceName(name.data()); } template inline void PropertyView::setName(const char *name) { INDI::strlcpy(this->name, name); } template inline void PropertyView::setName(const std::string &name) { setName(name.data()); } template inline void PropertyView::setLabel(const char *label) { INDI::strlcpy(this->label, label); } template inline void PropertyView::setLabel(const std::string &label) { setLabel(label.data()); } template inline void PropertyView::setGroupName(const char *name) { INDI::strlcpy(this->group, name); } template inline void PropertyView::setGroupName(const std::string &name) { setGroupName(name.data()); } template inline void PropertyView::setState(IPState state) { this->s = state; } template inline void PropertyView::setTimestamp(const char *timestamp) { INDI::strlcpy(this->timestamp, timestamp); } template inline void PropertyView::setTimestamp(const std::string ×tamp) { setTimestamp(timestamp.data()); } template template inline void PropertyView::setWidgets(WidgetType (&w)[N]) { setWidgets(static_cast(w), N); } template inline void PropertyView::setAux(void *user) { this->aux = user; } template <> inline void PropertyView::save(FILE *f) const { IUSaveConfigText(f, this); } template <> inline void PropertyView::save(FILE *f) const { IUSaveConfigNumber(f, this); } template <> inline void PropertyView::save(FILE *f) const { IUSaveConfigSwitch(f, this); } template <> inline void PropertyView::save(FILE *f) const { (void)f; /* IUSaveConfigLight(f, this); */ } template <> inline void PropertyView::save(FILE *f) const { IUSaveConfigBLOB(f, this); } template inline void PropertyView::setTimeout(double timeout) { this->timeout = timeout; } template <> inline void PropertyView::setTimeout(double) { } template inline void PropertyView::setPermission(IPerm permission) { this->p = permission; } template <> inline void PropertyView::setPermission(IPerm) { } template inline void PropertyView::setRule(ISRule) { } template <> inline void PropertyView::setRule(ISRule rule) { this->r = rule; } template inline bool PropertyView::setRule(const std::string &) { return false; } template <> inline bool PropertyView::setRule(const std::string &rule) { return crackISRule(rule.data(), &this->r) == 0; } template inline WidgetView *PropertyView::findWidgetByName(const char *) const { return nullptr; } template <> inline WidgetView *PropertyView::findWidgetByName(const char *name) const { return static_cast *>(IUFindText(this, name)); } template <> inline WidgetView *PropertyView::findWidgetByName(const char *name) const { return static_cast *>(IUFindNumber(this, name)); } template <> inline WidgetView *PropertyView::findWidgetByName(const char *name) const { return static_cast *>(IUFindSwitch(this, name)); } template <> inline WidgetView *PropertyView::findWidgetByName(const char *name) const { return static_cast *>(IUFindLight(this, name)); } template <> inline WidgetView *PropertyView::findWidgetByName(const char *name) const { return static_cast *>(IUFindBLOB(this, name)); } template inline IPerm PropertyView::getPermission() const { return this->p; } template <> inline IPerm PropertyView::getPermission() const { return IP_RO; } template inline ISRule PropertyView::getRule() const { return ISR_NOFMANY; } template <> inline ISRule PropertyView::getRule() const { return this->r; } template inline double PropertyView::getTimeout() const { return this->timeout; } template <> inline double PropertyView::getTimeout() const { return 0; } template <> inline void PropertyView::setWidgets(WidgetType *w, size_t size) { this->tp = w; this->ntp = size; } template <> inline void PropertyView::setWidgets(WidgetType *w, size_t size) { this->np = w; this->nnp = size; } template <> inline void PropertyView::setWidgets(WidgetType *w, size_t size) { this->sp = w; this->nsp = size; } template <> inline void PropertyView::setWidgets(WidgetType *w, size_t size) { this->lp = w; this->nlp = size; } template <> inline void PropertyView::setWidgets(WidgetType *w, size_t size) { this->bp = w; this->nbp = size; } template <> inline int PropertyView::count() const { return this->ntp; } template <> inline int PropertyView::count() const { return this->nnp; } template <> inline int PropertyView::count() const { return this->nsp; } template <> inline int PropertyView::count() const { return this->nlp; } template <> inline int PropertyView::count() const { return this->nbp; } template <> inline PropertyView::WidgetType *PropertyView::widget() const { return static_cast(this->tp); } template <> inline PropertyView::WidgetType *PropertyView::widget() const { return static_cast(this->np); } template <> inline PropertyView::WidgetType *PropertyView::widget() const { return static_cast(this->sp); } template <> inline PropertyView::WidgetType *PropertyView::widget() const { return static_cast(this->lp); } template <> inline PropertyView::WidgetType *PropertyView::widget() const { return static_cast(this->bp); } }