util: FaceUri: modernize and simplify code with C++11 features
Change-Id: Ie6a59567a635b6c69b970e9e1e50eafff14ea7fa
diff --git a/src/util/face-uri.cpp b/src/util/face-uri.cpp
index f38e8c2..c09da8a 100644
--- a/src/util/face-uri.cpp
+++ b/src/util/face-uri.cpp
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/**
- * Copyright (c) 2015-2016, Regents of the University of California,
+ * Copyright (c) 2014-2016, Regents of the University of California,
* Arizona Board of Regents,
* Colorado State University,
* University Pierre & Marie Curie, Sorbonne University,
@@ -27,13 +27,14 @@
#include "face-uri.hpp"
#include "dns.hpp"
+#include "ethernet.hpp"
-#include <set>
#include <boost/concept_check.hpp>
-#include <boost/regex.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/mpl/vector.hpp>
#include <boost/mpl/for_each.hpp>
+#include <boost/regex.hpp>
+#include <set>
namespace ndn {
namespace util {
@@ -53,10 +54,8 @@
}
FaceUri::FaceUri(const char* uri)
+ : FaceUri(std::string(uri))
{
- if (!parse(uri)) {
- BOOST_THROW_EXCEPTION(Error("Malformed URI: " + std::string(uri)));
- }
}
bool
@@ -64,9 +63,9 @@
{
m_scheme.clear();
m_host.clear();
- m_isV6 = false;
m_port.clear();
m_path.clear();
+ m_isV6 = false;
static const boost::regex protocolExp("(\\w+\\d?(\\+\\w+)?)://([^/]*)(\\/[^?]*)?");
boost::smatch protocolMatch;
@@ -124,19 +123,19 @@
}
FaceUri::FaceUri(const boost::asio::ip::tcp::endpoint& endpoint, const std::string& scheme)
- : m_scheme(scheme)
{
m_isV6 = endpoint.address().is_v6();
+ m_scheme = scheme;
m_host = endpoint.address().to_string();
m_port = to_string(endpoint.port());
}
#ifdef BOOST_ASIO_HAS_LOCAL_SOCKETS
FaceUri::FaceUri(const boost::asio::local::stream_protocol::endpoint& endpoint)
- : m_isV6(false)
+ : m_scheme("unix")
+ , m_path(endpoint.path())
+ , m_isV6(false)
{
- m_scheme = "unix";
- m_path = endpoint.path();
}
#endif // BOOST_ASIO_HAS_LOCAL_SOCKETS
@@ -150,10 +149,10 @@
}
FaceUri::FaceUri(const ethernet::Address& address)
- : m_isV6(true)
+ : m_scheme("ether")
+ , m_host(address.toString())
+ , m_isV6(true)
{
- m_scheme = "ether";
- m_host = address.toString();
}
FaceUri
@@ -178,11 +177,11 @@
bool
FaceUri::operator==(const FaceUri& rhs) const
{
- return (m_scheme == rhs.m_scheme &&
- m_host == rhs.m_host &&
- m_isV6 == rhs.m_isV6 &&
- m_port == rhs.m_port &&
- m_path == rhs.m_path);
+ return m_isV6 == rhs.m_isV6 &&
+ m_scheme == rhs.m_scheme &&
+ m_host == rhs.m_host &&
+ m_port == rhs.m_port &&
+ m_path == rhs.m_path;
}
bool
@@ -216,6 +215,7 @@
return os;
}
+
/** \brief a CanonizeProvider provides FaceUri canonization functionality for a group of schemes
*/
class CanonizeProvider : noncopyable
@@ -244,11 +244,7 @@
std::set<std::string>
getSchemes() const override
{
- std::set<std::string> schemes;
- schemes.insert(m_baseScheme);
- schemes.insert(m_v4Scheme);
- schemes.insert(m_v6Scheme);
- return schemes;
+ return {m_baseScheme, m_v4Scheme, m_v6Scheme};
}
bool
@@ -272,8 +268,8 @@
else {
return false;
}
- return !static_cast<bool>(ec) && addr.to_string() == faceUri.getHost() &&
- this->checkAddress(addr).first;
+
+ return !ec && addr.to_string() == faceUri.getHost() && checkAddress(addr).first;
}
void
@@ -300,7 +296,7 @@
}
// make a copy because caller may modify faceUri
- shared_ptr<FaceUri> uri = make_shared<FaceUri>(faceUri);
+ auto uri = make_shared<FaceUri>(faceUri);
dns::asyncResolve(faceUri.getHost(),
bind(&IpHostCanonizeProvider<Protocol>::onDnsSuccess, this, uri, onSuccess, onFailure, _1),
bind(&IpHostCanonizeProvider<Protocol>::onDnsFailure, this, uri, onFailure, _1),
@@ -313,25 +309,25 @@
uint16_t defaultUnicastPort = 6363,
uint16_t defaultMulticastPort = 56363)
: m_baseScheme(baseScheme)
- , m_v4Scheme(baseScheme + "4")
- , m_v6Scheme(baseScheme + "6")
+ , m_v4Scheme(baseScheme + '4')
+ , m_v6Scheme(baseScheme + '6')
, m_defaultUnicastPort(defaultUnicastPort)
, m_defaultMulticastPort(defaultMulticastPort)
{
}
private:
- // faceUri is a shared_ptr passed by value because this function can take ownership
void
- onDnsSuccess(shared_ptr<FaceUri> faceUri,
+ onDnsSuccess(const shared_ptr<FaceUri>& faceUri,
const FaceUri::CanonizeSuccessCallback& onSuccess,
const FaceUri::CanonizeFailureCallback& onFailure,
const dns::IpAddress& ipAddress) const
{
- std::pair<bool, std::string> checkAddressRes = this->checkAddress(ipAddress);
- if (!checkAddressRes.first) {
- onFailure(checkAddressRes.second);
- return;
+ bool isOk = false;
+ std::string reason;
+ std::tie(isOk, reason) = this->checkAddress(ipAddress);
+ if (!isOk) {
+ return onFailure(reason);
}
uint16_t port = 0;
@@ -343,8 +339,7 @@
port = boost::lexical_cast<uint16_t>(faceUri->getPort());
}
catch (const boost::bad_lexical_cast&) {
- onFailure("invalid port number");
- return;
+ return onFailure("invalid port number '" + faceUri->getPort() + "'");
}
}
@@ -353,9 +348,9 @@
onSuccess(canonicalUri);
}
- // faceUri is a shared_ptr passed by value because this function can take ownership
void
- onDnsFailure(shared_ptr<FaceUri> faceUri, const FaceUri::CanonizeFailureCallback& onFailure,
+ onDnsFailure(const shared_ptr<FaceUri>& faceUri,
+ const FaceUri::CanonizeFailureCallback& onFailure,
const std::string& reason) const
{
onFailure(reason);
@@ -418,9 +413,7 @@
std::set<std::string>
getSchemes() const override
{
- std::set<std::string> schemes;
- schemes.insert("ether");
- return schemes;
+ return {"ether"};
}
bool
@@ -433,7 +426,7 @@
return false;
}
- ethernet::Address addr = ethernet::Address::fromString(faceUri.getHost());
+ auto addr = ethernet::Address::fromString(faceUri.getHost());
return addr.toString() == faceUri.getHost();
}
@@ -443,10 +436,9 @@
const FaceUri::CanonizeFailureCallback& onFailure,
boost::asio::io_service& io, const time::nanoseconds& timeout) const override
{
- ethernet::Address addr = ethernet::Address::fromString(faceUri.getHost());
+ auto addr = ethernet::Address::fromString(faceUri.getHost());
if (addr.isNull()) {
- onFailure("cannot parse address");
- return;
+ return onFailure("invalid ethernet address '" + faceUri.getHost() + "'");
}
FaceUri canonicalUri(addr);
@@ -491,13 +483,11 @@
}
};
-typedef boost::mpl::vector<
- UdpCanonizeProvider*,
- TcpCanonizeProvider*,
- EtherCanonizeProvider*,
- UdpDevCanonizeProvider*
- > CanonizeProviders;
-typedef std::map<std::string, shared_ptr<CanonizeProvider> > CanonizeProviderTable;
+using CanonizeProviders = boost::mpl::vector<UdpCanonizeProvider*,
+ TcpCanonizeProvider*,
+ EtherCanonizeProvider*,
+ UdpDevCanonizeProvider*>;
+using CanonizeProviderTable = std::map<std::string, shared_ptr<CanonizeProvider>>;
class CanonizeProviderTableInitializer
{
@@ -508,17 +498,17 @@
{
}
- template<typename CP> void
+ template<typename CP>
+ void
operator()(CP*)
{
shared_ptr<CanonizeProvider> cp = make_shared<CP>();
-
- std::set<std::string> schemes = cp->getSchemes();
+ auto schemes = cp->getSchemes();
BOOST_ASSERT(!schemes.empty());
- for (std::set<std::string>::iterator it = schemes.begin();
- it != schemes.end(); ++it) {
- BOOST_ASSERT(m_providerTable.count(*it) == 0);
- m_providerTable[*it] = cp;
+
+ for (const auto& scheme : schemes) {
+ BOOST_ASSERT(m_providerTable.count(scheme) == 0);
+ m_providerTable[scheme] = cp;
}
}
@@ -536,23 +526,21 @@
}
auto it = providerTable.find(scheme);
- if (it == providerTable.end()) {
- return nullptr;
- }
- return it->second.get();
+ return it == providerTable.end() ? nullptr : it->second.get();
}
+
bool
FaceUri::canCanonize(const std::string& scheme)
{
- return getCanonizeProvider(scheme) != 0;
+ return getCanonizeProvider(scheme) != nullptr;
}
bool
FaceUri::isCanonical() const
{
const CanonizeProvider* cp = getCanonizeProvider(this->getScheme());
- if (cp == 0) {
+ if (cp == nullptr) {
return false;
}
@@ -574,7 +562,6 @@
static CanonizeSuccessCallback successNop = bind([]{});
static CanonizeFailureCallback failureNop = bind([]{});
-
cp->canonize(*this,
onSuccess ? onSuccess : successNop,
onFailure ? onFailure : failureNop,
diff --git a/src/util/face-uri.hpp b/src/util/face-uri.hpp
index b222323..f726388 100644
--- a/src/util/face-uri.hpp
+++ b/src/util/face-uri.hpp
@@ -1,12 +1,12 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/**
- * Copyright (c) 2014-2016, Regents of the University of California,
- * Arizona Board of Regents,
- * Colorado State University,
- * University Pierre & Marie Curie, Sorbonne University,
- * Washington University in St. Louis,
- * Beijing Institute of Technology,
- * The University of Memphis
+ * Copyright (c) 2014-2016, Regents of the University of California,
+ * Arizona Board of Regents,
+ * Colorado State University,
+ * University Pierre & Marie Curie, Sorbonne University,
+ * Washington University in St. Louis,
+ * Beijing Institute of Technology,
+ * The University of Memphis.
*
* This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
*
@@ -29,15 +29,19 @@
#define NDN_UTIL_FACE_URI_HPP
#include "../common.hpp"
-#include <boost/asio/ip/udp.hpp>
-#include <boost/asio/ip/tcp.hpp>
-#include <boost/asio/local/stream_protocol.hpp>
-#include "ethernet.hpp"
#include "time.hpp"
+#include <boost/asio/ip/tcp.hpp>
+#include <boost/asio/ip/udp.hpp>
+#include <boost/asio/local/stream_protocol.hpp>
+
namespace ndn {
namespace util {
+namespace ethernet {
+class Address;
+} // namespace ethernet
+
/** \brief represents the underlying protocol and address used by a Face
* \sa http://redmine.named-data.net/projects/nfd/wiki/FaceMgmt#FaceUri
*/
@@ -82,7 +86,7 @@
explicit
FaceUri(const boost::asio::ip::tcp::endpoint& endpoint);
- /// construct tcp canonical FaceUri with customized scheme
+ /// construct tcp canonical FaceUri with custom scheme
FaceUri(const boost::asio::ip::tcp::endpoint& endpoint, const std::string& scheme);
#ifdef BOOST_ASIO_HAS_LOCAL_SOCKETS
@@ -176,15 +180,16 @@
void
canonize(const CanonizeSuccessCallback& onSuccess,
const CanonizeFailureCallback& onFailure,
- boost::asio::io_service& io, const time::nanoseconds& timeout) const;
+ boost::asio::io_service& io,
+ const time::nanoseconds& timeout) const;
private:
std::string m_scheme;
std::string m_host;
- /// whether to add [] around host when writing string
- bool m_isV6;
std::string m_port;
std::string m_path;
+ /// whether to add [] around host when writing string
+ bool m_isV6;
friend std::ostream& operator<<(std::ostream& os, const FaceUri& uri);
};