From 5506edc662c0ef860ff709e4a43a3262a81bf4fd Mon Sep 17 00:00:00 2001 From: Tilman Blumhagen Date: Fri, 19 May 2023 15:34:59 +0200 Subject: [PATCH] iiopp: Relax required standard to C++11 (requires Boost for optional-type) The requirement of C++17 was only due to minor features. The missing std::optional is now replaced by boost::optional if C++17 is not available. Of course in that case Boost headers must be available. The examples still depend on C++17. This avoids the dependency on Boot. Signed-off-by: Tilman Blumhagen --- bindings/cpp/iiopp.h | 44 ++++++++++++++++++++++++++++++-------------- 1 file changed, 30 insertions(+), 14 deletions(-) diff --git a/bindings/cpp/iiopp.h b/bindings/cpp/iiopp.h index 983d2ecbe..06ccfda10 100644 --- a/bindings/cpp/iiopp.h +++ b/bindings/cpp/iiopp.h @@ -16,7 +16,12 @@ #include #include + +#if __cplusplus < 201703L +#include +#else #include +#endif #include #include #include @@ -24,7 +29,9 @@ /** @brief Public C++ API * - * This is a C++17 wrapper for @ref iio.h + * This is a C++ wrapper for @ref iio.h + * + * It requires C++17 or C++11 with Boost (for boost::optional). * * It provides: * @@ -49,6 +56,12 @@ namespace iiopp { +#if __cplusplus < 201703L +using boost::optional; +#else +using std::optional; +#endif + class Context; class Device; class Buffer; @@ -109,7 +122,7 @@ class IAttr /** @brief Optional string, used for C-functions that return @c nullptr for "no value". */ -typedef std::optional optstr; +typedef optional optstr; namespace impl { @@ -224,7 +237,7 @@ class AttrT : public IAttr }; template -std::optional attr(obj_T const * obj, cstr name) +optional attr(obj_T const * obj, cstr name) { char const * s = find_attr_T(obj, name); if (s) @@ -233,7 +246,7 @@ std::optional attr(obj_T const * obj, cstr name) } template -std::optional attr(obj_T const * obj, unsigned int idx) +optional attr(obj_T const * obj, unsigned int idx) { char const * s = get_attr_T(obj, idx); if (s) @@ -317,8 +330,8 @@ class Channel typedef impl::AttrSeqT AttrSeq; #endif - std::optional attr(cstr name) {return impl::attr(p, name);} - std::optional attr(unsigned int idx) {return impl::attr(p, idx);} + optional attr(cstr name) {return impl::attr(p, name);} + optional attr(unsigned int idx) {return impl::attr(p, idx);} AttrSeq attrs; @@ -415,8 +428,8 @@ class Device : public impl::IndexedSequence typedef IAttr Attr; typedef impl::AttrSeqT AttrSeq; #endif - std::optional attr(cstr name) {return impl::attr(p, name);} - std::optional attr(unsigned int idx) {return impl::attr(p, idx);} + optional attr(cstr name) {return impl::attr(p, name);} + optional attr(unsigned int idx) {return impl::attr(p, idx);} AttrSeq attrs; @@ -442,8 +455,8 @@ class Device : public impl::IndexedSequence typedef impl::AttrSeqT DebugAttrSeq; #endif - std::optional debug_attr(cstr name) {return impl::attr(p, name);} - std::optional debug_attr(unsigned int idx) {return impl::attr(p, idx);} + optional debug_attr(cstr name) {return impl::attr(p, name);} + optional debug_attr(unsigned int idx) {return impl::attr(p, idx);} DebugAttrSeq debug_attrs; @@ -469,8 +482,8 @@ class Device : public impl::IndexedSequence typedef impl::AttrSeqT BufferAttrSeq; #endif - std::optional buffer_attr(cstr name) {return impl::attr(p, name);} - std::optional buffer_attr(unsigned int idx) {return impl::attr(p, idx);} + optional buffer_attr(cstr name) {return impl::attr(p, name);} + optional buffer_attr(unsigned int idx) {return impl::attr(p, idx);} BufferAttrSeq buffer_attrs; @@ -792,8 +805,11 @@ std::shared_ptr create_scan_block(optstr backend, int flags) */ inline double value(Channel ch) { - if (double val; !iio_channel_attr_read_double(ch, "input", &val)) - return val / 1000.; + { + double val; + if (!iio_channel_attr_read_double(ch, "input", &val)) + return val / 1000.; + } double scale = 1; iio_channel_attr_read_double(ch, "scale", &scale);