17#include <trantor/utils/Logger.h>
34 using SessionMap = std::map<std::string, std::any>;
45 T
get(
const std::string &key)
const
48 std::lock_guard<std::mutex> lck(mutex_);
49 auto it = sessionMap_.find(key);
50 if (it != sessionMap_.end())
52 if (
typeid(T) == it->second.type())
54 return *(std::any_cast<T>(&(it->second)));
58 LOG_ERROR <<
"Bad type";
77 std::lock_guard<std::mutex> lck(mutex_);
78 auto it = sessionMap_.find(key);
79 if (it != sessionMap_.end())
81 if (
typeid(T) == it->second.type())
83 return *(std::any_cast<T>(&(it->second)));
87 LOG_ERROR <<
"Bad type";
107 template <
typename T,
typename Callable>
108 void modify(
const std::string &key, Callable &&handler)
110 std::lock_guard<std::mutex> lck(mutex_);
111 auto it = sessionMap_.find(key);
112 if (it != sessionMap_.end())
114 if (
typeid(T) == it->second.type())
116 handler(*(std::any_cast<T>(&(it->second))));
120 LOG_ERROR <<
"Bad type";
127 sessionMap_.insert(std::make_pair(key, std::any(std::move(item))));
140 template <
typename Callable>
143 std::lock_guard<std::mutex> lck(mutex_);
144 handler(sessionMap_);
155 void insert(
const std::string &key,
const std::any &obj)
157 std::lock_guard<std::mutex> lck(mutex_);
158 sessionMap_.insert(std::make_pair(key, obj));
169 void insert(
const std::string &key, std::any &&obj)
171 std::lock_guard<std::mutex> lck(mutex_);
172 sessionMap_.insert(std::make_pair(key, std::move(obj)));
180 std::lock_guard<std::mutex> lck(mutex_);
181 sessionMap_.erase(key);
187 bool find(
const std::string &key)
189 std::lock_guard<std::mutex> lck(mutex_);
190 if (sessionMap_.find(key) == sessionMap_.end())
202 std::lock_guard<std::mutex> lck(mutex_);
211 std::lock_guard<std::mutex> lck(mutex_);
222 needToChange_ =
true;
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;
240 Session(
const std::string &
id,
bool needToSet)
241 : sessionId_(id), needToSet_(needToSet)
257 bool needToChangeSessionId()
const
259 return needToChange_;
266 bool needSetToClient()
const
271 void setSessionId(
const std::string &
id)
273 std::lock_guard<std::mutex> lck(mutex_);
275 needToChange_ =
false;
279using SessionPtr = std::shared_ptr<Session>;
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