out_dev.h
Go to the documentation of this file.
#ifndef __utl_dev_out_dev_h__
#define __utl_dev_out_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 out_dev {
_CRTP_IMPL(impl_t);
using out_dev_t = out_dev <impl_t, data_t, streamsize>;
public:
using data_type = data_t;
using pointer_type = data_t*;
using type = out_dev_t;
protected:
~out_dev () = default;
out_dev () = default;
out_dev(const out_dev_t&) = delete;
out_dev_t& operator= (const out_dev_t&) = delete;
private:
size_t put_ (const data_t& data) { return impl().put_ (data); }
size_t put_ (const data_t* data, size_t n) {
return impl().put_ (data, n);
}
public:
size_t put (const data_t& data) {
return put_ (data);
}
size_t put (const data_t* data, size_t n) {
return put_ (data, n);
}
public:
template <typename _Src_t>
out_dev_t& operator<< (_Src_t& src) {
static_assert ((sizeof (_Src_t)%sizeof(data_t) == 0),
"Source size must be an integer multiple of device's data size");
put_ (reinterpret_cast<data_t*>(&src), sizeof(_Src_t)/sizeof(data_t));
return *this;
}
template <typename _Src_t>
out_dev_t& operator<< (_Src_t* src) = delete;
out_dev_t& operator<< (const data_t& src) {
put_ (src);
return *this;
}
using iterator = outdev_it <out_dev_t, data_t*, streamsize>;
using const_iterator = outdev_it <const out_dev_t, data_t*, streamsize>;
iterator begin () noexcept { return iterator(this, iterator::beg); }
const_iterator begin () const noexcept { return const_iterator(this, iterator::beg); }
const_iterator cbegin () const noexcept { return const_iterator(this, iterator::beg); }
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 out_dev <virtual_tag, data_t, streamsize> {
using out_dev_t = out_dev <virtual_tag, data_t, streamsize>;
public:
using data_type = data_t;
using pointer_type = data_t*;
using type = out_dev_t;
public:
virtual ~out_dev () = default;
protected:
out_dev () = default;
out_dev(const out_dev&) = delete;
out_dev_t& operator= (const out_dev_t&) = delete;
private:
virtual size_t put_ (const data_t& data) = 0;
virtual size_t put_ (const data_t* data, size_t n) = 0;
public:
size_t put (const data_t& data) { return put_ (data); }
size_t put (const data_t* data, size_t n) { return put_ (data, n); }
public:
template <typename _Src_t>
out_dev_t& operator<< (_Src_t& src) {
static_assert ((sizeof (_Src_t)%sizeof(data_t) == 0),
"Source size must be an integer multiple of device's data size");
put_ (reinterpret_cast<data_t*>(&src), sizeof(_Src_t)/sizeof(data_t));
return *this;
}
template <typename _Src_t>
out_dev_t& operator<< (_Src_t* src) = delete;
out_dev_t& operator<< (const data_t& src) {
put_ (src);
return *this;
}
using iterator = outdev_it <out_dev_t, data_t*, streamsize>;
using const_iterator = outdev_it <const out_dev_t, data_t*, streamsize>;
iterator begin () noexcept { return iterator(this, iterator::beg); }
const_iterator begin () const noexcept { return const_iterator(this, iterator::beg); }
const_iterator cbegin () const noexcept { return const_iterator(this, iterator::beg); }
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 Out_dev = requires (_Tp t, const _Tp ct, typename _Tp::data_type v) {
// Object type
requires !std::is_copy_constructible<_Tp>::value;
requires !std::is_copy_assignable<_Tp>::value;
// Methods
{t.put(v)} -> size_t;
{t.put(&v, sizeof(v))} -> size_t;
// Operators
t << v;
// Iterators
typename _Tp::const_iterator;
requires Outdev_it<typename _Tp::iterator>;
requires Outdev_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 out_dev_details {
using std::declval;
template <class _Tp> using try_put1_t = decltype (declval<_Tp>().put (declval<const typename _Tp::data_type&>()));
template <class _Tp> using try_put2_t = decltype (declval<_Tp>().put (declval<const typename _Tp::data_type*>(),
declval<size_t>()));
// operators
//template <class _Tp> using try_insert_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_out_dev_ : false_ { };
template <typename _Tp>
struct is_out_dev_ <_Tp,
void_t <
typename _Tp::data_type,
typename _Tp::pointer_type,
typename _Tp::iterator,
typename _Tp::const_iterator,
use_if_same_t <try_put1_t <_Tp>, size_t>,
use_if_same_t <try_put2_t <_Tp>, size_t>,
//if_same_t <try_insert_t<_Tp>,_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 Out_dev = out_dev_details::is_out_dev_<_Tp>::value;
#endif
} //namespace utl
#endif /* #ifndef __utl_dev_out_dev_h__ */
utl::out_dev::pointer_type
data_t * pointer_type
Definition: out_dev.h:62
utl::out_dev< virtual_tag, data_t, streamsize >::pointer_type
data_t * pointer_type
Definition: out_dev.h:184
utl::out_dev< virtual_tag, data_t, streamsize >
A virtual base class specialization.
Definition: out_dev.h:177
utl::out_dev::end
iterator end() noexcept
Definition: out_dev.h:161
meta.h
Include all meta library.
utl::out_dev< virtual_tag, data_t, streamsize >::cbegin
const_iterator cbegin() const noexcept
Definition: out_dev.h:273
utl::meta::void_t
void void_t
void_t type alias
Definition: detection.h:55
utl::out_dev< virtual_tag, data_t, streamsize >::begin
const_iterator begin() const noexcept
Definition: out_dev.h:272
utl::out_dev< virtual_tag, data_t, streamsize >::data_type
data_t data_type
Definition: out_dev.h:183
utl::meta::true_
bool_< true > true_
The type used as a compile-time boolean with true value.
Definition: integral.h:68
utl::out_dev::_CRTP_IMPL
_CRTP_IMPL(impl_t)
utl::out_dev_details::try_put2_t
decltype(declval< _Tp >().put(declval< const typename _Tp::data_type * >(), declval< size_t >())) try_put2_t
Definition: out_dev.h:316
crtp.h
utl::out_dev::put
size_t put(const data_t *data, size_t n)
Put interface. This function should send a stream of data_t objects to device.
Definition: out_dev.h:111
utl::out_dev::out_dev_t
out_dev< impl_t, data_t, streamsize > out_dev_t
class type syntactic sugar
Definition: out_dev.h:56
utl::out_dev_details::try_put1_t
decltype(declval< _Tp >().put(declval< const typename _Tp::data_type & >())) try_put1_t
Definition: out_dev.h:314
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::out_dev::begin
iterator begin() noexcept
.begin implementation
Definition: out_dev.h:156
utl::out_dev_details::tryc_end_t
decltype(declval< const _Tp >().begin()) tryc_end_t
Definition: out_dev.h:324
utl::out_dev::put_
size_t put_(const data_t &data)
Definition: out_dev.h:80
utl::out_dev::iterator
outdev_it< out_dev_t, data_t *, streamsize > iterator
Iterator.
Definition: out_dev.h:152
utl::outdev_it
Output device iterator type. We "future call" interface methods from owner class to provide iterator ...
Definition: dev_iterators.h:250
utl::out_dev_details::is_out_dev_
Primary template to catch any non output device types.
Definition: out_dev.h:329
utl::meta::false_
bool_< false > false_
The type used as a compile-time boolean with false value.
Definition: integral.h:69
utl::out_dev< virtual_tag, data_t, streamsize >::end
const_iterator end() const noexcept
Definition: out_dev.h:277
utl::Out_dev
constexpr bool Out_dev
Definition: out_dev.h:357
utl::out_dev_details::try_cbegin_t
decltype(declval< const _Tp >().cbegin()) try_cbegin_t
Definition: out_dev.h:322
utl::out_dev_details::try_cend_t
decltype(declval< const _Tp >().cend()) try_cend_t
Definition: out_dev.h:325
utl::size_t
std::size_t size_t
Definition: types.h:37
utl
STL&#39;s core language concepts.
Definition: _1wire.h:30
utl::out_dev< virtual_tag, data_t, streamsize >::end
iterator end() noexcept
Definition: out_dev.h:276
utl::out_dev_details::try_begin_t
decltype(declval< _Tp >().begin()) try_begin_t
Definition: out_dev.h:320
utl::out_dev_details::try_end_t
decltype(declval< _Tp >().begin()) try_end_t
Definition: out_dev.h:323
utl::outdev_it::beg
Points the first item (relative address)
Definition: dev_iterators.h:266
utl::out_dev< virtual_tag, data_t, streamsize >::put
size_t put(const data_t *data, size_t n)
Definition: out_dev.h:232
utl::out_dev< virtual_tag, data_t, streamsize >::cend
const_iterator cend() const noexcept
Definition: out_dev.h:278
utl::out_dev::put
size_t put(const data_t &data)
Put interface. This function should send a single data_t object to device.
Definition: out_dev.h:99
utl::out_dev::const_iterator
outdev_it< const out_dev_t, data_t *, streamsize > const_iterator
Const iterator.
Definition: out_dev.h:153
utl::out_dev::cbegin
const_iterator cbegin() const noexcept
Definition: out_dev.h:158
utl::out_dev::operator=
out_dev_t & operator=(const out_dev_t &)=delete
dev_iterators.h
Iterator collection for devices.
utl::out_dev< virtual_tag, data_t, streamsize >::begin
iterator begin() noexcept
.begin implementation
Definition: out_dev.h:271
utl::out_dev::put_
size_t put_(const data_t *data, size_t n)
Definition: out_dev.h:81
utl::out_dev_details::tryc_begin_t
decltype(declval< const _Tp >().begin()) tryc_begin_t
Definition: out_dev.h:321
utl::out_dev::operator<<
out_dev_t & operator<<(_Src_t &src)
Template operator<< implementation for for all by value/ref parameters.
Definition: out_dev.h:131
utl::out_dev::cend
const_iterator cend() const noexcept
Definition: out_dev.h:163
utl::virtual_tag
virtual support tag type
Definition: crtp.h:40
utl::out_dev::begin
const_iterator begin() const noexcept
Definition: out_dev.h:157
utl::out_dev::data_type
data_t data_type
Definition: out_dev.h:61
utl::out_dev< virtual_tag, data_t, streamsize >::put
size_t put(const data_t &data)
Definition: out_dev.h:231
utl::out_dev::~out_dev
~out_dev()=default
Allow destructor from derived only.
utl::out_dev::end
const_iterator end() const noexcept
Definition: out_dev.h:162
impl.h
Implementation detail main forward header.
utl::out_dev::out_dev
out_dev()=default
A default constructor from derived only.
utl::out_dev
Abstract base classes for output devices.
Definition: out_dev.h:54