Skip to content

Commit 5506edc

Browse files
sternmullpcercuei
authored andcommitted
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 <[email protected]>
1 parent c333647 commit 5506edc

File tree

1 file changed

+30
-14
lines changed

1 file changed

+30
-14
lines changed

bindings/cpp/iiopp.h

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,22 @@
1616

1717
#include <iio.h>
1818
#include <string>
19+
20+
#if __cplusplus < 201703L
21+
#include <boost/optional.hpp>
22+
#else
1923
#include <optional>
24+
#endif
2025
#include <stdexcept>
2126
#include <system_error>
2227
#include <cassert>
2328
#include <memory>
2429

2530
/** @brief Public C++ API
2631
*
27-
* This is a C++17 wrapper for @ref iio.h
32+
* This is a C++ wrapper for @ref iio.h
33+
*
34+
* It requires C++17 or C++11 with Boost (for <tt>boost::optional</tt>).
2835
*
2936
* It provides:
3037
*
@@ -49,6 +56,12 @@
4956
namespace iiopp
5057
{
5158

59+
#if __cplusplus < 201703L
60+
using boost::optional;
61+
#else
62+
using std::optional;
63+
#endif
64+
5265
class Context;
5366
class Device;
5467
class Buffer;
@@ -109,7 +122,7 @@ class IAttr
109122

110123
/** @brief Optional string, used for C-functions that return @c nullptr for "no value".
111124
*/
112-
typedef std::optional<cstr> optstr;
125+
typedef optional<cstr> optstr;
113126

114127
namespace impl
115128
{
@@ -224,7 +237,7 @@ class AttrT : public IAttr
224237
};
225238

226239
template <class obj_T, class attr_T, char const * find_attr_T(obj_T const *, char const *)>
227-
std::optional<attr_T> attr(obj_T const * obj, cstr name)
240+
optional<attr_T> attr(obj_T const * obj, cstr name)
228241
{
229242
char const * s = find_attr_T(obj, name);
230243
if (s)
@@ -233,7 +246,7 @@ std::optional<attr_T> attr(obj_T const * obj, cstr name)
233246
}
234247

235248
template <class obj_T, class attr_T, char const * get_attr_T(obj_T const *, unsigned int)>
236-
std::optional<attr_T> attr(obj_T const * obj, unsigned int idx)
249+
optional<attr_T> attr(obj_T const * obj, unsigned int idx)
237250
{
238251
char const * s = get_attr_T(obj, idx);
239252
if (s)
@@ -317,8 +330,8 @@ class Channel
317330
typedef impl::AttrSeqT<Channel, Attr> AttrSeq;
318331
#endif
319332

320-
std::optional<Attr> attr(cstr name) {return impl::attr<iio_channel, Attr, iio_channel_find_attr>(p, name);}
321-
std::optional<Attr> attr(unsigned int idx) {return impl::attr<iio_channel, Attr, iio_channel_get_attr>(p, idx);}
333+
optional<Attr> attr(cstr name) {return impl::attr<iio_channel, Attr, iio_channel_find_attr>(p, name);}
334+
optional<Attr> attr(unsigned int idx) {return impl::attr<iio_channel, Attr, iio_channel_get_attr>(p, idx);}
322335

323336
AttrSeq attrs;
324337

@@ -415,8 +428,8 @@ class Device : public impl::IndexedSequence<Device, Channel>
415428
typedef IAttr Attr;
416429
typedef impl::AttrSeqT<Channel, Attr> AttrSeq;
417430
#endif
418-
std::optional<Attr> attr(cstr name) {return impl::attr<iio_device, Attr, iio_device_find_attr>(p, name);}
419-
std::optional<Attr> attr(unsigned int idx) {return impl::attr<iio_device, Attr, iio_device_get_attr>(p, idx);}
431+
optional<Attr> attr(cstr name) {return impl::attr<iio_device, Attr, iio_device_find_attr>(p, name);}
432+
optional<Attr> attr(unsigned int idx) {return impl::attr<iio_device, Attr, iio_device_get_attr>(p, idx);}
420433

421434
AttrSeq attrs;
422435

@@ -442,8 +455,8 @@ class Device : public impl::IndexedSequence<Device, Channel>
442455
typedef impl::AttrSeqT<Channel, DebugAttr> DebugAttrSeq;
443456
#endif
444457

445-
std::optional<DebugAttr> debug_attr(cstr name) {return impl::attr<iio_device, DebugAttr, iio_device_find_debug_attr>(p, name);}
446-
std::optional<DebugAttr> debug_attr(unsigned int idx) {return impl::attr<iio_device, DebugAttr, iio_device_get_debug_attr>(p, idx);}
458+
optional<DebugAttr> debug_attr(cstr name) {return impl::attr<iio_device, DebugAttr, iio_device_find_debug_attr>(p, name);}
459+
optional<DebugAttr> debug_attr(unsigned int idx) {return impl::attr<iio_device, DebugAttr, iio_device_get_debug_attr>(p, idx);}
447460

448461
DebugAttrSeq debug_attrs;
449462

@@ -469,8 +482,8 @@ class Device : public impl::IndexedSequence<Device, Channel>
469482
typedef impl::AttrSeqT<Channel, BufferAttr> BufferAttrSeq;
470483
#endif
471484

472-
std::optional<BufferAttr> buffer_attr(cstr name) {return impl::attr<iio_device, BufferAttr, iio_device_find_buffer_attr>(p, name);}
473-
std::optional<BufferAttr> buffer_attr(unsigned int idx) {return impl::attr<iio_device, BufferAttr, iio_device_get_buffer_attr>(p, idx);}
485+
optional<BufferAttr> buffer_attr(cstr name) {return impl::attr<iio_device, BufferAttr, iio_device_find_buffer_attr>(p, name);}
486+
optional<BufferAttr> buffer_attr(unsigned int idx) {return impl::attr<iio_device, BufferAttr, iio_device_get_buffer_attr>(p, idx);}
474487

475488
BufferAttrSeq buffer_attrs;
476489

@@ -792,8 +805,11 @@ std::shared_ptr<ScanBlock> create_scan_block(optstr backend, int flags)
792805
*/
793806
inline double value(Channel ch)
794807
{
795-
if (double val; !iio_channel_attr_read_double(ch, "input", &val))
796-
return val / 1000.;
808+
{
809+
double val;
810+
if (!iio_channel_attr_read_double(ch, "input", &val))
811+
return val / 1000.;
812+
}
797813

798814
double scale = 1;
799815
iio_channel_attr_read_double(ch, "scale", &scale);

0 commit comments

Comments
 (0)