uTL
micro Template library
in_dev.h
Go to the documentation of this file.
1 
21 #ifndef __utl_dev_in_dev_h__
22 #define __utl_dev_in_dev_h__
23 
24 #include <utl/core/impl.h>
25 #include <utl/core/crtp.h>
26 #include <utl/dev/dev_iterators.h>
27 #include <utl/meta/meta.h>
28 
29 namespace utl {
30 
35 
53  template <typename impl_t, typename data_t, size_t streamsize =0>
54  class in_dev {
55  _CRTP_IMPL(impl_t);
57 
60  public:
61  using data_type = data_t;
62  using pointer_type = data_t*;
64  using type = in_dev_t;
65 
68  protected:
70  ~in_dev () = default;
71  in_dev () = default;
72  in_dev(const in_dev_t&) = delete;
73  in_dev_t& operator= (const in_dev_t&) = delete;
74 
78  private:
79  size_t get_ (data_t& data) { return impl().get_ (data); }
80  size_t get_ (data_t* data, size_t n) { return impl().get (data, n); }
82 
86  public:
97  size_t get (data_t& data) {
98  return get_ (data);
99  }
100 
109  size_t get (data_t* data, size_t n) {
110  return get_ (data, n);
111  }
113 
117  public:
128  template <typename _Dst_t>
129  in_dev_t& operator>> (_Dst_t& dst) {
130  static_assert ((sizeof (_Dst_t)%sizeof(data_t) == 0),
131  "Target size must be an integer multiple of device's data size");
132  get_ (reinterpret_cast<data_t*>(&dst), sizeof(_Dst_t)/sizeof(data_t));
133  return *this;
134  }
136  template <typename _Dst_t>
137  in_dev_t& operator>> (_Dst_t* dst) = delete;
138 
140  in_dev_t& operator>> (data_t& dst) {
141  get_ (dst);
142  return *this;
143  }
145 
152 
154  iterator begin () noexcept { return iterator(this, iterator::init); }
155  const_iterator begin () const noexcept { return const_iterator(this, iterator::init); }
156  const_iterator cbegin () const noexcept { return const_iterator(this, iterator::init); }
159  iterator end () noexcept { return iterator(this, iterator::eos); }
160  const_iterator end () const noexcept { return const_iterator(this, iterator::eos); }
161  const_iterator cend () const noexcept { return const_iterator(this, iterator::eos); }
164  };
165 
173  template <typename data_t, size_t streamsize>
174  class in_dev <virtual_tag, data_t, streamsize> {
177 
180  public:
181  using data_type = data_t;
182  using pointer_type = data_t*;
184  using type = in_dev_t;
185 
189  public:
191  virtual ~in_dev () = default;
192  protected:
193  in_dev () = default;
194  in_dev(const in_dev_t&) = delete;
195  in_dev_t& operator= (const in_dev_t&) = delete;
196 
201  private:
212  virtual size_t get_ (data_t& data) = 0;
213 
222  virtual size_t get_ (data_t* data, size_t n) = 0;
224 
228  public:
230  size_t get (data_t& data) { return get_ (data); }
231  size_t get (data_t* data, size_t n) { return get_ (data, n); }
233 
237  public:
248  template <typename _Dst_t>
249  in_dev_t& operator>> (_Dst_t& dst) {
250  static_assert ((sizeof (_Dst_t)%sizeof(data_t) == 0),
251  "Target size must be an integer multiple of device's data size");
252  get_ (reinterpret_cast<data_t*>(&dst), sizeof(_Dst_t)/sizeof(data_t));
253  return *this;
254  }
256  template <typename _Dst_t>
257  in_dev_t& operator>> (_Dst_t* dst) = delete;
258 
260  in_dev_t& operator>> (data_t& dst) {
261  get_ (dst);
262  return *this;
263  }
265 
272 
274  iterator begin () noexcept { return iterator(this, iterator::init); }
275  const_iterator begin () const noexcept { return const_iterator(this, iterator::init); }
276  const_iterator cbegin () const noexcept { return const_iterator(this, iterator::init); }
279  iterator end () noexcept { return iterator(this, iterator::eos); }
280  const_iterator end () const noexcept { return const_iterator(this, iterator::eos); }
281  const_iterator cend () const noexcept { return const_iterator(this, iterator::eos); }
284  };
285 
286  /*
287  * Input device predicate (concept)
288  */
289  namespace in_dev_details {
290  using std::declval;
291 
292  // main api members
293  template <class _Tp> using try_get1_t = decltype (declval<_Tp>().get (declval<typename _Tp::data_type&>()));
294  template <class _Tp> using try_get2_t = decltype (declval<_Tp>().get (declval<typename _Tp::data_type*>(), declval<size_t>()));
295  // operators
296  //template <class _Tp> using try_extract_t= decltype (declval<_Tp>() >> declval<typename _Tp::data_type&>());
297  // iterator members
298  template <class _Tp> using try_begin_t = decltype (declval<_Tp>().begin());
299  template <class _Tp> using tryc_begin_t = decltype (declval<const _Tp>().begin());
300  template <class _Tp> using try_cbegin_t = decltype (declval<const _Tp>().cbegin());
301  template <class _Tp> using try_end_t = decltype (declval<_Tp>().begin());
302  template <class _Tp> using tryc_end_t = decltype (declval<const _Tp>().begin());
303  template <class _Tp> using try_cend_t = decltype (declval<const _Tp>().cend());
304 
306  template <typename _Tp, typename =void>
307  struct is_in_dev_ : false_ {};
308 
310  template <typename _Tp>
311  struct is_in_dev_ <_Tp,
312  void_t <
313  typename _Tp::data_type,
314  typename _Tp::pointer_type,
315  typename _Tp::iterator,
316  typename _Tp::const_iterator,
317  use_if_same_t <try_get1_t <_Tp>, size_t>,
318  use_if_same_t <try_get2_t <_Tp>, size_t>,
319  //if_same_t <try_extract_t<_Tp>,typename _Tp&>,
320  use_if_same_t <try_begin_t<_Tp>, typename _Tp::iterator>,
321  use_if_same_t <tryc_begin_t<_Tp>, typename _Tp::const_iterator>,
322  use_if_same_t <try_cbegin_t<_Tp>, typename _Tp::const_iterator>,
323  use_if_same_t <try_end_t<_Tp>, typename _Tp::iterator>,
324  use_if_same_t <tryc_end_t<_Tp>, typename _Tp::const_iterator>,
325  use_if_same_t <try_cend_t<_Tp>, typename _Tp::const_iterator>
326  >
327  > : true_ {};
328  }
334  template <typename _Tp>
336 
337 }
339 
340 #endif /* #ifndef __utl_dev_in_dev_h__ */
iterator end() noexcept
Definition: in_dev.h:159
data_t * pointer_type
Definition: in_dev.h:62
Include all meta library.
data_t data_type
Definition: in_dev.h:61
in_dev_t & operator=(const in_dev_t &)=delete
const_iterator cbegin() const noexcept
Definition: in_dev.h:156
void void_t
void_t type alias
Definition: detection.h:55
Primary template to catch any non input device types.
Definition: in_dev.h:307
const_iterator cend() const noexcept
Definition: in_dev.h:281
_CRTP_IMPL(impl_t)
bool_< true > true_
The type used as a compile-time boolean with true value.
Definition: integral.h:68
decltype(declval< const _Tp >().cbegin()) try_cbegin_t
Definition: in_dev.h:300
decltype(declval< const _Tp >().cend()) try_cend_t
Definition: in_dev.h:303
decltype(declval< _Tp >().get(declval< typename _Tp::data_type & >())) try_get1_t
Definition: in_dev.h:293
~in_dev()=default
Allow destructor from derived only.
meta::eval< meta::enable_if< meta::same_< _T1, _T2 >::value, _Ret > > use_if_same_t
Definition: stl.h:55
decltype(declval< _Tp >().begin()) try_end_t
Definition: in_dev.h:301
const_iterator begin() const noexcept
Definition: in_dev.h:155
in_dev_t & operator>>(_Dst_t &dst)
Template operator >> implementation for for all by value/ref parameters.
Definition: in_dev.h:129
bool_< false > false_
The type used as a compile-time boolean with false value.
Definition: integral.h:69
const_iterator end() const noexcept
Definition: in_dev.h:160
indev_it< in_dev_t, data_t *, streamsize > iterator
Iterator.
Definition: in_dev.h:150
iterator begin() noexcept
.begin implementation
Definition: in_dev.h:274
STL&#39;s core language concepts.
Definition: _1wire.h:30
decltype(declval< _Tp >().begin()) try_begin_t
Definition: in_dev.h:298
const_iterator begin() const noexcept
Definition: in_dev.h:275
in_dev< impl_t, data_t, streamsize > in_dev_t
class type syntactic sugar
Definition: in_dev.h:56
Input device iterator type. We "future call" interface methods from owner class to provide iterator f...
indev_it< in_dev_t, const data_t *, streamsize > const_iterator
Const iterator.
Definition: in_dev.h:151
constexpr bool In_dev
Definition: in_dev.h:335
decltype(declval< _Tp >().get(declval< typename _Tp::data_type * >(), declval< size_t >())) try_get2_t
Definition: in_dev.h:294
Abstract base class for input devices.
Definition: in_dev.h:54
in_dev()=default
A default constructor from derived only.
Iterator collection for devices.
decltype(declval< const _Tp >().begin()) tryc_begin_t
Definition: in_dev.h:299
size_t get_(data_t *data, size_t n)
Definition: in_dev.h:80
const_iterator end() const noexcept
Definition: in_dev.h:280
virtual support tag type
Definition: crtp.h:40
A virtual base class specialization.
Definition: in_dev.h:174
size_t get_(data_t &data)
Definition: in_dev.h:79
const_iterator cend() const noexcept
Definition: in_dev.h:161
decltype(declval< const _Tp >().begin()) tryc_end_t
Definition: in_dev.h:302
const_iterator cbegin() const noexcept
Definition: in_dev.h:276
iterator begin() noexcept
.begin implementation
Definition: in_dev.h:154
Implementation detail main forward header.