From 7c73228e78b5f23025c153ead93a850ae50105ab Mon Sep 17 00:00:00 2001 From: Stefano Avallone Date: Mon, 18 Oct 2021 20:02:45 +0200 Subject: [PATCH] network: Remove begin() and end() from Queue class begin() and end() are not necessarily meaningful for all the containers. A GetContainer() method returning a const reference to the container is added, so that begin() and end() can be called through it. --- src/network/utils/drop-tail-queue.h | 11 ++-- src/network/utils/queue.h | 94 +++------------------------- src/wifi/model/wifi-mac-queue.cc | 56 ++++++++--------- src/wifi/model/wifi-mac-queue.h | 3 +- src/wifi/test/wifi-mac-ofdma-test.cc | 2 +- src/wifi/test/wifi-mac-queue-test.cc | 4 +- 6 files changed, 47 insertions(+), 123 deletions(-) diff --git a/src/network/utils/drop-tail-queue.h b/src/network/utils/drop-tail-queue.h index 7096937f1..5c28c65ca 100644 --- a/src/network/utils/drop-tail-queue.h +++ b/src/network/utils/drop-tail-queue.h @@ -52,8 +52,7 @@ public: virtual Ptr Peek (void) const; private: - using Queue::begin; - using Queue::end; + using Queue::GetContainer; using Queue::DoEnqueue; using Queue::DoDequeue; using Queue::DoRemove; @@ -105,7 +104,7 @@ DropTailQueue::Enqueue (Ptr item) { NS_LOG_FUNCTION (this << item); - return DoEnqueue (end (), item); + return DoEnqueue (GetContainer ().end (), item); } template @@ -114,7 +113,7 @@ DropTailQueue::Dequeue (void) { NS_LOG_FUNCTION (this); - Ptr item = DoDequeue (begin ()); + Ptr item = DoDequeue (GetContainer ().begin ()); NS_LOG_LOGIC ("Popped " << item); @@ -127,7 +126,7 @@ DropTailQueue::Remove (void) { NS_LOG_FUNCTION (this); - Ptr item = DoRemove (begin ()); + Ptr item = DoRemove (GetContainer ().begin ()); NS_LOG_LOGIC ("Removed " << item); @@ -140,7 +139,7 @@ DropTailQueue::Peek (void) const { NS_LOG_FUNCTION (this); - return DoPeek (begin ()); + return DoPeek (GetContainer ().begin ()); } // The following explicit template instantiation declarations prevent all the diff --git a/src/network/utils/queue.h b/src/network/utils/queue.h index b9952af59..d253a3125 100644 --- a/src/network/utils/queue.h +++ b/src/network/utils/queue.h @@ -313,68 +313,11 @@ protected: typedef typename Container::iterator Iterator; /** - * \brief Get a const iterator which refers to the first item in the queue. + * Get a const reference to the container of queue items. * - * Subclasses can browse the items in the queue by using a const iterator - * - * \code - * for (auto i = begin (); i != end (); ++i) - * { - * (*i)->method (); // some const method of the Item class - * } - * \endcode - * - * \returns a const iterator which refers to the first item in the queue. + * \return a const reference to the container of queue items */ - ConstIterator begin (void) const; - - /** - * \brief Get an iterator which refers to the first item in the queue. - * - * Subclasses can browse the items in the queue by using an iterator - * - * \code - * for (auto i = begin (); i != end (); ++i) - * { - * (*i)->method (); // some method of the Item class - * } - * \endcode - * - * \returns an iterator which refers to the first item in the queue. - */ - Iterator begin (void); - - /** - * \brief Get a const iterator which indicates past-the-last item in the queue. - * - * Subclasses can browse the items in the queue by using a const iterator - * - * \code - * for (auto i = begin (); i != end (); ++i) - * { - * (*i)->method (); // some const method of the Item class - * } - * \endcode - * - * \returns a const iterator which indicates past-the-last item in the queue. - */ - ConstIterator end (void) const; - - /** - * \brief Get an iterator which indicates past-the-last item in the queue. - * - * Subclasses can browse the items in the queue by using an iterator - * - * \code - * for (auto i = begin (); i != end (); ++i) - * { - * (*i)->method (); // some method of the Item class - * } - * \endcode - * - * \returns an iterator which indicates past-the-last item in the queue. - */ - Iterator end (void); + const Container& GetContainer (void) const; /** * Push an item in the queue @@ -500,6 +443,13 @@ Queue::~Queue () { } +template +const Container& +Queue::GetContainer (void) const +{ + return m_packets; +} + template bool Queue::DoEnqueue (ConstIterator pos, Ptr item) @@ -632,30 +582,6 @@ Queue::DoPeek (ConstIterator pos) const return *pos; } -template -typename Queue::ConstIterator Queue::begin (void) const -{ - return m_packets.cbegin (); -} - -template -typename Queue::Iterator Queue::begin (void) -{ - return m_packets.begin (); -} - -template -typename Queue::ConstIterator Queue::end (void) const -{ - return m_packets.cend (); -} - -template -typename Queue::Iterator Queue::end (void) -{ - return m_packets.end (); -} - template void Queue::DropBeforeEnqueue (Ptr item) diff --git a/src/wifi/model/wifi-mac-queue.cc b/src/wifi/model/wifi-mac-queue.cc index 33dbbfcc9..3a31110de 100644 --- a/src/wifi/model/wifi-mac-queue.cc +++ b/src/wifi/model/wifi-mac-queue.cc @@ -115,7 +115,7 @@ WifiMacQueue::Enqueue (Ptr item) { NS_LOG_FUNCTION (this << *item); - return Insert (end (), item); + return Insert (GetContainer ().cend (), item); } bool @@ -123,7 +123,7 @@ WifiMacQueue::PushFront (Ptr item) { NS_LOG_FUNCTION (this << *item); - return Insert (begin (), item); + return Insert (GetContainer ().cbegin (), item); } bool @@ -140,9 +140,9 @@ WifiMacQueue::Insert (ConstIterator pos, Ptr item) } // the queue is full; scan the list in the attempt to remove stale packets - ConstIterator it = begin (); + ConstIterator it = GetContainer ().cbegin (); const Time now = Simulator::Now (); - while (it != end ()) + while (it != GetContainer ().cend ()) { if (it == pos && TtlExceeded (it, now)) { @@ -159,15 +159,15 @@ WifiMacQueue::Insert (ConstIterator pos, Ptr item) if (m_dropPolicy == DROP_OLDEST) { NS_LOG_DEBUG ("Remove the oldest item in the queue"); - if (pos == begin ()) + if (pos == GetContainer ().cbegin ()) { // Avoid invalidating pos - DoRemove (begin ()); - pos = begin (); + DoRemove (GetContainer ().cbegin ()); + pos = GetContainer ().cbegin (); } else { - DoRemove (begin ()); + DoRemove (GetContainer ().cbegin ()); } } @@ -179,7 +179,7 @@ WifiMacQueue::Dequeue (void) { NS_LOG_FUNCTION (this); const Time now = Simulator::Now (); - for (ConstIterator it = begin (); it != end (); ) + for (ConstIterator it = GetContainer ().cbegin (); it != GetContainer ().cend (); ) { if (!TtlExceeded (it, now)) { @@ -209,7 +209,7 @@ WifiMacQueue::Peek (void) const { NS_LOG_FUNCTION (this); const Time now = Simulator::Now (); - for (auto it = begin (); it != end (); it++) + for (auto it = GetContainer ().cbegin (); it != GetContainer ().cend (); it++) { // skip packets that stayed in the queue for too long. They will be // actually removed from the queue by the next call to a non-const method @@ -228,9 +228,9 @@ WifiMacQueue::PeekByAddress (Mac48Address dest, Ptr item NS_LOG_FUNCTION (this << dest << item); NS_ASSERT (item == nullptr || item->IsQueued ()); - ConstIterator it = (item != nullptr ? std::next (item->m_queueIt) : begin ()); + ConstIterator it = (item != nullptr ? std::next (item->m_queueIt) : GetContainer ().cbegin ()); const Time now = Simulator::Now (); - while (it != end ()) + while (it != GetContainer ().cend ()) { // skip packets that stayed in the queue for too long. They will be // actually removed from the queue by the next call to a non-const method @@ -254,9 +254,9 @@ WifiMacQueue::PeekByTid (uint8_t tid, Ptr item) const NS_LOG_FUNCTION (this << +tid << item); NS_ASSERT (item == nullptr || item->IsQueued ()); - ConstIterator it = (item != nullptr ? std::next (item->m_queueIt) : begin ()); + ConstIterator it = (item != nullptr ? std::next (item->m_queueIt) : GetContainer ().cbegin ()); const Time now = Simulator::Now (); - while (it != end ()) + while (it != GetContainer ().cend ()) { // skip packets that stayed in the queue for too long. They will be // actually removed from the queue by the next call to a non-const method @@ -279,9 +279,9 @@ WifiMacQueue::PeekByTidAndAddress (uint8_t tid, Mac48Address dest, PtrIsQueued ()); - ConstIterator it = (item != nullptr ? std::next (item->m_queueIt) : begin ()); + ConstIterator it = (item != nullptr ? std::next (item->m_queueIt) : GetContainer ().cbegin ()); const Time now = Simulator::Now (); - while (it != end ()) + while (it != GetContainer ().cend ()) { // skip packets that stayed in the queue for too long. They will be // actually removed from the queue by the next call to a non-const method @@ -306,9 +306,9 @@ WifiMacQueue::PeekFirstAvailable (const Ptr blockedPacke NS_LOG_FUNCTION (this << item); NS_ASSERT (item == nullptr || item->IsQueued ()); - ConstIterator it = (item != nullptr ? std::next (item->m_queueIt) : begin ()); + ConstIterator it = (item != nullptr ? std::next (item->m_queueIt) : GetContainer ().cbegin ()); const Time now = Simulator::Now (); - while (it != end ()) + while (it != GetContainer ().cend ()) { // skip packets that stayed in the queue for too long. They will be // actually removed from the queue by the next call to a non-const method @@ -332,7 +332,7 @@ WifiMacQueue::Remove (void) NS_LOG_FUNCTION (this); const Time now = Simulator::Now (); - for (ConstIterator it = begin (); it != end (); ) + for (ConstIterator it = GetContainer ().cbegin (); it != GetContainer ().cend (); ) { if (!TtlExceeded (it, now)) { @@ -353,20 +353,20 @@ WifiMacQueue::Remove (Ptr item, bool removeExpired) { ConstIterator next = std::next (item->m_queueIt); DoRemove (item->m_queueIt); - return (next == end () ? nullptr : *next); + return (next == GetContainer ().cend () ? nullptr : *next); } const Time now = Simulator::Now (); // remove stale items queued before the given position - ConstIterator it = begin (); - while (it != end ()) + ConstIterator it = GetContainer ().cbegin (); + while (it != GetContainer ().cend ()) { if (*it == item) { ConstIterator next = std::next (item->m_queueIt); DoRemove (item->m_queueIt); - return (next == end () ? nullptr : *next); + return (next == GetContainer ().cend () ? nullptr : *next); } else if (!TtlExceeded (it, now)) { @@ -402,7 +402,7 @@ WifiMacQueue::GetNPacketsByAddress (Mac48Address dest) uint32_t nPackets = 0; const Time now = Simulator::Now (); - for (ConstIterator it = begin (); it != end (); ) + for (ConstIterator it = GetContainer ().cbegin (); it != GetContainer ().cend (); ) { if (!TtlExceeded (it, now)) { @@ -425,7 +425,7 @@ WifiMacQueue::GetNPacketsByTidAndAddress (uint8_t tid, Mac48Address dest) uint32_t nPackets = 0; const Time now = Simulator::Now (); - for (ConstIterator it = begin (); it != end (); ) + for (ConstIterator it = GetContainer ().cbegin (); it != GetContainer ().cend (); ) { if (!TtlExceeded (it, now)) { @@ -448,7 +448,7 @@ WifiMacQueue::IsEmpty (void) NS_LOG_FUNCTION (this); const Time now = Simulator::Now (); - for (ConstIterator it = begin (); it != end (); ) + for (ConstIterator it = GetContainer ().cbegin (); it != GetContainer ().cend (); ) { if (!TtlExceeded (it, now)) { @@ -467,7 +467,7 @@ WifiMacQueue::GetNPackets (void) const Time now = Simulator::Now (); // remove packets that stayed in the queue for too long - for (ConstIterator it = begin (); it != end (); ) + for (ConstIterator it = GetContainer ().cbegin (); it != GetContainer ().cend (); ) { if (!TtlExceeded (it, now)) { @@ -484,7 +484,7 @@ WifiMacQueue::GetNBytes (void) const Time now = Simulator::Now (); // remove packets that stayed in the queue for too long - for (ConstIterator it = begin (); it != end (); ) + for (ConstIterator it = GetContainer ().cbegin (); it != GetContainer ().cend (); ) { if (!TtlExceeded (it, now)) { diff --git a/src/wifi/model/wifi-mac-queue.h b/src/wifi/model/wifi-mac-queue.h index 0e9d0199f..9cb8db9fe 100644 --- a/src/wifi/model/wifi-mac-queue.h +++ b/src/wifi/model/wifi-mac-queue.h @@ -85,8 +85,7 @@ public: /// allow the usage of iterators and const iterators using Queue::ConstIterator; using Queue::Iterator; - using Queue::begin; - using Queue::end; + using Queue::GetContainer; /** * Set the maximum delay before the packet is discarded. diff --git a/src/wifi/test/wifi-mac-ofdma-test.cc b/src/wifi/test/wifi-mac-ofdma-test.cc index 02cae2e88..fd5e6e81e 100644 --- a/src/wifi/test/wifi-mac-ofdma-test.cc +++ b/src/wifi/test/wifi-mac-ofdma-test.cc @@ -480,7 +480,7 @@ OfdmaAckSequenceTest::Transmit (std::string context, WifiConstPsduMap psduMap, W auto dev = DynamicCast (m_apDevice); Ptr queue = dev->GetMac ()->GetQosTxop (AC_BE)->GetWifiMacQueue (); m_flushed = 0; - for (auto it = queue->begin (); it != queue->end (); ) + for (auto it = queue->GetContainer ().begin (); it != queue->GetContainer ().end (); ) { auto tmp = it++; if (!(*tmp)->IsInFlight ()) diff --git a/src/wifi/test/wifi-mac-queue-test.cc b/src/wifi/test/wifi-mac-queue-test.cc index 8ed3eadfd..d8a987737 100644 --- a/src/wifi/test/wifi-mac-queue-test.cc +++ b/src/wifi/test/wifi-mac-queue-test.cc @@ -72,7 +72,7 @@ WifiMacQueueDropOldestTest::DoRun () } // Check that all elements are inserted successfully. - auto it = wifiMacQueue->begin (); + auto it = wifiMacQueue->GetContainer ().begin (); NS_TEST_EXPECT_MSG_EQ (wifiMacQueue->GetNPackets (), 5, "Queue has unexpected number of elements"); for (uint32_t i = 5; i > 0; i--) { @@ -94,7 +94,7 @@ WifiMacQueueDropOldestTest::DoRun () packetUids.at (4) = packet->GetUid (); // Check that front packet was replaced correctly. - it = wifiMacQueue->begin (); + it = wifiMacQueue->GetContainer ().begin (); NS_TEST_EXPECT_MSG_EQ (wifiMacQueue->GetNPackets (), 5, "Queue has unexpected number of elements"); for (uint32_t i = 5; i > 0; i--) {