From be177ed00219b9cbe1c68cdc56f5e079e5ebc14a Mon Sep 17 00:00:00 2001 From: Patrick Nicolas Date: Sun, 2 Mar 2025 19:21:59 +0100 Subject: [PATCH] Template utility to cast member functions --- server/utils/method.h | 57 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 server/utils/method.h diff --git a/server/utils/method.h b/server/utils/method.h new file mode 100644 index 00000000..0b881f99 --- /dev/null +++ b/server/utils/method.h @@ -0,0 +1,57 @@ +/* + * WiVRn VR streaming + * Copyright (C) 2025 Guillaume Meunier + * Copyright (C) 2025 Patrick Nicolas + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include + +namespace details +{ +template +struct method_trait +{}; + +template +struct method_trait +{ + static Result magic(Class::base * arg, Args... args) + { + return std::invoke(Method, static_cast(arg), args...); + } +}; +} // namespace details + +namespace wivrn +{ + +// Convert a pointer to member function to a free function that takes the object as first argument. +// The class needs to have a "base" type which will be used for the first argument. +// +// Example: +// class derived: public base_class +// { +// using base = base_class; +// void foo(int x); +// }; +// +// method_pointer<&derived::foo> is equivalent to +// void free_foo(base_class * base, int x) { +// return static_cast(base)->foo(x); +// } +template +auto method_pointer = ::details::method_trait::magic; +} // namespace wivrn