AnyFunction.hpp

Based on std::function, but with a generic base class.

(Built from GenericFunction.h)

Note

Status: ALPHA

class BaseFunction
#include <AnyFunction.hpp>

An BaseFunction object can be converted back into the derived type with the .Convert<return(args…)>() member function.

Subclassed by DerivedFunction< RETURN(PARAMS…)>

Public Functions

inline virtual ~BaseFunction()
virtual size_t NumArgs() const = 0
template<typename RETURN, typename ...Ts>
auto Call(Ts&&... args)

A generic form of the function call operator; use arg types to determine derived form.

template<typename RETURN, typename ...Ts>
bool CallOK(Ts&&...)

Test if a function call will succeed before trying it.

template<typename RETURN, typename ...Ts>
bool CallTypeOK()

Test if a function call will succeed before trying it, based only on types.

template<typename RETURN, typename ...Ts>
inline auto operator()(Ts&&... args)

A generic form of the function call operator; use arg types to determine derived form.

template<typename T>
auto Convert()

Convert this BaseFunction into a derived Function.

template<typename T>
bool ConvertOK()

Determine if this BaseFunction can be converted into a derived Function.

virtual Ptr<BaseFunction> Clone() = 0
template<typename ...Ts>
class DerivedFunction
#include <AnyFunction.hpp>

Undefined base type for DerivedFunction, to create an error if a function type is not passed in.

template<typename RETURN, typename ...PARAMS>
class DerivedFunction<RETURN(PARAMS...)> : public BaseFunction
#include <AnyFunction.hpp>

Specialized form for proper function types.

Public Functions

template<typename T>
inline DerivedFunction(T in_fun)

Forward all args to std::function constructor…

inline virtual size_t NumArgs() const override
template<typename ...Ts>
inline RETURN Call(Ts&&... args)

Forward all args to std::function call.

template<typename ...Ts>
inline RETURN operator()(Ts&&... args)

Forward all args to std::function call.

inline const fun_t &GetFunction() const

Get the std::function to be called.

inline virtual Ptr<BaseFunction> Clone() override

Protected Types

using fun_t = std::function<RETURN(PARAMS...)>

Protected Attributes

fun_t fun

The std::function to be called.

class AnyFunction
#include <AnyFunction.hpp>

AnyFunction manages the function pointers to be dynamically handled.

Public Functions

inline AnyFunction()
inline AnyFunction(const AnyFunction &other)
inline AnyFunction(AnyFunction &&other) noexcept
inline AnyFunction &operator=(const AnyFunction &other)
inline AnyFunction &operator=(AnyFunction &&other) noexcept
template<typename T>
inline AnyFunction(T in_fun)

If an argument is provided, set the function.

inline ~AnyFunction()
inline void Clear()
inline size_t NumArgs() const
inline Ptr<BaseFunction> CloneFunc() const
inline operator bool()
template<typename T>
inline void Set(std::function<T> in_fun)
template<typename RETURN = void, typename ...Ts>
inline auto Call(Ts&&... args)

Call this function with specific types; must be correct!

template<typename RETURN, typename ...Ts>
inline bool CallOK(Ts&&... args)

Test if a function call will succeed before trying it.

template<typename RETURN, typename ...Ts>
inline bool CallTypeOK()

Test if a function call will succeed before trying it, based only on types.

template<typename RETURN = void, typename ...Ts>
inline auto operator()(Ts&&... args)

A generic form of the function call operator; use arg types to determine derived form.

template<typename T>
inline auto Convert()

Convert this BaseFunction into a derived Function.

template<typename T>
inline bool ConvertOK()

Determine if this BaseFunction can be converted into a derived Function.

Private Functions

template<typename T>
inline auto MakePtr(T in_fun)

Helper to build a proper derived function.

Private Members

Ptr<BaseFunction> fun = nullptr