Skip to content

Commit

Permalink
iiopp: Relax required standard to C++11 (requires Boost for optional-…
Browse files Browse the repository at this point in the history
…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]>
  • Loading branch information
sternmull authored and pcercuei committed May 24, 2023
1 parent c333647 commit 5506edc
Showing 1 changed file with 30 additions and 14 deletions.
44 changes: 30 additions & 14 deletions bindings/cpp/iiopp.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,22 @@

#include <iio.h>
#include <string>

#if __cplusplus < 201703L
#include <boost/optional.hpp>
#else
#include <optional>
#endif
#include <stdexcept>
#include <system_error>
#include <cassert>
#include <memory>

/** @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 <tt>boost::optional</tt>).
*
* It provides:
*
Expand All @@ -49,6 +56,12 @@
namespace iiopp
{

#if __cplusplus < 201703L
using boost::optional;
#else
using std::optional;
#endif

class Context;
class Device;
class Buffer;
Expand Down Expand Up @@ -109,7 +122,7 @@ class IAttr

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

namespace impl
{
Expand Down Expand Up @@ -224,7 +237,7 @@ class AttrT : public IAttr
};

template <class obj_T, class attr_T, char const * find_attr_T(obj_T const *, char const *)>
std::optional<attr_T> attr(obj_T const * obj, cstr name)
optional<attr_T> attr(obj_T const * obj, cstr name)
{
char const * s = find_attr_T(obj, name);
if (s)
Expand All @@ -233,7 +246,7 @@ std::optional<attr_T> attr(obj_T const * obj, cstr name)
}

template <class obj_T, class attr_T, char const * get_attr_T(obj_T const *, unsigned int)>
std::optional<attr_T> attr(obj_T const * obj, unsigned int idx)
optional<attr_T> attr(obj_T const * obj, unsigned int idx)
{
char const * s = get_attr_T(obj, idx);
if (s)
Expand Down Expand Up @@ -317,8 +330,8 @@ class Channel
typedef impl::AttrSeqT<Channel, Attr> AttrSeq;
#endif

std::optional<Attr> attr(cstr name) {return impl::attr<iio_channel, Attr, iio_channel_find_attr>(p, name);}
std::optional<Attr> attr(unsigned int idx) {return impl::attr<iio_channel, Attr, iio_channel_get_attr>(p, idx);}
optional<Attr> attr(cstr name) {return impl::attr<iio_channel, Attr, iio_channel_find_attr>(p, name);}
optional<Attr> attr(unsigned int idx) {return impl::attr<iio_channel, Attr, iio_channel_get_attr>(p, idx);}

AttrSeq attrs;

Expand Down Expand Up @@ -415,8 +428,8 @@ class Device : public impl::IndexedSequence<Device, Channel>
typedef IAttr Attr;
typedef impl::AttrSeqT<Channel, Attr> AttrSeq;
#endif
std::optional<Attr> attr(cstr name) {return impl::attr<iio_device, Attr, iio_device_find_attr>(p, name);}
std::optional<Attr> attr(unsigned int idx) {return impl::attr<iio_device, Attr, iio_device_get_attr>(p, idx);}
optional<Attr> attr(cstr name) {return impl::attr<iio_device, Attr, iio_device_find_attr>(p, name);}
optional<Attr> attr(unsigned int idx) {return impl::attr<iio_device, Attr, iio_device_get_attr>(p, idx);}

AttrSeq attrs;

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

std::optional<DebugAttr> debug_attr(cstr name) {return impl::attr<iio_device, DebugAttr, iio_device_find_debug_attr>(p, name);}
std::optional<DebugAttr> debug_attr(unsigned int idx) {return impl::attr<iio_device, DebugAttr, iio_device_get_debug_attr>(p, idx);}
optional<DebugAttr> debug_attr(cstr name) {return impl::attr<iio_device, DebugAttr, iio_device_find_debug_attr>(p, name);}
optional<DebugAttr> debug_attr(unsigned int idx) {return impl::attr<iio_device, DebugAttr, iio_device_get_debug_attr>(p, idx);}

DebugAttrSeq debug_attrs;

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

std::optional<BufferAttr> buffer_attr(cstr name) {return impl::attr<iio_device, BufferAttr, iio_device_find_buffer_attr>(p, name);}
std::optional<BufferAttr> buffer_attr(unsigned int idx) {return impl::attr<iio_device, BufferAttr, iio_device_get_buffer_attr>(p, idx);}
optional<BufferAttr> buffer_attr(cstr name) {return impl::attr<iio_device, BufferAttr, iio_device_find_buffer_attr>(p, name);}
optional<BufferAttr> buffer_attr(unsigned int idx) {return impl::attr<iio_device, BufferAttr, iio_device_get_buffer_attr>(p, idx);}

BufferAttrSeq buffer_attrs;

Expand Down Expand Up @@ -792,8 +805,11 @@ std::shared_ptr<ScanBlock> 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);
Expand Down

0 comments on commit 5506edc

Please sign in to comment.