any_ref.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_DETAIL_ANY_REF_HPP_INCLUDED
5 #define LEXY_DETAIL_ANY_REF_HPP_INCLUDED
6 
8 
9 // Essentially a void*, but we can cast it in a constexpr context.
10 // The cost is an extra layer of indirection.
11 
12 namespace lexy::_detail
13 {
14 template <typename T>
15 class any_holder;
16 
17 // Store a pointer to this instead of a void*.
18 class any_base
19 {
20 public:
21  any_base(const any_base&) = delete;
22  any_base& operator=(const any_base&) = delete;
23 
24  template <typename T>
25  constexpr T& get() noexcept
26  {
27  return static_cast<any_holder<T>*>(this)->get();
28  }
29  template <typename T>
30  constexpr const T& get() const noexcept
31  {
32  return static_cast<const any_holder<T>*>(this)->get();
33  }
34 
35 private:
36  constexpr any_base() = default;
37  ~any_base() = default;
38 
39  template <typename T>
40  friend class any_holder;
41 };
42 
43 using any_ref = any_base*;
44 using any_cref = const any_base*;
45 
46 // Need to store the object in here.
47 template <typename T>
48 class any_holder : public any_base
49 {
50 public:
51  constexpr explicit any_holder(T&& obj) : _obj(LEXY_MOV(obj)) {}
52 
53  constexpr T& get() noexcept
54  {
55  return _obj;
56  }
57  constexpr const T& get() const noexcept
58  {
59  return _obj;
60  }
61 
62 private:
63  T _obj;
64 };
65 } // namespace lexy::_detail
66 
67 #endif // LEXY_DETAIL_ANY_REF_HPP_INCLUDED
68 
lexy::_detail::any_holder::any_holder
constexpr any_holder(T &&obj)
Definition: any_ref.hpp:51
LEXY_MOV
#define LEXY_MOV(...)
Definition: config.hpp:21
lexy::_detail::any_base::operator=
any_base & operator=(const any_base &)=delete
config.hpp
lexy::_detail::any_holder::get
constexpr T & get() noexcept
Definition: any_ref.hpp:53
lexy::_detail::any_base::~any_base
~any_base()=default
lexy::_detail::any_holder::get
constexpr const T & get() const noexcept
Definition: any_ref.hpp:57
lexy::_detail::any_base::get
constexpr const T & get() const noexcept
Definition: any_ref.hpp:30
lexy::_detail::any_base
Definition: any_ref.hpp:18
lexy::_detail
Definition: any_ref.hpp:12
lexy::_detail::any_base::any_base
constexpr any_base()=default
lexy::_detail::any_holder::_obj
T _obj
Definition: any_ref.hpp:63
lexy::_detail::any_holder
Definition: any_ref.hpp:15
lexy::_detail::any_base::get
constexpr T & get() noexcept
Definition: any_ref.hpp:25


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