mirror of
https://github.com/oxen-io/lokinet.git
synced 2024-11-02 03:40:12 +00:00
273270916e
This commit reflects changes to clang-format rules. Unfortunately, these rule changes create a massive change to the codebase, which causes an apparent rewrite of git history. Git blame's --ignore-rev flag can be used to ignore this commit when attempting to `git blame` some code.
76 lines
2.2 KiB
C++
76 lines
2.2 KiB
C++
#ifndef LLARP_UTIL_MEMFN
|
|
#define LLARP_UTIL_MEMFN
|
|
|
|
#include <type_traits>
|
|
#include <utility>
|
|
#include <memory>
|
|
|
|
namespace llarp
|
|
{
|
|
namespace util
|
|
{
|
|
// Wraps a member function and instance into a callable object that invokes
|
|
// the method (non-const overload).
|
|
template <
|
|
typename Return,
|
|
typename Class,
|
|
typename Derived,
|
|
typename... Arg,
|
|
typename = std::enable_if_t<std::is_base_of<Class, Derived>::value>>
|
|
auto
|
|
memFn(Return (Class::*f)(Arg...), Derived* self)
|
|
{
|
|
return [f, self](Arg... args) -> Return { return (self->*f)(std::forward<Arg>(args)...); };
|
|
}
|
|
|
|
// Wraps a member function and instance into a lambda that invokes the
|
|
// method (const overload).
|
|
template <
|
|
typename Return,
|
|
typename Class,
|
|
typename Derived,
|
|
typename... Arg,
|
|
typename = std::enable_if_t<std::is_base_of<Class, Derived>::value>>
|
|
auto
|
|
memFn(Return (Class::*f)(Arg...) const, const Derived* self)
|
|
{
|
|
return [f, self](Arg... args) -> Return { return (self->*f)(std::forward<Arg>(args)...); };
|
|
}
|
|
|
|
// Wraps a member function and shared pointer to an instance into a lambda
|
|
// that invokes the method.
|
|
template <
|
|
typename Return,
|
|
typename Class,
|
|
typename Derived,
|
|
typename... Arg,
|
|
typename = std::enable_if_t<std::is_base_of<Class, Derived>::value>>
|
|
auto
|
|
memFn(Return (Class::*f)(Arg...), std::shared_ptr<Derived> self)
|
|
{
|
|
return [f, self = std::move(self)](Arg... args) -> Return {
|
|
return (self.get()->*f)(std::forward<Arg>(args)...);
|
|
};
|
|
}
|
|
|
|
// Wraps a member function and shared pointer to an instance into a lambda
|
|
// that invokes the method (const method overload).
|
|
template <
|
|
typename Return,
|
|
typename Class,
|
|
typename Derived,
|
|
typename... Arg,
|
|
typename = std::enable_if_t<std::is_base_of<Class, Derived>::value>>
|
|
auto
|
|
memFn(Return (Class::*f)(Arg...) const, std::shared_ptr<Derived> self)
|
|
{
|
|
return [f, self = std::move(self)](Arg... args) -> Return {
|
|
return (self.get()->*f)(std::forward<Arg>(args)...);
|
|
};
|
|
}
|
|
|
|
} // namespace util
|
|
} // namespace llarp
|
|
|
|
#endif
|