2017-01-11 23:45:03 +01:00
|
|
|
/*
|
|
|
|
|
* Copyright (C) 2008-2017 TrinityCore <http://www.trinitycore.org/>
|
|
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
|
* under the terms of the GNU General Public License as published by the
|
|
|
|
|
* Free Software Foundation; either version 2 of the License, or (at your
|
|
|
|
|
* option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
|
|
|
* more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "QueryCallback.h"
|
|
|
|
|
|
2017-01-12 19:40:33 +01:00
|
|
|
template<typename T>
|
|
|
|
|
inline void Destroy(T& t)
|
|
|
|
|
{
|
|
|
|
|
t.~T();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
|
void DestroyActiveMember(T& obj)
|
|
|
|
|
{
|
|
|
|
|
if (!obj._isPrepared)
|
|
|
|
|
Destroy(obj._string);
|
|
|
|
|
else
|
|
|
|
|
Destroy(obj._prepared);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
|
void MoveFrom(T& to, T&& from)
|
|
|
|
|
{
|
|
|
|
|
DestroyActiveMember(to);
|
|
|
|
|
to._isPrepared = from._isPrepared;
|
|
|
|
|
if (!to._isPrepared)
|
|
|
|
|
to._string = std::move(from._string);
|
|
|
|
|
else
|
|
|
|
|
to._prepared = std::move(from._prepared);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct QueryCallbackNew::QueryCallbackData
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
friend class QueryCallbackNew;
|
|
|
|
|
|
|
|
|
|
QueryCallbackData(std::function<void(QueryResult)>&& callback) : _string(std::move(callback)), _isPrepared(false) { }
|
|
|
|
|
QueryCallbackData(std::function<void(PreparedQueryResult)>&& callback) : _prepared(std::move(callback)), _isPrepared(true) { }
|
|
|
|
|
QueryCallbackData(QueryCallbackData&& right) { MoveFrom(*this, std::move(right)); }
|
2017-01-12 20:00:02 +01:00
|
|
|
QueryCallbackData& operator=(QueryCallbackData&& right) { MoveFrom(*this, std::move(right)); return *this; }
|
2017-01-12 19:40:33 +01:00
|
|
|
~QueryCallbackData() { DestroyActiveMember(*this); }
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
QueryCallbackData(QueryCallbackData const&) = delete;
|
|
|
|
|
QueryCallbackData& operator=(QueryCallbackData const&) = delete;
|
|
|
|
|
|
|
|
|
|
template<typename T> friend void MoveFrom(T& to, T&& from);
|
|
|
|
|
template<typename T> friend void DestroyActiveMember(T& obj);
|
|
|
|
|
|
|
|
|
|
union
|
|
|
|
|
{
|
|
|
|
|
std::function<void(QueryResult)> _string;
|
|
|
|
|
std::function<void(PreparedQueryResult)> _prepared;
|
|
|
|
|
};
|
|
|
|
|
bool _isPrepared;
|
|
|
|
|
};
|
|
|
|
|
|
2017-01-11 23:45:03 +01:00
|
|
|
QueryCallbackNew::QueryCallbackNew(std::future<QueryResult>&& result) : _string(std::move(result)), _isPrepared(false)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QueryCallbackNew::QueryCallbackNew(std::future<PreparedQueryResult>&& result) : _prepared(std::move(result)), _isPrepared(true)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QueryCallbackNew::QueryCallbackNew(QueryCallbackNew&& right)
|
|
|
|
|
{
|
2017-01-12 19:40:33 +01:00
|
|
|
MoveFrom(*this, std::move(right));
|
|
|
|
|
_callbacks = std::move(right._callbacks);
|
2017-01-11 23:45:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QueryCallbackNew& QueryCallbackNew::operator=(QueryCallbackNew&& right)
|
|
|
|
|
{
|
2017-01-12 19:40:33 +01:00
|
|
|
MoveFrom(*this, std::move(right));
|
|
|
|
|
_callbacks = std::move(right._callbacks);
|
2017-01-11 23:45:03 +01:00
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QueryCallbackNew::~QueryCallbackNew()
|
|
|
|
|
{
|
2017-01-12 19:40:33 +01:00
|
|
|
DestroyActiveMember(*this);
|
2017-01-11 23:45:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QueryCallbackNew&& QueryCallbackNew::WithCallback(std::function<void(QueryResult)>&& callback)
|
|
|
|
|
{
|
2017-01-12 19:40:33 +01:00
|
|
|
ASSERT(!_callbacks.empty() || !_isPrepared, "Attempted to set callback function for string query on a prepared async query");
|
|
|
|
|
_callbacks.emplace(std::move(callback));
|
2017-01-11 23:45:03 +01:00
|
|
|
return std::move(*this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QueryCallbackNew&& QueryCallbackNew::WithPreparedCallback(std::function<void(PreparedQueryResult)>&& callback)
|
|
|
|
|
{
|
2017-01-12 19:40:33 +01:00
|
|
|
ASSERT(!_callbacks.empty() || _isPrepared, "Attempted to set callback function for prepared query on a string async query");
|
|
|
|
|
_callbacks.emplace(std::move(callback));
|
2017-01-11 23:45:03 +01:00
|
|
|
return std::move(*this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QueryCallbackNew::Status QueryCallbackNew::InvokeIfReady()
|
|
|
|
|
{
|
2017-01-12 19:40:33 +01:00
|
|
|
QueryCallbackData& callback = _callbacks.front();
|
2017-01-11 23:45:03 +01:00
|
|
|
if (!_isPrepared)
|
|
|
|
|
{
|
2017-01-12 19:40:33 +01:00
|
|
|
if (_string.valid() && _string.wait_for(std::chrono::seconds(0)) == std::future_status::ready)
|
2017-01-11 23:45:03 +01:00
|
|
|
{
|
2017-01-12 19:40:33 +01:00
|
|
|
std::function<void(QueryResult)> cb(std::move(callback._string));
|
|
|
|
|
cb(_string.get());
|
|
|
|
|
_callbacks.pop();
|
|
|
|
|
if (_callbacks.empty())
|
|
|
|
|
{
|
|
|
|
|
ASSERT(!_isPrepared ? !_string.valid() : !_prepared.valid());
|
|
|
|
|
return Completed;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ASSERT(_isPrepared == _callbacks.front()._isPrepared);
|
|
|
|
|
return NextStep;
|
2017-01-11 23:45:03 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2017-01-12 19:40:33 +01:00
|
|
|
if (_prepared.valid() && _prepared.wait_for(std::chrono::seconds(0)) == std::future_status::ready)
|
2017-01-11 23:45:03 +01:00
|
|
|
{
|
2017-01-12 19:40:33 +01:00
|
|
|
std::future<PreparedQueryResult> f(std::move(_prepared));
|
|
|
|
|
std::function<void(PreparedQueryResult)> cb(std::move(callback._prepared));
|
|
|
|
|
cb(_prepared.get());
|
|
|
|
|
_callbacks.pop();
|
|
|
|
|
if (_callbacks.empty())
|
|
|
|
|
{
|
|
|
|
|
ASSERT(!_isPrepared ? !_string.valid() : !_prepared.valid());
|
|
|
|
|
return Completed;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ASSERT(_isPrepared == _callbacks.front()._isPrepared);
|
|
|
|
|
return NextStep;
|
2017-01-11 23:45:03 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return NotReady;
|
|
|
|
|
}
|