any_span.hpp 1.16 KB
Newer Older
1
2
3
#pragma once

#include <py/core/type_info.hpp>
4
#include <py/core/any_ref.hpp>
5
6
7
8
9
10
11

#include <range/v3/range_access.hpp>
#include <range/v3/range_concepts.hpp>
#include <range/v3/view/all.hpp>
#include <range/v3/view_facade.hpp>

#include <cstddef>
Michaël Roynard's avatar
Michaël Roynard committed
12
#include <iterator>
13
14
15

namespace mln::py
{
Michaël Roynard's avatar
Michaël Roynard committed
16
17
18
19
20
21
  class any_span : public ::ranges::view_facade<any_span, ::ranges::finite>
  {
  private:
    friend ::ranges::range_access;

    struct cursor
22
23
    {
    private:
Michaël Roynard's avatar
Michaël Roynard committed
24
25
      std::byte*  m_ptr;
      std::size_t m_stride;
26
      Info        m_info;
27

Michaël Roynard's avatar
Michaël Roynard committed
28
29
    public:
      cursor() = default;
30
      cursor(std::byte* buf_start, std::size_t stride, Info i);
31

32
      any_ref read() const;
Michaël Roynard's avatar
Michaël Roynard committed
33
34
35
36
      void  next();
      void  prev();
      bool  equal(const cursor& c) const;
    };
37

Michaël Roynard's avatar
Michaël Roynard committed
38
39
40
  public:
    any_span() = default;
    any_span(std::byte* buf, std::size_t sz, Info i);
41

42
43
44
45
46
    template <typename T>
    any_span(T* buf, std::size_t sz)
    {
        m_buffer = static_cast<std::byte*>(buf);
        m_size = sz;
47
        m_type = Trait<T>::value;
48
49
    }

Michaël Roynard's avatar
Michaël Roynard committed
50
51
52
  private:
    cursor begin_cursor();
    cursor end_cursor();
53

Michaël Roynard's avatar
Michaël Roynard committed
54
55
56
57
58
    std::byte*  m_buffer;
    std::size_t m_size;
    Info        m_type;
  };
} // namespace mln::py