SNode.C
Loading...
Searching...
No Matches
AttributeInjector.h
Go to the documentation of this file.
1/*
2 * SNode.C - a slim toolkit for network communication
3 * Copyright (C) Volker Christian <me@vchrist.at>
4 * 2020, 2021, 2022, 2023, 2024, 2025
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published
8 * by the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#ifndef ATTRIBUTEINJECTOR_H
21#define ATTRIBUTEINJECTOR_H
22
23#ifndef DOXYGEN_SHOULD_SKIP_THIS
24
25#include <algorithm>
26#include <functional>
27#include <map>
28#include <memory>
29
30#endif /* DOXYGEN_SHOULD_SKIP_THIS */
31
32namespace utils {
33
34 template <unsigned N>
35 struct fixed_string {
36 char buf[N + 1]{};
37 constexpr fixed_string(char const* s) { // cppcheck-suppress noExplicitConstructor
38 for (unsigned i = 0; i != N; ++i) {
39 buf[i] = s[i];
40 }
41 }
42
43 constexpr operator char const*() const {
44 return buf;
45 }
46
47 auto operator<=>(const fixed_string&) const = default;
48 };
49 template <unsigned N>
50 fixed_string(char const (&)[N]) -> fixed_string<N - 1>;
51
52 template <typename Attribute>
54
55 template <InjectableAttribute Attribute>
57 public:
58 constexpr explicit AttributeProxy(const Attribute& attribute)
59 : attribute(attribute) { // copy constructor neccessary
60 }
61
62 constexpr Attribute& operator*() {
63 return attribute;
64 }
65
66 private:
68 };
69
71 public:
72 template <InjectableAttribute Attribute>
73 constexpr bool setAttribute(const Attribute& attribute, bool overwrite = false) {
74 bool inserted = false;
75
76 if (!this->attribute || overwrite) {
77 attributeType = typeid(Attribute).name();
78 this->attribute = std::shared_ptr<void>(new AttributeProxy<Attribute>(attribute));
79 inserted = true;
80 }
81
82 return inserted;
83 }
84
85 template <InjectableAttribute Attribute>
86 constexpr bool setAttribute(const Attribute&& attribute, bool overwrite = false) {
87 bool inserted = false;
88
89 if (!this->attribute || overwrite) {
90 attributeType = typeid(Attribute).name();
91 this->attribute = std::shared_ptr<void>(new AttributeProxy<Attribute>(attribute));
92 inserted = true;
93 }
94
95 return inserted;
96 }
97
98 template <InjectableAttribute Attribute>
99 constexpr bool getAttribute(const std::function<void(Attribute&)>& onFound) const {
100 const bool found = false;
101
102 if (attribute != nullptr && attributeType == typeid(Attribute).name()) {
103 onFound(**std::static_pointer_cast<AttributeProxy<Attribute>>(attribute));
104 }
105
106 return found;
107 }
108
109 template <InjectableAttribute Attribute>
110 constexpr void getAttribute(const std::function<void(Attribute&)>& onFound,
111 const std::function<void(const std::string&)>& onNotFound) const {
112 if (attribute != nullptr && attributeType == typeid(Attribute).name()) {
113 onFound(**std::static_pointer_cast<AttributeProxy<Attribute>>(attribute));
114 } else {
115 onNotFound(std::string(typeid(Attribute).name()));
116 }
117 }
118
119 private:
120 std::shared_ptr<void> attribute{nullptr};
122 };
123
125 public:
126 template <InjectableAttribute Attribute, fixed_string key = "">
127 constexpr bool setAttribute(const Attribute& attribute, const std::string& subKey = "", bool overwrite = false) {
128 bool inserted = false;
129
130 if (attributes.find(typeid(Attribute).name() + std::string(key) + subKey) == attributes.end() || overwrite) {
131 attributes[typeid(Attribute).name() + std::string(key) + subKey] =
132 std::shared_ptr<void>(new AttributeProxy<Attribute>(attribute));
133 inserted = true;
134 }
135
136 return inserted;
137 }
138
139 template <InjectableAttribute Attribute, fixed_string key = "">
140 constexpr bool setAttribute(const Attribute&& attribute, const std::string& subKey = "", bool overwrite = false) {
141 bool inserted = false;
142
143 if (attributes.find(typeid(Attribute).name() + std::string(key) + subKey) == attributes.end() || overwrite) {
144 attributes[typeid(Attribute).name() + std::string(key) + subKey] =
145 std::shared_ptr<void>(new AttributeProxy<Attribute>(attribute));
146 inserted = true;
147 }
148
149 return inserted;
150 }
151
152 template <InjectableAttribute Attribute, fixed_string key = "">
153 constexpr bool delAttribute(const std::string& subKey = "") {
154 bool deleted = attributes.erase(typeid(Attribute).name() + std::string(key) + subKey) > 0;
155
156 return deleted;
157 }
158
159 template <InjectableAttribute Attribute, fixed_string key = "">
160 constexpr bool hasAttribute(const std::string& subKey = "") const {
161 bool found = attributes.find(typeid(Attribute).name() + std::string(key) + subKey) != attributes.end();
162
163 return found;
164 }
165
166 template <InjectableAttribute Attribute, fixed_string key = "">
167 bool getAttribute(const std::function<void(Attribute&)>& onFound, const std::string& subKey = "") const {
168 bool found = false;
169
170 std::map<std::string, std::shared_ptr<void>>::const_iterator it =
171 attributes.find(typeid(Attribute).name() + std::string(key) + subKey);
172
173 if (it != attributes.end()) {
174 found = true;
175
176 onFound(**std::static_pointer_cast<AttributeProxy<Attribute>>(it->second));
177 }
178
179 return found;
180 }
181
182 template <InjectableAttribute Attribute, fixed_string key = "">
183 void getAttribute(const std::function<void(Attribute&)>& onFound,
184 const std::function<void(const std::string&)>& onNotFound,
185 const std::string& subKey = "") const {
186 std::map<std::string, std::shared_ptr<void>>::const_iterator it =
187 attributes.find(typeid(Attribute).name() + std::string(key) + subKey);
188
189 if (it != attributes.end()) {
190 onFound(**std::static_pointer_cast<AttributeProxy<Attribute>>(it->second));
191 } else {
192 onNotFound(std::string(typeid(Attribute).name()) + std::string(key) + subKey);
193 }
194 }
195
196 void reset() {
197 attributes.clear();
198 }
199
200 private:
202 };
203
204} // namespace utils
205
206#endif // ATTRIBUTEINJECTOR_H
#define DECLARE_ROUTER_REQUESTMETHOD(METHOD)
Definition Router.h:48
#define APPLICATION(req, res)
Definition Router.h:45
unsigned long lastTick
Definition Controller.h:77
void next(const std::string &how) const
const std::shared_ptr< Request > & getRequest()
RootRoute * rootRoute
Definition Controller.h:72
Controller(const std::shared_ptr< web::http::server::Request > &request, const std::shared_ptr< web::http::server::Response > &response)
std::shared_ptr< Response > response
Definition Controller.h:70
Controller(const Controller &controller)
void setCurrentRoute(Route *currentRoute)
void setRootRoute(RootRoute *rootRoute)
std::shared_ptr< Request > request
Definition Controller.h:69
bool dispatchNext(const std::string &parentMountPath)
int getFlags() const
Controller & operator=(const Controller &controller) noexcept
const std::shared_ptr< Response > & getResponse()
void operator()(const std::string &how="") const
Definition Next.cpp:34
Next(Controller &controller)
Definition Next.cpp:30
Controller controller
Definition Next.h:44
Request & extend()
Definition Request.cpp:52
std::string httpVersion
Definition Request.h:74
web::http::CiStringMap< std::string > cookies
Definition Request.h:80
Request(Request &&) noexcept=delete
std::string originalUrl
Definition Request.h:54
const std::string & cookie(const std::string &key) const
Definition Request.cpp:67
std::string url
Definition Request.h:73
Request & operator=(Request &&) noexcept=delete
std::vector< char > body
Definition Request.h:81
const std::string & get(const std::string &key, int i=0) const
Definition Request.cpp:63
web::http::CiStringMap< std::string > queries
Definition Request.h:78
std::string method
Definition Request.h:72
Request(const std::shared_ptr< web::http::server::Request > &request) noexcept
Definition Request.cpp:34
std::shared_ptr< web::http::server::Request > requestBase
Definition Request.h:61
Request(Request &)=delete
const std::string & query(const std::string &key) const
Definition Request.cpp:77
web::http::CiStringMap< std::string > headers
Definition Request.h:79
const std::string & param(const std::string &id, const std::string &fallBack="")
Definition Request.cpp:48
Request & operator=(Request &)=delete
std::string path
Definition Request.h:55
Request()=default
std::string nullstr
Definition Request.h:84
Route & head(const Router &router) const
Definition Router.cpp:70
Route & all(const Router &router) const
Definition Router.cpp:61
Route & options(const Router &router) const
Definition Router.cpp:67
Route & connect(const Router &router) const
Definition Router.cpp:66
Route & post(const Router &router) const
Definition Router.cpp:64
Route & del(const Router &router) const
Definition Router.cpp:65
Router(const Router &)=default
Route & use(const Router &router) const
Definition Router.cpp:60
Route & get(const Router &router) const
Definition Router.cpp:62
Route & trace(const Router &router) const
Definition Router.cpp:68
Route & patch(const Router &router) const
Definition Router.cpp:69
void laxRouting(bool strict=false)
Definition Router.cpp:56
Route & put(const Router &router) const
Definition Router.cpp:63
std::shared_ptr< RootRoute > rootRoute
Definition Router.h:95
WebAppT(const std::string &name, const Router &router)
Definition WebAppT.h:58
WebAppT(const std::string &name)
Definition WebAppT.h:54
static void free()
Definition WebApp.cpp:50
static void init(int argc, char *argv[])
Definition WebApp.cpp:34
static void stop()
Definition WebApp.cpp:42
static core::TickStatus tick(const utils::Timeval &timeOut=0)
Definition WebApp.cpp:46
static core::State state()
Definition WebApp.cpp:54
static int start(const utils::Timeval &timeOut={LONG_MAX, 0})
Definition WebApp.cpp:38
WebApp(const Router &router)
Definition WebApp.cpp:30
constexpr AttributeProxy(const Attribute &attribute)
constexpr Attribute & operator*()
constexpr bool setAttribute(const Attribute &&attribute, const std::string &subKey="", bool overwrite=false)
constexpr bool hasAttribute(const std::string &subKey="") const
constexpr bool delAttribute(const std::string &subKey="")
bool getAttribute(const std::function< void(Attribute &)> &onFound, const std::string &subKey="") const
constexpr bool setAttribute(const Attribute &attribute, const std::string &subKey="", bool overwrite=false)
constexpr bool setAttribute(const Attribute &attribute, bool overwrite=false)
constexpr bool setAttribute(const Attribute &&attribute, bool overwrite=false)
std::shared_ptr< void > attribute
constexpr bool getAttribute(const std::function< void(Attribute &)> &onFound) const
int main(int argc, char *argv[])
Definition Config.h:37
fixed_string(char const (&)[N]) -> fixed_string< N - 1 >
auto operator<=>(const fixed_string &) const =default
constexpr fixed_string(char const *s)
constexpr operator char const *() const