2019-03-31 11:10:21 +08:00
|
|
|
// eventpp library
|
|
|
|
// Copyright (C) 2018 Wang Qi (wqking)
|
|
|
|
// Github: https://github.com/wqking/eventpp
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
// Include the head
|
|
|
|
#include "eventpp/hetercallbacklist.h"
|
|
|
|
|
2020-12-04 19:45:43 +08:00
|
|
|
#include "tutorial.h"
|
2019-03-31 11:10:21 +08:00
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
TEST_CASE("HeterCallbackList tutorial 1, basic")
|
|
|
|
{
|
2021-01-20 20:59:29 +08:00
|
|
|
std::cout << std::endl << "HeterCallbackList tutorial 1, basic" << std::endl;
|
2019-03-31 11:10:21 +08:00
|
|
|
|
|
|
|
// The namespace is eventpp
|
|
|
|
// the first parameter is a HeterTuple of the listener prototypes.
|
|
|
|
eventpp::HeterCallbackList<eventpp::HeterTuple<void (), void (int)> > callbackList;
|
|
|
|
|
|
|
|
// Add a callback.
|
|
|
|
// []() {} is the callback.
|
|
|
|
// Lambda is not required, any function or std::function
|
|
|
|
// or whatever function object with the required prototype is fine.
|
|
|
|
callbackList.append([]() {
|
|
|
|
std::cout << "Got callback for void()." << std::endl;
|
|
|
|
});
|
|
|
|
callbackList.append([](int) {
|
|
|
|
std::cout << "Got callback for void(int)." << std::endl;
|
|
|
|
});
|
|
|
|
|
|
|
|
// Invoke the callback list
|
|
|
|
// Output: Got callback for void().
|
|
|
|
callbackList();
|
|
|
|
// Output: Got callback for void(int).
|
|
|
|
callbackList(5);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("HeterCallbackList tutorial 2, for each")
|
|
|
|
{
|
2021-01-20 20:59:29 +08:00
|
|
|
std::cout << std::endl << "HeterCallbackList tutorial 2, for each" << std::endl;
|
2019-03-31 11:10:21 +08:00
|
|
|
|
2019-03-31 19:38:49 +08:00
|
|
|
using CL = eventpp::HeterCallbackList<eventpp::HeterTuple<void (), void (int)> >;
|
2019-03-31 11:10:21 +08:00
|
|
|
CL callbackList;
|
|
|
|
|
|
|
|
// Add some callbacks.
|
|
|
|
callbackList.append([]() {
|
|
|
|
std::cout << "Got callback void()." << std::endl;
|
|
|
|
});
|
2019-03-31 19:38:49 +08:00
|
|
|
callbackList.append([]() {
|
|
|
|
std::cout << "Got callback void() again." << std::endl;
|
|
|
|
});
|
2019-05-30 22:11:20 +08:00
|
|
|
callbackList.append([](int /*a*/) {
|
2019-03-31 19:38:49 +08:00
|
|
|
std::cout << "Got callback void(int a)." << std::endl;
|
2019-03-31 11:10:21 +08:00
|
|
|
});
|
2019-05-30 22:11:20 +08:00
|
|
|
callbackList.append([](int /*a*/) {
|
2019-03-31 19:38:49 +08:00
|
|
|
std::cout << "Got callback void(int a) again." << std::endl;
|
2019-03-31 11:10:21 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
// Now call forEach to remove the second callback
|
2019-03-31 19:38:49 +08:00
|
|
|
// forEach has one template parameter, here is void(). It's which prototype to iterate for.
|
|
|
|
// The forEach callback prototype is void(const HeterCallbackList::Handle & handle, const std::function<prototype> & callback)
|
2019-03-31 11:10:21 +08:00
|
|
|
int index = 0;
|
2019-03-31 19:38:49 +08:00
|
|
|
callbackList.forEach<void()>([&callbackList, &index](const CL::Handle & handle, const std::function<void()> & /*callback*/) {
|
2019-03-31 11:10:21 +08:00
|
|
|
std::cout << "forEach(Handle, Callback), invoked " << index << std::endl;
|
|
|
|
if(index == 1) {
|
|
|
|
callbackList.remove(handle);
|
|
|
|
std::cout << "forEach(Handle, Callback), removed second callback" << std::endl;
|
|
|
|
}
|
|
|
|
++index;
|
|
|
|
});
|
|
|
|
|
2020-12-04 19:45:43 +08:00
|
|
|
// The forEach callback prototype is void(const std::function<prototype> & callback)
|
|
|
|
// We can also use C++14 generic lambda to simplify the callback, for example,
|
|
|
|
// callbackList.forEach<void (int)>([&callbackList, &index](const auto & callback)
|
|
|
|
callbackList.forEach<void (int)>([&callbackList, &index](const std::function<void(int)> & callback) {
|
2019-03-31 11:10:21 +08:00
|
|
|
std::cout << "forEach(Callback), invoked" << std::endl;
|
2019-03-31 19:38:49 +08:00
|
|
|
callback(3);
|
2019-03-31 11:10:21 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
// Invoke the callback list
|
|
|
|
callbackList();
|
|
|
|
}
|
2019-03-31 19:38:49 +08:00
|
|
|
|