inbuf_dev.h
Go to the documentation of this file.
#ifndef __utl_dev_inbuf_dev_h__
#define __utl_dev_inbuf_dev_h__
#include <utl/core/impl.h>
#include <utl/core/crtp.h>
#include <utl/dev/dev_iterators.h>
#include <utl/meta/meta.h>
namespace utl {
template <typename impl_t, typename data_t, size_t streamsize =0>
class inbuf_dev {
_CRTP_IMPL(impl_t);
using inbuf_dev_t = inbuf_dev <impl_t, data_t, streamsize>;
public:
using data_type = data_t;
using pointer_type = data_t*;
using type = inbuf_dev_t;
protected:
~inbuf_dev () = default;
inbuf_dev () = default;
inbuf_dev(const inbuf_dev_t&) = delete;
inbuf_dev_t& operator= (const inbuf_dev_t&) = delete;
private:
size_t in_avail_ () { return impl().in_avail_(); }
size_t get_ (data_t& data) { return impl().get_(data); }
size_t get_ (data_t* data, size_t n) { return impl().get_ (data, n); }
public:
size_t in_avail () {
return in_avail_ ();
}
size_t get (data_t& data) {
return get_ (data);
}
size_t get (data_t* data, size_t n) {
return get_ (data, n);
}
public:
template <typename _Dst_t>
inbuf_dev_t& operator>> (_Dst_t& dst) {
static_assert ((sizeof (_Dst_t)%sizeof(data_t) == 0),
"Target size must be an integer multiple of device's data size");
get_ (reinterpret_cast<data_t*>(&dst), sizeof(_Dst_t)/sizeof(data_t));
return *this;
}
template <typename _Dst_t>
inbuf_dev_t& operator>> (_Dst_t* dst) = delete;
inbuf_dev_t& operator>> (data_t& dst) {
get_ (dst);
return *this;
}
using iterator = indev_it <inbuf_dev_t, data_t*, streamsize>;
using const_iterator = indev_it <inbuf_dev_t, const data_t*, streamsize>;
iterator begin () noexcept { return iterator(this, iterator::init); }
const_iterator begin () const noexcept { return const_iterator(this, iterator::init); }
const_iterator cbegin () const noexcept { return const_iterator(this, iterator::init); }
iterator end () noexcept { return iterator(this, iterator::eos); }
const_iterator end () const noexcept { return const_iterator(this, iterator::eos); }
const_iterator cend () const noexcept { return const_iterator(this, iterator::eos); }
};
template <typename data_t, size_t streamsize>
class inbuf_dev <virtual_tag, data_t, streamsize> {
using inbuf_dev_t = inbuf_dev <virtual_tag, data_t, streamsize>;
public:
using data_type = data_t;
using pointer_type = data_t*;
using type = inbuf_dev_t;
public:
virtual ~inbuf_dev () = default;
protected:
inbuf_dev () = default;
inbuf_dev(const inbuf_dev_t&) = delete;
inbuf_dev_t& operator= (const inbuf_dev_t&) = delete;
private:
virtual size_t in_avail_ () = 0;
virtual size_t get_ (data_t& data) = 0;
virtual size_t get_ (data_t* data, size_t n) = 0;
public:
size_t in_avail() { return in_avail_(); }
size_t get (data_t& data) { return get_ (data); }
size_t get (data_t* data, size_t n) { return get_ (data, n); }
template <typename _Dst_t>
inbuf_dev_t& operator>> (_Dst_t& dst) {
static_assert ((sizeof (_Dst_t)%sizeof(data_t) == 0),
"Target size must be an integer multiple of device's data size");
get_ (reinterpret_cast<data_t*>(&dst), sizeof(_Dst_t)/sizeof(data_t));
return *this;
}
template <typename _Dst_t>
inbuf_dev_t& operator>> (_Dst_t* dst) = delete;
inbuf_dev_t& operator>> (data_t& dst) {
get_ (dst);
return *this;
}
using iterator = indev_it <inbuf_dev_t, data_t*, streamsize>;
using const_iterator = indev_it <inbuf_dev_t, const data_t*, streamsize>;
iterator begin () noexcept { return iterator(this, iterator::init); }
const_iterator begin () const noexcept { return const_iterator(this, iterator::init); }
const_iterator cbegin () const noexcept { return const_iterator(this, iterator::init); }
iterator end () noexcept { return iterator(this, iterator::eos); }
const_iterator end () const noexcept { return const_iterator(this, iterator::eos); }
const_iterator cend () const noexcept { return const_iterator(this, iterator::eos); }
};
#if defined _utl_have_concepts
template <typename _Tp>
concept bool Inbuf_dev = requires (_Tp t, const _Tp ct, typename _Tp::data_type v) {
// Object type
// requires std::is_default_constructible<_Tp>::value;
requires !std::is_copy_constructible<_Tp>::value;
requires !std::is_copy_assignable<_Tp>::value;
// Methods
{t.get(v, 0)} -> size_t;
{t.get(&v, 1, 0)} -> size_t;
// Operators
t >> v;
// Iterators
typename _Tp::const_iterator; //XXX: change to concept: is_idxdev_iterator<_Tp>
requires Indev_it<typename _Tp::iterator>;
//requires Indev_it<typename _Tp::const_iterator>;
{ t.begin() } -> typename _Tp::iterator;
// {ct.begin()} -> typename _Tp::const_iterator;
// { t.cbegin()} -> typename _Tp::const_iterator;
{ t.end() } -> typename _Tp::iterator;
// {ct.end()} -> typename _Tp::const_iterator;
// { t.cend()} -> typename _Tp::const_iterator;
};
#else
namespace inbuf_dev_details {
using std::declval;
// main api members
template <class _Tp> using try_get1_t = decltype (declval<_Tp>().get (declval<typename _Tp::data_type&>()));
template <class _Tp> using try_get2_t = decltype (declval<_Tp>().get (declval<typename _Tp::data_type*>(), declval<size_t>()));
// operators
//template <class _Tp> using try_extract_t= decltype (declval<_Tp>() >> declval<typename _Tp::data_type&>());
// iterator members
template <class _Tp> using try_begin_t = decltype (declval<_Tp>().begin());
template <class _Tp> using tryc_begin_t = decltype (declval<const _Tp>().begin());
template <class _Tp> using try_cbegin_t = decltype (declval<const _Tp>().cbegin());
template <class _Tp> using try_end_t = decltype (declval<_Tp>().begin());
template <class _Tp> using tryc_end_t = decltype (declval<const _Tp>().begin());
template <class _Tp> using try_cend_t = decltype (declval<const _Tp>().cend());
template <typename _Tp, typename =void>
struct is_inbuf_dev_ : false_ {};
template <typename _Tp>
struct is_inbuf_dev_ <_Tp,
void_t <
typename _Tp::data_type,
typename _Tp::pointer_type,
typename _Tp::iterator,
typename _Tp::const_iterator,
use_if_same_t <try_get1_t <_Tp>, size_t>,
use_if_same_t <try_get2_t <_Tp>, size_t>,
//if_same_t <try_extract_t<_Tp>,typename _Tp&>,
use_if_same_t <try_begin_t<_Tp>, typename _Tp::iterator>,
use_if_same_t <tryc_begin_t<_Tp>, typename _Tp::const_iterator>,
use_if_same_t <try_cbegin_t<_Tp>, typename _Tp::const_iterator>,
use_if_same_t <try_end_t<_Tp>, typename _Tp::iterator>,
use_if_same_t <tryc_end_t<_Tp>, typename _Tp::const_iterator>,
use_if_same_t <try_cend_t<_Tp>, typename _Tp::const_iterator>
>
> : true_ {};
}
template <typename _Tp>
constexpr bool Inbuf_dev = inbuf_dev_details::is_inbuf_dev_ <_Tp>::value;
#endif
}
#endif /* #ifndef __utl_dev_inbuf_dev_h__ */
meta.h
Include all meta library.
utl::inbuf_dev::inbuf_dev_t
inbuf_dev< impl_t, data_t, streamsize > inbuf_dev_t
class type syntactic sugar
Definition: inbuf_dev.h:57
utl::inbuf_dev::get_
size_t get_(data_t &data)
Definition: inbuf_dev.h:82
utl::inbuf_dev_details::try_cend_t
decltype(declval< const _Tp >().cend()) try_cend_t
Definition: inbuf_dev.h:348
utl::meta::void_t
void void_t
void_t type alias
Definition: detection.h:55
utl::inbuf_dev::begin
const_iterator begin() const noexcept
Definition: inbuf_dev.h:166
utl::inbuf_dev< virtual_tag, data_t, streamsize >::end
const_iterator end() const noexcept
Definition: inbuf_dev.h:300
utl::inbuf_dev::cend
const_iterator cend() const noexcept
Definition: inbuf_dev.h:172
utl::inbuf_dev::pointer_type
data_t * pointer_type
Definition: inbuf_dev.h:63
utl::meta::true_
bool_< true > true_
The type used as a compile-time boolean with true value.
Definition: integral.h:68
crtp.h
utl::inbuf_dev_details::tryc_begin_t
decltype(declval< const _Tp >().begin()) tryc_begin_t
Definition: inbuf_dev.h:344
utl::inbuf_dev::begin
iterator begin() noexcept
.begin implementation
Definition: inbuf_dev.h:165
utl::inbuf_dev::end
iterator end() noexcept
Definition: inbuf_dev.h:170
utl::use_if_same_t
meta::eval< meta::enable_if< meta::same_< _T1, _T2 >::value, _Ret > > use_if_same_t
Definition: stl.h:55
utl::inbuf_dev::inbuf_dev
inbuf_dev()=default
A default constructor from derived only.
utl::inbuf_dev::get_
size_t get_(data_t *data, size_t n)
Definition: inbuf_dev.h:83
utl::inbuf_dev_details::try_get2_t
decltype(declval< _Tp >().get(declval< typename _Tp::data_type * >(), declval< size_t >())) try_get2_t
Definition: inbuf_dev.h:339
utl::inbuf_dev< virtual_tag, data_t, streamsize >::in_avail
size_t in_avail()
Definition: inbuf_dev.h:250
utl::inbuf_dev::cbegin
const_iterator cbegin() const noexcept
Definition: inbuf_dev.h:167
utl::Inbuf_dev
constexpr bool Inbuf_dev
Definition: inbuf_dev.h:380
utl::meta::false_
bool_< false > false_
The type used as a compile-time boolean with false value.
Definition: integral.h:69
utl::inbuf_dev< virtual_tag, data_t, streamsize >::pointer_type
data_t * pointer_type
Definition: inbuf_dev.h:196
utl::inbuf_dev_details::try_get1_t
decltype(declval< _Tp >().get(declval< typename _Tp::data_type & >())) try_get1_t
Definition: inbuf_dev.h:338
utl::inbuf_dev< virtual_tag, data_t, streamsize >::end
iterator end() noexcept
Definition: inbuf_dev.h:299
utl::inbuf_dev::in_avail_
size_t in_avail_()
Definition: inbuf_dev.h:81
utl::inbuf_dev_details::tryc_end_t
decltype(declval< const _Tp >().begin()) tryc_end_t
Definition: inbuf_dev.h:347
utl::inbuf_dev::~inbuf_dev
~inbuf_dev()=default
Allow destructor from derived only.
utl::inbuf_dev_details::try_end_t
decltype(declval< _Tp >().begin()) try_end_t
Definition: inbuf_dev.h:346
utl::size_t
std::size_t size_t
Definition: types.h:37
utl::inbuf_dev::const_iterator
indev_it< inbuf_dev_t, const data_t *, streamsize > const_iterator
Const iterator.
Definition: inbuf_dev.h:162
utl
STL&#39;s core language concepts.
Definition: _1wire.h:30
utl::inbuf_dev::iterator
indev_it< inbuf_dev_t, data_t *, streamsize > iterator
Iterator.
Definition: inbuf_dev.h:161
utl::indev_it
Input device iterator type. We "future call" interface methods from owner class to provide iterator f...
Definition: dev_iterators.h:461
utl::inbuf_dev_details::try_begin_t
decltype(declval< _Tp >().begin()) try_begin_t
Definition: inbuf_dev.h:343
utl::inbuf_dev_details::is_inbuf_dev_
Primary template to catch any non input device types.
Definition: inbuf_dev.h:352
utl::inbuf_dev::data_type
data_t data_type
Definition: inbuf_dev.h:62
utl::inbuf_dev::operator>>
inbuf_dev_t & operator>>(_Dst_t &dst)
Template operator >> implementation for for all by value/ref parameters.
Definition: inbuf_dev.h:140
utl::inbuf_dev< virtual_tag, data_t, streamsize >::cend
const_iterator cend() const noexcept
Definition: inbuf_dev.h:301
utl::inbuf_dev_details::try_cbegin_t
decltype(declval< const _Tp >().cbegin()) try_cbegin_t
Definition: inbuf_dev.h:345
utl::inbuf_dev< virtual_tag, data_t, streamsize >::data_type
data_t data_type
Definition: inbuf_dev.h:195
utl::inbuf_dev::_CRTP_IMPL
_CRTP_IMPL(impl_t)
utl::inbuf_dev::end
const_iterator end() const noexcept
Definition: inbuf_dev.h:171
utl::inbuf_dev< virtual_tag, data_t, streamsize >::begin
iterator begin() noexcept
.begin implementation
Definition: inbuf_dev.h:294
dev_iterators.h
Iterator collection for devices.
utl::inbuf_dev::operator=
inbuf_dev_t & operator=(const inbuf_dev_t &)=delete
utl::inbuf_dev< virtual_tag, data_t, streamsize >::begin
const_iterator begin() const noexcept
Definition: inbuf_dev.h:295
utl::inbuf_dev< virtual_tag, data_t, streamsize >
A virtual base class specialization.
Definition: inbuf_dev.h:188
utl::inbuf_dev
Abstract base class for input buffered devices.
Definition: inbuf_dev.h:55
utl::virtual_tag
virtual support tag type
Definition: crtp.h:40
utl::inbuf_dev< virtual_tag, data_t, streamsize >::cbegin
const_iterator cbegin() const noexcept
Definition: inbuf_dev.h:296
impl.h
Implementation detail main forward header.
utl::inbuf_dev::in_avail
size_t in_avail()
Definition: inbuf_dev.h:95