table: rename StrategyInfoHost::getOrCreateStrategyInfo to insertStrategyInfo
refs #3205
Change-Id: Icd3ab9e4fdb5a2ffc39a8d0e0c713e22f13fe28c
diff --git a/daemon/fw/access-strategy.cpp b/daemon/fw/access-strategy.cpp
index 9d4df0c..a7eed83 100644
--- a/daemon/fw/access-strategy.cpp
+++ b/daemon/fw/access-strategy.cpp
@@ -136,7 +136,7 @@
this->sendInterest(pitEntry, *face);
// schedule RTO timeout
- shared_ptr<PitInfo> pi = pitEntry->getOrCreateStrategyInfo<PitInfo>();
+ shared_ptr<PitInfo> pi = pitEntry->insertStrategyInfo<PitInfo>();
pi->rtoTimer = scheduler::schedule(rto,
bind(&AccessStrategy::afterRtoTimeout, this, weak_ptr<pit::Entry>(pitEntry),
inFace.getId(), mi.lastNexthop));
@@ -229,14 +229,14 @@
{
measurements::Entry* me = this->getMeasurements().findLongestPrefixMatch(pitEntry);
if (me == nullptr) {
- return std::forward_as_tuple(Name(), nullptr);
+ return std::make_tuple(Name(), nullptr);
}
shared_ptr<MtInfo> mi = me->getStrategyInfo<MtInfo>();
BOOST_ASSERT(mi != nullptr);
// XXX after runtime strategy change, it's possible that me exists but mi doesn't exist;
// this case needs another longest prefix match until mi is found
- return std::forward_as_tuple(me->getName(), mi);
+ return std::make_tuple(me->getName(), mi);
}
shared_ptr<AccessStrategy::MtInfo>
@@ -255,7 +255,7 @@
static const time::nanoseconds ME_LIFETIME = time::seconds(8);
this->getMeasurements().extendLifetime(*me, ME_LIFETIME);
- return me->getOrCreateStrategyInfo<MtInfo>();
+ return me->insertStrategyInfo<MtInfo>();
}
AccessStrategy::FaceInfo::FaceInfo()
diff --git a/daemon/fw/asf-measurements.cpp b/daemon/fw/asf-measurements.cpp
index c15aca0..c7f68c9 100644
--- a/daemon/fw/asf-measurements.cpp
+++ b/daemon/fw/asf-measurements.cpp
@@ -228,7 +228,7 @@
// Set or update entry lifetime
extendLifetime(*me);
- shared_ptr<NamespaceInfo> info = me->getOrCreateStrategyInfo<NamespaceInfo>();
+ shared_ptr<NamespaceInfo> info = me->insertStrategyInfo<NamespaceInfo>();
BOOST_ASSERT(info != nullptr);
return info;
@@ -252,7 +252,7 @@
// Set or update entry lifetime
extendLifetime(*me);
- shared_ptr<NamespaceInfo> info = me->getOrCreateStrategyInfo<NamespaceInfo>();
+ shared_ptr<NamespaceInfo> info = me->insertStrategyInfo<NamespaceInfo>();
BOOST_ASSERT(info != nullptr);
return *info;
diff --git a/daemon/fw/ncc-strategy.cpp b/daemon/fw/ncc-strategy.cpp
index b127fea..c56eef4 100644
--- a/daemon/fw/ncc-strategy.cpp
+++ b/daemon/fw/ncc-strategy.cpp
@@ -54,7 +54,7 @@
return;
}
- shared_ptr<PitEntryInfo> pitEntryInfo = pitEntry->getOrCreateStrategyInfo<PitEntryInfo>();
+ shared_ptr<PitEntryInfo> pitEntryInfo = pitEntry->insertStrategyInfo<PitEntryInfo>();
bool isNewPitEntry = !hasPendingOutRecords(*pitEntry);
if (!isNewPitEntry) {
return;
diff --git a/daemon/fw/retx-suppression-exponential.cpp b/daemon/fw/retx-suppression-exponential.cpp
index 3cca0ab..5edff29 100644
--- a/daemon/fw/retx-suppression-exponential.cpp
+++ b/daemon/fw/retx-suppression-exponential.cpp
@@ -82,7 +82,7 @@
time::steady_clock::TimePoint now = time::steady_clock::now();
time::steady_clock::Duration sinceLastOutgoing = now - lastOutgoing;
- shared_ptr<PitInfo> pi = pitEntry.getOrCreateStrategyInfo<PitInfo>(m_initialInterval);
+ shared_ptr<PitInfo> pi = pitEntry.insertStrategyInfo<PitInfo>(m_initialInterval);
bool shouldSuppress = sinceLastOutgoing < pi->suppressionInterval;
if (shouldSuppress) {
diff --git a/daemon/fw/strategy-info.hpp b/daemon/fw/strategy-info.hpp
index 133d53c..aae3862 100644
--- a/daemon/fw/strategy-info.hpp
+++ b/daemon/fw/strategy-info.hpp
@@ -38,7 +38,7 @@
public:
#ifdef DOXYGEN
/** \return an integer that uniquely identifies this StrategyInfo type
- * \sa http://redmine.named-data.net/projects/nfd/wiki/StrategyInfoType
+ * \sa https://redmine.named-data.net/projects/nfd/wiki/StrategyInfoType
*/
static constexpr int
getTypeId()
@@ -48,15 +48,12 @@
#endif
virtual
- ~StrategyInfo();
+ ~StrategyInfo() = default;
+
+protected:
+ StrategyInfo() = default;
};
-
-inline
-StrategyInfo::~StrategyInfo()
-{
-}
-
} // namespace fw
} // namespace nfd
diff --git a/daemon/table/strategy-info-host.hpp b/daemon/table/strategy-info-host.hpp
index 7afe50d..23b5fcc 100644
--- a/daemon/table/strategy-info-host.hpp
+++ b/daemon/table/strategy-info-host.hpp
@@ -30,35 +30,65 @@
namespace nfd {
-/** \brief base class for an entity onto which StrategyInfo objects may be placed
+/** \brief base class for an entity onto which StrategyInfo items may be placed
*/
class StrategyInfoHost
{
public:
/** \brief get a StrategyInfo item
- * \tparam T type of StrategyInfo, must be a subclass of from nfd::fw::StrategyInfo
- * \retval nullptr if no StrategyInfo of type T is stored
+ * \tparam T type of StrategyInfo, must be a subclass of fw::StrategyInfo
+ * \return an existing StrategyInfo item of type T, or nullptr if it does not exist
*/
template<typename T>
shared_ptr<T>
- getStrategyInfo() const;
+ getStrategyInfo() const
+ {
+ static_assert(std::is_base_of<fw::StrategyInfo, T>::value,
+ "T must inherit from StrategyInfo");
+
+ auto it = m_items.find(T::getTypeId());
+ if (it == m_items.end()) {
+ return nullptr;
+ }
+ return static_pointer_cast<T, fw::StrategyInfo>(it->second);
+ }
/** \brief set a StrategyInfo item
- * \tparam T type of StrategyInfo, must be a subclass of from nfd::fw::StrategyInfo
+ * \tparam T type of StrategyInfo, must be a subclass of nfd::fw::StrategyInfo
*/
template<typename T>
void
- setStrategyInfo(shared_ptr<T> strategyInfo);
+ setStrategyInfo(shared_ptr<T> item)
+ {
+ static_assert(std::is_base_of<fw::StrategyInfo, T>::value,
+ "T must inherit from StrategyInfo");
- /** \brief get or create a StrategyInfo item
- * \tparam T type of StrategyInfo, must be a subclass of from nfd::fw::StrategyInfo
- *
- * If no StrategyInfo of type T is stored, it's created with \p args;
- * otherwise, the existing item is returned.
+ if (item == nullptr) {
+ m_items.erase(T::getTypeId());
+ }
+ else {
+ m_items[T::getTypeId()] = item;
+ }
+ }
+
+ /** \brief insert a StrategyInfo item
+ * \tparam T type of StrategyInfo, must be a subclass of fw::StrategyInfo
+ * \return a new or existing StrategyInfo item of type T
*/
template<typename T, typename ...A>
shared_ptr<T>
- getOrCreateStrategyInfo(A&&... args);
+ insertStrategyInfo(A&&... args)
+ {
+ static_assert(std::is_base_of<fw::StrategyInfo, T>::value,
+ "T must inherit from StrategyInfo");
+
+ shared_ptr<T> item = this->getStrategyInfo<T>();
+ if (!static_cast<bool>(item)) {
+ item = std::make_shared<T>(std::forward<A>(args)...);
+ this->setStrategyInfo(item);
+ }
+ return item;
+ }
/** \brief clear all StrategyInfo items
*/
@@ -69,51 +99,6 @@
std::map<int, shared_ptr<fw::StrategyInfo>> m_items;
};
-
-template<typename T>
-shared_ptr<T>
-StrategyInfoHost::getStrategyInfo() const
-{
- static_assert(std::is_base_of<fw::StrategyInfo, T>::value,
- "T must inherit from StrategyInfo");
-
- auto it = m_items.find(T::getTypeId());
- if (it == m_items.end()) {
- return nullptr;
- }
- return static_pointer_cast<T, fw::StrategyInfo>(it->second);
-}
-
-template<typename T>
-void
-StrategyInfoHost::setStrategyInfo(shared_ptr<T> item)
-{
- static_assert(std::is_base_of<fw::StrategyInfo, T>::value,
- "T must inherit from StrategyInfo");
-
- if (item == nullptr) {
- m_items.erase(T::getTypeId());
- }
- else {
- m_items[T::getTypeId()] = item;
- }
-}
-
-template<typename T, typename ...A>
-shared_ptr<T>
-StrategyInfoHost::getOrCreateStrategyInfo(A&&... args)
-{
- static_assert(std::is_base_of<fw::StrategyInfo, T>::value,
- "T must inherit from StrategyInfo");
-
- shared_ptr<T> item = this->getStrategyInfo<T>();
- if (!static_cast<bool>(item)) {
- item = std::make_shared<T>(std::forward<A>(args)...);
- this->setStrategyInfo(item);
- }
- return item;
-}
-
} // namespace nfd
#endif // NFD_DAEMON_TABLE_STRATEGY_INFO_HOST_HPP
diff --git a/tests/daemon/table/measurements.t.cpp b/tests/daemon/table/measurements.t.cpp
index 79b2b94..539c409 100644
--- a/tests/daemon/table/measurements.t.cpp
+++ b/tests/daemon/table/measurements.t.cpp
@@ -134,7 +134,7 @@
BOOST_AUTO_TEST_CASE(FindLongestPrefixMatch)
{
measurements.get("/A");
- measurements.get("/A/B/C").getOrCreateStrategyInfo<DummyStrategyInfo1>();
+ measurements.get("/A/B/C").insertStrategyInfo<DummyStrategyInfo1>();
measurements.get("/A/B/C/D");
Entry* found1 = measurements.findLongestPrefixMatch("/A/B/C/D/E");
@@ -156,7 +156,7 @@
Pit pit(nameTree);
measurements.get("/A");
- measurements.get("/A/B/C").getOrCreateStrategyInfo<DummyStrategyInfo1>();
+ measurements.get("/A/B/C").insertStrategyInfo<DummyStrategyInfo1>();
measurements.get("/A/B/C/D");
shared_ptr<Interest> interest = makeInterest("/A/B/C/D/E");
diff --git a/tests/daemon/table/strategy-choice.t.cpp b/tests/daemon/table/strategy-choice.t.cpp
index 42a36b0..91634e9 100644
--- a/tests/daemon/table/strategy-choice.t.cpp
+++ b/tests/daemon/table/strategy-choice.t.cpp
@@ -222,10 +222,10 @@
BOOST_CHECK(table.insert("ndn:/", nameP));
// { '/'=>P }
- measurements.get("ndn:/").getOrCreateStrategyInfo<PStrategyInfo>();
- measurements.get("ndn:/A").getOrCreateStrategyInfo<PStrategyInfo>();
- measurements.get("ndn:/A/B").getOrCreateStrategyInfo<PStrategyInfo>();
- measurements.get("ndn:/A/C").getOrCreateStrategyInfo<PStrategyInfo>();
+ measurements.get("ndn:/").insertStrategyInfo<PStrategyInfo>();
+ measurements.get("ndn:/A").insertStrategyInfo<PStrategyInfo>();
+ measurements.get("ndn:/A/B").insertStrategyInfo<PStrategyInfo>();
+ measurements.get("ndn:/A/C").insertStrategyInfo<PStrategyInfo>();
BOOST_CHECK(table.insert("ndn:/A/B", nameP));
// { '/'=>P, '/A/B'=>P }
diff --git a/tests/daemon/table/strategy-info-host.t.cpp b/tests/daemon/table/strategy-info-host.t.cpp
index 746d51a..4eaf9b0 100644
--- a/tests/daemon/table/strategy-info-host.t.cpp
+++ b/tests/daemon/table/strategy-info-host.t.cpp
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/**
- * Copyright (c) 2014-2015, 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,
@@ -104,16 +104,16 @@
{
StrategyInfoHost host;
- host.getOrCreateStrategyInfo<DummyStrategyInfo>(3503);
+ host.insertStrategyInfo<DummyStrategyInfo>(3503);
BOOST_REQUIRE(host.getStrategyInfo<DummyStrategyInfo>() != nullptr);
BOOST_CHECK_EQUAL(host.getStrategyInfo<DummyStrategyInfo>()->m_id, 3503);
- host.getOrCreateStrategyInfo<DummyStrategyInfo>(1032);
+ host.insertStrategyInfo<DummyStrategyInfo>(1032);
BOOST_REQUIRE(host.getStrategyInfo<DummyStrategyInfo>() != nullptr);
BOOST_CHECK_EQUAL(host.getStrategyInfo<DummyStrategyInfo>()->m_id, 3503);
host.setStrategyInfo<DummyStrategyInfo>(nullptr);
- host.getOrCreateStrategyInfo<DummyStrategyInfo>(9956);
+ host.insertStrategyInfo<DummyStrategyInfo>(9956);
BOOST_REQUIRE(host.getStrategyInfo<DummyStrategyInfo>() != nullptr);
BOOST_CHECK_EQUAL(host.getStrategyInfo<DummyStrategyInfo>()->m_id, 9956);
}
@@ -122,11 +122,11 @@
{
StrategyInfoHost host;
- host.getOrCreateStrategyInfo<DummyStrategyInfo>(8063);
+ host.insertStrategyInfo<DummyStrategyInfo>(8063);
BOOST_REQUIRE(host.getStrategyInfo<DummyStrategyInfo>() != nullptr);
BOOST_CHECK_EQUAL(host.getStrategyInfo<DummyStrategyInfo>()->m_id, 8063);
- host.getOrCreateStrategyInfo<DummyStrategyInfo2>(2871);
+ host.insertStrategyInfo<DummyStrategyInfo2>(2871);
BOOST_REQUIRE(host.getStrategyInfo<DummyStrategyInfo2>() != nullptr);
BOOST_CHECK_EQUAL(host.getStrategyInfo<DummyStrategyInfo2>()->m_id, 2871);