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 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation;
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 *
17 * Author: Stefano Avallone <stavallo@unina.it>
18 */
19
20#ifndef WIFI_OPT_FIELD_H
21#define WIFI_OPT_FIELD_H
22
23#include <functional>
24#include <optional>
25
26namespace ns3
27{
28
29/**
30 * \ingroup wifi
31 *
32 * OptFieldWithPresenceInd is a class modeling an optional field (in an Information Element, a
33 * management frame, etc.) having an associated Presence Indicator bit. This class is a wrapper
34 * around std::optional (most of its functions are exposed, more can be added if needed) that
35 * additionally sets the Presence Indicator flag appropriately when operations like reset or
36 * assignment of a value are performed on the optional field.
37 */
38template <typename T>
40{
41 public:
42 /// @brief constructor
43 /// @param presenceFlag the Presence Indicator flag
44 OptFieldWithPresenceInd(bool& presenceFlag);
45
46 /// @brief Destroy the value (if any) contained in the optional field.
47 /// @return a reference to this object
48 constexpr OptFieldWithPresenceInd& operator=(std::nullopt_t);
49
50 /// @brief Assign the given value to the optional field
51 /// @param other the given value
52 /// @return a reference to this object
53 constexpr OptFieldWithPresenceInd& operator=(const std::optional<T>& other);
54
55 /// @brief Assign the given value to the optional field
56 /// @param other the given value
57 /// @return a reference to this object
58 constexpr OptFieldWithPresenceInd& operator=(std::optional<T>&& other);
59
60 /// @brief Operator bool
61 /// @return whether this object contains a value
62 constexpr explicit operator bool() const;
63
64 /// @brief Check whether this object contains a value
65 /// @return whether this object contains a value
66 constexpr bool has_value() const;
67
68 /// @return a pointer to the contained value
69 constexpr const T* operator->() const;
70
71 /// @return a pointer to the contained value
72 constexpr T* operator->();
73
74 /// @return a reference to the contained value
75 constexpr const T& operator*() const;
76
77 /// @return a reference to the contained value
78 constexpr T& operator*();
79
80 /// @brief Construct the contained value in-place
81 /// @tparam Args the type of arguments to pass to the constructor
82 /// @param args the arguments to pass to the constructor
83 /// @return a reference to the new contained value
84 template <class... Args>
85 constexpr T& emplace(Args&&... args);
86
87 /// @brief Destroy the value (if any) contained in the optional field
88 constexpr void reset();
89
90 private:
91 std::optional<T> m_field; ///< the optional field
92 std::reference_wrapper<bool> m_presenceFlag; ///< the Presence Indicator flag
93};
94
95} // namespace ns3
96
97/***************************************************************
98 * Implementation of the templates declared above.
99 ***************************************************************/
100
101namespace ns3
102{
103
104template <typename T>
106 : m_presenceFlag(presenceFlag)
107{
108 m_presenceFlag.get() = false;
109}
110
111template <typename T>
114{
115 m_field.reset();
116 m_presenceFlag.get() = false;
117 return *this;
118}
119
120template <typename T>
122OptFieldWithPresenceInd<T>::operator=(const std::optional<T>& other)
123{
124 m_field = other;
125 m_presenceFlag.get() = other.has_value();
126 return *this;
127}
128
129template <typename T>
132{
133 m_field = std::move(other);
134 m_presenceFlag.get() = other.has_value();
135 return *this;
136}
137
138template <typename T>
140{
141 return m_field.has_value();
142}
143
144template <typename T>
145constexpr bool
147{
148 return m_field.has_value();
149}
150
151template <typename T>
152constexpr const T*
154{
155 return &(*m_field);
156}
157
158template <typename T>
159constexpr T*
161{
162 return &(*m_field);
163}
164
165template <typename T>
166constexpr const T&
168{
169 return *m_field;
170}
171
172template <typename T>
173constexpr T&
175{
176 return *m_field;
177}
178
179template <typename T>
180template <class... Args>
181constexpr T&
183{
184 m_field.emplace(std::forward<Args>(args)...);
185 m_presenceFlag.get() = true;
186 return *m_field;
187}
188
189template <typename T>
190constexpr void
192{
193 m_field.reset();
194 m_presenceFlag.get() = false;
195}
196
197} // namespace ns3
198
199#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.