drogon
C++14/17-based HTTP application framework
Loading...
Searching...
No Matches
Session.h
Go to the documentation of this file.
1
14
15#pragma once
16
17#include <trantor/utils/Logger.h>
18#include <map>
19#include <memory>
20#include <mutex>
21#include <thread>
22#include <optional>
23#include <any>
24
25namespace drogon
26{
31class Session
32{
33 public:
34 using SessionMap = std::map<std::string, std::any>;
35
44 template <typename T>
45 T get(const std::string &key) const
46 {
47 {
48 std::lock_guard<std::mutex> lck(mutex_);
49 auto it = sessionMap_.find(key);
50 if (it != sessionMap_.end())
51 {
52 if (typeid(T) == it->second.type())
53 {
54 return *(std::any_cast<T>(&(it->second)));
55 }
56 else
57 {
58 LOG_ERROR << "Bad type";
59 }
60 }
61 }
62 return T();
63 }
64
73 template <typename T>
74 std::optional<T> getOptional(const std::string &key) const
75 {
76 {
77 std::lock_guard<std::mutex> lck(mutex_);
78 auto it = sessionMap_.find(key);
79 if (it != sessionMap_.end())
80 {
81 if (typeid(T) == it->second.type())
82 {
83 return *(std::any_cast<T>(&(it->second)));
84 }
85 else
86 {
87 LOG_ERROR << "Bad type";
88 }
89 }
90 }
91 return std::nullopt;
92 }
93
107 template <typename T, typename Callable>
108 void modify(const std::string &key, Callable &&handler)
109 {
110 std::lock_guard<std::mutex> lck(mutex_);
111 auto it = sessionMap_.find(key);
112 if (it != sessionMap_.end())
113 {
114 if (typeid(T) == it->second.type())
115 {
116 handler(*(std::any_cast<T>(&(it->second))));
117 }
118 else
119 {
120 LOG_ERROR << "Bad type";
121 }
122 }
123 else
124 {
125 auto item = T();
126 handler(item);
127 sessionMap_.insert(std::make_pair(key, std::any(std::move(item))));
128 }
129 }
130
140 template <typename Callable>
141 void modify(Callable &&handler)
142 {
143 std::lock_guard<std::mutex> lck(mutex_);
144 handler(sessionMap_);
145 }
146
155 void insert(const std::string &key, const std::any &obj)
156 {
157 std::lock_guard<std::mutex> lck(mutex_);
158 sessionMap_.insert(std::make_pair(key, obj));
159 }
160
169 void insert(const std::string &key, std::any &&obj)
170 {
171 std::lock_guard<std::mutex> lck(mutex_);
172 sessionMap_.insert(std::make_pair(key, std::move(obj)));
173 }
174
178 void erase(const std::string &key)
179 {
180 std::lock_guard<std::mutex> lck(mutex_);
181 sessionMap_.erase(key);
182 }
183
187 bool find(const std::string &key)
188 {
189 std::lock_guard<std::mutex> lck(mutex_);
190 if (sessionMap_.find(key) == sessionMap_.end())
191 {
192 return false;
193 }
194 return true;
195 }
196
200 void clear()
201 {
202 std::lock_guard<std::mutex> lck(mutex_);
203 sessionMap_.clear();
204 }
205
209 std::string sessionId() const
210 {
211 std::lock_guard<std::mutex> lck(mutex_);
212 return sessionId_;
213 }
214
221 {
222 needToChange_ = true;
223 needToSet_ = true;
224 }
225
226 Session() = delete;
227
228 private:
229 SessionMap sessionMap_;
230 mutable std::mutex mutex_;
231 std::string sessionId_;
232 bool needToSet_{false};
233 bool needToChange_{false};
234 friend class SessionManager;
235 friend class HttpAppFrameworkImpl;
236
240 Session(const std::string &id, bool needToSet)
241 : sessionId_(id), needToSet_(needToSet)
242 {
243 }
244
248 void hasSet()
249 {
250 needToSet_ = false;
251 }
252
257 bool needToChangeSessionId() const
258 {
259 return needToChange_;
260 }
261
266 bool needSetToClient() const
267 {
268 return needToSet_;
269 }
270
271 void setSessionId(const std::string &id)
272 {
273 std::lock_guard<std::mutex> lck(mutex_);
274 sessionId_ = id;
275 needToChange_ = false;
276 }
277};
278
279using SessionPtr = std::shared_ptr<Session>;
280
281} // namespace drogon
This class represents a session stored in the framework. One can get or set any type of data to a ses...
Definition Session.h:32
void clear()
Clear all data in the session.
Definition Session.h:200
std::optional< T > getOptional(const std::string &key) const
Get the data identified by the key parameter and return an optional object that wraps the data.
Definition Session.h:74
void modify(const std::string &key, Callable &&handler)
Modify or visit the data identified by the key parameter.
Definition Session.h:108
std::string sessionId() const
Get the session ID of the current session.
Definition Session.h:209
void insert(const std::string &key, const std::any &obj)
Insert a key-value pair.
Definition Session.h:155
void changeSessionIdToClient()
Let the framework create a new session ID for this session and set it to the client.
Definition Session.h:220
T get(const std::string &key) const
Get the data identified by the key parameter.
Definition Session.h:45
void insert(const std::string &key, std::any &&obj)
Insert a key-value pair.
Definition Session.h:169
bool find(const std::string &key)
Return true if the data identified by the key exists.
Definition Session.h:187
void modify(Callable &&handler)
Modify or visit the session data.
Definition Session.h:141
void erase(const std::string &key)
Erase the data identified by the given key.
Definition Session.h:178
Drogon Test is a minimal effort test framework developed because the major C++ test frameworks doesn'...
Definition Attribute.h:23