TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Callback.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2008-2016 TrinityCore <http://www.trinitycore.org/>
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License as published by the
6  * Free Software Foundation; either version 2 of the License, or (at your
7  * option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12  * more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef _CALLBACK_H
19 #define _CALLBACK_H
20 
21 #include <future>
22 #include "QueryResult.h"
23 
24 typedef std::future<QueryResult> QueryResultFuture;
25 typedef std::promise<QueryResult> QueryResultPromise;
26 typedef std::future<PreparedQueryResult> PreparedQueryResultFuture;
27 typedef std::promise<PreparedQueryResult> PreparedQueryResultPromise;
28 
29 #define CALLBACK_STAGE_INVALID uint8(-1)
30 
31 template <typename Result, typename ParamType, bool chain = false>
33 {
34  public:
36 
38  void SetFutureResult(std::future<Result> value)
39  {
40  _result = std::move(value);
41  }
42 
43  std::future<Result>& GetFutureResult()
44  {
45  return _result;
46  }
47 
48  bool IsReady()
49  {
50  return _result.valid() && _result.wait_for(std::chrono::seconds(0)) == std::future_status::ready;
51  }
52 
53  void GetResult(Result& res)
54  {
55  res = _result.get();
56  }
57 
58  void FreeResult()
59  {
60  // Nothing to do here, the constructor of std::future will take care of the cleanup
61  }
62 
63  void SetParam(ParamType value)
64  {
65  _param = value;
66  }
67 
68  ParamType GetParam()
69  {
70  return _param;
71  }
72 
74  void ResetStage()
75  {
76  if (!chain)
77  return;
78 
79  _stage = 0;
80  }
81 
83  void NextStage()
84  {
85  if (!chain)
86  return;
87 
88  ++_stage;
89  }
90 
93  {
94  return _stage;
95  }
96 
98  void Reset()
99  {
100  SetParam(ParamType());
101  FreeResult();
102  ResetStage();
103  }
104 
105  private:
106  std::future<Result> _result;
107  ParamType _param;
109 
110  QueryCallback(QueryCallback const& right) = delete;
111  QueryCallback& operator=(QueryCallback const& right) = delete;
112 };
113 
114 template <typename Result, typename ParamType1, typename ParamType2, bool chain = false>
116 {
117  public:
119 
121  void SetFutureResult(std::future<Result> value)
122  {
123  _result = std::move(value);
124  }
125 
126  std::future<Result>& GetFutureResult()
127  {
128  return _result;
129  }
130 
131  bool IsReady()
132  {
133  return _result.valid() && _result.wait_for(std::chrono::seconds(0)) == std::future_status::ready;
134  }
135 
136  void GetResult(Result& res)
137  {
138  res = _result.get();
139  }
140 
141  void FreeResult()
142  {
143  // Nothing to do here, the constructor of std::future will take care of the cleanup
144  }
145 
146  void SetFirstParam(ParamType1 value)
147  {
148  _param_1 = value;
149  }
150 
151  void SetSecondParam(ParamType2 value)
152  {
153  _param_2 = value;
154  }
155 
156  ParamType1 GetFirstParam()
157  {
158  return _param_1;
159  }
160 
161  ParamType2 GetSecondParam()
162  {
163  return _param_2;
164  }
165 
167  void ResetStage()
168  {
169  if (!chain)
170  return;
171 
172  _stage = 0;
173  }
174 
176  void NextStage()
177  {
178  if (!chain)
179  return;
180 
181  ++_stage;
182  }
183 
186  {
187  return _stage;
188  }
189 
191  void Reset()
192  {
195  FreeResult();
196  ResetStage();
197  }
198 
199  private:
200  std::future<Result> _result;
201  ParamType1 _param_1;
202  ParamType2 _param_2;
204 
205  QueryCallback_2(QueryCallback_2 const& right) = delete;
206  QueryCallback_2& operator=(QueryCallback_2 const& right) = delete;
207 };
208 
209 #endif
QueryCallback()
Definition: Callback.h:35
uint8 GetStage()
Returns the callback stage (or CALLBACK_STAGE_INVALID if invalid)
Definition: Callback.h:185
std::future< QueryResult > QueryResultFuture
Definition: Callback.h:24
std::future< Result > _result
Definition: Callback.h:200
void SetParam(ParamType value)
Definition: Callback.h:63
void SetFirstParam(ParamType1 value)
Definition: Callback.h:146
bool IsReady()
Definition: Callback.h:131
QueryCallback_2()
Definition: Callback.h:118
ParamType1 GetFirstParam()
Definition: Callback.h:156
void GetResult(Result &res)
Definition: Callback.h:53
ParamType2 _param_2
Definition: Callback.h:202
void FreeResult()
Definition: Callback.h:141
QueryCallback_2 & operator=(QueryCallback_2 const &right)=delete
void NextStage()
Advances the callback chain to the next stage, so upper level code can act on its results accordingly...
Definition: Callback.h:176
arena_t NULL
Definition: jemalloc_internal.h:624
uint8 _stage
Definition: Callback.h:108
void ResetStage()
Resets the stage of the callback chain.
Definition: Callback.h:167
float seconds()
Definition: units.h:97
std::promise< QueryResult > QueryResultPromise
Definition: Callback.h:25
ParamType GetParam()
Definition: Callback.h:68
QueryCallback & operator=(QueryCallback const &right)=delete
Definition: Callback.h:32
void GetResult(Result &res)
Definition: Callback.h:136
ParamType1 _param_1
Definition: Callback.h:201
std::future< Result > & GetFutureResult()
Definition: Callback.h:43
void ResetStage()
Resets the stage of the callback chain.
Definition: Callback.h:74
void SetFutureResult(std::future< Result > value)
The parameter of this function should be a resultset returned from either .AsyncQuery or ...
Definition: Callback.h:121
std::promise< PreparedQueryResult > PreparedQueryResultPromise
Definition: Callback.h:27
void SetSecondParam(ParamType2 value)
Definition: Callback.h:151
uint8 _stage
Definition: Callback.h:203
ParamType _param
Definition: Callback.h:107
void NextStage()
Advances the callback chain to the next stage, so upper level code can act on its results accordingly...
Definition: Callback.h:83
uint8 GetStage()
Returns the callback stage (or CALLBACK_STAGE_INVALID if invalid)
Definition: Callback.h:92
Definition: Callback.h:115
std::future< PreparedQueryResult > PreparedQueryResultFuture
Definition: Callback.h:26
ParamType2 GetSecondParam()
Definition: Callback.h:161
void Reset()
Resets all underlying variables (param, result and stage)
Definition: Callback.h:98
void Reset()
Resets all underlying variables (param, result and stage)
Definition: Callback.h:191
uint8_t uint8
Definition: Define.h:152
const FieldDescriptor value
Definition: descriptor.h:1522
std::future< Result > & GetFutureResult()
Definition: Callback.h:126
void SetFutureResult(std::future< Result > value)
The parameter of this function should be a resultset returned from either .AsyncQuery or ...
Definition: Callback.h:38
#define CALLBACK_STAGE_INVALID
Definition: Callback.h:29
void FreeResult()
Definition: Callback.h:58
std::future< Result > _result
Definition: Callback.h:106
bool IsReady()
Definition: Callback.h:48