include/Cnx/Lambda.h file

Basic lambda/closure implementation for C.

MIT License

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Typedefs

using CnxLambdaCaptures = void*
Stores the captures/bindings of a lambda This is implicitly passed as the first parameter to a LambdaFunction, but as a user, will never need to be used or interacted with directly.

Functions

static void lambda_free(void* lambda)
Frees the given lambda, freeing associated memory and making it invalid for future use.

Defines

#define Lambda(ReturnType, ...)
The type of a complete lambda instance, binding a lambda function definition with a set of captured variables.
#define LambdaFunction(Name, ...)
A Lambda function definition. Can be bound to an instance of a lambda with lambda or lambda_with_allocator.
#define LambdaBinding(...)
A type storing a lambda's bound captures.
#define lambda_with_allocator(alloc, function_name, ...)
Binds the given function and captures as a lambda type, that can be called later.
#define lambda(function_name, ...)
Binds the given function and captures as a lambda type, that can be called later.
#define lambda_call(lambda, ...)
Calls the given lambda with the provided arguments as function parameters.
#define lambda_clone(lambda)
Returns a clone of the given lambda, ensuring that (by-value) captures stay valid for the lifetime of the clone.
#define ScopedLambda
Declaration tag to scope a lambda, ensuring it is freed when it goes out of scoped and associated resources are cleaned up.
#define __lambda_binding_cast(captures, BindingType)
Casts the given CnxLambdaCaptures to the given BindingType
#define lambda_binding(...)
Retrieves the bound captures from the enclosing lambda.
#define lambda_cast(lambda, Type)
Casts the given lambda to the given named type, so it can be passed to a function or stored.