From 5d5399a2676e46dae98dd832d2cefae40e553f95 Mon Sep 17 00:00:00 2001 From: David Given Date: Sun, 17 Aug 2025 00:55:10 +0200 Subject: [PATCH] Add another weirdly missing file. --- dep/lexy/include/lexy/callback/object.hpp | 98 +++++++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 dep/lexy/include/lexy/callback/object.hpp diff --git a/dep/lexy/include/lexy/callback/object.hpp b/dep/lexy/include/lexy/callback/object.hpp new file mode 100644 index 00000000..7e538737 --- /dev/null +++ b/dep/lexy/include/lexy/callback/object.hpp @@ -0,0 +1,98 @@ +// Copyright (C) 2020-2025 Jonathan Müller and lexy contributors +// SPDX-License-Identifier: BSL-1.0 + +#ifndef LEXY_CALLBACK_OBJECT_HPP_INCLUDED +#define LEXY_CALLBACK_OBJECT_HPP_INCLUDED + +#include + +namespace lexy::_detail +{ +template +using _detect_brace_construct = decltype(T{LEXY_DECLVAL(Args)...}); +template +constexpr auto is_brace_constructible = _detail::is_detected<_detect_brace_construct, T, Args...>; + +template +constexpr auto is_constructible + = std::is_constructible_v || is_brace_constructible; +} // namespace lexy::_detail + +namespace lexy +{ +template +struct _construct +{ + using return_type = T; + + constexpr T operator()(T&& t) const + { + return LEXY_MOV(t); + } + constexpr T operator()(const T& t) const + { + return t; + } + + template + constexpr auto operator()(Args&&... args) const + -> std::enable_if_t<_detail::is_constructible, T> + { + if constexpr (std::is_constructible_v) + return T(LEXY_FWD(args)...); + else + return T{LEXY_FWD(args)...}; + } +}; +template <> +struct _construct +{ + using return_type = void; + + constexpr void operator()() const {} +}; + +/// A callback that constructs an object of type T by forwarding the arguments. +template +constexpr auto construct = _construct{}; + +template +struct _new +{ + using return_type = PtrT; + + constexpr PtrT operator()(T&& t) const + { + auto ptr = new T(LEXY_MOV(t)); + return PtrT(ptr); + } + constexpr PtrT operator()(const T& t) const + { + auto ptr = new T(t); + return PtrT(ptr); + } + + template + constexpr auto operator()(Args&&... args) const + -> std::enable_if_t<_detail::is_constructible, PtrT> + { + if constexpr (std::is_constructible_v) + { + auto ptr = new T(LEXY_FWD(args)...); + return PtrT(ptr); + } + else + { + auto ptr = new T{LEXY_FWD(args)...}; + return PtrT(ptr); + } + } +}; + +/// A callback that constructs an object of type T on the heap by forwarding the arguments. +template +constexpr auto new_ = _new{}; +} // namespace lexy + +#endif // LEXY_CALLBACK_OBJECT_HPP_INCLUDED +