From 7a67bff2463180a1d9a734b10c5e2ddcc00002dd Mon Sep 17 00:00:00 2001 From: Denis Blank Date: Tue, 9 Jun 2015 23:17:35 +0200 Subject: [PATCH] some tests with callbacks --- CMakeLists.txt | 2 +- fluent++/Callback.h | 42 ++++++++++++++++++++++++++++++++++++++++++ fluent++/fluent++.hpp | 2 +- test.cpp | 20 ++++++++++++++++++-- 4 files changed, 62 insertions(+), 4 deletions(-) create mode 100644 fluent++/Callback.h diff --git a/CMakeLists.txt b/CMakeLists.txt index fa557d6..a0b0724 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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++) diff --git a/fluent++/Callback.h b/fluent++/Callback.h new file mode 100644 index 0000000..90fba9c --- /dev/null +++ b/fluent++/Callback.h @@ -0,0 +1,42 @@ +/* + * Copyright (C) 2008-2015 TrinityCore + * + * 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 . + */ + +#ifndef _CALLBACK_H_ +#define _CALLBACK_H_ + +#include +#include +#include + + +template +using Callback = std::function; + +template +using SharedCallback = std::shared_ptr>; + +template +using WeakCallback = std::weak_ptr>; + +template +auto make_shared_callback(Callback&& callback) + -> SharedCallback +{ + return std::make_shared>(std::forward>(callback)); +} + +#endif /// _TASK_SCHEDULER_H_ diff --git a/fluent++/fluent++.hpp b/fluent++/fluent++.hpp index 15c6aeb..e1a1ad2 100644 --- a/fluent++/fluent++.hpp +++ b/fluent++/fluent++.hpp @@ -49,7 +49,7 @@ public: } template - fluent_step then(Callback const& callback) + fluent_step then(Callback&& callback) { return std::move(*this); } diff --git a/test.cpp b/test.cpp index 0d9bd2a..7e48b18 100644 --- a/test.cpp +++ b/test.cpp @@ -1,8 +1,7 @@ #include "fluent++.hpp" -template -using Callback = std::function; +#include "Callback.h" void CastSpell(int id, Callback const& callback) { @@ -34,5 +33,22 @@ int main(int argc, char** argv) std::cout << "finish everything" << std::endl; }); + typedef Callback cbd1; + typedef WeakCallback cbd2; + typedef SharedCallback cbd3; + + cbd1 _cbd1; + cbd2 _cbd2; + cbd3 _cbd3; + + auto cb = make_shared_callback([](bool) + { + + }); + + auto cbt = std::make_shared>([]() + { + }); + return 0; }