Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handling opaque C pointers #1602

Open
deadlocklogic opened this issue May 17, 2024 · 0 comments
Open

Handling opaque C pointers #1602

deadlocklogic opened this issue May 17, 2024 · 0 comments

Comments

@deadlocklogic
Copy link

deadlocklogic commented May 17, 2024

How to properly handle opaque C pointers? They are very popular in many C libraries.
Right now I doing so, consider glfw where GLFWwindow and GLFWmonitor are opaque pointers.
Consider binding:

GLFWAPI GLFWwindow* glfwCreateWindow(int width, int height, const char* title, GLFWmonitor* monitor, GLFWwindow* share);

Binding it like:

state["glfwCreateWindow"] = &glfwCreateWindow;

Fails to compile with incomplete type 'GLFWwindow' used in type trait expression.

So I am binding it while introducing a proxy holder like so:

template <typename T = void>
struct OpaquePtr {
  T *ptr = nullptr;
};

state["glfwCreateWindow"] = [](int width, int height, char const* title, OpaquePtr<GLFWmonitor>* monitor, OpaquePtr<GLFWwindow>* share) {
    return OpaquePtr<GLFWwindow> { glfwCreateWindow(width, height, title, monitor ? monitor->ptr : nullptr, share ? share->ptr : nullptr) };
};

And I have to modify all the related API to afford the newly introduced proxy holder OpaquePtr for incomplete types.

I completely understand that sol2 needs complete types so it can participate in its type system so it can catch type mismatch errors instead of letting the program crash.
@ThePhD Is this the best of what could be done or better solutions exist?
Thanks.

Edit:

Actually sol is trying to check against is_base_of_v trait at some point in sol::is_container and sol::is_lua_reference.
So specializing sol::is_lua_reference<TYPE> : std::false_type with the opaque type solves the problem.
Is it worth introducing a new traits is_c_type?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant