uatlib
permit.hpp
Go to the documentation of this file.
1 
4 #ifndef UAT_PERMIT_HPP
5 #define UAT_PERMIT_HPP
6 
7 #include <uat/type.hpp>
8 
9 #include <memory>
10 
11 #include <boost/functional/hash.hpp>
12 
13 namespace uat
14 {
15 
21 {
22 public:
24  template <region_compatible R> region_view(const R& a) : region_(std::addressof(a)) {}
25 
26  region_view() noexcept = delete;
27 
28  region_view(const region_view&) = default;
29  region_view(region_view&&) noexcept = default;
30 
31  auto operator=(const region_view&) -> region_view& = default;
32  auto operator=(region_view&&) noexcept -> region_view& = default;
33 
38  template <region_compatible R> auto downcast() const -> const R& { return *reinterpret_cast<const R*>(region_); }
39 
40 private:
41  const void* region_;
42 };
43 
49 template <region_compatible Region> struct permit
50 {
51  permit() noexcept = delete;
52  permit(Region s, uint_t time) noexcept : location(std::move(s)), time(time) {}
53 
54  auto operator==(const permit& other) const -> bool { return location == other.location && time == other.time; }
55  auto operator!=(const permit& other) const -> bool { return location != other.location || time != other.time; }
56 
57  Region location;
59 };
60 
62 template <std::size_t I, typename R> decltype(auto) get(permit<R>& ts)
63 {
64  if constexpr (I == 0)
65  return ts.location;
66  else
67  return ts.time;
68 }
69 
71 template <std::size_t I, typename R> decltype(auto) get(const permit<R>& ts)
72 {
73  if constexpr (I == 0)
74  return ts.location;
75  else
76  return ts.time;
77 }
78 
79 } // namespace uat
80 
81 namespace std
82 {
83 
85 template <typename Region> struct hash<uat::permit<Region>>
86 {
88  auto operator()(const uat::permit<Region>& p) const noexcept -> size_t
89  {
90  size_t seed = 0;
91  boost::hash_combine(seed, std::hash<Region>{}(p.location));
92  boost::hash_combine(seed, std::hash<std::size_t>{}(p.time));
93  return seed;
94  }
95 };
96 
98 template <typename R> struct tuple_size<uat::permit<R>> : public integral_constant<size_t, 2>
99 {};
100 
102 template <typename R> struct tuple_element<0, uat::permit<R>>
103 {
104  using type = R&;
105 };
106 
108 template <typename R> struct tuple_element<0, const uat::permit<R>>
109 {
110  using type = const R&;
111 };
112 
114 template <typename R> struct tuple_element<1, uat::permit<R>>
115 {
116  using type = uat::uint_t;
117 };
118 
120 template <typename R> struct tuple_element<1, const uat::permit<R>>
121 {
122  using type = uat::uint_t;
123 };
124 
125 } // namespace std
126 
127 #endif // UAT_PERMIT_HPP
A non-owning wrapper to an atomic region in the airspace.
Definition: permit.hpp:21
region_view(const R &a)
Constructs a non-owning wrapper to a region that satisfies the region_compatible concept.
Definition: permit.hpp:24
auto downcast() const -> const R &
Definition: permit.hpp:38
A tuple containing a region and a time step.
Definition: permit.hpp:50
Region location
The region.
Definition: permit.hpp:57
uint_t time
The time step.
Definition: permit.hpp:58
Basic types and utilities.
std::size_t uint_t
Default unsigned integer type.
Definition: type.hpp:29