mirror of
https://github.com/Naios/continuable.git
synced 2025-12-06 16:56:44 +08:00
finish weak callback proxies
This commit is contained in:
parent
797d09c8c0
commit
3b6aa8517a
@ -20,19 +20,20 @@
|
|||||||
|
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
|
|
||||||
|
#include <boost/any.hpp>
|
||||||
|
#include <boost/optional.hpp>
|
||||||
|
|
||||||
#include "Callback.h"
|
#include "Callback.h"
|
||||||
|
|
||||||
class CallbackContainer
|
class CallbackContainer
|
||||||
{
|
{
|
||||||
std::shared_ptr<CallbackContainer> self_reference;
|
std::shared_ptr<CallbackContainer> self_reference;
|
||||||
|
|
||||||
|
typedef size_t handle_t;
|
||||||
|
|
||||||
size_t handle;
|
size_t handle;
|
||||||
|
|
||||||
struct InternalReference
|
std::unordered_map<decltype(handle), boost::any> container;
|
||||||
{
|
|
||||||
};
|
|
||||||
|
|
||||||
std::unordered_map<decltype(handle), InternalReference> container;
|
|
||||||
|
|
||||||
template<typename _CTy, typename... Args>
|
template<typename _CTy, typename... Args>
|
||||||
struct ProxyFactory;
|
struct ProxyFactory;
|
||||||
@ -40,14 +41,24 @@ class CallbackContainer
|
|||||||
template<typename _CTy, typename... Args>
|
template<typename _CTy, typename... Args>
|
||||||
struct ProxyFactory<_CTy, std::tuple<Args...>>
|
struct ProxyFactory<_CTy, std::tuple<Args...>>
|
||||||
{
|
{
|
||||||
// Creates a weak callback proxy which prevents invoking to an invalid context.
|
// Creates a weak proxy callback which prevents invoking to an invalid context.
|
||||||
static callback_of_t<_CTy> CreateProxy(_CTy&& callback)
|
static callback_of_t<_CTy> CreateProxy(std::weak_ptr<CallbackContainer> const& weak_owner,
|
||||||
|
size_t const handle, weak_callback_of_t<_CTy> const& weak_callback)
|
||||||
{
|
{
|
||||||
return [callback](Args&&... args)
|
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
|
// Invoke the original callback
|
||||||
callback(std::forward<Args...>(args...));
|
// FIXME: std::forward<Args..>(args...) causes errors when void
|
||||||
|
(*callback)(args...);
|
||||||
|
|
||||||
|
// Unregister the callback
|
||||||
|
owner->InvalidateCallback(handle);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -67,7 +78,6 @@ public:
|
|||||||
CallbackContainer& Clear()
|
CallbackContainer& Clear()
|
||||||
{
|
{
|
||||||
container.clear();
|
container.clear();
|
||||||
handle = 0L;
|
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -75,13 +85,32 @@ public:
|
|||||||
auto operator()(_CTy&& callback)
|
auto operator()(_CTy&& callback)
|
||||||
-> callback_of_t<_CTy>
|
-> 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
|
// Create a weak proxy callback which removes the callback on execute
|
||||||
|
auto const this_handle = handle++;
|
||||||
callback_of_t<_CTy> proxy =
|
callback_of_t<_CTy> proxy =
|
||||||
ProxyFactory<_CTy, ::fu::argument_type_of_t<_CTy>>::
|
ProxyFactory<_CTy, ::fu::argument_type_of_t<_CTy>>::
|
||||||
CreateProxy(std::forward<_CTy>(callback));
|
CreateProxy(self_reference, this_handle, shared_callback);
|
||||||
|
|
||||||
|
container.insert(std::make_pair(this_handle, boost::any(std::move(shared_callback))));
|
||||||
return std::move(proxy);
|
return std::move(proxy);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
boost::optional<handle_t> GetLastCallbackHandle() const
|
||||||
|
{
|
||||||
|
if (handle == 0L)
|
||||||
|
return boost::none;
|
||||||
|
else
|
||||||
|
return boost::make_optional(handle);
|
||||||
|
}
|
||||||
|
|
||||||
|
CallbackContainer& InvalidateCallback(handle_t const handle)
|
||||||
|
{
|
||||||
|
container.erase(handle);
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif /// _CALLBACK_CONTAINER_H_
|
#endif /// _CALLBACK_CONTAINER_H_
|
||||||
|
|||||||
42
test.cpp
42
test.cpp
@ -4,6 +4,9 @@
|
|||||||
#include "Callback.h"
|
#include "Callback.h"
|
||||||
#include "CallbackContainer.h"
|
#include "CallbackContainer.h"
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
#include <exception>
|
||||||
|
|
||||||
void CastSpell(int id, Callback<bool> const& callback)
|
void CastSpell(int id, Callback<bool> const& callback)
|
||||||
{
|
{
|
||||||
std::cout << "Cast " << id << std::endl;
|
std::cout << "Cast " << id << std::endl;
|
||||||
@ -47,12 +50,47 @@ int main(int argc, char** argv)
|
|||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Callback<> weak_cb_test;
|
||||||
|
|
||||||
|
{
|
||||||
CallbackContainer callback;
|
CallbackContainer callback;
|
||||||
|
|
||||||
auto mycb = callback([](bool success)
|
std::shared_ptr<int> dealloc_test(new int{2}, [](int* me)
|
||||||
{
|
{
|
||||||
|
std::cout << "dealloc ok" << std::endl;
|
||||||
|
delete me;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
auto const cb_void = callback([dealloc_test]
|
||||||
|
{
|
||||||
|
std::cout << "huhu i'm a..." << std::endl;
|
||||||
|
|
||||||
|
auto const cp = dealloc_test;
|
||||||
|
});
|
||||||
|
|
||||||
|
dealloc_test.reset();
|
||||||
|
|
||||||
|
auto const cb_bool = callback([](bool success)
|
||||||
|
{
|
||||||
|
std::cout << "...weak wrapped callback!" << std::endl;
|
||||||
|
});
|
||||||
|
|
||||||
|
weak_cb_test = callback([]
|
||||||
|
{
|
||||||
|
std::cout << "huhu i'm crashsafe (you wont see me)!" << std::endl;
|
||||||
|
std::logic_error("bad logic");
|
||||||
|
});
|
||||||
|
|
||||||
|
cb_void();
|
||||||
|
|
||||||
|
int i = 0;
|
||||||
|
++i;
|
||||||
|
|
||||||
|
cb_bool(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
// This will never be executed because the CallbackContainer was deallocated and its weak callback proxies are crash safe.
|
||||||
|
weak_cb_test();
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user