Coder Social home page Coder Social logo

lambda's Introduction


lambda

A utility for working with C-Libraries from C++


Basic Examples

Using lambdas with capture as a function pointer

extern "C" void some_c_function(void (*callback)(int a))
{
    // ...
}

std::string some_string;

some_c_function(lambda::pointer_to([&](int)
{
    some_string = "Captured by reference!";
}));

A utility for passing void* user data

extern "C" void some_c_function(void (*callback)(int a, void* user_data), void* user_data)
{
    // ...
}

std::string some_string;
auto user_data = lambda::user_data(some_string);

some_c_function([](int a, void* data) {
    auto &[some_string] = decltype(user_data)::from(data).tuple();
    some_string = "Easy user-data!";
}, user_data);

For more examples, and some useful comments, you might want to take a look at the tests!


Installation

Note
This library requires a C++20 capable compiler.

  • FetchContent

    include(FetchContent)
    FetchContent_Declare(lambda GIT_REPOSITORY "https://github.com/Curve/lambda")
    
    FetchContent_MakeAvailable(lambda)
    target_link_libraries(<YourLibrary> lambda::ptr)
  • Git Submodule

    git submodule add "https://github.com/Curve/lambda"
    # Somewhere in your CMakeLists.txt
    add_subdirectory("<path_to_lambda>")
    target_link_libraries(<YourLibrary> lambda::ptr)

Windows

Q: Does this Library work on windows?
A: Yes

Q: Does this library work with MSVC?
A: Kind of, beware that lambda captures on MSVC are broken¹ and MSVC Versions below 19.36 are affected by a bug that causes compilation errors.

¹: This bug has already been reported and will hopefully be fixed with a new MSVC update.

❌ Broken ✅ Working
some_c_function(lambda::pointer_to([&]{/*...*/}));
auto fn_ptr = lambda::pointer_to([&]{/*...*/});
some_c_function(fn_ptr);

Q: Are there alternatives to MSVC?
A: Yes! I'd recommend to just use clang-cl

lambda's People

Contributors

curve avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.