uTL
micro Template library
inbuf_dev.h
Go to the documentation of this file.
1 
22 #ifndef __utl_dev_inbuf_dev_h__
23 #define __utl_dev_inbuf_dev_h__
24 
25 #include <utl/core/impl.h>
26 #include <utl/core/crtp.h>
27 #include <utl/dev/dev_iterators.h>
28 #include <utl/meta/meta.h>
29 
30 namespace utl {
31 
36 
54  template <typename impl_t, typename data_t, size_t streamsize =0>
55  class inbuf_dev {
56  _CRTP_IMPL(impl_t);
58 
61  public:
62  using data_type = data_t;
63  using pointer_type = data_t*;
65  using type = inbuf_dev_t;
66 
70  protected:
72  ~inbuf_dev () = default;
73  inbuf_dev () = default;
74  inbuf_dev(const inbuf_dev_t&) = delete;
75  inbuf_dev_t& operator= (const inbuf_dev_t&) = delete;
76 
80  private:
81  size_t in_avail_ () { return impl().in_avail_(); }
82  size_t get_ (data_t& data) { return impl().get_(data); }
83  size_t get_ (data_t* data, size_t n) { return impl().get_ (data, n); }
85 
89  public:
95  size_t in_avail () {
96  return in_avail_ ();
97  }
98 
108  size_t get (data_t& data) {
109  return get_ (data);
110  }
111 
120  size_t get (data_t* data, size_t n) {
121  return get_ (data, n);
122  }
124 
128  public:
139  template <typename _Dst_t>
140  inbuf_dev_t& operator>> (_Dst_t& dst) {
141  static_assert ((sizeof (_Dst_t)%sizeof(data_t) == 0),
142  "Target size must be an integer multiple of device's data size");
143  get_ (reinterpret_cast<data_t*>(&dst), sizeof(_Dst_t)/sizeof(data_t));
144  return *this;
145  }
147  template <typename _Dst_t>
148  inbuf_dev_t& operator>> (_Dst_t* dst) = delete;
149 
151  inbuf_dev_t& operator>> (data_t& dst) {
152  get_ (dst);
153  return *this;
154  }
156 
163 
165  iterator begin () noexcept { return iterator(this, iterator::init); }
166  const_iterator begin () const noexcept { return const_iterator(this, iterator::init); }
167  const_iterator cbegin () const noexcept { return const_iterator(this, iterator::init); }
170  iterator end () noexcept { return iterator(this, iterator::eos); }
171  const_iterator end () const noexcept { return const_iterator(this, iterator::eos); }
172  const_iterator cend () const noexcept { return const_iterator(this, iterator::eos); }
175  };
176 
177 
178 
187  template <typename data_t, size_t streamsize>
188  class inbuf_dev <virtual_tag, data_t, streamsize> {
191 
194  public:
195  using data_type = data_t;
196  using pointer_type = data_t*;
198  using type = inbuf_dev_t;
199 
203  public:
205  virtual ~inbuf_dev () = default;
206  protected:
207  inbuf_dev () = default;
208  inbuf_dev(const inbuf_dev_t&) = delete;
209  inbuf_dev_t& operator= (const inbuf_dev_t&) = delete;
210 
215  private:
221  virtual size_t in_avail_ () = 0;
222 
232  virtual size_t get_ (data_t& data) = 0;
233 
242  virtual size_t get_ (data_t* data, size_t n) = 0;
244 
248  public:
250  size_t in_avail() { return in_avail_(); }
251  size_t get (data_t& data) { return get_ (data); }
252  size_t get (data_t* data, size_t n) { return get_ (data, n); }
254 
258 
268  template <typename _Dst_t>
269  inbuf_dev_t& operator>> (_Dst_t& dst) {
270  static_assert ((sizeof (_Dst_t)%sizeof(data_t) == 0),
271  "Target size must be an integer multiple of device's data size");
272  get_ (reinterpret_cast<data_t*>(&dst), sizeof(_Dst_t)/sizeof(data_t));
273  return *this;
274  }
276  template <typename _Dst_t>
277  inbuf_dev_t& operator>> (_Dst_t* dst) = delete;
278 
280  inbuf_dev_t& operator>> (data_t& dst) {
281  get_ (dst);
282  return *this;
283  }
285 
292 
294  iterator begin () noexcept { return iterator(this, iterator::init); }
295  const_iterator begin () const noexcept { return const_iterator(this, iterator::init); }
296  const_iterator cbegin () const noexcept { return const_iterator(this, iterator::init); }
299  iterator end () noexcept { return iterator(this, iterator::eos); }
300  const_iterator end () const noexcept { return const_iterator(this, iterator::eos); }
301  const_iterator cend () const noexcept { return const_iterator(this, iterator::eos); }
304  };
305 
309 #if defined _utl_have_concepts
311  template <typename _Tp>
312  concept bool Inbuf_dev = requires (_Tp t, const _Tp ct, typename _Tp::data_type v) {
313  // Object type
314 // requires std::is_default_constructible<_Tp>::value;
315  requires !std::is_copy_constructible<_Tp>::value;
316  requires !std::is_copy_assignable<_Tp>::value;
317  // Methods
318  {t.get(v, 0)} -> size_t;
319  {t.get(&v, 1, 0)} -> size_t;
320  // Operators
321  t >> v;
322  // Iterators
323  typename _Tp::const_iterator; //XXX: change to concept: is_idxdev_iterator<_Tp>
324  requires Indev_it<typename _Tp::iterator>;
325  //requires Indev_it<typename _Tp::const_iterator>;
326  { t.begin() } -> typename _Tp::iterator;
327 // {ct.begin()} -> typename _Tp::const_iterator;
328 // { t.cbegin()} -> typename _Tp::const_iterator;
329  { t.end() } -> typename _Tp::iterator;
330 // {ct.end()} -> typename _Tp::const_iterator;
331 // { t.cend()} -> typename _Tp::const_iterator;
332  };
333 #else
334  namespace inbuf_dev_details {
335  using std::declval;
336 
337  // main api members
338  template <class _Tp> using try_get1_t = decltype (declval<_Tp>().get (declval<typename _Tp::data_type&>()));
339  template <class _Tp> using try_get2_t = decltype (declval<_Tp>().get (declval<typename _Tp::data_type*>(), declval<size_t>()));
340  // operators
341  //template <class _Tp> using try_extract_t= decltype (declval<_Tp>() >> declval<typename _Tp::data_type&>());
342  // iterator members
343  template <class _Tp> using try_begin_t = decltype (declval<_Tp>().begin());
344  template <class _Tp> using tryc_begin_t = decltype (declval<const _Tp>().begin());
345  template <class _Tp> using try_cbegin_t = decltype (declval<const _Tp>().cbegin());
346  template <class _Tp> using try_end_t = decltype (declval<_Tp>().begin());
347  template <class _Tp> using tryc_end_t = decltype (declval<const _Tp>().begin());
348  template <class _Tp> using try_cend_t = decltype (declval<const _Tp>().cend());
349 
351  template <typename _Tp, typename =void>
352  struct is_inbuf_dev_ : false_ {};
353 
355  template <typename _Tp>
356  struct is_inbuf_dev_ <_Tp,
357  void_t <
358  typename _Tp::data_type,
359  typename _Tp::pointer_type,
360  typename _Tp::iterator,
361  typename _Tp::const_iterator,
362  use_if_same_t <try_get1_t <_Tp>, size_t>,
363  use_if_same_t <try_get2_t <_Tp>, size_t>,
364  //if_same_t <try_extract_t<_Tp>,typename _Tp&>,
365  use_if_same_t <try_begin_t<_Tp>, typename _Tp::iterator>,
366  use_if_same_t <tryc_begin_t<_Tp>, typename _Tp::const_iterator>,
367  use_if_same_t <try_cbegin_t<_Tp>, typename _Tp::const_iterator>,
368  use_if_same_t <try_end_t<_Tp>, typename _Tp::iterator>,
369  use_if_same_t <tryc_end_t<_Tp>, typename _Tp::const_iterator>,
370  use_if_same_t <try_cend_t<_Tp>, typename _Tp::const_iterator>
371  >
372  > : true_ {};
373  }
379  template <typename _Tp>
381 #endif
382 }
384 
386 #endif /* #ifndef __utl_dev_inbuf_dev_h__ */
Include all meta library.
inbuf_dev< impl_t, data_t, streamsize > inbuf_dev_t
class type syntactic sugar
Definition: inbuf_dev.h:57
size_t get_(data_t &data)
Definition: inbuf_dev.h:82
decltype(declval< const _Tp >().cend()) try_cend_t
Definition: inbuf_dev.h:348
void void_t
void_t type alias
Definition: detection.h:55
const_iterator begin() const noexcept
Definition: inbuf_dev.h:166
const_iterator end() const noexcept
Definition: inbuf_dev.h:300
const_iterator cend() const noexcept
Definition: inbuf_dev.h:172
data_t * pointer_type
Definition: inbuf_dev.h:63
bool_< true > true_
The type used as a compile-time boolean with true value.
Definition: integral.h:68
decltype(declval< const _Tp >().begin()) tryc_begin_t
Definition: inbuf_dev.h:344
iterator begin() noexcept
.begin implementation
Definition: inbuf_dev.h:165
iterator end() noexcept
Definition: inbuf_dev.h:170
meta::eval< meta::enable_if< meta::same_< _T1, _T2 >::value, _Ret > > use_if_same_t
Definition: stl.h:55
inbuf_dev()=default
A default constructor from derived only.
size_t get_(data_t *data, size_t n)
Definition: inbuf_dev.h:83
decltype(declval< _Tp >().get(declval< typename _Tp::data_type * >(), declval< size_t >())) try_get2_t
Definition: inbuf_dev.h:339
const_iterator cbegin() const noexcept
Definition: inbuf_dev.h:167
constexpr bool Inbuf_dev
Definition: inbuf_dev.h:380
bool_< false > false_
The type used as a compile-time boolean with false value.
Definition: integral.h:69
decltype(declval< _Tp >().get(declval< typename _Tp::data_type & >())) try_get1_t
Definition: inbuf_dev.h:338
size_t in_avail_()
Definition: inbuf_dev.h:81
decltype(declval< const _Tp >().begin()) tryc_end_t
Definition: inbuf_dev.h:347
~inbuf_dev()=default
Allow destructor from derived only.
decltype(declval< _Tp >().begin()) try_end_t
Definition: inbuf_dev.h:346
std::size_t size_t
Definition: types.h:37
indev_it< inbuf_dev_t, const data_t *, streamsize > const_iterator
Const iterator.
Definition: inbuf_dev.h:162
STL&#39;s core language concepts.
Definition: _1wire.h:30
indev_it< inbuf_dev_t, data_t *, streamsize > iterator
Iterator.
Definition: inbuf_dev.h:161
Input device iterator type. We "future call" interface methods from owner class to provide iterator f...
decltype(declval< _Tp >().begin()) try_begin_t
Definition: inbuf_dev.h:343
Primary template to catch any non input device types.
Definition: inbuf_dev.h:352
data_t data_type
Definition: inbuf_dev.h:62
inbuf_dev_t & operator>>(_Dst_t &dst)
Template operator >> implementation for for all by value/ref parameters.
Definition: inbuf_dev.h:140
const_iterator cend() const noexcept
Definition: inbuf_dev.h:301
decltype(declval< const _Tp >().cbegin()) try_cbegin_t
Definition: inbuf_dev.h:345
_CRTP_IMPL(impl_t)
const_iterator end() const noexcept
Definition: inbuf_dev.h:171
iterator begin() noexcept
.begin implementation
Definition: inbuf_dev.h:294
Iterator collection for devices.
inbuf_dev_t & operator=(const inbuf_dev_t &)=delete
const_iterator begin() const noexcept
Definition: inbuf_dev.h:295
A virtual base class specialization.
Definition: inbuf_dev.h:188
Abstract base class for input buffered devices.
Definition: inbuf_dev.h:55
virtual support tag type
Definition: crtp.h:40
const_iterator cbegin() const noexcept
Definition: inbuf_dev.h:296
Implementation detail main forward header.
size_t in_avail()
Definition: inbuf_dev.h:95