drogon
C++14/17-based HTTP application framework
Loading...
Searching...
No Matches
Attribute.h
1
14
15#pragma once
16
17#include <trantor/utils/Logger.h>
18#include <map>
19#include <memory>
20#include <any>
21
22namespace drogon
23{
29{
30 public:
39 template <typename T>
40 const T &get(const std::string &key) const
41 {
42 static const T nullVal = T();
43 auto it = attributesMap_.find(key);
44 if (it != attributesMap_.end())
45 {
46 if (typeid(T) == it->second.type())
47 {
48 return *(std::any_cast<T>(&(it->second)));
49 }
50 else
51 {
52 LOG_ERROR << "Bad type";
53 }
54 }
55 return nullVal;
56 }
57
61 std::any &operator[](const std::string &key)
62 {
63 return attributesMap_[key];
64 }
65
73 void insert(const std::string &key, const std::any &obj)
74 {
75 attributesMap_[key] = obj;
76 }
77
85 void insert(const std::string &key, std::any &&obj)
86 {
87 attributesMap_[key] = std::move(obj);
88 }
89
93 void erase(const std::string &key)
94 {
95 attributesMap_.erase(key);
96 }
97
101 bool find(const std::string &key)
102 {
103 if (attributesMap_.find(key) == attributesMap_.end())
104 {
105 return false;
106 }
107 return true;
108 }
109
113 void clear()
114 {
115 attributesMap_.clear();
116 }
117
121 Attributes() = default;
122
123 private:
124 using AttributesMap = std::map<std::string, std::any>;
125 AttributesMap attributesMap_;
126};
127
128using AttributesPtr = std::shared_ptr<Attributes>;
129
130} // namespace drogon
const T & get(const std::string &key) const
Get the data identified by the key parameter.
Definition Attribute.h:40
void clear()
Clear all attributes.
Definition Attribute.h:113
bool find(const std::string &key)
Return true if the data identified by the key exists.
Definition Attribute.h:101
void insert(const std::string &key, std::any &&obj)
Insert a key-value pair.
Definition Attribute.h:85
void insert(const std::string &key, const std::any &obj)
Insert a key-value pair.
Definition Attribute.h:73
Attributes()=default
Constructor, usually called by the framework.
std::any & operator[](const std::string &key)
Get the 'any' object identified by the given key.
Definition Attribute.h:61
void erase(const std::string &key)
Erase the data identified by the given key.
Definition Attribute.h:93
Drogon Test is a minimal effort test framework developed because the major C++ test frameworks doesn'...
Definition Attribute.h:23