Sane C++ Libraries
C++ Platform Abstraction Libraries
SC::Function< FuncType > Struct Template Reference

Wraps function pointers, member functions and lambdas without ever allocating. More...

Detailed Description

template<typename FuncType>
struct SC::Function< FuncType >

Wraps function pointers, member functions and lambdas without ever allocating.


Template Parameters
FuncTypeType of function to be wrapped (Lambda, free function or pointer to member function)
Note
Size of lambdas or less than LAMBDA_SIZE (currently 2 * sizeof(void*)).
If lambda is bigger than LAMBDA_SIZE the constructor will static assert.

Example:

// A regular class with a member function
struct SomeClass
{
float memberValue = 2.0;
int memberFunc(float a) { return static_cast<int>(a + memberValue); }
};
// A Functor with operator ()
struct SomeFunctor
{
float memberValue = 2.0;
int operator()(float a) { return static_cast<int>(a + memberValue); }
};
// Free function
int someFunc(float a) { return static_cast<int>(a * 2); }
// Class too big to be grabbed by copy
struct BigClass
{
SC::uint64_t values[4];
};
void SC::FunctionTest::functionDocumentationSnippet()
{
SomeClass someClass;
Function<int(float)> func;
func = &someFunc; // Bind free func
func.bind<SomeClass, &SomeClass::memberFunc>(someClass); // Bind member func
func = [](float a) -> int { return static_cast<int>(a + 1.5); }; // Bind lambda func
func = SomeFunctor{2.5}; // Bind a functor
// If you feel brave enough you can retrieve the bound functor by knowing its type
SC_ASSERT_RELEASE(func.dynamicCastTo<SomeFunctor>()->memberValue == 2.5f);
// This will static_assert because sizeof(BigClass) is bigger than LAMBDA_SIZE
// BigClass bigClass;
// func = [bigClass](float a) -> int { return static_cast<int>(a);};
}
#define SC_ASSERT_RELEASE(e)
Assert expression e to be true.
Definition: Assert.h:66
unsigned long long uint64_t
Platform independent (8) bytes unsigned int.
Definition: PrimitiveTypes.h:42

The documentation for this struct was generated from the following file: