aboutsummaryrefslogtreecommitdiff
path: root/include/memory/asm_pointer.hpp
blob: 9ec2218231fdba1b42ad9ecc402b1ce6b4e4479c (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
#ifndef TEACHOS_MEMORY_ASM_POINTER_HPP
#define TEACHOS_MEMORY_ASM_POINTER_HPP

namespace teachos::memory
{

  /**
   * @brief A pointer that is defined in some assembly source file.
   *
   * @tparam Type The type of the pointer
   * @since 0.0.1
   */
  template<typename Type>
  struct asm_pointer
  {
    /**
     * @brief The type of the underlying pointer.
     */
    using pointer = Type *;

    /**
     * @brief Construct a new asm_pointer for a given assembly-defined pointer.
     * @param pointer A pointer defined in assembly.
     */
    constexpr asm_pointer(Type *& pointer)
        : m_pointer{&pointer}
    {
    }

    /**
     * @brief Access the underlying pointer.
     * @return The pointer wrapped by this asm_pointer.
     */
    auto constexpr operator*() -> pointer & { return *m_pointer; }

    /**
     * @brief Access the underlying pointer.
     * @return The pointer wrapped by this asm_pointer.
     */
    auto constexpr operator*() const -> pointer const & { return *m_pointer; }

  private:
    pointer * m_pointer;
  };

  /**
   * @copydoc asm_pointer
   *
   * @note This specialization allows the use of this type for pointers to constant data.
   * @since 0.0.1
   */
  template<typename Type>
  struct asm_pointer<Type const>
  {
    /** @copydoc asm_pointer<Type>::asm_pointer */
    constexpr asm_pointer(Type const & pointer)
        : m_pointer{&pointer}
    {
    }

    /** @copydoc asm_pointer<Type>::operator*() */
    auto constexpr operator*() -> Type const & { return *m_pointer; }
    /** @copydoc asm_pointer<Type>::operator*() const */
    auto constexpr operator*() const -> Type const & { return *m_pointer; }

  private:
    Type const * m_pointer;
  };

}  // namespace teachos::memory

#endif