Simplify some code with Boost.Operators

Change-Id: Ic873bcbaf6be00d5c35601cfc8090df534d815ee
This commit is contained in:
Davide Pesavento committed 2023-08-29 21:04:08 -04:00
1 parent 11f1fd8340
commit 8f0b8b6063
20 files changed
+98 -174

No files matched your search

+3 -7
View File
@@ -27,11 +27,13 @@
#define NFD_CORE_NETWORK_HPP
#include <boost/asio/ip/address.hpp>
#include <boost/operators.hpp>
#include <string_view>
namespace nfd {
class Network
class Network : private boost::equality_comparable<Network>
{
public:
Network();
@@ -62,12 +64,6 @@ private: // non-member operators
lhs.m_maxAddress == rhs.m_maxAddress;
}
friend bool
operator!=(const Network& lhs, const Network& rhs) noexcept
{
return !(lhs == rhs);
}
friend std::ostream&
operator<<(std::ostream& os, const Network& network);
+23 -18
View File
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
* Copyright (c) 2014-2022, Regents of the University of California,
* Copyright (c) 2014-2023, Regents of the University of California,
* Arizona Board of Regents,
* Colorado State University,
* University Pierre & Marie Curie, Sorbonne University,
@@ -34,15 +34,15 @@ namespace nfd {
* \brief Represents a counter that encloses an integer value.
*
* SimpleCounter is noncopyable, because increment should be called on the counter,
* not a copy of it; it's implicitly convertible to an integral type to be observed.
* not a copy of it. It's implicitly convertible to an integral type to be observed.
*/
class SimpleCounter : noncopyable
{
public:
typedef uint64_t rep;
using rep = uint64_t;
/**
* \brief Observe the counter's value.
* \brief Return the counter's value.
*/
operator rep() const noexcept
{
@@ -62,14 +62,16 @@ protected:
rep m_value = 0;
};
/** \brief Represents a counter of number of packets.
/**
* \brief Represents a counter of number of packets.
*
* \warning The counter value may wrap after exceeding the range of underlying integer type.
* \warning The counter value may wrap after exceeding the range of the underlying integer type.
*/
class PacketCounter : public SimpleCounter
{
public:
/** \brief Increment the counter by one.
/**
* \brief Increment the counter by one.
*/
PacketCounter&
operator++() noexcept
@@ -77,17 +79,18 @@ public:
++m_value;
return *this;
}
// postfix ++ operator is not provided because it's not needed
};
/** \brief Represents a counter of number of bytes.
/**
* \brief Represents a counter of number of bytes.
*
* \warning The counter value may wrap after exceeding the range of underlying integer type.
* \warning The counter value may wrap after exceeding the range of the underlying integer type.
*/
class ByteCounter : public SimpleCounter
{
public:
/** \brief Increase the counter.
/**
* \brief Increase the counter.
*/
ByteCounter&
operator+=(rep n) noexcept
@@ -97,16 +100,17 @@ public:
}
};
/** \brief Provides a counter that observes the size of a table.
* \tparam T a type that provides a size() const member function
/**
* \brief Provides a counter that observes the size of a table.
* \tparam T a type that provides a `size()` const member function
*
* if table not specified in constructor, it can be added later by invoking observe()
* If the table is not specified in the constructor, it can be added later by calling observe().
*/
template<typename T>
class SizeCounter : noncopyable
{
public:
typedef size_t Rep;
using rep = size_t;
explicit constexpr
SizeCounter(const T* table = nullptr) noexcept
@@ -120,16 +124,17 @@ public:
m_table = table;
}
/** \brief Observe the counter.
/**
* \brief Return the counter's value, i.e., the current size of the table being observed.
*/
operator Rep() const
operator rep() const
{
BOOST_ASSERT(m_table != nullptr);
return m_table->size();
}
private:
const T* m_table;
const T* m_table = nullptr;
};
} // namespace nfd
+3 -7
View File
@@ -28,12 +28,14 @@
#include "core/common.hpp"
#include <boost/operators.hpp>
#include <boost/property_tree/ptree_fwd.hpp>
#include <ndn-cxx/net/network-interface.hpp>
namespace nfd::face {
class NetworkPredicateBase
class NetworkPredicateBase : private boost::equality_comparable<NetworkPredicateBase>
{
public:
NetworkPredicateBase();
@@ -78,12 +80,6 @@ private: // non-member operators (hidden friends)
lhs.m_blacklist == rhs.m_blacklist;
}
friend bool
operator!=(const NetworkPredicateBase& lhs, const NetworkPredicateBase& rhs)
{
return !(lhs == rhs);
}
NFD_PUBLIC_WITH_TESTS_ELSE_PROTECTED:
std::set<std::string> m_whitelist;
std::set<std::string> m_blacklist;
+1 -5
View File
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
* Copyright (c) 2014-2022, Regents of the University of California,
* Copyright (c) 2014-2023, Regents of the University of California,
* Arizona Board of Regents,
* Colorado State University,
* University Pierre & Marie Curie, Sorbonne University,
@@ -28,12 +28,8 @@
#include "common/logger.hpp"
#include "face/channel.hpp"
#include <ndn-cxx/util/concepts.hpp>
namespace nfd {
NDN_CXX_ASSERT_FORWARD_ITERATOR(FaceTable::const_iterator);
NFD_LOG_INIT(FaceTable);
Face*
+3 -7
View File
@@ -28,12 +28,14 @@
#include "core/common.hpp"
#include <boost/operators.hpp>
namespace nfd::rib {
/**
* \brief Represents a FIB update.
*/
class FibUpdate
class FibUpdate : private boost::equality_comparable<FibUpdate>
{
public:
enum Action {
@@ -57,12 +59,6 @@ public: // non-member operators (hidden friends)
lhs.action == rhs.action;
}
friend bool
operator!=(const FibUpdate& lhs, const FibUpdate& rhs) noexcept
{
return !(lhs == rhs);
}
public:
Name name;
uint64_t faceId = 0;
+11 -12
View File
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
* Copyright (c) 2014-2022, Regents of the University of California,
* Copyright (c) 2014-2023, Regents of the University of California,
* Arizona Board of Regents,
* Colorado State University,
* University Pierre & Marie Curie, Sorbonne University,
@@ -33,7 +33,8 @@
namespace nfd::rib {
/** \brief State of a readvertised route.
/**
* \brief State of a readvertised route.
*/
class ReadvertisedRoute : noncopyable
{
@@ -41,25 +42,23 @@ public:
explicit
ReadvertisedRoute(const Name& prefix)
: prefix(prefix)
, nRibRoutes(0)
, retryDelay(0)
{
}
friend bool
operator<(const ReadvertisedRoute& lhs, const ReadvertisedRoute& rhs)
{
return lhs.prefix < rhs.prefix;
}
public:
Name prefix; ///< readvertised prefix
mutable ndn::security::SigningInfo signer; ///< signer for commands
mutable size_t nRibRoutes; ///< number of RIB routes that cause the readvertisement
mutable time::milliseconds retryDelay; ///< retry interval (not used for refresh)
mutable size_t nRibRoutes = 0; ///< number of RIB routes that cause the readvertisement
mutable time::milliseconds retryDelay = 0_ms; ///< retry interval (not used for refresh)
mutable scheduler::ScopedEventId retryEvt; ///< retry or refresh event
};
inline bool
operator<(const ReadvertisedRoute& lhs, const ReadvertisedRoute& rhs)
{
return lhs.prefix < rhs.prefix;
}
using ReadvertisedRouteContainer = std::set<ReadvertisedRoute>;
} // namespace nfd::rib
-7
View File
@@ -31,13 +31,6 @@ namespace nfd::rib {
NFD_LOG_INIT(Rib);
bool
operator<(const RibRouteRef& lhs, const RibRouteRef& rhs)
{
return std::tie(lhs.entry->getName(), lhs.route->faceId, lhs.route->origin) <
std::tie(rhs.entry->getName(), rhs.route->faceId, rhs.route->origin);
}
static inline bool
sortRoutes(const Route& lhs, const Route& rhs)
{
+10 -5
View File
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
* Copyright (c) 2014-2022, Regents of the University of California,
* Copyright (c) 2014-2023, Regents of the University of California,
* Arizona Board of Regents,
* Colorado State University,
* University Pierre & Marie Curie, Sorbonne University,
@@ -37,16 +37,21 @@ using ndn::nfd::ControlParameters;
class FibUpdater;
/** \brief References a route.
/**
* \brief References a route.
*/
struct RibRouteRef
{
shared_ptr<RibEntry> entry;
RibEntry::const_iterator route;
};
bool
operator<(const RibRouteRef& lhs, const RibRouteRef& rhs);
friend bool
operator<(const RibRouteRef& lhs, const RibRouteRef& rhs) noexcept
{
return std::tie(lhs.entry->getName(), lhs.route->faceId, lhs.route->origin) <
std::tie(rhs.entry->getName(), rhs.route->faceId, rhs.route->origin);
}
};
/**
* \brief Represents the Routing Information Base.
+1 -7
View File
@@ -40,7 +40,7 @@ namespace nfd::rib {
/**
* \brief Represents a route for a name prefix.
*/
class Route : public ndn::nfd::RouteFlagsTraits<Route>
class Route : public ndn::nfd::RouteFlagsTraits<Route>, private boost::equality_comparable<Route>
{
public:
/** \brief Default constructor.
@@ -89,12 +89,6 @@ public: // non-member operators (hidden friends)
lhs.announcement == rhs.announcement;
}
friend bool
operator!=(const Route& lhs, const Route& rhs)
{
return !(lhs == rhs);
}
public:
uint64_t faceId = 0;
ndn::nfd::RouteOrigin origin = ndn::nfd::ROUTE_ORIGIN_APP;
+1 -5
View File
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
* Copyright (c) 2014-2022, Regents of the University of California,
* Copyright (c) 2014-2023, Regents of the University of California,
* Arizona Board of Regents,
* Colorado State University,
* University Pierre & Marie Curie, Sorbonne University,
@@ -27,12 +27,8 @@
#include "pit-entry.hpp"
#include "measurements-entry.hpp"
#include <ndn-cxx/util/concepts.hpp>
namespace nfd::fib {
NDN_CXX_ASSERT_FORWARD_ITERATOR(Fib::const_iterator);
const unique_ptr<Entry> Fib::s_emptyEntry = make_unique<Entry>(Name());
static inline bool
-14
View File
@@ -27,14 +27,8 @@
#include "name-tree.hpp"
#include "common/logger.hpp"
#include <boost/range/concepts.hpp>
#include <ndn-cxx/util/concepts.hpp>
namespace nfd::name_tree {
NDN_CXX_ASSERT_FORWARD_ITERATOR(Iterator);
BOOST_CONCEPT_ASSERT((boost::ForwardRangeConcept<Range>));
NFD_LOG_INIT(NameTreeIterator);
Iterator::Iterator() = default;
@@ -56,14 +50,6 @@ Iterator::operator++()
return *this;
}
Iterator
Iterator::operator++(int)
{
Iterator copy = *this;
this->operator++();
return copy;
}
std::ostream&
operator<<(std::ostream& os, const Iterator& i)
{
+2 -23
View File
@@ -28,6 +28,7 @@
#include "name-tree-hashtable.hpp"
#include <boost/operators.hpp>
#include <boost/range/iterator_range_core.hpp>
namespace nfd::name_tree {
@@ -74,15 +75,9 @@ class EnumerationImpl;
/**
* \brief NameTree iterator.
*/
class Iterator
class Iterator : public boost::forward_iterator_helper<Iterator, const Entry>
{
public:
using iterator_category = std::forward_iterator_tag;
using value_type = const Entry;
using difference_type = std::ptrdiff_t;
using pointer = value_type*;
using reference = value_type&;
Iterator();
Iterator(shared_ptr<EnumerationImpl> impl, const Entry* ref);
@@ -94,31 +89,15 @@ public:
return *m_entry;
}
const Entry*
operator->() const noexcept
{
BOOST_ASSERT(m_impl != nullptr);
return m_entry;
}
Iterator&
operator++();
Iterator
operator++(int);
friend bool
operator==(const Iterator& lhs, const Iterator& rhs) noexcept
{
return lhs.m_entry == rhs.m_entry;
}
friend bool
operator!=(const Iterator& lhs, const Iterator& rhs) noexcept
{
return !(lhs == rhs);
}
private:
/** \brief Enumeration implementation; nullptr for end iterator.
*/
+1 -20
View File
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
* Copyright (c) 2014-2022, Regents of the University of California,
* Copyright (c) 2014-2023, Regents of the University of California,
* Arizona Board of Regents,
* Colorado State University,
* University Pierre & Marie Curie, Sorbonne University,
@@ -25,18 +25,8 @@
#include "pit-iterator.hpp"
#include <ndn-cxx/util/concepts.hpp>
namespace nfd::pit {
NDN_CXX_ASSERT_FORWARD_ITERATOR(Iterator);
Iterator::Iterator(const NameTree::const_iterator& ntIt, size_t iPitEntry)
: m_ntIt(ntIt)
, m_iPitEntry(iPitEntry)
{
}
Iterator&
Iterator::operator++()
{
@@ -48,16 +38,7 @@ Iterator::operator++()
m_iPitEntry = 0;
BOOST_ASSERT(m_ntIt == NameTree::const_iterator() || m_ntIt->hasPitEntries());
}
return *this;
}
Iterator
Iterator::operator++(int)
{
Iterator copy = *this;
this->operator++();
return copy;
}
} // namespace nfd::pit
+11 -27
View File
@@ -34,42 +34,32 @@ namespace nfd::pit {
/**
* \brief PIT iterator.
*/
class Iterator
class Iterator : public boost::forward_iterator_helper<Iterator, const Entry>
{
public:
using iterator_category = std::forward_iterator_tag;
using value_type = const Entry;
using difference_type = std::ptrdiff_t;
using pointer = value_type*;
using reference = value_type&;
/** \brief Constructor.
* \param ntIt a name tree iterator that visits name tree entries with one or more PIT entries
* \param iPitEntry make this iterator to dereference to the i-th PIT entry in name tree entry
/**
* \brief Constructor.
* \param ntIt a name tree iterator that visits name tree entries with one or more PIT entries
* \param iPitEntry make this iterator to dereference to the i-th PIT entry in name tree entry
*/
explicit
Iterator(const NameTree::const_iterator& ntIt = {}, size_t iPitEntry = 0);
const Entry&
operator*() const
Iterator(const NameTree::const_iterator& ntIt = {}, size_t iPitEntry = 0)
: m_ntIt(ntIt)
, m_iPitEntry(iPitEntry)
{
return *this->operator->();
}
const shared_ptr<Entry>&
operator->() const
const Entry&
operator*() const noexcept
{
BOOST_ASSERT(m_ntIt != NameTree::const_iterator());
BOOST_ASSERT(m_iPitEntry < m_ntIt->getPitEntries().size());
return m_ntIt->getPitEntries()[m_iPitEntry];
return *m_ntIt->getPitEntries()[m_iPitEntry];
}
Iterator&
operator++();
Iterator
operator++(int);
friend bool
operator==(const Iterator& lhs, const Iterator& rhs) noexcept
{
@@ -77,12 +67,6 @@ public:
lhs.m_iPitEntry == rhs.m_iPitEntry;
}
friend bool
operator!=(const Iterator& lhs, const Iterator& rhs) noexcept
{
return !(lhs == rhs);
}
private:
NameTree::const_iterator m_ntIt; ///< current name tree entry
size_t m_iPitEntry; ///< current PIT entry within m_ntIt->getPitEntries()
+1 -5
View File
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
* Copyright (c) 2014-2022, Regents of the University of California,
* Copyright (c) 2014-2023, Regents of the University of California,
* Arizona Board of Regents,
* Colorado State University,
* University Pierre & Marie Curie, Sorbonne University,
@@ -30,12 +30,8 @@
#include "common/logger.hpp"
#include "fw/strategy.hpp"
#include <ndn-cxx/util/concepts.hpp>
namespace nfd::strategy_choice {
NDN_CXX_ASSERT_FORWARD_ITERATOR(StrategyChoice::const_iterator);
NFD_LOG_INIT(StrategyChoice);
using fw::Strategy;
+5 -1
View File
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
* Copyright (c) 2014-2022, Regents of the University of California,
* Copyright (c) 2014-2023, Regents of the University of California,
* Arizona Board of Regents,
* Colorado State University,
* University Pierre & Marie Curie, Sorbonne University,
@@ -29,8 +29,12 @@
#include "tests/daemon/global-io-fixture.hpp"
#include "tests/daemon/face/dummy-face.hpp"
#include <ndn-cxx/util/concepts.hpp>
namespace nfd::tests {
NDN_CXX_ASSERT_FORWARD_ITERATOR(FaceTable::const_iterator);
BOOST_AUTO_TEST_SUITE(Fw)
BOOST_FIXTURE_TEST_SUITE(TestFaceTable, GlobalIoFixture)
+5 -1
View File
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
* Copyright (c) 2014-2022, Regents of the University of California,
* Copyright (c) 2014-2023, Regents of the University of California,
* Arizona Board of Regents,
* Colorado State University,
* University Pierre & Marie Curie, Sorbonne University,
@@ -31,10 +31,14 @@
#include "tests/daemon/global-io-fixture.hpp"
#include "tests/daemon/face/dummy-face.hpp"
#include <ndn-cxx/util/concepts.hpp>
namespace nfd::tests {
using namespace nfd::fib;
NDN_CXX_ASSERT_FORWARD_ITERATOR(Fib::const_iterator);
BOOST_AUTO_TEST_SUITE(Table)
BOOST_FIXTURE_TEST_SUITE(TestFib, GlobalIoFixture)
+7 -1
View File
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
* Copyright (c) 2014-2022, Regents of the University of California,
* Copyright (c) 2014-2023, Regents of the University of California,
* Arizona Board of Regents,
* Colorado State University,
* University Pierre & Marie Curie, Sorbonne University,
@@ -30,10 +30,16 @@
#include <unordered_set>
#include <boost/range/concepts.hpp>
#include <ndn-cxx/util/concepts.hpp>
namespace nfd::tests {
using namespace nfd::name_tree;
NDN_CXX_ASSERT_FORWARD_ITERATOR(NameTree::const_iterator);
BOOST_CONCEPT_ASSERT((boost::ForwardRangeConcept<Range>));
BOOST_AUTO_TEST_SUITE(Table)
BOOST_FIXTURE_TEST_SUITE(TestNameTree, GlobalIoFixture)
+5 -1
View File
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
* Copyright (c) 2014-2022, Regents of the University of California,
* Copyright (c) 2014-2023, Regents of the University of California,
* Arizona Board of Regents,
* Colorado State University,
* University Pierre & Marie Curie, Sorbonne University,
@@ -28,10 +28,14 @@
#include "tests/test-common.hpp"
#include "tests/daemon/global-io-fixture.hpp"
#include <ndn-cxx/util/concepts.hpp>
namespace nfd::tests {
using namespace nfd::pit;
NDN_CXX_ASSERT_FORWARD_ITERATOR(Pit::const_iterator);
BOOST_AUTO_TEST_SUITE(Table)
BOOST_FIXTURE_TEST_SUITE(TestPit, GlobalIoFixture)
+5 -1
View File
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
* Copyright (c) 2014-2022, Regents of the University of California,
* Copyright (c) 2014-2023, Regents of the University of California,
* Arizona Board of Regents,
* Colorado State University,
* University Pierre & Marie Curie, Sorbonne University,
@@ -29,8 +29,12 @@
#include "tests/daemon/global-io-fixture.hpp"
#include "tests/daemon/fw/dummy-strategy.hpp"
#include <ndn-cxx/util/concepts.hpp>
namespace nfd::tests {
NDN_CXX_ASSERT_FORWARD_ITERATOR(StrategyChoice::const_iterator);
class StrategyChoiceFixture : public GlobalIoFixture
{
protected: