Pigweed
 
Loading...
Searching...
No Matches
no_destructor.h
1// Copyright 2025 The Pigweed Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License"); you may not
4// use this file except in compliance with the License. You may obtain a copy of
5// 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, WITHOUT
11// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12// License for the specific language governing permissions and limitations under
13// the License.
14#pragma once
15
16#include <type_traits>
17#include <utility>
18
19#include "pw_polyfill/language_feature_macros.h"
20
21namespace pw {
22
72template <typename T>
74 public:
75 using value_type = T;
76
77 // Initializes a T in place.
78 //
79 // This overload is disabled when it might collide with copy/move.
80 template <typename... Args,
81 typename std::enable_if<!std::is_same<void(std::decay_t<Args>&...),
82 void(NoDestructor&)>::value,
83 int>::type = 0>
84 explicit constexpr NoDestructor(Args&&... args)
85 : storage_(std::forward<Args>(args)...) {}
86
87 // Move or copy from the contained type. This allows for construction from an
88 // initializer list, e.g. for std::vector.
89 explicit constexpr NoDestructor(const T& x) : storage_(x) {}
90 explicit constexpr NoDestructor(T&& x) : storage_(std::move(x)) {}
91
92 NoDestructor(const NoDestructor&) = delete;
93 NoDestructor& operator=(const NoDestructor&) = delete;
94
95 ~NoDestructor() = default;
96
97 constexpr const T& operator*() const { return get(); }
98 constexpr T& operator*() { return get(); }
99
100 constexpr const T* operator->() const { return &get(); }
101 constexpr T* operator->() { return &get(); }
102
103 private:
104 constexpr T& get() {
105 if constexpr (std::is_trivially_destructible_v<T>) {
106 return storage_;
107 } else {
108 return storage_.value;
109 }
110 }
111
112 constexpr const T& get() const {
113 if constexpr (std::is_trivially_destructible_v<T>) {
114 return storage_;
115 } else {
116 return storage_.value;
117 }
118 }
119
120 union NonTrivialStorage {
121 template <typename... Args>
122 constexpr NonTrivialStorage(Args&&... args)
123 : value(std::forward<Args>(args)...) {}
124
125 // Unfortunately, this cannot be trivially destructible because having a
126 // union member of non-trivially destructible T implicitly deletes the
127 // destructor. Trivial destruction may be possible in future C++ standards.
128 PW_CONSTEXPR_CPP20 ~NonTrivialStorage() {}
129
130 T value;
131 };
132
133 std::conditional_t<std::is_trivially_destructible_v<T>, T, NonTrivialStorage>
134 storage_;
135};
136
137} // namespace pw
Definition: no_destructor.h:73
Provides basic helpers for reading and writing UTF-8 encoded strings.
Definition: alignment.h:27