C/C++ API Reference
Loading...
Searching...
No Matches
globals.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 <new>
17#include <type_traits>
18#include <utility>
19
20namespace pw {
21
23
40template <typename T>
42 public:
43 using value_type = T;
44
45 // Initializes a T in place.
46 //
47 // This overload is disabled when it might collide with copy/move.
48 template <
49 typename... Args,
50 typename std::enable_if<!std::is_same<void(std::decay_t<Args>&...),
51 void(RuntimeInitGlobal&)>::value,
52 int>::type = 0>
53 explicit RuntimeInitGlobal(Args&&... args) {
54 new (storage_) T(std::forward<Args>(args)...);
55 asm volatile(""); // Prevent constant initialization
56 }
57
58 // Move or copy from the contained type. This allows for construction from an
59 // initializer list, e.g. for std::vector.
60 explicit RuntimeInitGlobal(const T& x) {
61 new (storage_) T(x);
62 asm volatile(""); // Prevent constant initialization
63 }
64
65 explicit RuntimeInitGlobal(T&& x) {
66 new (storage_) T(std::move(x));
67 asm volatile(""); // Prevent constant initialization
68 }
69
70 RuntimeInitGlobal(const RuntimeInitGlobal&) = delete;
71 RuntimeInitGlobal& operator=(const RuntimeInitGlobal&) = delete;
72
73 ~RuntimeInitGlobal() = default;
74
75 const T& operator*() const { return *operator->(); }
76 T& operator*() { return *operator->(); }
77
78 const T* operator->() const {
79 return std::launder(reinterpret_cast<const T*>(&storage_));
80 }
81 T* operator->() { return std::launder(reinterpret_cast<T*>(&storage_)); }
82
83 private:
84 // Use aligned storage instead of a union to remove the destructor, since GCC
85 // incorrectly prevents RuntimeInitGlobal from wrapping types with private
86 // destructors if the type is stored in a union.
87 alignas(T) unsigned char storage_[sizeof(T)];
88};
89
90} // namespace pw
Definition: globals.h:41
The Pigweed namespace.
Definition: alignment.h:27