18#include <trantor/utils/NonCopyable.h>
60class IOThreadStorage :
public trantor::NonCopyable
64 using InitCallback = std::function<void(ValueType &,
size_t)>;
66 template <
typename... Args>
67 IOThreadStorage(Args &&...args)
69 static_assert(std::is_constructible<C, Args &&...>::value,
70 "Unable to construct storage with given signature");
72 assert(numThreads > 0 &&
73 numThreads != (std::numeric_limits<size_t>::max)());
76 storage_.reserve(numThreads + 1);
78 for (
size_t i = 0; i <= numThreads; ++i)
80 storage_.emplace_back(std::forward<Args>(args)...);
84 void init(
const InitCallback &initCB)
86 for (
size_t i = 0; i < storage_.size(); ++i)
88 initCB(storage_[i], i);
100 assert(idx < storage_.size());
101 return storage_[idx];
107 assert(idx < storage_.size());
108 return storage_[idx];
119 assert(idx < storage_.size());
120 storage_[idx] = newData;
126 assert(idx < storage_.size());
127 storage_[idx] = std::move(newData);
130 inline ValueType *operator->()
133 assert(idx < storage_.size());
134 return &storage_[idx];
137 inline ValueType &operator*()
142 inline const ValueType *operator->()
const
145 assert(idx < storage_.size());
146 return &storage_[idx];
149 inline const ValueType &operator*()
const
155 std::vector<ValueType> storage_;
158inline trantor::EventLoop *getIOThreadStorageLoop(
size_t index)
noexcept(
false)
162 throw std::out_of_range(
"Event loop index is out of range");
virtual trantor::EventLoop * getIOLoop(size_t id) const =0
Get an IO loop with id. E.g. 0 <= id < #Total thread-loops.
virtual size_t getThreadNum() const =0
Get the number of threads for IO event loops.
virtual size_t getCurrentThreadIndex() const =0
Get the Current Thread Index whose range is [0, the total number of IO threads].
virtual trantor::EventLoop * getLoop() const =0
Get the main event loop of the framework;.
void setThreadData(const ValueType &newData)
Sets the thread data for the current thread.
Definition IOThreadStorage.h:116
ValueType & getThreadData()
Get the thread storage associate with the current thread.
Definition IOThreadStorage.h:97
Drogon Test is a minimal effort test framework developed because the major C++ test frameworks doesn'...
Definition Attribute.h:23
HttpAppFramework & app()
A wrapper of the instance() method.
Definition HttpAppFramework.h:1656