iceoryx2
C++ Language Bindings
Loading...
Searching...
No Matches
static_lifetime_guard.hpp
Go to the documentation of this file.
1// Copyright (c) 2023 by Apex.AI Inc. All rights reserved.
2// Copyright (c) 2025 Contributors to the Eclipse Foundation
3//
4// See the NOTICE file(s) distributed with this work for additional
5// information regarding copyright ownership.
6//
7// This program and the accompanying materials are made available under the
8// terms of the Apache Software License 2.0 which is available at
9// https://www.apache.org/licenses/LICENSE-2.0, or the MIT license
10// which is available at https://opensource.org/licenses/MIT.
11//
12// SPDX-License-Identifier: Apache-2.0 OR MIT
13
14#ifndef IOX2_BB_DESIGN_STATIC_LIFETIME_GUARD_HPP
15#define IOX2_BB_DESIGN_STATIC_LIFETIME_GUARD_HPP
16
18
19#include <type_traits>
20#include <utility>
21
22namespace iox2 {
23namespace legacy {
48template <typename T>
50 public:
52
54
56
58
59 // assignment does nothing since the objects have no state,
60 // copy and move exist to support passing/returning a value object
63
72
76
77 private:
78 struct alignas(T) storage_t {
79 // AXIVION Next Construct AutosarC++19_03-M0.1.3 : the field is intentionally unused and serves as a mean to provide memory
80 // AXIVION Next Construct AutosarC++19_03-A1.1.1 : object size depends on template parameter and has to be taken care of at the specific template instantiation
81 // AXIVION Next Construct AutosarC++19_03-A18.1.1 : required as low level building block, encapsulated in abstraction and not directly used
82 // NOLINTNEXTLINE(hicpp-avoid-c-arrays, cppcoreguidelines-avoid-c-arrays)
83 unsigned char data[sizeof(T)];
84 };
85
86 static constexpr uint32_t UNINITIALIZED { 0 };
87 static constexpr uint32_t INITALIZING { 1 };
88 static constexpr uint32_t INITALIZED { 2 };
89
90 // NOLINTJUSTIFICATION these static variables are private and mutability is required
91 // NOLINTBEGIN (cppcoreguidelines-avoid-non-const-global-variables)
92 static storage_t s_storage;
93 static concurrent::Atomic<uint64_t> s_count;
94 static concurrent::Atomic<uint32_t> s_instanceState;
95 static T* s_instance;
96 // NOLINTEND (cppcoreguidelines-avoid-non-const-global-variables)
97
98 static void destroy();
99
100 protected:
114};
115
116} // namespace legacy
117} // namespace iox2
118
119#include "iox2/legacy/detail/static_lifetime_guard.inl"
120
121#endif // IOX2_BB_DESIGN_STATIC_LIFETIME_GUARD_HPP
Manages a static instance of type T in a way so that each existing StaticLifetimeGuard prevents the d...
static uint64_t setCount(uint64_t count)
Set the instance life time count.
static T & instance(Args &&... args) noexcept
Construct the instance to be guarded with constructor arguments.
static uint64_t count()
Get the current count value.
constexpr bool always_false_v
Helper value to bind a static_assert to a type.