2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
42#ifndef ATTRIBUTEINJECTOR_H
43#define ATTRIBUTEINJECTOR_H
45#ifndef DOXYGEN_SHOULD_SKIP_THIS
59 constexpr fixed_string(
char const* s) {
60 for (
unsigned i = 0; i != N; ++i) {
65 constexpr operator
char const*()
const {
69 auto operator<=>(
const fixed_string&)
const =
default;
72 fixed_string(
char const (&)[N]) -> fixed_string<N - 1>;
77 template <InjectableAttribute Attribute>
78 class AttributeProxy {
80 constexpr explicit AttributeProxy(
const Attribute& attribute)
81 : attribute(attribute) {
84 constexpr Attribute& operator*() {
92 class SingleAttributeInjector {
94 template <InjectableAttribute Attribute>
95 constexpr bool setAttribute(
const Attribute& attribute,
bool overwrite =
false) {
96 bool inserted =
false;
98 if (!
this->attribute || overwrite) {
99 attributeType =
typeid(Attribute).name();
100 this->attribute = std::shared_ptr<
void>(
new AttributeProxy<Attribute>(attribute));
107 template <InjectableAttribute Attribute>
108 constexpr bool setAttribute(
const Attribute&& attribute,
bool overwrite =
false) {
109 bool inserted =
false;
111 if (!
this->attribute || overwrite) {
112 attributeType =
typeid(Attribute).name();
113 this->attribute = std::shared_ptr<
void>(
new AttributeProxy<Attribute>(attribute));
120 template <InjectableAttribute Attribute>
121 constexpr bool getAttribute(
const std::function<
void(Attribute&)>& onFound)
const {
122 const bool found =
false;
124 if (attribute !=
nullptr && attributeType ==
typeid(Attribute).name()) {
125 onFound(**std::static_pointer_cast<AttributeProxy<Attribute>>(attribute));
131 template <InjectableAttribute Attribute>
132 constexpr void getAttribute(
const std::function<
void(Attribute&)>& onFound,
133 const std::function<
void(
const std::string&)>& onNotFound)
const {
134 if (attribute !=
nullptr && attributeType ==
typeid(Attribute).name()) {
135 onFound(**std::static_pointer_cast<AttributeProxy<Attribute>>(attribute));
137 onNotFound(std::string(
typeid(Attribute).name()));
142 std::shared_ptr<
void> attribute{
nullptr};
143 std::string attributeType;
146 class MultibleAttributeInjector {
148 template <InjectableAttribute Attribute, fixed_string key =
"">
149 constexpr bool setAttribute(
const Attribute& attribute,
const std::string& subKey =
"",
bool overwrite =
false) {
150 bool inserted =
false;
152 if (attributes.find(
typeid(Attribute).name() + std::string(key) + subKey) == attributes.end() || overwrite) {
153 attributes[
typeid(Attribute).name() + std::string(key) + subKey] =
154 std::shared_ptr<
void>(
new AttributeProxy<Attribute>(attribute));
161 template <InjectableAttribute Attribute, fixed_string key =
"">
162 constexpr bool setAttribute(
const Attribute&& attribute,
const std::string& subKey =
"",
bool overwrite =
false) {
163 bool inserted =
false;
165 if (attributes.find(
typeid(Attribute).name() + std::string(key) + subKey) == attributes.end() || overwrite) {
166 attributes[
typeid(Attribute).name() + std::string(key) + subKey] =
167 std::shared_ptr<
void>(
new AttributeProxy<Attribute>(attribute));
174 template <InjectableAttribute Attribute, fixed_string key =
"">
175 constexpr bool delAttribute(
const std::string& subKey =
"") {
176 bool deleted = attributes.erase(
typeid(Attribute).name() + std::string(key) + subKey) > 0;
181 template <InjectableAttribute Attribute, fixed_string key =
"">
182 constexpr bool hasAttribute(
const std::string& subKey =
"")
const {
183 bool found = attributes.find(
typeid(Attribute).name() + std::string(key) + subKey) != attributes.end();
188 template <InjectableAttribute Attribute, fixed_string key =
"">
189 bool getAttribute(
const std::function<
void(Attribute&)>& onFound,
const std::string& subKey =
"")
const {
192 std::map<std::string, std::shared_ptr<
void>>::const_iterator it =
193 attributes.find(
typeid(Attribute).name() + std::string(key) + subKey);
195 if (it != attributes.end()) {
198 onFound(**std::static_pointer_cast<AttributeProxy<Attribute>>(it->second));
204 template <InjectableAttribute Attribute, fixed_string key =
"">
205 void getAttribute(
const std::function<
void(Attribute&)>& onFound,
206 const std::function<
void(
const std::string&)>& onNotFound,
207 const std::string& subKey =
"")
const {
208 std::map<std::string, std::shared_ptr<
void>>::const_iterator it =
209 attributes.find(
typeid(Attribute).name() + std::string(key) + subKey);
211 if (it != attributes.end()) {
212 onFound(**std::static_pointer_cast<AttributeProxy<Attribute>>(it->second));
214 onNotFound(std::string(
typeid(Attribute).name()) + std::string(key) + subKey);
223 std::map<std::string, std::shared_ptr<
void>> attributes;
#define DECLARE_ROUTER_REQUESTMETHOD(METHOD)
static express::Router getRouter()
static WebApp getWebApp(const std::string &name)
static WebApp getWebApp(const std::string &name)
int main(int argc, char *argv[])