Pigweed
C/C++ API Reference
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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
74template <typename T>
76 public:
77 using value_type = T;
78
79 // Initializes a T in place.
80 //
81 // This overload is disabled when it might collide with copy/move.
82 template <typename... Args,
83 typename std::enable_if<!std::is_same<void(std::decay_t<Args>&...),
84 void(NoDestructor&)>::value,
85 int>::type = 0>
86 explicit constexpr NoDestructor(Args&&... args)
87 : storage_(std::forward<Args>(args)...) {}
88
89 // Move or copy from the contained type. This allows for construction from an
90 // initializer list, e.g. for std::vector.
91 explicit constexpr NoDestructor(const T& x) : storage_(x) {}
92 explicit constexpr NoDestructor(T&& x) : storage_(std::move(x)) {}
93
94 NoDestructor(const NoDestructor&) = delete;
95 NoDestructor& operator=(const NoDestructor&) = delete;
96
97 ~NoDestructor() = default;
98
99 constexpr const T& operator*() const { return get(); }
100 constexpr T& operator*() { return get(); }
101
102 constexpr const T* operator->() const { return &get(); }
103 constexpr T* operator->() { return &get(); }
104
105 private:
106 constexpr T& get() {
107 if constexpr (std::is_trivially_destructible_v<T>) {
108 return storage_;
109 } else {
110 return storage_.value;
111 }
112 }
113
114 constexpr const T& get() const {
115 if constexpr (std::is_trivially_destructible_v<T>) {
116 return storage_;
117 } else {
118 return storage_.value;
119 }
120 }
121
122 union NonTrivialStorage {
123 template <typename... Args>
124 constexpr NonTrivialStorage(Args&&... args)
125 : value(std::forward<Args>(args)...) {}
126
127 // Unfortunately, this cannot be trivially destructible because having a
128 // union member of non-trivially destructible T implicitly deletes the
129 // destructor. Trivial destruction may be possible in future C++ standards.
130 PW_CONSTEXPR_CPP20 ~NonTrivialStorage() {}
131
132 T value;
133 };
134
135 std::conditional_t<std::is_trivially_destructible_v<T>, T, NonTrivialStorage>
136 storage_;
137};
138
139} // namespace pw
Definition: no_destructor.h:75
Provides basic helpers for reading and writing UTF-8 encoded strings.
Definition: alignment.h:27