/* LICENSE BEGIN This file is part of the SixtyFPS Project -- https://sixtyfps.io Copyright (c) 2020 Olivier Goffart Copyright (c) 2020 Simon Hausmann SPDX-License-Identifier: GPL-3.0-only This file is also available under commercial licensing terms. Please contact info@sixtyfps.io for more information. LICENSE END */ #pragma once #include #include "sixtyfps_properties_internal.h" namespace sixtyfps { template struct Callback; template struct Callback { Callback() { cbindgen_private::sixtyfps_callback_init(&inner); } ~Callback() { cbindgen_private::sixtyfps_callback_drop(&inner); } Callback(const Callback &) = delete; Callback(Callback &&) = delete; Callback &operator=(const Callback &) = delete; template void set_handler(F binding) const { cbindgen_private::sixtyfps_callback_set_handler( &inner, [](void *user_data, const void *arg, void *ret) { *reinterpret_cast(ret) = std::apply(*reinterpret_cast(user_data), *reinterpret_cast(arg)); }, new F(std::move(binding)), [](void *user_data) { delete reinterpret_cast(user_data); }); } Ret call(const Arg &...arg) const { Ret r{}; Tuple tuple{arg...}; cbindgen_private::sixtyfps_callback_call(&inner, &tuple, &r); return r; } private: using Tuple = std::tuple; cbindgen_private::CallbackOpaque inner; }; template struct Callback { Callback() { cbindgen_private::sixtyfps_callback_init(&inner); } ~Callback() { cbindgen_private::sixtyfps_callback_drop(&inner); } Callback(const Callback &) = delete; Callback(Callback &&) = delete; Callback &operator=(const Callback &) = delete; template void set_handler(F binding) const { cbindgen_private::sixtyfps_callback_set_handler( &inner, [](void *user_data, const void *arg, void *) { std::apply(*reinterpret_cast(user_data), *reinterpret_cast(arg)); }, new F(std::move(binding)), [](void *user_data) { delete reinterpret_cast(user_data); }); } void call(const Arg &...arg) const { Tuple tuple{arg...}; cbindgen_private::sixtyfps_callback_call(&inner, &tuple, reinterpret_cast(0x1)); } private: using Tuple = std::tuple; cbindgen_private::CallbackOpaque inner; }; namespace private_api { template struct CallbackSignatureHelper { using Result = R(A); }; template struct CallbackSignatureHelper { using Result = R(); }; template using CallbackHelper = Callback::Result>; } } // namespace sixtyfps