face: use lambda expression and smart pointers
Refs: #2112
Change-Id: I73aab91b0b6841c55ef2892493901a13b7382cdd
diff --git a/src/detail/face-impl.hpp b/src/detail/face-impl.hpp
index f6d7d91..c13906a 100644
--- a/src/detail/face-impl.hpp
+++ b/src/detail/face-impl.hpp
@@ -218,13 +218,12 @@
unregistrator = static_cast<Registrator>(&Controller::start<FibRemoveNextHopCommand>);
}
- RegisteredPrefix::Unregistrator bindedUnregistrator =
- std::bind(unregistrator, m_face.m_nfdController, unregisterParameters, _1, _2,
+ RegisteredPrefix::Unregistrator boundUnregistrator =
+ bind(unregistrator, m_face.m_nfdController.get(), unregisterParameters, _1, _2,
options);
- // @todo get rid of "std::" after #2109
shared_ptr<RegisteredPrefix> prefixToRegister =
- make_shared<RegisteredPrefix>(prefix, filter, bindedUnregistrator);
+ make_shared<RegisteredPrefix>(prefix, filter, boundUnregistrator);
((*m_face.m_nfdController).*registrator)(registerParameters,
bind(&Impl::afterPrefixRegistered, this,
@@ -267,7 +266,6 @@
// it was a combined operation
m_interestFilterTable.remove(filter);
}
-
(*i)->unregister(bind(&Impl::finalizeUnregisterPrefix, this, i, onSuccess),
bind(onFailure, _2));
}
diff --git a/src/face.cpp b/src/face.cpp
index 744ee0d..b36f955 100644
--- a/src/face.cpp
+++ b/src/face.cpp
@@ -39,7 +39,7 @@
, m_isDirectNfdFibManagementRequested(false)
, m_impl(new Impl(*this))
{
- construct(m_internalKeyChain);
+ construct(*m_internalKeyChain);
}
Face::Face(boost::asio::io_service& ioService)
@@ -48,7 +48,7 @@
, m_isDirectNfdFibManagementRequested(false)
, m_impl(new Impl(*this))
{
- construct(m_internalKeyChain);
+ construct(*m_internalKeyChain);
}
Face::Face(const std::string& host, const std::string& port/* = "6363"*/)
@@ -57,8 +57,7 @@
, m_internalKeyChain(new KeyChain())
, m_impl(new Impl(*this))
{
- construct(make_shared<TcpTransport>(host, port),
- m_internalKeyChain);
+ construct(make_shared<TcpTransport>(host, port), *m_internalKeyChain);
}
Face::Face(const shared_ptr<Transport>& transport)
@@ -68,8 +67,7 @@
, m_isDirectNfdFibManagementRequested(false)
, m_impl(new Impl(*this))
{
- construct(transport,
- m_internalKeyChain);
+ construct(transport, *m_internalKeyChain);
}
Face::Face(const shared_ptr<Transport>& transport,
@@ -79,8 +77,7 @@
, m_isDirectNfdFibManagementRequested(false)
, m_impl(new Impl(*this))
{
- construct(transport,
- m_internalKeyChain);
+ construct(transport, *m_internalKeyChain);
}
Face::Face(shared_ptr<Transport> transport,
@@ -91,12 +88,11 @@
, m_isDirectNfdFibManagementRequested(false)
, m_impl(new Impl(*this))
{
- construct(transport,
- &keyChain);
+ construct(transport, keyChain);
}
void
-Face::construct(KeyChain* keyChain)
+Face::construct(KeyChain& keyChain)
{
// transport=unix:///var/run/nfd.sock
// transport=tcp://localhost:6363
@@ -121,7 +117,6 @@
throw ConfigFile::Error(error.what());
}
- shared_ptr<Transport> transport;
const std::string protocol = uri->getScheme();
if (protocol == "unix")
@@ -140,10 +135,9 @@
}
void
-Face::construct(shared_ptr<Transport> transport,
- KeyChain* keyChain)
+Face::construct(shared_ptr<Transport> transport, KeyChain& keyChain)
{
- m_nfdController = new nfd::Controller(*this, *keyChain);
+ m_nfdController.reset(new nfd::Controller(*this, keyChain));
m_impl->m_pitTimeoutCheckTimerActive = false;
m_transport = transport;
@@ -181,15 +175,7 @@
}
}
-Face::~Face()
-{
- if (m_internalKeyChain != nullptr) {
- delete m_internalKeyChain;
- }
-
- delete m_nfdController;
- delete m_impl;
-}
+Face::~Face() = default;
const PendingInterestId*
Face::expressInterest(const Interest& interest, const OnData& onData, const OnTimeout& onTimeout)
@@ -201,8 +187,7 @@
throw Error("Interest size exceeds maximum limit");
// If the same ioService thread, dispatch directly calls the method
- m_ioService.dispatch(bind(&Impl::asyncExpressInterest, m_impl,
- interestToExpress, onData, onTimeout));
+ m_ioService.dispatch([=] { m_impl->asyncExpressInterest(interestToExpress, onData, onTimeout); });
return reinterpret_cast<const PendingInterestId*>(interestToExpress.get());
}
@@ -236,13 +221,13 @@
}
// If the same ioService thread, dispatch directly calls the method
- m_ioService.dispatch(bind(&Impl::asyncPutData, m_impl, dataPtr));
+ m_ioService.dispatch([=] { m_impl->asyncPutData(dataPtr); });
}
void
Face::removePendingInterest(const PendingInterestId* pendingInterestId)
{
- m_ioService.post(bind(&Impl::asyncRemovePendingInterest, m_impl, pendingInterestId));
+ m_ioService.post([=] { m_impl->asyncRemovePendingInterest(pendingInterestId); });
}
size_t
@@ -343,7 +328,7 @@
shared_ptr<InterestFilterRecord> filter =
make_shared<InterestFilterRecord>(interestFilter, onInterest);
- getIoService().post(bind(&Impl::asyncSetInterestFilter, m_impl, filter));
+ getIoService().post([=] { m_impl->asyncSetInterestFilter(filter); });
return reinterpret_cast<const InterestFilterId*>(filter.get());
}
@@ -386,14 +371,15 @@
void
Face::unsetInterestFilter(const RegisteredPrefixId* registeredPrefixId)
{
- m_ioService.post(bind(&Impl::asyncUnregisterPrefix, m_impl, registeredPrefixId,
- UnregisterPrefixSuccessCallback(), UnregisterPrefixFailureCallback()));
+ m_ioService.post([=] { m_impl->asyncUnregisterPrefix(registeredPrefixId,
+ UnregisterPrefixSuccessCallback(),
+ UnregisterPrefixFailureCallback()); });
}
void
Face::unsetInterestFilter(const InterestFilterId* interestFilterId)
{
- m_ioService.post(bind(&Impl::asyncUnsetInterestFilter, m_impl, interestFilterId));
+ m_ioService.post([=] { m_impl->asyncUnsetInterestFilter(interestFilterId); });
}
void
@@ -401,8 +387,7 @@
const UnregisterPrefixSuccessCallback& onSuccess,
const UnregisterPrefixFailureCallback& onFailure)
{
- m_ioService.post(bind(&Impl::asyncUnregisterPrefix, m_impl, registeredPrefixId,
- onSuccess, onFailure));
+ m_ioService.post([=] { m_impl->asyncUnregisterPrefix(registeredPrefixId,onSuccess, onFailure); });
}
void
@@ -449,7 +434,7 @@
void
Face::shutdown()
{
- m_ioService.post(bind(&Face::asyncShutdown, this));
+ m_ioService.post([this] { this->asyncShutdown(); });
}
void
diff --git a/src/face.hpp b/src/face.hpp
index 96fae40..d7399e8 100644
--- a/src/face.hpp
+++ b/src/face.hpp
@@ -540,15 +540,14 @@
* @throws ConfigFile::Error on parse error and unsupported protocols
*/
void
- construct(KeyChain* keyChain);
+ construct(KeyChain& keyChain);
/**
* @throws Face::Error on unsupported protocol
* @note shared_ptr is passed by value because ownership is transferred to this function
*/
void
- construct(shared_ptr<Transport> transport,
- KeyChain* keyChain);
+ construct(shared_ptr<Transport> transport, KeyChain& keyChain);
bool
isSupportedNfdProtocol(const std::string& protocol);
@@ -583,13 +582,13 @@
* currently Face does not keep the KeyChain passed in constructor
* because it's not needed, but this may change in the future
*/
- KeyChain* m_internalKeyChain;
+ unique_ptr<KeyChain> m_internalKeyChain;
- nfd::Controller* m_nfdController;
+ unique_ptr<nfd::Controller> m_nfdController;
bool m_isDirectNfdFibManagementRequested;
class Impl;
- Impl* m_impl;
+ unique_ptr<Impl> m_impl;
};
inline bool