object.hpp
Go to the documentation of this file.
1 // Copyright (C) 2020-2023 Jonathan Müller and lexy contributors
2 // SPDX-License-Identifier: BSL-1.0
3 
4 #ifndef LEXY_CALLBACK_OBJECT_HPP_INCLUDED
5 #define LEXY_CALLBACK_OBJECT_HPP_INCLUDED
6 
7 #include <lexy/callback/base.hpp>
8 
9 namespace lexy::_detail
10 {
11 template <typename T, typename... Args>
12 using _detect_brace_construct = decltype(T{LEXY_DECLVAL(Args)...});
13 template <typename T, typename... Args>
15 
16 template <typename T, typename... Args>
17 constexpr auto is_constructible
18  = std::is_constructible_v<T, Args...> || is_brace_constructible<T, Args...>;
19 } // namespace lexy::_detail
20 
21 namespace lexy
22 {
23 template <typename T>
24 struct _construct
25 {
26  using return_type = T;
27 
28  constexpr T operator()(T&& t) const
29  {
30  return LEXY_MOV(t);
31  }
32  constexpr T operator()(const T& t) const
33  {
34  return t;
35  }
36 
37  template <typename... Args>
38  constexpr auto operator()(Args&&... args) const
39  -> std::enable_if_t<_detail::is_constructible<T, Args&&...>, T>
40  {
41  if constexpr (std::is_constructible_v<T, Args&&...>)
42  return T(LEXY_FWD(args)...);
43  else
44  return T{LEXY_FWD(args)...};
45  }
46 };
47 template <>
49 {
50  using return_type = void;
51 
52  constexpr void operator()() const {}
53 };
54 
56 template <typename T>
57 constexpr auto construct = _construct<T>{};
58 
59 template <typename T, typename PtrT>
60 struct _new
61 {
62  using return_type = PtrT;
63 
64  constexpr PtrT operator()(T&& t) const
65  {
66  auto ptr = new T(LEXY_MOV(t));
67  return PtrT(ptr);
68  }
69  constexpr PtrT operator()(const T& t) const
70  {
71  auto ptr = new T(t);
72  return PtrT(ptr);
73  }
74 
75  template <typename... Args>
76  constexpr auto operator()(Args&&... args) const
77  -> std::enable_if_t<_detail::is_constructible<T, Args&&...>, PtrT>
78  {
79  if constexpr (std::is_constructible_v<T, Args&&...>)
80  {
81  auto ptr = new T(LEXY_FWD(args)...);
82  return PtrT(ptr);
83  }
84  else
85  {
86  auto ptr = new T{LEXY_FWD(args)...};
87  return PtrT(ptr);
88  }
89  }
90 };
91 
93 template <typename T, typename PtrT = T*>
94 constexpr auto new_ = _new<T, PtrT>{};
95 } // namespace lexy
96 
97 #endif // LEXY_CALLBACK_OBJECT_HPP_INCLUDED
98 
LEXY_MOV
#define LEXY_MOV(...)
Definition: config.hpp:21
lexy::_detail::is_detected
constexpr bool is_detected
Definition: detect.hpp:28
base.hpp
lexy::_new
Definition: object.hpp:60
lexy::_detail::_detect_brace_construct
decltype(T{LEXY_DECLVAL(Args)...}) _detect_brace_construct
Definition: object.hpp:12
lexy::new_
constexpr auto new_
A callback that constructs an object of type T on the heap by forwarding the arguments.
Definition: object.hpp:94
lexy::_construct::return_type
T return_type
Definition: object.hpp:26
lexy::_construct
Definition: object.hpp:24
LEXY_FWD
#define LEXY_FWD(...)
Definition: config.hpp:22
lexy::_construct< void >::return_type
void return_type
Definition: object.hpp:50
lexy
Definition: any_ref.hpp:12
lexy::_new::operator()
constexpr auto operator()(Args &&... args) const -> std::enable_if_t< _detail::is_constructible< T, Args &&... >, PtrT >
Definition: object.hpp:76
detail::void
j template void())
Definition: json.hpp:4893
lexy::_construct< void >::operator()
constexpr void operator()() const
Definition: object.hpp:52
lexy::_construct::operator()
constexpr auto operator()(Args &&... args) const -> std::enable_if_t< _detail::is_constructible< T, Args &&... >, T >
Definition: object.hpp:38
lexy::_construct::operator()
constexpr T operator()(T &&t) const
Definition: object.hpp:28
lexy::_detail
Definition: any_ref.hpp:12
lexy::_detail::is_constructible
constexpr auto is_constructible
Definition: object.hpp:18
lexy::construct
constexpr auto construct
A callback that constructs an object of type T by forwarding the arguments.
Definition: object.hpp:57
lexy::_detail::is_brace_constructible
constexpr auto is_brace_constructible
Definition: object.hpp:14
lexy::_new::return_type
PtrT return_type
Definition: object.hpp:62
LEXY_DECLVAL
#define LEXY_DECLVAL(...)
Definition: config.hpp:24
lexy::_new::operator()
constexpr PtrT operator()(T &&t) const
Definition: object.hpp:64
lexy::_construct::operator()
constexpr T operator()(const T &t) const
Definition: object.hpp:32
lexy::_new::operator()
constexpr PtrT operator()(const T &t) const
Definition: object.hpp:69
detail::enable_if_t
typename std::enable_if< B, T >::type enable_if_t
Definition: json.hpp:3095


behaviortree_cpp_v4
Author(s): Davide Faconti
autogenerated on Fri Jun 28 2024 02:20:07