drogon
C++14/17-based HTTP application framework
Loading...
Searching...
No Matches
drogon::Session Class Reference

This class represents a session stored in the framework. One can get or set any type of data to a session object. More...

#include <drogon/Session.h>

Public Types

using SessionMap = std::map<std::string, std::any>

Public Member Functions

template<typename T>
get (const std::string &key) const
 Get the data identified by the key parameter.
template<typename T>
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.
template<typename T, typename Callable>
void modify (const std::string &key, Callable &&handler)
 Modify or visit the data identified by the key parameter.
template<typename Callable>
void modify (Callable &&handler)
 Modify or visit the session data.
void insert (const std::string &key, const std::any &obj)
 Insert a key-value pair.
void insert (const std::string &key, std::any &&obj)
 Insert a key-value pair.
void erase (const std::string &key)
 Erase the data identified by the given key.
bool find (const std::string &key)
 Return true if the data identified by the key exists.
void clear ()
 Clear all data in the session.
std::string sessionId () const
 Get the session ID of the current session.
void changeSessionIdToClient ()
 Let the framework create a new session ID for this session and set it to the client.

Friends

class SessionManager
class HttpAppFrameworkImpl

Detailed Description

This class represents a session stored in the framework. One can get or set any type of data to a session object.

Member Function Documentation

◆ changeSessionIdToClient()

void drogon::Session::changeSessionIdToClient ( )
inline

Let the framework create a new session ID for this session and set it to the client.

Note
This method does not change the session ID now.

◆ get()

template<typename T>
T drogon::Session::get ( const std::string & key) const
inline

Get the data identified by the key parameter.

Note
if the data is not found, a default value is returned. For example:
auto userName = sessionPtr->get<std::string>("user name");

◆ getOptional()

template<typename T>
std::optional< T > drogon::Session::getOptional ( const std::string & key) const
inline

Get the data identified by the key parameter and return an optional object that wraps the data.

Template Parameters
T
Parameters
key
Returns
optional<T>

◆ insert() [1/2]

void drogon::Session::insert ( const std::string & key,
const std::any & obj )
inline

Insert a key-value pair.

Note
here the any object can be created implicitly. for example
sessionPtr->insert("user name", userNameString);
If the key already exists, the element is not inserted.

◆ insert() [2/2]

void drogon::Session::insert ( const std::string & key,
std::any && obj )
inline

Insert a key-value pair.

Note
here the any object can be created implicitly. for example
sessionPtr->insert("user name", userNameString);
If the key already exists, the element is not inserted.

◆ modify() [1/2]

template<typename Callable>
void drogon::Session::modify ( Callable && handler)
inline

Modify or visit the session data.

Template Parameters
CallableThe signature of the callable should be equivalent to void (Session::SessionMap &) or void (const Session::SessionMap &)
Parameters
handlerA callable that can modify the sessionMap_ inside the session.
Note
This function is multiple-thread safe.

◆ modify() [2/2]

template<typename T, typename Callable>
void drogon::Session::modify ( const std::string & key,
Callable && handler )
inline

Modify or visit the data identified by the key parameter.

Template Parameters
Tthe type of the data.
Parameters
key
handlerA callable that can modify or visit the data. The signature of the handler should be equivalent to 'void(T&)' or 'void(const T&)'
Note
This function is multiple-thread safe. if the data identified by the key doesn't exist, a new one is created and passed to the handler. The changing of the data is protected by the mutex of the session.

The documentation for this class was generated from the following file: