From d98e139d08d2d24426d75384f8789ccc7279bca4 Mon Sep 17 00:00:00 2001 From: dyknon Date: Mon, 7 Sep 2026 23:28:33 +0900 Subject: thinfunc --- thinfunc.h | 87 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 thinfunc.h (limited to 'thinfunc.h') diff --git a/thinfunc.h b/thinfunc.h new file mode 100644 index 0000000..c75c33a --- /dev/null +++ b/thinfunc.h @@ -0,0 +1,87 @@ +/* Heap-allocation free & small memory footprint + * std::function alternative with limited capabilities. + */ // single-header library + +/* Copyright (c) 2026 dyknon + * + * 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 + * (including the next paragraph) 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. + */ + +#ifndef INC_THINFUNC_H +#define INC_THINFUNC_H + +namespace tf{ + +template +struct func; +template +struct func{ + virtual R operator()(Args...) = 0; +}; + +template +class anchor; +template +class bound_anchor; + +template +class anchor{ +public: + template (T::*Self), R (T::*Method)(Args...)> + using bound_type = bound_anchor (T::*), R (T::*)(Args...), Self, Method>; +private: + alignas(bound_type) + char body[sizeof(bound_type)]; +public: + template (T::*Self), R (T::*Method)(Args...)> + auto& bind(){ + return *(new (this->body) bound_type()); + } + template (T::*Self), R (T::*Method)(Args...)> + auto& assume_bound() const{ + return *(bound_type *)this->body; + } +}; + +template< + class T, class R, class... Args, + anchor (T::*Self), R (T::*Method)(Args...) +> +class bound_anchor (T::*), R (T::*)(Args...), + Self, Method + >: public func{ +public: + bound_anchor(){} + bound_anchor(const bound_anchor&) = delete; + bound_anchor(const bound_anchor&&) = delete; + bound_anchor& operator=(const bound_anchor&) = delete; + bound_anchor& operator=(const bound_anchor&&) = delete; + virtual R operator()(Args... args){ + auto offset = (char *)&(((T *)0)->*Self) - (char *)0; + T *container = (T *)((char *)this - offset); + return (container->*Method)(args...); + } +}; + +} // namespace rf + +#endif //INC_THINFUNC_H -- cgit v1.2.3