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