aboutsummaryrefslogtreecommitdiff
path: root/libs/kstd/kstd/asm_ptr.hpp
blob: 5ffc480f2648da104b266c613284e81b03b77237 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#ifndef KSTD_ASM_POINTER_HPP
#define KSTD_ASM_POINTER_HPP

#include <bit>
#include <cstddef>

namespace kstd
{

  //! A pointer that is defined in some assembly source file.
  //!
  //! @tparam Type The type of the pointer
  template<typename Type>
  struct asm_ptr
  {
    using value_type = Type;
    using pointer = value_type *;
    using const_pointer = value_type const *;
    using reference = value_type &;
    using const_reference = value_type const &;

    asm_ptr() = delete;
    asm_ptr(asm_ptr const &) = delete;
    asm_ptr(asm_ptr &&) = delete;
    ~asm_ptr() = delete;

    constexpr auto operator=(asm_ptr const &) = delete;
    constexpr auto operator=(asm_ptr &&) = delete;

    auto get() const noexcept -> pointer
    {
      return m_ptr;
    }

    constexpr auto operator+(std::ptrdiff_t offset) const noexcept -> pointer
    {
      return std::bit_cast<pointer>(m_ptr) + offset;
    }

    constexpr auto operator*() noexcept -> reference
    {
      return *(std::bit_cast<pointer>(m_ptr));
    }

    constexpr auto operator*() const noexcept -> const_reference
    {
      return *(std::bit_cast<const_pointer>(m_ptr));
    }

    constexpr auto operator[](std::ptrdiff_t offset) noexcept -> reference
    {
      return *(*this + offset);
    }

    constexpr auto operator[](std::ptrdiff_t offset) const noexcept -> const_reference
    {
      return *(*this + offset);
    }

    constexpr auto operator->() noexcept -> pointer
    {
      return m_ptr;
    }

    constexpr auto operator->() const noexcept -> const_pointer
    {
      return m_ptr;
    }

  private:
    pointer m_ptr;
  };

}  // namespace kstd

#endif