Orbit Framework 2.0.0
A high-performance asynchronous C++20 HTTP server framework.
Loading...
Searching...
No Matches
RequestContext.hpp
Go to the documentation of this file.
1#pragma once
3#include <any>
4#include <array>
5#include <deque>
6#include <optional>
7#include <string>
8#include <string_view>
9#include <type_traits>
10#include <typeindex>
11#include <unordered_map>
12#include <utility>
13
14namespace orbit::http {
15
66public:
68 template <typename T>
69 T& set(T value) {
70 static_assert(!std::is_same_v<T, std::any>,
71 "RequestContext::set<std::any>() is not supported: assigning an any into the slot's "
72 "own any merges them instead of nesting one inside the other");
73 Slot& slot = slot_for(std::type_index(typeid(T)));
74 slot.value = std::move(value);
75 return *std::any_cast<T>(&slot.value);
76 }
77
79 template <typename T>
80 T* get() {
81 Slot* slot = find(std::type_index(typeid(T)));
82 return slot ? std::any_cast<T>(&slot->value) : nullptr;
83 }
84 template <typename T>
85 const T* get() const {
86 const Slot* slot = find(std::type_index(typeid(T)));
87 return slot ? std::any_cast<T>(&slot->value) : nullptr;
88 }
89
91 template <typename T>
92 bool has() const {
93 return get<T>() != nullptr;
94 }
95
98 template <typename T, typename... Args>
99 T& ensure(Args&&... args) {
100 if (T* existing = get<T>()) return *existing;
101 return set<T>(T(std::forward<Args>(args)...));
102 }
103
106 template <typename T>
107 bool erase() {
108 Slot* slot = find(std::type_index(typeid(T)));
109 if (!slot) return false;
110 slot->value.reset();
111 slot->key = std::type_index(typeid(void));
112 slot->used = false;
113 return true;
114 }
115
116 // --- Named attributes, for interop or keys only known at runtime ---
117
119 void set_attr(std::string name, std::string value) { attrs_[std::move(name)] = std::move(value); }
120
122 const std::string* attr(std::string_view name) const {
123 auto it = attrs_.find(std::string(name));
124 return it == attrs_.end() ? nullptr : &it->second;
125 }
126
127private:
128 struct Slot {
129 std::type_index key{typeid(void)};
130 std::any value;
131 bool used = false;
132 };
133
134 static constexpr size_t kInlineCapacity = 4;
135
136 Slot* find(std::type_index key) {
137 for (auto& slot : inline_) {
138 if (slot.used && slot.key == key) return &slot;
139 }
140 if (overflow_) {
141 for (auto& slot : *overflow_) {
142 if (slot.used && slot.key == key) return &slot;
143 }
144 }
145 return nullptr;
146 }
147 const Slot* find(std::type_index key) const { return const_cast<RequestContext*>(this)->find(key); }
148
149 // A slot for @p key: its own if already set, else a reused erased slot
150 // or a fresh one. Never moves another slot's contents, so every
151 // pointer or reference this class has already handed out stays valid.
152 Slot& slot_for(std::type_index key) {
153 if (Slot* existing = find(key)) return *existing;
154 for (auto& slot : inline_) {
155 if (!slot.used) {
156 slot.key = key;
157 slot.used = true;
158 return slot;
159 }
160 }
161 // A default-constructed std::deque allocates (libstdc++: a map
162 // array and a node) even while empty, unlike std::vector; built
163 // only once a 5th distinct type actually needs it, so a request
164 // that never grows past the inline slots still allocates nothing.
165 if (!overflow_) overflow_.emplace();
166 for (auto& slot : *overflow_) {
167 if (!slot.used) {
168 slot.key = key;
169 slot.used = true;
170 return slot;
171 }
172 }
173 // deque::push_back never invalidates references to existing
174 // elements (only iterators), unlike vector on reallocation.
175 overflow_->push_back(Slot{key, {}, true});
176 return overflow_->back();
177 }
178
179 std::array<Slot, kInlineCapacity> inline_{};
180 std::optional<std::deque<Slot>> overflow_;
181 std::unordered_map<std::string, std::string> attrs_;
182};
183
184} // namespace orbit::http
Type-indexed, per-request storage: middleware that computes something for later middleware or the han...
Definition RequestContext.hpp:65
std::unordered_map< std::string, std::string > attrs_
Definition RequestContext.hpp:181
void set_attr(std::string name, std::string value)
Sets a named string attribute (distinct storage from set<T>()).
Definition RequestContext.hpp:119
static constexpr size_t kInlineCapacity
Definition RequestContext.hpp:134
Slot * find(std::type_index key)
Definition RequestContext.hpp:136
std::optional< std::deque< Slot > > overflow_
Definition RequestContext.hpp:180
const Slot * find(std::type_index key) const
Definition RequestContext.hpp:147
T & ensure(Args &&... args)
Definition RequestContext.hpp:99
T & set(T value)
Sets (replacing any previous value) and returns a reference to it.
Definition RequestContext.hpp:69
std::array< Slot, kInlineCapacity > inline_
Definition RequestContext.hpp:179
Slot & slot_for(std::type_index key)
Definition RequestContext.hpp:152
const std::string * attr(std::string_view name) const
A named attribute, or null if it was never set.
Definition RequestContext.hpp:122
T * get()
The value for T, or null if none is set.
Definition RequestContext.hpp:80
bool has() const
True if a value for T is set.
Definition RequestContext.hpp:92
bool erase()
Definition RequestContext.hpp:107
const T * get() const
Definition RequestContext.hpp:85
Definition Task.hpp:10
Definition RequestContext.hpp:128
bool used
Definition RequestContext.hpp:131
std::type_index key
Definition RequestContext.hpp:129
std::any value
Definition RequestContext.hpp:130