Function modify

Summary

#include <lib/inc/drogon/Session.h>

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

(2) template <typename Callable>
    void modify(Callable &&handler)

Function overload

Synopsis

#include <lib/inc/drogon/Session.h>

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

Description

Modify or visit the data identified by the key parameter.

Template Parameters

T - the type of the data.

Parameters

key -

handler - A 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.

Source

Lines 105-127 in lib/inc/drogon/Session.h.

template <typename T, typename Callable>
void modify(const std::string &key, Callable &&handler)
{
    std::lock_guard<std::mutex> lck(mutex_);
    auto it = sessionMap_.find(key);
    if (it != sessionMap_.end())
    {
        if (typeid(T) == it->second.type())
        {
            handler(*(any_cast<T>(&(it->second))));
        }
        else
        {
            LOG_ERROR << "Bad type";
        }
    }
    else
    {
        auto item = T();
        handler(item);
        sessionMap_.insert(std::make_pair(key, any(std::move(item))));
    }
}

Synopsis

#include <lib/inc/drogon/Session.h>

template <typename Callable>
void modify(Callable &&handler)

Description

Modify or visit the session data.

Template Parameters

Callable - The signature of the callable should be equivalent to void (Session::SessionMap &) or void (const Session::SessionMap &)

Parameters

handler - A callable that can modify the sessionMap_ inside the session.

Note
This function is multiple-thread safe.

Source

Lines 137-142 in lib/inc/drogon/Session.h.

template <typename Callable>
void modify(Callable &&handler)
{
    std::lock_guard<std::mutex> lck(mutex_);
    handler(sessionMap_);
}





Add Discussion as Guest

Log in