Woman, Life, Freedom


Adaptive AUTOSAR
ARA public interface header documentation
atomic_optional.h
1#ifndef ATOMIC_OPTIONAL_H
2#define ATOMIC_OPTIONAL_H
3
4#include <type_traits>
5#include <utility>
6#include <stdexcept>
7#include <atomic>
8
9namespace ara
10{
11 namespace exec
12 {
14 namespace helper
15 {
18 template <typename T>
20 {
21 private:
22 std::atomic<T> mValue;
23 std::atomic<bool> mHasValue;
24
25 public:
26 AtomicOptional() noexcept : mHasValue{false}
27 {
28 }
29
32 constexpr AtomicOptional(T value) noexcept : mValue{value},
33 mHasValue{true}
34 {
35 }
36
37 ~AtomicOptional() noexcept = default;
38 AtomicOptional &operator=(const AtomicOptional& other) = delete;
39
40 AtomicOptional &operator=(T value) noexcept
41 {
42 mValue = value;
43 mHasValue = true;
44
45 return *this;
46 }
47
49 void Reset() noexcept
50 {
51 mHasValue = false;
52 }
53
56 bool HasValue() const noexcept
57 {
58 return mHasValue;
59 }
60
64 T Value() const
65 {
66 if (mHasValue)
67 {
68 return mValue.load();
69 }
70 else
71 {
72 throw std::runtime_error("Optional contains no value.");
73 }
74 }
75 };
76 }
77 }
78}
79
80#endif
A wrapper around a possible atomic value.
Definition: atomic_optional.h:20
constexpr AtomicOptional(T value) noexcept
Constructor.
Definition: atomic_optional.h:32
bool HasValue() const noexcept
Indicate whether the instance has an atomic value or not.
Definition: atomic_optional.h:56
T Value() const
Get instance possible non-atomic value.
Definition: atomic_optional.h:64
void Reset() noexcept
Reset the instance atomic value.
Definition: atomic_optional.h:49