Skip to content

Commit a2a3c65

Browse files
committed
Core/Database: Add Empty and CancelAll functions to AsyncCallbackProcessor and enable callback concept check
(cherry picked from commit b4f7948)
1 parent 2bdc0d6 commit a2a3c65

File tree

6 files changed

+58
-17
lines changed

6 files changed

+58
-17
lines changed

src/common/Utilities/AsyncCallbackProcessor.h

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,13 @@
1515
* with this program. If not, see <http://www.gnu.org/licenses/>.
1616
*/
1717

18-
#ifndef AsyncCallbackProcessor_h__
19-
#define AsyncCallbackProcessor_h__
18+
#ifndef TRINITYCORE_ASYNC_CALLBACK_PROCESSOR_H
19+
#define TRINITYCORE_ASYNC_CALLBACK_PROCESSOR_H
2020

21-
#include "Define.h"
22-
#include <algorithm>
21+
#include "AsyncCallbackProcessorFwd.h"
2322
#include <vector>
2423

25-
//template <class T>
26-
//concept AsyncCallback = requires(T t) { { t.InvokeIfReady() } -> std::convertible_to<bool> };
27-
28-
template<typename T> // requires AsyncCallback<T>
24+
template<AsyncCallback T>
2925
class AsyncCallbackProcessor
3026
{
3127
public:
@@ -34,8 +30,7 @@ class AsyncCallbackProcessor
3430

3531
T& AddCallback(T&& query)
3632
{
37-
_callbacks.emplace_back(std::move(query));
38-
return _callbacks.back();
33+
return _callbacks.emplace_back(std::move(query));
3934
}
4035

4136
void ProcessReadyCallbacks()
@@ -45,19 +40,29 @@ class AsyncCallbackProcessor
4540

4641
std::vector<T> updateCallbacks{ std::move(_callbacks) };
4742

48-
updateCallbacks.erase(std::remove_if(updateCallbacks.begin(), updateCallbacks.end(), [](T& callback)
43+
std::erase_if(updateCallbacks, [](T& callback)
4944
{
50-
return callback.InvokeIfReady();
51-
}), updateCallbacks.end());
45+
return InvokeAsyncCallbackIfReady(callback);
46+
});
5247

5348
_callbacks.insert(_callbacks.end(), std::make_move_iterator(updateCallbacks.begin()), std::make_move_iterator(updateCallbacks.end()));
5449
}
5550

51+
bool Empty() const
52+
{
53+
return _callbacks.empty();
54+
}
55+
56+
void CancelAll()
57+
{
58+
_callbacks.clear();
59+
}
60+
5661
private:
5762
AsyncCallbackProcessor(AsyncCallbackProcessor const&) = delete;
5863
AsyncCallbackProcessor& operator=(AsyncCallbackProcessor const&) = delete;
5964

6065
std::vector<T> _callbacks;
6166
};
6267

63-
#endif // AsyncCallbackProcessor_h__
68+
#endif // TRINITYCORE_ASYNC_CALLBACK_PROCESSOR_H
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
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 TRINITYCORE_ASYNC_CALLBACK_PROCESSOR_FWD_H
19+
#define TRINITYCORE_ASYNC_CALLBACK_PROCESSOR_FWD_H
20+
21+
#include <concepts>
22+
23+
template <typename T>
24+
concept AsyncCallback = requires(T& t) { { InvokeAsyncCallbackIfReady(t) } -> std::convertible_to<bool>; };
25+
26+
template<AsyncCallback T>
27+
class AsyncCallbackProcessor;
28+
29+
#endif // TRINITYCORE_ASYNC_CALLBACK_PROCESSOR_FWD_H

src/server/database/Database/DatabaseEnvFwd.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#ifndef DatabaseEnvFwd_h__
1919
#define DatabaseEnvFwd_h__
2020

21+
#include "AsyncCallbackProcessorFwd.h"
2122
#include <future>
2223
#include <memory>
2324

@@ -48,9 +49,7 @@ using PreparedQueryResultFuture = std::future<PreparedQueryResult>;
4849
using PreparedQueryResultPromise = std::promise<PreparedQueryResult>;
4950

5051
class QueryCallback;
51-
52-
template<typename T>
53-
class AsyncCallbackProcessor;
52+
bool InvokeAsyncCallbackIfReady(QueryCallback& callback);
5453

5554
using QueryCallbackProcessor = AsyncCallbackProcessor<QueryCallback>;
5655

@@ -63,6 +62,7 @@ template<typename T>
6362
class Transaction;
6463

6564
class TransactionCallback;
65+
bool InvokeAsyncCallbackIfReady(TransactionCallback& callback);
6666

6767
template<typename T>
6868
using SQLTransaction = std::shared_ptr<Transaction<T>>;
@@ -83,6 +83,7 @@ using LoginDatabaseQueryHolder = SQLQueryHolder<LoginDatabaseConnection>;
8383
using WorldDatabaseQueryHolder = SQLQueryHolder<WorldDatabaseConnection>;
8484

8585
class SQLQueryHolderCallback;
86+
bool InvokeAsyncCallbackIfReady(SQLQueryHolderCallback& callback);
8687

8788
// mysql
8889
struct MySQLHandle;

src/server/database/Database/QueryCallback.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,6 @@ class TC_DATABASE_API QueryCallback
6666
std::queue<QueryCallbackData, std::list<QueryCallbackData>> _callbacks;
6767
};
6868

69+
inline bool InvokeAsyncCallbackIfReady(QueryCallback& callback) { return callback.InvokeIfReady(); }
70+
6971
#endif // _QUERY_CALLBACK_H

src/server/database/Database/QueryHolder.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,6 @@ class TC_DATABASE_API SQLQueryHolderCallback
8585
std::function<void(SQLQueryHolderBase const&)> m_callback;
8686
};
8787

88+
inline bool InvokeAsyncCallbackIfReady(SQLQueryHolderCallback& callback) { return callback.InvokeIfReady(); }
89+
8890
#endif

src/server/database/Database/Transaction.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,4 +120,6 @@ class TC_DATABASE_API TransactionCallback
120120
std::function<void(bool)> m_callback;
121121
};
122122

123+
inline bool InvokeAsyncCallbackIfReady(TransactionCallback& callback) { return callback.InvokeIfReady(); }
124+
123125
#endif

0 commit comments

Comments
 (0)