image2d_data.hpp 1.59 KB
Newer Older
1
2
3
4
#pragma once

#include <py/core/type_info.hpp>

5
6
7
8
#include <cstddef>
#include <memory>


9
namespace mln::py
10
{
11
12
  template <typename T>
  class image2d_data;
13

14
15
  struct image2d_data_base
  {
16
    image2d_data_base() = default;
17
    image2d_data_base(std::size_t n);
18

19
    virtual ~image2d_data_base() = default;
20

21
22
23
    std::size_t m_size;
    std::byte*  m_buf;
  };
Michaël Roynard's avatar
Michaël Roynard committed
24

25
26
  template <>
  class image2d_data<void> : public image2d_data_base
Michaël Roynard's avatar
Michaël Roynard committed
27
  {
28
29
  public:
    image2d_data() = default;
30

Michaël Roynard's avatar
Michaël Roynard committed
31
32
    image2d_data(std::size_t n, Info::type_id tid)
      : image2d_data_base{n * Info::dyn_sizeof(tid)}
33
34
35
36
    {
      m_buf = m_alloc.allocate(m_size);
      std::uninitialized_default_construct_n(m_buf, m_size);
    }
37

38
    ~image2d_data() override { m_alloc.deallocate(m_buf, m_size); }
39

40
    std::byte* buf() const { return m_buf; }
41

42
43
44
  protected:
    std::allocator<std::byte> m_alloc;
  };
45

46
47
  template <typename T>
  class image2d_data : public image2d_data_base
Michaël Roynard's avatar
Michaël Roynard committed
48
  {
49
50
51
52
53
54
55
  public:
    image2d_data(std::size_t n)
      : image2d_data_base{n}
    {
      m_buf = reinterpret_cast<std::byte*>(m_alloc.allocate(n));
      std::uninitialized_default_construct_n(reinterpret_cast<T*>(m_buf), m_size);
    }
56

57
58
59
60
61
62
63
64
65
66
67
68
    image2d_data(std::size_t n, T val)
      : image2d_data_base{n}
    {
      m_buf = reinterpret_cast<std::byte*>(m_alloc.allocate(n));
      std::uninitialized_fill_n(reinterpret_cast<T*>(m_buf), m_size, val);
    }

    ~image2d_data() override
    {
      std::destroy_n(reinterpret_cast<T*>(m_buf), m_size);
      m_alloc.deallocate(reinterpret_cast<T*>(m_buf), m_size);
    }
69

70
71
72
  private:
    std::allocator<T> m_alloc;
  };
Edwin Carlinet's avatar
Edwin Carlinet committed
73
} // namespace mln::py