Files
TCPlayerbotCore/src/server/shared/Threading/Callback.h
T

210 lines
5.4 KiB
C++
Raw Normal View History

2010-10-07 15:54:07 +02:00
/*
2015-01-01 00:27:46 +01:00
* Copyright (C) 2008-2015 TrinityCore <http://www.trinitycore.org/>
2010-10-07 15:54:07 +02:00
*
* 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/>.
2008-10-02 16:23:55 -05:00
*/
2009-10-17 15:51:44 -07:00
2010-08-18 19:48:51 +02:00
#ifndef _CALLBACK_H
#define _CALLBACK_H
2009-10-17 15:51:44 -07:00
#include <future>
2010-09-03 00:04:14 +02:00
#include "QueryResult.h"
2009-10-17 15:51:44 -07:00
typedef std::future<QueryResult> QueryResultFuture;
typedef std::promise<QueryResult> QueryResultPromise;
typedef std::future<PreparedQueryResult> PreparedQueryResultFuture;
typedef std::promise<PreparedQueryResult> PreparedQueryResultPromise;
2009-10-17 15:51:44 -07:00
#define CALLBACK_STAGE_INVALID uint8(-1)
template <typename Result, typename ParamType, bool chain = false>
2010-08-18 19:48:51 +02:00
class QueryCallback
2008-10-02 16:23:55 -05:00
{
2010-08-18 19:48:51 +02:00
public:
QueryCallback() : _param(), _stage(chain ? 0 : CALLBACK_STAGE_INVALID) { }
2010-09-12 01:40:27 +02:00
//! The parameter of this function should be a resultset returned from either .AsyncQuery or .AsyncPQuery
2014-07-09 12:24:20 -05:00
void SetFutureResult(std::future<Result> value)
2010-08-18 19:48:51 +02:00
{
_result = std::move(value);
2010-08-18 19:48:51 +02:00
}
2010-09-12 01:40:27 +02:00
std::future<Result>& GetFutureResult()
2010-08-18 19:48:51 +02:00
{
return _result;
2010-08-18 19:48:51 +02:00
}
2010-09-12 01:40:27 +02:00
bool IsReady()
2010-08-18 19:48:51 +02:00
{
2014-07-02 21:56:40 -05:00
return _result.valid() && _result.wait_for(std::chrono::seconds(0)) == std::future_status::ready;
2010-08-18 19:48:51 +02:00
}
2010-09-12 01:40:27 +02:00
void GetResult(Result& res)
2010-08-18 19:48:51 +02:00
{
res = _result.get();
2010-08-18 19:48:51 +02:00
}
2010-09-12 01:40:27 +02:00
2010-08-18 19:48:51 +02:00
void FreeResult()
{
// Nothing to do here, the constructor of std::future will take care of the cleanup
2010-08-18 19:48:51 +02:00
}
2010-09-12 01:40:27 +02:00
2010-08-18 19:48:51 +02:00
void SetParam(ParamType value)
{
_param = value;
2010-08-18 19:48:51 +02:00
}
2010-09-12 01:40:27 +02:00
2010-08-18 19:48:51 +02:00
ParamType GetParam()
{
return _param;
}
//! Resets the stage of the callback chain
void ResetStage()
{
if (!chain)
return;
_stage = 0;
}
//! Advances the callback chain to the next stage, so upper level code can act on its results accordingly
void NextStage()
{
if (!chain)
return;
++_stage;
}
//! Returns the callback stage (or CALLBACK_STAGE_INVALID if invalid)
uint8 GetStage()
{
return _stage;
}
//! Resets all underlying variables (param, result and stage)
void Reset()
{
SetParam(NULL);
FreeResult();
ResetStage();
2010-08-18 19:48:51 +02:00
}
2010-09-12 01:40:27 +02:00
2010-08-18 19:48:51 +02:00
private:
std::future<Result> _result;
ParamType _param;
uint8 _stage;
2014-04-20 16:48:35 +02:00
QueryCallback(QueryCallback const& right) = delete;
QueryCallback& operator=(QueryCallback const& right) = delete;
2010-08-18 19:48:51 +02:00
};
template <typename Result, typename ParamType1, typename ParamType2, bool chain = false>
2010-08-24 19:04:25 +02:00
class QueryCallback_2
{
public:
QueryCallback_2() : _stage(chain ? 0 : CALLBACK_STAGE_INVALID) { }
2010-09-12 01:40:27 +02:00
//! The parameter of this function should be a resultset returned from either .AsyncQuery or .AsyncPQuery
2014-07-09 12:24:20 -05:00
void SetFutureResult(std::future<Result> value)
2010-08-24 19:04:25 +02:00
{
_result = std::move(value);
2010-08-24 19:04:25 +02:00
}
2010-09-12 01:40:27 +02:00
std::future<Result>& GetFutureResult()
2010-08-24 19:04:25 +02:00
{
return _result;
2010-08-24 19:04:25 +02:00
}
2010-09-12 01:40:27 +02:00
bool IsReady()
2010-08-24 19:04:25 +02:00
{
2014-07-02 21:56:40 -05:00
return _result.valid() && _result.wait_for(std::chrono::seconds(0)) == std::future_status::ready;
2010-08-24 19:04:25 +02:00
}
2010-09-12 01:40:27 +02:00
void GetResult(Result& res)
2010-08-24 19:04:25 +02:00
{
res = _result.get();
2010-08-24 19:04:25 +02:00
}
2010-09-12 01:40:27 +02:00
2010-08-24 19:04:25 +02:00
void FreeResult()
{
// Nothing to do here, the constructor of std::future will take care of the cleanup
2010-08-24 19:04:25 +02:00
}
2010-09-12 01:40:27 +02:00
2010-08-24 19:04:25 +02:00
void SetFirstParam(ParamType1 value)
{
_param_1 = value;
2010-08-24 19:04:25 +02:00
}
void SetSecondParam(ParamType2 value)
{
_param_2 = value;
2010-08-24 19:04:25 +02:00
}
2010-09-12 01:40:27 +02:00
2010-08-24 19:04:25 +02:00
ParamType1 GetFirstParam()
{
return _param_1;
2010-08-24 19:04:25 +02:00
}
ParamType2 GetSecondParam()
{
return _param_2;
}
//! Resets the stage of the callback chain
void ResetStage()
{
if (!chain)
return;
_stage = 0;
}
//! Advances the callback chain to the next stage, so upper level code can act on its results accordingly
void NextStage()
{
if (!chain)
return;
++_stage;
}
//! Returns the callback stage (or CALLBACK_STAGE_INVALID if invalid)
uint8 GetStage()
{
return _stage;
}
//! Resets all underlying variables (param, result and stage)
void Reset()
{
2011-12-21 07:18:47 -05:00
SetFirstParam(NULL);
SetSecondParam(NULL);
FreeResult();
ResetStage();
2010-08-24 19:04:25 +02:00
}
2010-09-12 01:40:27 +02:00
2010-08-24 19:04:25 +02:00
private:
std::future<Result> _result;
ParamType1 _param_1;
ParamType2 _param_2;
uint8 _stage;
2014-04-20 16:48:35 +02:00
QueryCallback_2(QueryCallback_2 const& right) = delete;
QueryCallback_2& operator=(QueryCallback_2 const& right) = delete;
2010-08-24 19:04:25 +02:00
};
2014-07-01 00:54:09 +02:00
#endif