drogon
C++14/17-based HTTP application framework
Loading...
Searching...
No Matches
drogon::IOThreadStorage< C > Class Template Reference

Utility class for thread storage handling. More...

#include <drogon/IOThreadStorage.h>

Inheritance diagram for drogon::IOThreadStorage< C >:
Collaboration diagram for drogon::IOThreadStorage< C >:

Public Types

using ValueType = C
using InitCallback = std::function<void(ValueType &, size_t)>

Public Member Functions

template<typename... Args>
 IOThreadStorage (Args &&...args)
void init (const InitCallback &initCB)
ValueType & getThreadData ()
 Get the thread storage associate with the current thread.
const ValueType & getThreadData () const
void setThreadData (const ValueType &newData)
 Sets the thread data for the current thread.
void setThreadData (ValueType &&newData)
ValueType * operator-> ()
ValueType & operator* ()
const ValueType * operator-> () const
const ValueType & operator* () const

Detailed Description

template<typename C>
class drogon::IOThreadStorage< C >

Utility class for thread storage handling.

Thread storage allows the efficient handling of reusable data without thread synchronisation. For instance, such a thread storage would be useful to store database connections.

Example usage:

struct MyThreadData {
int threadLocal = 42;
std::string something = "foo";
};
class MyController : public HttpController<MyController> {
public:
METHOD_LIST_BEGIN
ADD_METHOD_TO(MyController::endpoint, "/some/path", Get);
METHOD_LIST_END
void login(const HttpRequestPtr &req,
std::function<void (const HttpResponsePtr &)> &&callback) {
assert(storage_->threadLocal == 42);
// handle the request
}
private:
IOThreadStorage<MyThreadData> storage_;
};
The reflection base class template for HTTP controllers.
Definition HttpController.h:60

Member Function Documentation

◆ getThreadData()

template<typename C>
ValueType & drogon::IOThreadStorage< C >::getThreadData ( )
inline

Get the thread storage associate with the current thread.

This function may only be called in a request handler

◆ setThreadData()

template<typename C>
void drogon::IOThreadStorage< C >::setThreadData ( const ValueType & newData)
inline

Sets the thread data for the current thread.

This function may only be called in a request handler


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