A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
wifi-opt-field.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2024 Universita' degli Studi di Napoli Federico II
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Stefano Avallone <stavallo@unina.it>
7 */
8
9#ifndef WIFI_OPT_FIELD_H
10#define WIFI_OPT_FIELD_H
11
12#include <functional>
13#include <optional>
14
15namespace ns3
16{
17
18/**
19 * \ingroup wifi
20 *
21 * OptFieldWithPresenceInd is a class modeling an optional field (in an Information Element, a
22 * management frame, etc.) having an associated Presence Indicator bit. This class is a wrapper
23 * around std::optional (most of its functions are exposed, more can be added if needed) that
24 * additionally sets the Presence Indicator flag appropriately when operations like reset or
25 * assignment of a value are performed on the optional field.
26 */
27template <typename T>
29{
30 public:
31 /// @brief constructor
32 /// @param presenceFlag the Presence Indicator flag
33 OptFieldWithPresenceInd(bool& presenceFlag);
34
35 /// @brief Destroy the value (if any) contained in the optional field.
36 /// @return a reference to this object
37 constexpr OptFieldWithPresenceInd& operator=(std::nullopt_t);
38
39 /// @brief Assign the given value to the optional field
40 /// @param other the given value
41 /// @return a reference to this object
42 constexpr OptFieldWithPresenceInd& operator=(const std::optional<T>& other);
43
44 /// @brief Assign the given value to the optional field
45 /// @param other the given value
46 /// @return a reference to this object
47 constexpr OptFieldWithPresenceInd& operator=(std::optional<T>&& other);
48
49 /// @brief Operator bool
50 /// @return whether this object contains a value
51 constexpr explicit operator bool() const;
52
53 /// @brief Check whether this object contains a value
54 /// @return whether this object contains a value
55 constexpr bool has_value() const;
56
57 /// @return a pointer to the contained value
58 constexpr const T* operator->() const;
59
60 /// @return a pointer to the contained value
61 constexpr T* operator->();
62
63 /// @return a reference to the contained value
64 constexpr const T& operator*() const;
65
66 /// @return a reference to the contained value
67 constexpr T& operator*();
68
69 /// @brief Construct the contained value in-place
70 /// @tparam Args the type of arguments to pass to the constructor
71 /// @param args the arguments to pass to the constructor
72 /// @return a reference to the new contained value
73 template <class... Args>
74 constexpr T& emplace(Args&&... args);
75
76 /// @brief Destroy the value (if any) contained in the optional field
77 constexpr void reset();
78
79 private:
80 std::optional<T> m_field; ///< the optional field
81 std::reference_wrapper<bool> m_presenceFlag; ///< the Presence Indicator flag
82};
83
84} // namespace ns3
85
86/***************************************************************
87 * Implementation of the templates declared above.
88 ***************************************************************/
89
90namespace ns3
91{
92
93template <typename T>
95 : m_presenceFlag(presenceFlag)
96{
97 m_presenceFlag.get() = false;
98}
99
100template <typename T>
103{
104 m_field.reset();
105 m_presenceFlag.get() = false;
106 return *this;
107}
108
109template <typename T>
111OptFieldWithPresenceInd<T>::operator=(const std::optional<T>& other)
112{
113 m_field = other;
114 m_presenceFlag.get() = other.has_value();
115 return *this;
116}
117
118template <typename T>
121{
122 m_field = std::move(other);
123 m_presenceFlag.get() = other.has_value();
124 return *this;
125}
126
127template <typename T>
129{
130 return m_field.has_value();
131}
132
133template <typename T>
134constexpr bool
136{
137 return m_field.has_value();
138}
139
140template <typename T>
141constexpr const T*
143{
144 return &(*m_field);
145}
146
147template <typename T>
148constexpr T*
150{
151 return &(*m_field);
152}
153
154template <typename T>
155constexpr const T&
157{
158 return *m_field;
159}
160
161template <typename T>
162constexpr T&
164{
165 return *m_field;
166}
167
168template <typename T>
169template <class... Args>
170constexpr T&
172{
173 m_field.emplace(std::forward<Args>(args)...);
174 m_presenceFlag.get() = true;
175 return *m_field;
176}
177
178template <typename T>
179constexpr void
181{
182 m_field.reset();
183 m_presenceFlag.get() = false;
184}
185
186} // namespace ns3
187
188#endif /* WIFI_OPT_FIELD_H */
OptFieldWithPresenceInd is a class modeling an optional field (in an Information Element,...
std::optional< T > m_field
the optional field
constexpr void reset()
Destroy the value (if any) contained in the optional field.
std::reference_wrapper< bool > m_presenceFlag
the Presence Indicator flag
constexpr OptFieldWithPresenceInd & operator=(std::nullopt_t)
Destroy the value (if any) contained in the optional field.
constexpr OptFieldWithPresenceInd & operator=(std::optional< T > &&other)
Assign the given value to the optional field.
constexpr const T * operator->() const
constexpr T & emplace(Args &&... args)
Construct the contained value in-place.
OptFieldWithPresenceInd(bool &presenceFlag)
constructor
constexpr OptFieldWithPresenceInd & operator=(const std::optional< T > &other)
Assign the given value to the optional field.
constexpr bool has_value() const
Check whether this object contains a value.
constexpr const T & operator*() const
Every class exported by the ns3 library is enclosed in the ns3 namespace.