|
| 1 | +// Copyright 2022 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#pragma once |
| 16 | + |
| 17 | +#include "macros/always_inline.h" |
| 18 | +#include "mem/relocate.h" |
| 19 | +#include "ptr/ptr_concepts.h" |
| 20 | + |
| 21 | +namespace sus::ptr { |
| 22 | +template <class T> |
| 23 | +class [[sus_trivial_abi]] Own; |
| 24 | +} |
| 25 | + |
| 26 | +namespace sus::ptr::__private { |
| 27 | + |
| 28 | +template <class T> |
| 29 | +class InUse { |
| 30 | + public: |
| 31 | + constexpr sus_always_inline ~InUse() noexcept { own.t_ = &t; } |
| 32 | + |
| 33 | + constexpr sus_always_inline const auto* operator->() const&& noexcept |
| 34 | + requires(HasArrow<T>) |
| 35 | + { |
| 36 | + return t.operator->(); |
| 37 | + } |
| 38 | + constexpr sus_always_inline auto* operator->() && noexcept |
| 39 | + requires(HasArrowMut<T>) |
| 40 | + { |
| 41 | + return t.operator->(); |
| 42 | + } |
| 43 | + |
| 44 | + constexpr sus_always_inline const auto* operator->() const&& noexcept |
| 45 | + requires(!HasArrowMut<T>) |
| 46 | + { |
| 47 | + return &t; |
| 48 | + } |
| 49 | + constexpr sus_always_inline auto* operator->() && noexcept |
| 50 | + requires(!HasArrowMut<T>) |
| 51 | + { |
| 52 | + return &t; |
| 53 | + } |
| 54 | + |
| 55 | + T& t; |
| 56 | + Own<T>& own; |
| 57 | +}; |
| 58 | + |
| 59 | +} // namespace sus::ptr::__private |
0 commit comments