mirror of
https://github.com/Naios/continuable.git
synced 2025-12-07 01:06:44 +08:00
some tests with callbacks
This commit is contained in:
parent
1ac3ebee51
commit
3ca62d4dbc
@ -25,7 +25,7 @@ if(CMAKE_BUILD_TOOL MATCHES "(msdev|devenv|nmake)")
|
||||
add_definitions(-Wall -Wextra)
|
||||
endif()
|
||||
|
||||
file(GLOB_RECURSE FLUENT_SOURCES fluent++/*.cpp fluent++/*.hpp)
|
||||
file(GLOB_RECURSE FLUENT_SOURCES fluent++/*.cpp fluent++/*.hpp fluent++/*.h)
|
||||
add_library(fluent++ STATIC ${FLUENT_SOURCES})
|
||||
|
||||
include_directories(fluent++)
|
||||
|
||||
42
fluent++/Callback.h
Normal file
42
fluent++/Callback.h
Normal file
@ -0,0 +1,42 @@
|
||||
/*
|
||||
* Copyright (C) 2008-2015 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/>.
|
||||
*/
|
||||
|
||||
#ifndef _CALLBACK_H_
|
||||
#define _CALLBACK_H_
|
||||
|
||||
#include <functional>
|
||||
#include <utility>
|
||||
#include <memory>
|
||||
|
||||
|
||||
template<typename... Args>
|
||||
using Callback = std::function<void(Args...)>;
|
||||
|
||||
template<typename... Args>
|
||||
using SharedCallback = std::shared_ptr<Callback<Args...>>;
|
||||
|
||||
template<typename... Args>
|
||||
using WeakCallback = std::weak_ptr<Callback<Args...>>;
|
||||
|
||||
template<typename... Args>
|
||||
auto make_shared_callback(Callback<Args...>&& callback)
|
||||
-> SharedCallback<Args...>
|
||||
{
|
||||
return std::make_shared<Callback<Args...>>(std::forward<Callback<Args...>>(callback));
|
||||
}
|
||||
|
||||
#endif /// _TASK_SCHEDULER_H_
|
||||
@ -49,7 +49,7 @@ public:
|
||||
}
|
||||
|
||||
template <typename Callback>
|
||||
fluent_step then(Callback const& callback)
|
||||
fluent_step then(Callback&& callback)
|
||||
{
|
||||
return std::move(*this);
|
||||
}
|
||||
|
||||
20
test.cpp
20
test.cpp
@ -1,8 +1,7 @@
|
||||
|
||||
#include "fluent++.hpp"
|
||||
|
||||
template <typename... Args>
|
||||
using Callback = std::function<void(Args...)>;
|
||||
#include "Callback.h"
|
||||
|
||||
void CastSpell(int id, Callback<bool> const& callback)
|
||||
{
|
||||
@ -34,5 +33,22 @@ int main(int argc, char** argv)
|
||||
std::cout << "finish everything" << std::endl;
|
||||
});
|
||||
|
||||
typedef Callback<bool> cbd1;
|
||||
typedef WeakCallback<int> cbd2;
|
||||
typedef SharedCallback<std::string> cbd3;
|
||||
|
||||
cbd1 _cbd1;
|
||||
cbd2 _cbd2;
|
||||
cbd3 _cbd3;
|
||||
|
||||
auto cb = make_shared_callback<bool>([](bool)
|
||||
{
|
||||
|
||||
});
|
||||
|
||||
auto cbt = std::make_shared<std::function<void()>>([]()
|
||||
{
|
||||
});
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user