mirror of
https://github.com/Naios/continuable.git
synced 2025-12-07 17:26:47 +08:00
renames
This commit is contained in:
parent
0c59d5fa98
commit
825c84f365
@ -28,15 +28,15 @@ if(CMAKE_BUILD_TOOL MATCHES "(msdev|devenv|nmake)")
|
||||
add_definitions(-Wall -Wextra)
|
||||
endif()
|
||||
|
||||
file(GLOB_RECURSE FLUENT_SOURCES fluent++/*.cpp fluent++/*.hpp fluent++/*.h)
|
||||
add_library(fluent++ STATIC ${FLUENT_SOURCES})
|
||||
file(GLOB_RECURSE FLUENT_SOURCES fluent/*.cpp fluent/*.hpp fluent/*.h)
|
||||
add_library(fluent STATIC ${FLUENT_SOURCES})
|
||||
|
||||
include_directories(fluent++)
|
||||
include_directories(fluent)
|
||||
|
||||
set(TEST_SOURCES test.cpp)
|
||||
|
||||
add_executable(fluent_test ${TEST_SOURCES})
|
||||
|
||||
target_link_libraries(fluent_test
|
||||
fluent++
|
||||
fluent
|
||||
)
|
||||
|
||||
@ -1,112 +0,0 @@
|
||||
|
||||
/*
|
||||
* Copyright (C) 2015 Naios <naios-dev@live.de>
|
||||
*
|
||||
* 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 3 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>
|
||||
|
||||
#include "functional_unwrap.hpp"
|
||||
|
||||
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...>>;
|
||||
|
||||
namespace detail
|
||||
{
|
||||
template<typename Function>
|
||||
struct do_unwrap_callback;
|
||||
|
||||
template<typename _RTy, typename... _ATy>
|
||||
struct do_unwrap_callback<std::function<_RTy(_ATy...)>>
|
||||
{
|
||||
typedef Callback<_ATy...> CallbackType;
|
||||
|
||||
typedef SharedCallback<_ATy...> SharedCallbackType;
|
||||
|
||||
typedef WeakCallback<_ATy...> WeakCallbackType;
|
||||
};
|
||||
|
||||
template<typename _CTy>
|
||||
using unwrap_callback_t = do_unwrap_callback<::fu::function_type_of_t<_CTy>>;
|
||||
|
||||
/*
|
||||
template<typename... Args>
|
||||
struct WeakProxyFactory
|
||||
{
|
||||
static Callback<Args...> CreateProxyFromWeak(WeakCallback<Args...> const& weak_callback)
|
||||
{
|
||||
return [=](Args&&... args)
|
||||
{
|
||||
if (auto const callback = weak_callback.lock())
|
||||
(*callback)(args...); // FIXME: use std::forward
|
||||
};
|
||||
}
|
||||
|
||||
static Callback<Args...> CreateProxyFromShared(SharedCallback<Args...> const& shared_callback)
|
||||
{
|
||||
return CreateProxyFromWeak(WeakCallback<Args...>(shared_callback));
|
||||
}
|
||||
};
|
||||
*/
|
||||
|
||||
} // detail
|
||||
|
||||
template<typename _CTy>
|
||||
using callback_of_t = typename detail::unwrap_callback_t<_CTy>::CallbackType;
|
||||
|
||||
template<typename _CTy>
|
||||
using shared_callback_of_t = typename detail::unwrap_callback_t<_CTy>::SharedCallbackType;
|
||||
|
||||
template<typename _CTy>
|
||||
using weak_callback_of_t = typename detail::unwrap_callback_t<_CTy>::WeakCallbackType;
|
||||
|
||||
template<typename _CTy>
|
||||
inline shared_callback_of_t<_CTy>
|
||||
make_shared_callback(_CTy&& callback)
|
||||
{
|
||||
return std::make_shared<callback_of_t<_CTy>>
|
||||
(std::forward<callback_of_t<_CTy>>(callback));
|
||||
};
|
||||
|
||||
/* Disabled due to clang errors
|
||||
template<typename... Args>
|
||||
inline auto make_weak_wrapped_callback(WeakCallback<Args...> const& weak_callback)
|
||||
-> Callback<Args...>
|
||||
{
|
||||
// Some workarounds for clang...
|
||||
return detail::WeakProxyFactory<Args...>::CreateProxyFromWeak(weak_callback);
|
||||
}
|
||||
|
||||
template<typename... Args>
|
||||
inline auto make_weak_wrapped_callback(SharedCallback<Args...> const& shared_callback)
|
||||
-> Callback<Args...>
|
||||
{
|
||||
// Some workarounds for clang...
|
||||
return detail::WeakProxyFactory<Args...>::CreateProxyFromShared(shared_callback);
|
||||
}
|
||||
*/
|
||||
|
||||
#endif /// _CALLBACK_H_
|
||||
@ -1,81 +0,0 @@
|
||||
|
||||
/*
|
||||
* Copyright (C) 2015 Naios <naios-dev@live.de>
|
||||
*
|
||||
* 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 3 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 _CONTINUABLE_H_
|
||||
#define _CONTINUABLE_H_
|
||||
|
||||
#include "Callback.h"
|
||||
|
||||
template <typename... _ATy>
|
||||
struct Continuable
|
||||
{
|
||||
typedef Callback<Callback<_ATy...>&&> ForwardFunction;
|
||||
|
||||
// Function which expects a callback that is inserted from the Continuable
|
||||
// to chain everything together
|
||||
ForwardFunction _callback_insert;
|
||||
|
||||
Continuable<_ATy...>() { }
|
||||
Continuable<_ATy...>(ForwardFunction&& callback_insert)
|
||||
: _callback_insert(std::forward<ForwardFunction>(callback_insert)) { }
|
||||
|
||||
template <typename _CTy>
|
||||
Continuable<_ATy...>& then(_CTy&&)
|
||||
{
|
||||
return *this;
|
||||
}
|
||||
};
|
||||
|
||||
namespace detail
|
||||
{
|
||||
template <typename _FTy, typename _RTy, typename... _ATy>
|
||||
struct ContinuableFactory;
|
||||
|
||||
template <typename _FTy, typename _RTy, typename... _ATy>
|
||||
struct ContinuableFactory<_FTy, _RTy, ::fu::identity<Callback<_ATy...>&&>>
|
||||
{
|
||||
static auto CreateFrom(_FTy&& functional)
|
||||
-> Continuable<_ATy...>
|
||||
{
|
||||
return Continuable<_ATy...>(
|
||||
typename Continuable<_ATy...>::ForwardFunction(std::forward<_FTy>(functional)));
|
||||
}
|
||||
};
|
||||
|
||||
template <typename _FTy>
|
||||
using continuable_factory_t = ContinuableFactory<
|
||||
_FTy, ::fu::return_type_of_t<_FTy>, ::fu::argument_type_of_t<_FTy>>;
|
||||
|
||||
} // detail
|
||||
|
||||
/// Wraps a functional object which expects a r-value callback as argument into a continuable.
|
||||
/// The callable is invoked when the continuable shall continue.
|
||||
/// For example:
|
||||
/// make_continuable([](Callback<int>&& callback)
|
||||
/// {
|
||||
/// /* Continue here */
|
||||
/// callback(5);
|
||||
/// });
|
||||
template <typename _FTy>
|
||||
inline auto make_continuable(_FTy&& functional)
|
||||
-> decltype(detail::continuable_factory_t<_FTy>::CreateFrom(std::declval<_FTy>()))
|
||||
{
|
||||
return detail::continuable_factory_t<_FTy>::CreateFrom(std::forward<_FTy>(functional));
|
||||
}
|
||||
|
||||
#endif /// _CONTINUABLE_H_
|
||||
@ -1,125 +0,0 @@
|
||||
/*
|
||||
* 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 _WEAK_CALLBACK_CONTAINER_H_
|
||||
#define _WEAK_CALLBACK_CONTAINER_H_
|
||||
|
||||
#include <unordered_map>
|
||||
|
||||
#include <boost/any.hpp>
|
||||
#include <boost/optional.hpp>
|
||||
|
||||
#include "Callback.h"
|
||||
|
||||
class WeakCallbackContainer
|
||||
{
|
||||
std::shared_ptr<WeakCallbackContainer> self_reference;
|
||||
|
||||
typedef size_t handle_t;
|
||||
|
||||
size_t handle;
|
||||
|
||||
std::unordered_map<decltype(handle), boost::any> container;
|
||||
|
||||
template<typename _CTy, typename... Args>
|
||||
struct ProxyFactory;
|
||||
|
||||
template<typename _CTy, typename... Args>
|
||||
struct ProxyFactory<_CTy, ::fu::identity<Args...>>
|
||||
{
|
||||
// Creates a weak proxy callback which prevents invoking to an invalid context.
|
||||
// Removes itself from the owner with the given handler.
|
||||
static callback_of_t<_CTy> CreateProxy(std::weak_ptr<WeakCallbackContainer> const& weak_owner,
|
||||
size_t const handle, weak_callback_of_t<_CTy> const& weak_callback)
|
||||
{
|
||||
return [=](Args&&... args)
|
||||
{
|
||||
// Try to get a pointer to the owner
|
||||
if (auto const owner = weak_owner.lock())
|
||||
// And to the wrapped functional itself
|
||||
if (auto const callback = weak_callback.lock())
|
||||
{
|
||||
// Invoke the original callback
|
||||
(*callback)(args...); // FIXME: use std::forward
|
||||
|
||||
// Unregister the callback
|
||||
owner->InvalidateCallback(handle);
|
||||
}
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
public:
|
||||
WeakCallbackContainer()
|
||||
: self_reference(this, [](decltype(this)) { }), handle(0L) { }
|
||||
|
||||
~WeakCallbackContainer() = default;
|
||||
|
||||
WeakCallbackContainer(WeakCallbackContainer const&) = delete;
|
||||
WeakCallbackContainer(WeakCallbackContainer&&) = delete;
|
||||
|
||||
WeakCallbackContainer& operator= (WeakCallbackContainer const&) = delete;
|
||||
WeakCallbackContainer& operator= (WeakCallbackContainer&&) = delete;
|
||||
|
||||
WeakCallbackContainer& Clear()
|
||||
{
|
||||
container.clear();
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Weak wraps the given callable.
|
||||
template<typename _CTy>
|
||||
auto Wrap(_CTy&& callback)
|
||||
-> callback_of_t<_CTy>
|
||||
{
|
||||
// Create the shared callback
|
||||
shared_callback_of_t<_CTy> shared_callback = make_shared_callback(std::forward<_CTy>(callback));
|
||||
|
||||
// Create a weak proxy callback which removes the callback on execute
|
||||
auto const this_handle = handle++;
|
||||
callback_of_t<_CTy> proxy =
|
||||
ProxyFactory<_CTy, ::fu::argument_type_of_t<_CTy>>::
|
||||
CreateProxy(self_reference, this_handle, shared_callback);
|
||||
|
||||
container.insert(std::make_pair(this_handle, boost::any(std::move(shared_callback))));
|
||||
return std::move(proxy);
|
||||
}
|
||||
|
||||
/// Calls ::Wrap on the given callable,
|
||||
template<typename _CTy>
|
||||
inline auto operator()(_CTy&& callback)
|
||||
-> decltype(Wrap(std::declval<_CTy>()))
|
||||
{
|
||||
return Wrap(std::forward<_CTy>(callback));
|
||||
}
|
||||
|
||||
boost::optional<handle_t> GetLastCallbackHandle() const
|
||||
{
|
||||
if (handle == 0L)
|
||||
return boost::none;
|
||||
else
|
||||
return boost::make_optional(handle);
|
||||
}
|
||||
|
||||
WeakCallbackContainer& InvalidateCallback(handle_t const handle)
|
||||
{
|
||||
container.erase(handle);
|
||||
return *this;
|
||||
}
|
||||
};
|
||||
|
||||
#endif /// _WEAK_CALLBACK_CONTAINER_H_
|
||||
@ -1,4 +0,0 @@
|
||||
|
||||
#include "fluent++.hpp"
|
||||
|
||||
void do_export() { }
|
||||
@ -1,90 +0,0 @@
|
||||
|
||||
#ifndef _FLUENT_HPP_
|
||||
#define _FLUENT_HPP_
|
||||
|
||||
#include <memory>
|
||||
#include <utility>
|
||||
#include <iostream>
|
||||
|
||||
#include "functional_unwrap.hpp"
|
||||
|
||||
/*
|
||||
class fluent_step
|
||||
{
|
||||
bool released;
|
||||
|
||||
void release()
|
||||
{
|
||||
int i = 0;
|
||||
|
||||
std::cout << "-> release" << std::endl;
|
||||
}
|
||||
|
||||
public:
|
||||
fluent_step() : released(false)
|
||||
{
|
||||
std::cout << "+ construct" << std::endl;
|
||||
}
|
||||
|
||||
~fluent_step()
|
||||
{
|
||||
std::cout << "- destruct" << std::endl;
|
||||
|
||||
if (!released)
|
||||
release();
|
||||
}
|
||||
|
||||
fluent_step(fluent_step const&) = delete;
|
||||
fluent_step(fluent_step&& right) : released(false)
|
||||
{
|
||||
std::cout << "<-> move" << std::endl;
|
||||
|
||||
right.released = true;
|
||||
}
|
||||
|
||||
fluent_step& operator= (fluent_step const&) = delete;
|
||||
fluent_step& operator= (fluent_step&& right)
|
||||
{
|
||||
released = false;
|
||||
right.released = true;
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <typename Callback>
|
||||
fluent_step then(Callback&& callback)
|
||||
{
|
||||
return std::move(*this);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename _ATy = void()>
|
||||
fluent_step make_waterfall()
|
||||
{
|
||||
return fluent_step();
|
||||
}
|
||||
|
||||
|
||||
struct ProtoContinueable
|
||||
{
|
||||
template <typename Callback>
|
||||
ProtoContinueable then(Callback&& callback)
|
||||
{
|
||||
return ProtoContinueable();
|
||||
}
|
||||
|
||||
template <typename Container>
|
||||
ProtoContinueable weak(Container& container)
|
||||
{
|
||||
return ProtoContinueable();
|
||||
}
|
||||
|
||||
ProtoContinueable strong()
|
||||
{
|
||||
return ProtoContinueable();
|
||||
}
|
||||
};
|
||||
*/
|
||||
|
||||
void do_export();
|
||||
|
||||
#endif /// _FLUENT_HPP_
|
||||
@ -1,146 +0,0 @@
|
||||
|
||||
/*
|
||||
* Copyright (C) 2015 Naios <naios-dev@live.de>
|
||||
*
|
||||
* 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 3 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/>.
|
||||
*/
|
||||
|
||||
/*
|
||||
* The functional unwrap trait provides the possibility to extract
|
||||
* various types out of c++ functional types, like std::function, functors and more.
|
||||
*/
|
||||
|
||||
#ifndef _FUNCTIONAL_UNWRAP_HPP_
|
||||
#define _FUNCTIONAL_UNWRAP_HPP_
|
||||
|
||||
#include <functional>
|
||||
|
||||
namespace fu
|
||||
{
|
||||
template<typename... Args>
|
||||
struct identity
|
||||
{
|
||||
};
|
||||
|
||||
namespace detail
|
||||
{
|
||||
template<typename Function>
|
||||
struct unwrap_function_impl;
|
||||
|
||||
template<typename _RTy, typename... _ATy>
|
||||
struct unwrap_function_impl<_RTy(_ATy...)>
|
||||
{
|
||||
/// The return type of the function.
|
||||
typedef _RTy return_type;
|
||||
|
||||
/// The argument types of the function as pack in fu::identity.
|
||||
typedef identity<_ATy...> argument_type;
|
||||
|
||||
/// The function provided as std::function
|
||||
typedef std::function<_RTy(_ATy...)> function_type;
|
||||
|
||||
/// The function provided as function_ptr
|
||||
typedef _RTy(*function_ptr)(_ATy...);
|
||||
};
|
||||
|
||||
/// STL: std::function
|
||||
template<typename _RTy, typename... _ATy>
|
||||
struct unwrap_function_impl<std::function<_RTy(_ATy...)>>
|
||||
: unwrap_function_impl<_RTy(_ATy...)> { };
|
||||
|
||||
/// STL: std::tuple
|
||||
template<typename _RTy, typename... _ATy>
|
||||
struct unwrap_function_impl<std::tuple<_RTy, _ATy...>>
|
||||
: unwrap_function_impl<_RTy(_ATy...)> { };
|
||||
|
||||
/// Function pointers
|
||||
template<typename _RTy, typename... _ATy>
|
||||
struct unwrap_function_impl<_RTy(*const)(_ATy...)>
|
||||
: unwrap_function_impl<_RTy(_ATy...)> { };
|
||||
|
||||
/// Class Method pointers
|
||||
template<typename _CTy, typename _RTy, typename... _ATy>
|
||||
struct unwrap_function_impl<_RTy(_CTy::*)(_ATy...) const>
|
||||
: unwrap_function_impl<_RTy(_ATy...)> { };
|
||||
|
||||
/// Pack in fu::identity
|
||||
template<typename _RTy, typename... _ATy>
|
||||
struct unwrap_function_impl<identity<_RTy, _ATy...>>
|
||||
: unwrap_function_impl<_RTy(_ATy...)> { };
|
||||
|
||||
/// Unwrap through pointer of functor.
|
||||
template<typename Function>
|
||||
static auto select_best_unwrap_unary_arg(int)
|
||||
-> unwrap_function_impl<decltype(&Function::operator())>;
|
||||
|
||||
/// Unwrap through plain type.
|
||||
template<typename Function>
|
||||
static auto select_best_unwrap_unary_arg(long)
|
||||
-> unwrap_function_impl<Function>;
|
||||
|
||||
template<typename... _FTy>
|
||||
struct select_best_unwrap;
|
||||
|
||||
/// Enable only if 1 template argument is given.
|
||||
template<typename _FTy>
|
||||
struct select_best_unwrap<_FTy>
|
||||
{
|
||||
typedef decltype(select_best_unwrap_unary_arg<_FTy>(0)) type;
|
||||
};
|
||||
|
||||
// Enable if more then 1 template argument is given.
|
||||
// (Handles lazy typing)
|
||||
template<typename _RTy, typename... _ATy>
|
||||
struct select_best_unwrap<_RTy, _ATy...>
|
||||
{
|
||||
typedef unwrap_function_impl<_RTy(_ATy...)> type;
|
||||
};
|
||||
|
||||
} // detail
|
||||
|
||||
/// Trait to unwrap function parameters of various types:
|
||||
/// Function style definition: Result(Parameters...)
|
||||
/// STL `std::function` : std::function<Result(Parameters...)>
|
||||
/// STL `std::tuple` : std::tuple<Result, Parameters...>
|
||||
/// C++ Function pointers: `Result(*)(Parameters...)`
|
||||
/// C++ Class method pointers: `Result(Class::*)(Parameters...)`
|
||||
/// Lazy typed signatures: `Result, Parameters...`
|
||||
/// Also provides optimized unwrap of functors and functional objects.
|
||||
template<typename... Function>
|
||||
using unwrap_function =
|
||||
typename detail::select_best_unwrap<Function...>::type;
|
||||
|
||||
/// Trait which defines the return type of the function.
|
||||
template<typename... Function>
|
||||
using return_type_of_t =
|
||||
typename detail::select_best_unwrap<Function...>::type::return_type;
|
||||
|
||||
/// Trait which defines the argument types of the function packed in std::tuple.
|
||||
template<typename... Function>
|
||||
using argument_type_of_t =
|
||||
typename detail::select_best_unwrap<Function...>::type::argument_type;
|
||||
|
||||
/// Trait which defines the std::function type of the function.
|
||||
template<typename... Function>
|
||||
using function_type_of_t =
|
||||
typename detail::select_best_unwrap<Function...>::type::function_type;
|
||||
|
||||
/// Trait which defines the function pointer type of the function.
|
||||
template<typename... Function>
|
||||
using function_ptr_of_t =
|
||||
typename detail::select_best_unwrap<Function...>::type::function_ptr;
|
||||
|
||||
} // fu
|
||||
|
||||
#endif // _FUNCTIONAL_UNWRAP_HPP_
|
||||
5
test.cpp
5
test.cpp
@ -29,8 +29,8 @@ Continuable<SpellCastResult> CastSpell(int id)
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
typedef shared_callback_of_t<void(int)> sc1;
|
||||
typedef weak_callback_of_t<Callback<bool>> sc2;
|
||||
// typedef shared_callback_of_t<Callback<int>> sc1;
|
||||
// typedef weak_callback_of_t<Callback<bool>> sc2;
|
||||
|
||||
typedef Continuable<bool> cont123;
|
||||
|
||||
@ -58,5 +58,6 @@ int main(int argc, char** argv)
|
||||
|
||||
});
|
||||
|
||||
std::cout << "ok" << std::endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user