From 4fcdc10998ad175d66169d570cb5cf2c17a57db1 Mon Sep 17 00:00:00 2001 From: Stefano Avallone Date: Mon, 26 Feb 2024 12:51:05 +0100 Subject: [PATCH] wifi: Add a class for optional fields with presence indicator flag --- src/wifi/CMakeLists.txt | 1 + src/wifi/model/wifi-opt-field.h | 199 ++++++++++++++++++++++++++++++++ utils/codespell-ignored-lines | 1 + 3 files changed, 201 insertions(+) create mode 100644 src/wifi/model/wifi-opt-field.h diff --git a/src/wifi/CMakeLists.txt b/src/wifi/CMakeLists.txt index e35956ce7..53abedd2b 100644 --- a/src/wifi/CMakeLists.txt +++ b/src/wifi/CMakeLists.txt @@ -295,6 +295,7 @@ set(header_files model/wifi-mpdu-type.h model/wifi-mpdu.h model/wifi-net-device.h + model/wifi-opt-field.h model/wifi-phy-band.h model/wifi-phy-common.h model/wifi-phy-listener.h diff --git a/src/wifi/model/wifi-opt-field.h b/src/wifi/model/wifi-opt-field.h new file mode 100644 index 000000000..9fc3a8863 --- /dev/null +++ b/src/wifi/model/wifi-opt-field.h @@ -0,0 +1,199 @@ +/* + * Copyright (c) 2024 Universita' degli Studi di Napoli Federico II + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation; + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author: Stefano Avallone + */ + +#ifndef WIFI_OPT_FIELD_H +#define WIFI_OPT_FIELD_H + +#include +#include + +namespace ns3 +{ + +/** + * \ingroup wifi + * + * OptFieldWithPresenceInd is a class modeling an optional field (in an Information Element, a + * management frame, etc.) having an associated Presence Indicator bit. This class is a wrapper + * around std::optional (most of its functions are exposed, more can be added if needed) that + * additionally sets the Presence Indicator flag appropriately when operations like reset or + * assignment of a value are performed on the optional field. + */ +template +class OptFieldWithPresenceInd +{ + public: + /// @brief constructor + /// @param presenceFlag the Presence Indicator flag + OptFieldWithPresenceInd(bool& presenceFlag); + + /// @brief Destroy the value (if any) contained in the optional field. + /// @return a reference to this object + constexpr OptFieldWithPresenceInd& operator=(std::nullopt_t); + + /// @brief Assign the given value to the optional field + /// @param other the given value + /// @return a reference to this object + constexpr OptFieldWithPresenceInd& operator=(const std::optional& other); + + /// @brief Assign the given value to the optional field + /// @param other the given value + /// @return a reference to this object + constexpr OptFieldWithPresenceInd& operator=(std::optional&& other); + + /// @brief Operator bool + /// @return whether this object contains a value + constexpr explicit operator bool() const; + + /// @brief Check whether this object contains a value + /// @return whether this object contains a value + constexpr bool has_value() const; + + /// @return a pointer to the contained value + constexpr const T* operator->() const; + + /// @return a pointer to the contained value + constexpr T* operator->(); + + /// @return a reference to the contained value + constexpr const T& operator*() const; + + /// @return a reference to the contained value + constexpr T& operator*(); + + /// @brief Construct the contained value in-place + /// @tparam Args the type of arguments to pass to the constructor + /// @param args the arguments to pass to the constructor + /// @return a reference to the new contained value + template + constexpr T& emplace(Args&&... args); + + /// @brief Destroy the value (if any) contained in the optional field + constexpr void reset(); + + private: + std::optional m_field; ///< the optional field + std::reference_wrapper m_presenceFlag; ///< the Presence Indicator flag +}; + +} // namespace ns3 + +/*************************************************************** + * Implementation of the templates declared above. + ***************************************************************/ + +namespace ns3 +{ + +template +OptFieldWithPresenceInd::OptFieldWithPresenceInd(bool& presenceFlag) + : m_presenceFlag(presenceFlag) +{ + m_presenceFlag.get() = false; +} + +template +constexpr OptFieldWithPresenceInd& +OptFieldWithPresenceInd::operator=(std::nullopt_t) +{ + m_field.reset(); + m_presenceFlag.get() = false; + return *this; +} + +template +constexpr OptFieldWithPresenceInd& +OptFieldWithPresenceInd::operator=(const std::optional& other) +{ + m_field = other; + m_presenceFlag.get() = other.has_value(); + return *this; +} + +template +constexpr OptFieldWithPresenceInd& +OptFieldWithPresenceInd::operator=(std::optional&& other) +{ + m_field = std::move(other); + m_presenceFlag.get() = other.has_value(); + return *this; +} + +template +constexpr OptFieldWithPresenceInd::operator bool() const +{ + return m_field.has_value(); +} + +template +constexpr bool +OptFieldWithPresenceInd::has_value() const +{ + return m_field.has_value(); +} + +template +constexpr const T* +OptFieldWithPresenceInd::operator->() const +{ + return &(*m_field); +} + +template +constexpr T* +OptFieldWithPresenceInd::operator->() +{ + return &(*m_field); +} + +template +constexpr const T& +OptFieldWithPresenceInd::operator*() const +{ + return *m_field; +} + +template +constexpr T& +OptFieldWithPresenceInd::operator*() +{ + return *m_field; +} + +template +template +constexpr T& +OptFieldWithPresenceInd::emplace(Args&&... args) +{ + m_field.emplace(std::forward(args)...); + m_presenceFlag.get() = true; + return *m_field; +} + +template +constexpr void +OptFieldWithPresenceInd::reset() +{ + m_field.reset(); + m_presenceFlag.get() = false; +} + +} // namespace ns3 + +#endif /* WIFI_OPT_FIELD_H */ diff --git a/utils/codespell-ignored-lines b/utils/codespell-ignored-lines index c6e2bf6c0..162925979 100644 --- a/utils/codespell-ignored-lines +++ b/utils/codespell-ignored-lines @@ -81,6 +81,7 @@ Network Animator nam. It is not possible to run a simulation * Copyright (c) 2022 Universita' degli Studi di Napoli Federico II * Copyright (c) 2022 Universita' degli Studi di Napoli "Federico II" * Copyright (c) 2023 Universita' degli Studi di Napoli Federico II + * Copyright (c) 2024 Universita' degli Studi di Napoli Federico II ./src/core/examples/hash-example.cc 6ed21de2 brand-newness peripherial