Github   Telegram
Loading...
Searching...
No Matches
Public Member Functions | List of all members
server::request::RequestContext Class Referencefinal

Stores request-specific data during request processing. More...

#include <userver/server/request/request_context.hpp>

Public Member Functions

 RequestContext (RequestContext &&)=delete
 
 RequestContext (const RequestContext &)=delete
 
template<typename Data >
Data & SetUserData (Data data)
 Stores user data if it was not previously stored in this. More...
 
template<typename Data , typename... Args>
Data & EmplaceUserData (Args &&... args)
 Emplaces user data if it was not previously stored in this. More...
 
template<typename Data >
Data & GetUserData ()
 
template<typename Data >
const Data & GetUserData () const
 
template<typename Data >
std::remove_reference_t< Data > * GetUserDataOptional ()
 
template<typename Data >
const std::remove_reference_t< Data > * GetUserDataOptional () const
 
void EraseUserData ()
 Erases the user data. More...
 
template<typename Data >
Data & SetData (std::string name, Data data)
 Stores the data with specified name if it was not previously stored in this. More...
 
template<typename Data , typename... Args>
Data & EmplaceData (std::string name, Args &&... args)
 Emplaces the data with specified name if it was not previously stored in this. More...
 
template<typename Data >
Data & GetData (const std::string &name)
 
template<typename Data >
const Data & GetData (const std::string &name) const
 
template<typename Data >
std::remove_reference_t< Data > * GetDataOptional (const std::string &name)
 
template<typename Data >
const std::remove_reference_t< Data > * GetDataOptional (const std::string &name) const
 
void EraseData (const std::string &name)
 Erase data with specified name. More...
 

Detailed Description

Stores request-specific data during request processing.

For example: you can store some data in HandleRequestThrow() method and access this data in GetResponseDataForLogging() method.

Examples
samples/config_service/config_service.cpp, samples/flatbuf_service/flatbuf_service.cpp, samples/grpc_service/grpc_service.cpp, samples/hello_service/hello_service.cpp, samples/http_caching/http_caching.cpp, samples/mongo_service/mongo_service.cpp, samples/postgres_service/postgres_service.cpp, samples/redis_service/redis_service.cpp, samples/testsuite-support/src/logcapture.cpp, samples/testsuite-support/src/metrics.cpp, samples/testsuite-support/src/metrics.hpp, samples/testsuite-support/src/now.cpp, and samples/testsuite-support/src/testpoint.cpp.

Definition at line 20 of file request_context.hpp.

Member Function Documentation

◆ EmplaceData()

template<typename Data , typename... Args>
Data & server::request::RequestContext::EmplaceData ( std::string  name,
Args &&...  args 
)

Emplaces the data with specified name if it was not previously stored in this.

Exceptions
std::runtime_errorif data with such name was already stored.

Definition at line 166 of file request_context.hpp.

◆ EmplaceUserData()

template<typename Data , typename... Args>
Data & server::request::RequestContext::EmplaceUserData ( Args &&...  args)

Emplaces user data if it was not previously stored in this.

Exceptions
std::runtime_errorif user data was already stored.

Definition at line 129 of file request_context.hpp.

◆ EraseData()

void server::request::RequestContext::EraseData ( const std::string &  name)
inline

Erase data with specified name.

Definition at line 196 of file request_context.hpp.

◆ EraseUserData()

void server::request::RequestContext::EraseUserData ( )
inline

Erases the user data.

Definition at line 158 of file request_context.hpp.

◆ GetData() [1/2]

template<typename Data >
Data & server::request::RequestContext::GetData ( const std::string &  name)
Returns
Stored data with specified name.
Exceptions
std::runtime_errorif no data was stored
std::bad_any_castif data of different type was stored

Definition at line 172 of file request_context.hpp.

◆ GetData() [2/2]

template<typename Data >
const Data & server::request::RequestContext::GetData ( const std::string &  name) const
Returns
Stored data with specified name.
Exceptions
std::runtime_errorif no data was stored
std::bad_any_castif data of different type was stored

Definition at line 177 of file request_context.hpp.

◆ GetDataOptional() [1/2]

template<typename Data >
std::remove_reference_t< Data > * server::request::RequestContext::GetDataOptional ( const std::string &  name)
Returns
Stored data with specified name or nullptr if no data found.
Exceptions
std::bad_any_castif data of different type was stored.

Definition at line 183 of file request_context.hpp.

◆ GetDataOptional() [2/2]

template<typename Data >
const std::remove_reference_t< Data > * server::request::RequestContext::GetDataOptional ( const std::string &  name) const
Returns
Stored data with specified name or nullptr if no data found.
Exceptions
std::bad_any_castif data of different type was stored.

Definition at line 190 of file request_context.hpp.

◆ GetUserData() [1/2]

template<typename Data >
Data & server::request::RequestContext::GetUserData
Returns
Stored user data
Exceptions
std::runtime_errorif no data was stored
std::bad_any_castif data of different type was stored

Definition at line 135 of file request_context.hpp.

◆ GetUserData() [2/2]

template<typename Data >
const Data & server::request::RequestContext::GetUserData
Returns
Stored user data
Exceptions
std::runtime_errorif no data was stored
std::bad_any_castif data of different type was stored

Definition at line 140 of file request_context.hpp.

◆ GetUserDataOptional() [1/2]

template<typename Data >
std::remove_reference_t< Data > * server::request::RequestContext::GetUserDataOptional
Returns
A pointer to data of type Data if it was stored before during current request processing or nullptr otherwise

Definition at line 146 of file request_context.hpp.

◆ GetUserDataOptional() [2/2]

template<typename Data >
const std::remove_reference_t< Data > * server::request::RequestContext::GetUserDataOptional
Returns
A pointer to data of type Data if it was stored before during current request processing or nullptr otherwise

Definition at line 152 of file request_context.hpp.

◆ SetData()

template<typename Data >
Data & server::request::RequestContext::SetData ( std::string  name,
Data  data 
)

Stores the data with specified name if it was not previously stored in this.

Exceptions
std::runtime_errorif data with such name was already stored.

Definition at line 161 of file request_context.hpp.

◆ SetUserData()

template<typename Data >
Data & server::request::RequestContext::SetUserData ( Data  data)

Stores user data if it was not previously stored in this.

Exceptions
std::runtime_errorif user data was already stored.

Definition at line 124 of file request_context.hpp.


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