-
Notifications
You must be signed in to change notification settings - Fork 183
/
any_ref.hpp
101 lines (83 loc) · 3.14 KB
/
any_ref.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http:https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
#include <unifex/std_concepts.hpp>
#include <unifex/swap.hpp>
#include <unifex/detail/vtable.hpp>
#include <unifex/detail/with_type_erased_tag_invoke.hpp>
#include <type_traits>
#include <utility>
#include <unifex/detail/prologue.hpp>
namespace unifex {
template <typename... CPOs>
struct _any_ref {
class type;
};
template <typename... CPOs>
class _any_ref<CPOs...>::type
: private with_type_erased_tag_invoke<type, CPOs>... {
// Inline up to 3 vtable-entries. Making a max size of 4 pointers.
using vtable_holder_t = std::conditional_t<
(sizeof...(CPOs) <= 3),
detail::inline_vtable_holder<CPOs...>,
detail::indirect_vtable_holder<CPOs...>>;
public:
template(typename Concrete) //
(requires(!same_as<Concrete const, type const>)) //
/*implicit*/ type(Concrete& impl) noexcept
: vtable_(vtable_holder_t::template create<Concrete>())
, impl_((void*)std::addressof(impl)) {}
void swap(type& other) noexcept {
unifex::swap(vtable_, other.vtable_);
unifex::swap(impl_, other.impl_);
}
// Two any_ref's compare equal IFF they refer to the same object (shallow).
//
// TODO: This is not entirely accurate as it might be possible to have two
// any_ref objects constructed from the same object at the same address but
// with different static-types (e.g. one any_ref is constructed from a
// base-class) and thus they would have different vtables. However, we can't
// just compare vtables since it's not guaranteed that function pointers will
// point to the same address.
friend bool operator==(type const& left, type const& right) noexcept {
return left.impl_ == right.impl_;
}
friend bool operator!=(type const& left, type const& right) noexcept {
return !(left == right);
}
private:
friend void swap(type& left, type& right) noexcept { left.swap(right); }
friend const vtable_holder_t& get_vtable(const type& self) noexcept {
return self.vtable_;
}
friend void* get_object_address(const type& self) noexcept {
return self.impl_;
}
vtable_holder_t vtable_;
void* impl_;
};
// any_ref is a type-erased wrapper that has reference-semantics.
//
// It holds a type-erased reference to a concrete object and allows you to
// invoke CPOs on that object
//
// Copying an any_ref just copies the reference, not the underlying object.
template <typename... CPOs>
using any_ref = typename _any_ref<CPOs...>::type;
template <auto&... CPOs>
using any_ref_t = any_ref<tag_t<CPOs>...>;
} // namespace unifex
#include <unifex/detail/epilogue.hpp>