Tatooine
handle.h
Go to the documentation of this file.
1#ifndef TATOOINE_HANDLE_H
2#define TATOOINE_HANDLE_H
3//==============================================================================
4#include <tatooine/concepts.h>
6
7#include <cstdint>
8#include <ostream>
9#include <limits>
10//==============================================================================
11namespace tatooine {
12//==============================================================================
13template <typename Child, unsigned_integral Int = std::size_t>
14struct handle {
15 using int_t = Int;
16 static constexpr auto invalid_idx = std::numeric_limits<Int>::max();
17 //----------------------------------------------------------------------------
18 static constexpr auto invalid() { return Child{invalid_idx}; }
19 //============================================================================
20 private:
21 Int i{};
22 //============================================================================
23 public:
24 constexpr handle() : i{invalid_idx} {}
25 constexpr explicit handle(integral auto const _i) : i{static_cast<Int>(_i)} {}
26 constexpr handle(handle const&) = default;
27 constexpr handle(handle&&) noexcept = default;
28 ~handle() = default;
29
30 constexpr auto operator=(handle const&) -> handle& = default;
31 constexpr auto operator=(integral auto const i) -> handle& {
32 this->i = i;
33 return *this;
34 }
35 constexpr auto operator=(handle&&) noexcept -> handle& = default;
36 constexpr auto operator<=>(handle<Child, Int> const& other) const = default;
37 //============================================================================
38 constexpr auto operator++() -> auto& {
39 ++this->i;
40 return *static_cast<Child*>(this);
41 }
42 //----------------------------------------------------------------------------
43 constexpr auto operator++(int /*i*/) {
44 auto h = Child{i};
45 ++i;
46 return h;
47 }
48 //----------------------------------------------------------------------------
49 constexpr auto operator--() -> auto& {
50 --i;
51 return *static_cast<Child*>(this);
52 }
53 //----------------------------------------------------------------------------
54 constexpr auto operator--(int /*i*/) {
55 auto h = Child{i};
56 --i;
57 return h;
58 }
59 constexpr auto operator+=(integral auto const j) { i += j; }
60 constexpr auto operator-=(integral auto const j) { i -= j; }
61 constexpr auto operator*=(integral auto const j) { i *= j; }
62 constexpr auto operator/=(integral auto const j) { i /= j; }
63
64 auto index() const { return i; }
65 auto constexpr is_valid() const { return i != invalid_idx; }
66};
67//==============================================================================
68template <typename Child, unsigned_integral Int>
69auto operator+(handle<Child, Int> const h, integral auto const i) {
70 return Child{h.index() + i};
71}
72// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
73template <typename Child, unsigned_integral Int>
74auto operator+(integral auto const i, handle<Child, Int> const h) {
75 return Child{i + h.index()};
76}
77//------------------------------------------------------------------------------
78template <typename Child, unsigned_integral Int>
79auto operator-(handle<Child, Int> const h, integral auto const i) {
80 return Child{h.index() - i};
81}
82// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
83template <typename Child, unsigned_integral Int>
84auto operator-(integral auto const i, handle<Child, Int> const h) {
85 return Child{i - h.index()};
86}
87//------------------------------------------------------------------------------
88template <typename Child, unsigned_integral Int>
89auto operator*(handle<Child, Int> const h, integral auto const i) {
90 return Child{h.index() * i};
91}
92// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
93template <typename Child, unsigned_integral Int>
94auto operator*(integral auto const i, handle<Child, Int> const h) {
95 return Child{i * h.index()};
96}
97//------------------------------------------------------------------------------
98template <typename Child, unsigned_integral Int>
99auto operator/(handle<Child, Int> const h, integral auto const i) {
100 return Child{h.index() / i};
101}
102// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
103template <typename Child, unsigned_integral Int>
104auto operator/(integral auto const i, handle<Child, Int> const h) {
105 return Child{i / h.index()};
106}
107// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
108//template <typename Child, unsigned_integral Int>
109//auto operator<<(std::ostream & stream, handle<Child, Int> const h) -> auto& {
110// return stream << type_name<Child>() << "[" << h.index() << "]";
111//}
112// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
113template <typename Child, unsigned_integral Int>
114auto operator<<(std::ostream & stream, handle<Child, Int> const h) -> auto& {
115 return stream << h.index();
116}
117// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
118template <typename Child, unsigned_integral Int>
119auto constexpr index(handle<Child, Int> const h) {
120 return h.index();
121}
122//==============================================================================
123} // namespace tatooine
124//==============================================================================
125#endif
Definition: concepts.h:21
Definition: algorithm.h:6
auto operator<<(std::ostream &out, linspace< Real > const &l) -> auto &
Definition: linspace.h:165
auto constexpr operator/(Lhs const &lhs, Rhs const &rhs)
component-wise division
Definition: operator_overloads.h:103
constexpr auto operator*(diag_static_tensor< TensorA, M, N > const &A, static_vec auto const &b) -> vec< common_type< tatooine::value_type< TensorA >, tatooine::value_type< decltype(b)> >, M > requires(N==decltype(b)::dimension(0))
Definition: diag_tensor.h:141
auto constexpr index(handle< Child, Int > const h)
Definition: handle.h:119
auto constexpr operator-(static_tensor auto const &t)
Definition: operator_overloads.h:31
auto constexpr operator+(static_tensor auto const &lhs, arithmetic_or_complex auto const scalar)
Definition: operator_overloads.h:35
Definition: handle.h:14
constexpr auto operator/=(integral auto const j)
Definition: handle.h:62
constexpr handle(integral auto const _i)
Definition: handle.h:25
constexpr auto operator++(int)
Definition: handle.h:43
constexpr auto operator--(int)
Definition: handle.h:54
static constexpr auto invalid_idx
Definition: handle.h:16
constexpr auto operator+=(integral auto const j)
Definition: handle.h:59
auto constexpr is_valid() const
Definition: handle.h:65
constexpr auto operator=(handle &&) noexcept -> handle &=default
constexpr handle(handle const &)=default
auto index() const
Definition: handle.h:64
constexpr handle(handle &&) noexcept=default
constexpr auto operator*=(integral auto const j)
Definition: handle.h:61
Int int_t
Definition: handle.h:15
static constexpr auto invalid()
Definition: handle.h:18
Int i
Definition: handle.h:21
constexpr handle()
Definition: handle.h:24
constexpr auto operator--() -> auto &
Definition: handle.h:49
constexpr auto operator-=(integral auto const j)
Definition: handle.h:60