Skip to content

Commit

Permalink
Simplify some code with Boost.Operators
Browse files Browse the repository at this point in the history
Change-Id: Ic873bcbaf6be00d5c35601cfc8090df534d815ee
  • Loading branch information
Pesa committed Aug 30, 2023
1 parent 11f1fd8 commit 8f0b8b6
Show file tree
Hide file tree
Showing 20 changed files with 98 additions and 174 deletions.
10 changes: 3 additions & 7 deletions core/network.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -62,12 +64,6 @@ class Network
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);

Expand Down
41 changes: 23 additions & 18 deletions daemon/common/counter.hpp
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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
{
Expand All @@ -62,32 +62,35 @@ class SimpleCounter : noncopyable
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
{
++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
Expand All @@ -97,16 +100,17 @@ class ByteCounter : public SimpleCounter
}
};

/** \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
Expand All @@ -120,16 +124,17 @@ class SizeCounter : noncopyable
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
Expand Down
10 changes: 3 additions & 7 deletions daemon/face/network-predicate.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -78,12 +80,6 @@ class NetworkPredicateBase
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;
Expand Down
6 changes: 1 addition & 5 deletions daemon/fw/face-table.cpp
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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*
Expand Down
10 changes: 3 additions & 7 deletions daemon/rib/fib-update.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -57,12 +59,6 @@ class FibUpdate
lhs.action == rhs.action;
}

friend bool
operator!=(const FibUpdate& lhs, const FibUpdate& rhs) noexcept
{
return !(lhs == rhs);
}

public:
Name name;
uint64_t faceId = 0;
Expand Down
23 changes: 11 additions & 12 deletions daemon/rib/readvertise/readvertised-route.hpp
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -33,33 +33,32 @@

namespace nfd::rib {

/** \brief State of a readvertised route.
/**
* \brief State of a readvertised route.
*/
class ReadvertisedRoute : noncopyable
{
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
Expand Down
7 changes: 0 additions & 7 deletions daemon/rib/rib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
15 changes: 10 additions & 5 deletions daemon/rib/rib.hpp
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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.
Expand Down
8 changes: 1 addition & 7 deletions daemon/rib/route.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -89,12 +89,6 @@ class Route : public ndn::nfd::RouteFlagsTraits<Route>
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;
Expand Down
6 changes: 1 addition & 5 deletions daemon/table/fib.cpp
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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
Expand Down
14 changes: 0 additions & 14 deletions daemon/table/name-tree-iterator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)
{
Expand Down
Loading

0 comments on commit 8f0b8b6

Please sign in to comment.