AzerothCore 3.3.5a
OpenSource WoW Emulator
Loading...
Searching...
No Matches
QueryCallback Class Reference

#include "QueryCallback.h"

Classes

struct  QueryCallbackData
 

Public Member Functions

 QueryCallback (QueryResultFuture &&result)
 
 QueryCallback (PreparedQueryResultFuture &&result)
 
 QueryCallback (QueryCallback &&right) noexcept
 
QueryCallbackoperator= (QueryCallback &&right) noexcept
 
 ~QueryCallback ()
 
QueryCallback && WithCallback (std::function< void(QueryResult)> &&callback)
 
QueryCallback && WithPreparedCallback (std::function< void(PreparedQueryResult)> &&callback)
 
QueryCallback && WithChainingCallback (std::function< void(QueryCallback &, QueryResult)> &&callback)
 
QueryCallback && WithChainingPreparedCallback (std::function< void(QueryCallback &, PreparedQueryResult)> &&callback)
 
void SetNextQuery (QueryCallback &&next)
 
bool InvokeIfReady ()
 

Private Member Functions

 QueryCallback (QueryCallback const &right)=delete
 
QueryCallbackoperator= (QueryCallback const &right)=delete
 

Private Attributes

union {
   QueryResultFuture   _string
 
   PreparedQueryResultFuture   _prepared
 
}; 
 
bool _isPrepared
 
std::queue< QueryCallbackData, std::list< QueryCallbackData > > _callbacks
 

Friends

template<typename T >
void ConstructActiveMember (T *obj)
 
template<typename T >
void DestroyActiveMember (T *obj)
 
template<typename T >
void MoveFrom (T *to, T &&from)
 

Detailed Description

Constructor & Destructor Documentation

◆ QueryCallback() [1/4]

QueryCallback::QueryCallback ( QueryResultFuture &&  result)
explicit
115{
116 _isPrepared = false;
117 Construct(_string, std::move(result));
118}
void Construct(T &t, Args &&... args)
Definition: QueryCallback.cpp:23
QueryResultFuture _string
Definition: QueryCallback.h:60
bool _isPrepared
Definition: QueryCallback.h:64

References _isPrepared, _string, and Construct().

◆ QueryCallback() [2/4]

QueryCallback::QueryCallback ( PreparedQueryResultFuture &&  result)
explicit
121{
122 _isPrepared = true;
123 Construct(_prepared, std::move(result));
124}
PreparedQueryResultFuture _prepared
Definition: QueryCallback.h:61

References _isPrepared, _prepared, and Construct().

◆ QueryCallback() [3/4]

QueryCallback::QueryCallback ( QueryCallback &&  right)
noexcept
127{
128 _isPrepared = right._isPrepared;
130 MoveFrom(this, std::move(right));
131 _callbacks = std::move(right._callbacks);
132}
std::queue< QueryCallbackData, std::list< QueryCallbackData > > _callbacks
Definition: QueryCallback.h:67
friend void MoveFrom(T *to, T &&from)
Definition: QueryCallback.cpp:53
friend void ConstructActiveMember(T *obj)
Definition: QueryCallback.cpp:35

References _callbacks, _isPrepared, ConstructActiveMember(), and MoveFrom().

◆ ~QueryCallback()

QueryCallback::~QueryCallback ( )
153{
155}
friend void DestroyActiveMember(T *obj)
Definition: QueryCallback.cpp:44

References DestroyActiveMember.

◆ QueryCallback() [4/4]

QueryCallback::QueryCallback ( QueryCallback const &  right)
privatedelete

Member Function Documentation

◆ InvokeIfReady()

bool QueryCallback::InvokeIfReady ( )
187{
188 QueryCallbackData& callback = _callbacks.front();
189 auto checkStateAndReturnCompletion = [this]()
190 {
191 _callbacks.pop();
192 bool hasNext = !_isPrepared ? _string.valid() : _prepared.valid();
193 if (_callbacks.empty())
194 {
195 ASSERT(!hasNext);
196 return true;
197 }
198
199 // abort chain
200 if (!hasNext)
201 return true;
202
203 ASSERT(_isPrepared == _callbacks.front()._isPrepared);
204 return false;
205 };
206
207 if (!_isPrepared)
208 {
209 if (_string.valid() && _string.wait_for(0s) == std::future_status::ready)
210 {
211 QueryResultFuture f(std::move(_string));
212 std::function<void(QueryCallback&, QueryResult)> cb(std::move(callback._string));
213 cb(*this, f.get());
214 return checkStateAndReturnCompletion();
215 }
216 }
217 else
218 {
219 if (_prepared.valid() && _prepared.wait_for(0s) == std::future_status::ready)
220 {
222 std::function<void(QueryCallback&, PreparedQueryResult)> cb(std::move(callback._prepared));
223 cb(*this, f.get());
224 return checkStateAndReturnCompletion();
225 }
226 }
227
228 return false;
229}
#define ASSERT
Definition: Errors.h:68
std::future< PreparedQueryResult > PreparedQueryResultFuture
Definition: DatabaseEnvFwd.h:46
std::future< QueryResult > QueryResultFuture
Definition: DatabaseEnvFwd.h:28
std::shared_ptr< ResultSet > QueryResult
Definition: DatabaseEnvFwd.h:27
std::shared_ptr< PreparedResultSet > PreparedQueryResult
Definition: DatabaseEnvFwd.h:45
Definition: QueryCallback.h:29

References _callbacks, _isPrepared, QueryCallback::QueryCallbackData::_prepared, _prepared, QueryCallback::QueryCallbackData::_string, _string, and ASSERT.

◆ operator=() [1/2]

QueryCallback & QueryCallback::operator= ( QueryCallback &&  right)
noexcept
135{
136 if (this != &right)
137 {
138 if (_isPrepared != right._isPrepared)
139 {
141 _isPrepared = right._isPrepared;
143 }
144
145 MoveFrom(this, std::move(right));
146 _callbacks = std::move(right._callbacks);
147 }
148
149 return *this;
150}

References _callbacks, _isPrepared, ConstructActiveMember(), DestroyActiveMember(), and MoveFrom().

◆ operator=() [2/2]

QueryCallback & QueryCallback::operator= ( QueryCallback const &  right)
privatedelete

◆ SetNextQuery()

void QueryCallback::SetNextQuery ( QueryCallback &&  next)
182{
183 MoveFrom(this, std::move(next));
184}

References MoveFrom.

Referenced by WorldSession::HandleCharCreateOpcode().

◆ WithCallback()

QueryCallback && QueryCallback::WithCallback ( std::function< void(QueryResult)> &&  callback)
158{
159 return WithChainingCallback([callback](QueryCallback& /*this*/, QueryResult result) { callback(std::move(result)); });
160}
QueryCallback && WithChainingCallback(std::function< void(QueryCallback &, QueryResult)> &&callback)
Definition: QueryCallback.cpp:167

References WithChainingCallback().

◆ WithChainingCallback()

QueryCallback && QueryCallback::WithChainingCallback ( std::function< void(QueryCallback &, QueryResult)> &&  callback)
168{
169 ASSERT(!_callbacks.empty() || !_isPrepared, "Attempted to set callback function for string query on a prepared async query");
170 _callbacks.emplace(std::move(callback));
171 return std::move(*this);
172}

References _callbacks, _isPrepared, and ASSERT.

Referenced by WithCallback().

◆ WithChainingPreparedCallback()

QueryCallback && QueryCallback::WithChainingPreparedCallback ( std::function< void(QueryCallback &, PreparedQueryResult)> &&  callback)
175{
176 ASSERT(!_callbacks.empty() || _isPrepared, "Attempted to set callback function for prepared query on a string async query");
177 _callbacks.emplace(std::move(callback));
178 return std::move(*this);
179}

References _callbacks, _isPrepared, and ASSERT.

Referenced by WithPreparedCallback().

◆ WithPreparedCallback()

QueryCallback && QueryCallback::WithPreparedCallback ( std::function< void(PreparedQueryResult)> &&  callback)
163{
164 return WithChainingPreparedCallback([callback](QueryCallback& /*this*/, PreparedQueryResult result) { callback(std::move(result)); });
165}
QueryCallback && WithChainingPreparedCallback(std::function< void(QueryCallback &, PreparedQueryResult)> &&callback)
Definition: QueryCallback.cpp:174

References WithChainingPreparedCallback().

Friends And Related Function Documentation

◆ ConstructActiveMember

template<typename T >
void ConstructActiveMember ( T *  obj)
friend
36{
37 if (!obj->_isPrepared)
38 Construct(obj->_string);
39 else
40 Construct(obj->_prepared);
41}

◆ DestroyActiveMember

template<typename T >
void DestroyActiveMember ( T *  obj)
friend
45{
46 if (!obj->_isPrepared)
47 Destroy(obj->_string);
48 else
49 Destroy(obj->_prepared);
50}

Referenced by ~QueryCallback().

◆ MoveFrom

template<typename T >
void MoveFrom ( T *  to,
T &&  from 
)
friend
54{
55 ASSERT(to->_isPrepared == from._isPrepared);
56
57 if (!to->_isPrepared)
58 to->_string = std::move(from._string);
59 else
60 to->_prepared = std::move(from._prepared);
61}

Referenced by SetNextQuery().

Member Data Documentation

◆ 

union { ... } QueryCallback::@5

◆ _callbacks

std::queue<QueryCallbackData, std::list<QueryCallbackData> > QueryCallback::_callbacks
private

◆ _isPrepared

bool QueryCallback::_isPrepared
private

◆ _prepared

PreparedQueryResultFuture QueryCallback::_prepared

Referenced by InvokeIfReady(), and QueryCallback().

◆ _string

QueryResultFuture QueryCallback::_string

Referenced by InvokeIfReady(), and QueryCallback().