some tests with callbacks

This commit is contained in:
Naios 2015-06-09 23:17:35 +02:00
parent 1ac3ebee51
commit 3ca62d4dbc
4 changed files with 62 additions and 4 deletions

View File

@ -25,7 +25,7 @@ if(CMAKE_BUILD_TOOL MATCHES "(msdev|devenv|nmake)")
add_definitions(-Wall -Wextra) add_definitions(-Wall -Wextra)
endif() 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}) add_library(fluent++ STATIC ${FLUENT_SOURCES})
include_directories(fluent++) include_directories(fluent++)

42
fluent++/Callback.h Normal file
View 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_

View File

@ -49,7 +49,7 @@ public:
} }
template <typename Callback> template <typename Callback>
fluent_step then(Callback const& callback) fluent_step then(Callback&& callback)
{ {
return std::move(*this); return std::move(*this);
} }

View File

@ -1,8 +1,7 @@
#include "fluent++.hpp" #include "fluent++.hpp"
template <typename... Args> #include "Callback.h"
using Callback = std::function<void(Args...)>;
void CastSpell(int id, Callback<bool> const& callback) void CastSpell(int id, Callback<bool> const& callback)
{ {
@ -34,5 +33,22 @@ int main(int argc, char** argv)
std::cout << "finish everything" << std::endl; 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; return 0;
} }