table: StrategyChoice uses unique_ptr instead of shared_ptr

refs #3164

Change-Id: Id3110f72aab83982b0768e596a04bad9f7336975
diff --git a/tests/daemon/table/measurements-accessor.t.cpp b/tests/daemon/table/measurements-accessor.t.cpp
index 98a39c3..cab1dbb 100644
--- a/tests/daemon/table/measurements-accessor.t.cpp
+++ b/tests/daemon/table/measurements-accessor.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,
@@ -28,6 +28,7 @@
 
 #include "tests/test-common.hpp"
 #include "../fw/dummy-strategy.hpp"
+#include "../fw/install-strategy.hpp"
 
 namespace nfd {
 namespace tests {
@@ -59,24 +60,22 @@
 {
 protected:
   MeasurementsAccessorFixture()
-    : strategy1(make_shared<MeasurementsAccessorTestStrategy>(ref(forwarder), "ndn:/strategy1"))
-    , strategy2(make_shared<MeasurementsAccessorTestStrategy>(ref(forwarder), "ndn:/strategy2"))
+    : strategy1(install<MeasurementsAccessorTestStrategy>(forwarder, "ndn:/strategy1"))
+    , strategy2(install<MeasurementsAccessorTestStrategy>(forwarder, "ndn:/strategy2"))
     , measurements(forwarder.getMeasurements())
-    , accessor1(strategy1->getMeasurementsAccessor())
-    , accessor2(strategy2->getMeasurementsAccessor())
+    , accessor1(strategy1.getMeasurementsAccessor())
+    , accessor2(strategy2.getMeasurementsAccessor())
   {
     StrategyChoice& strategyChoice = forwarder.getStrategyChoice();
-    strategyChoice.install(strategy1);
-    strategyChoice.install(strategy2);
-    strategyChoice.insert("/"   , strategy1->getName());
-    strategyChoice.insert("/A"  , strategy2->getName());
-    strategyChoice.insert("/A/B", strategy1->getName());
+    strategyChoice.insert("/"   , strategy1.getName());
+    strategyChoice.insert("/A"  , strategy2.getName());
+    strategyChoice.insert("/A/B", strategy1.getName());
   }
 
 protected:
   Forwarder forwarder;
-  shared_ptr<MeasurementsAccessorTestStrategy> strategy1;
-  shared_ptr<MeasurementsAccessorTestStrategy> strategy2;
+  MeasurementsAccessorTestStrategy& strategy1;
+  MeasurementsAccessorTestStrategy& strategy2;
   Measurements& measurements;
   MeasurementsAccessor& accessor1;
   MeasurementsAccessor& accessor2;
diff --git a/tests/daemon/table/strategy-choice.t.cpp b/tests/daemon/table/strategy-choice.t.cpp
index 469cb1a..5897981 100644
--- a/tests/daemon/table/strategy-choice.t.cpp
+++ b/tests/daemon/table/strategy-choice.t.cpp
@@ -24,9 +24,10 @@
  */
 
 #include "table/strategy-choice.hpp"
-#include "tests/daemon/fw/dummy-strategy.hpp"
 
 #include "tests/test-common.hpp"
+#include "../fw/dummy-strategy.hpp"
+#include "../fw/install-strategy.hpp"
 
 namespace nfd {
 namespace tests {
@@ -40,13 +41,10 @@
 {
   Forwarder forwarder;
   Name nameP("ndn:/strategy/P");
-  shared_ptr<Strategy> strategyP = make_shared<DummyStrategy>(ref(forwarder), nameP);
+  install<DummyStrategy>(forwarder, nameP);
 
   StrategyChoice& table = forwarder.getStrategyChoice();
 
-  // install
-  BOOST_CHECK_EQUAL(table.install(strategyP), true);
-
   BOOST_CHECK(table.insert("ndn:/", nameP));
   // { '/'=>P }
 
@@ -64,16 +62,11 @@
   Name nameP("ndn:/strategy/P");
   Name nameQ("ndn:/strategy/Q");
   Name nameZ("ndn:/strategy/Z");
-  shared_ptr<Strategy> strategyP = make_shared<DummyStrategy>(ref(forwarder), nameP);
-  shared_ptr<Strategy> strategyQ = make_shared<DummyStrategy>(ref(forwarder), nameQ);
+  install<DummyStrategy>(forwarder, nameP);
+  install<DummyStrategy>(forwarder, nameQ);
 
   StrategyChoice& table = forwarder.getStrategyChoice();
 
-  // install
-  BOOST_CHECK_EQUAL(table.install(strategyP), true);
-  BOOST_CHECK_EQUAL(table.install(strategyQ), true);
-  BOOST_CHECK_EQUAL(table.install(strategyQ), false);
-
   BOOST_CHECK(table.insert("ndn:/", nameP));
   // { '/'=>P }
 
@@ -88,9 +81,8 @@
   BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/A")  .getName(), nameP);
   BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/A/B").getName(), nameP);
   // same instance
-  BOOST_CHECK_EQUAL(&table.findEffectiveStrategy("ndn:/"),    strategyP.get());
-  BOOST_CHECK_EQUAL(&table.findEffectiveStrategy("ndn:/A"),   strategyP.get());
-  BOOST_CHECK_EQUAL(&table.findEffectiveStrategy("ndn:/A/B"), strategyP.get());
+  BOOST_CHECK_EQUAL(&table.findEffectiveStrategy("ndn:/"),
+                    &table.findEffectiveStrategy("ndn:/A/B"));
 
   table.erase("ndn:/A"); // no effect
   // { '/'=>P, '/A/B'=>P }
@@ -130,15 +122,14 @@
   Forwarder forwarder;
   Name nameP("ndn:/strategy/P");
   Name nameQ("ndn:/strategy/Q");
-  shared_ptr<Strategy> strategyP = make_shared<DummyStrategy>(ref(forwarder), nameP);
-  shared_ptr<Strategy> strategyQ = make_shared<DummyStrategy>(ref(forwarder), nameQ);
-  StrategyChoice& table = forwarder.getStrategyChoice();
-  table.install(strategyP);
-  table.install(strategyQ);
+  install<DummyStrategy>(forwarder, nameP);
+  install<DummyStrategy>(forwarder, nameQ);
 
   shared_ptr<Data> dataABC = makeData("/A/B/C");
   Name fullName = dataABC->getFullName();
 
+  StrategyChoice& table = forwarder.getStrategyChoice();
+
   BOOST_CHECK(table.insert("/A", nameP));
   BOOST_CHECK(table.insert(fullName, nameQ));
 
@@ -157,11 +148,10 @@
   Forwarder forwarder;
   Name nameP("ndn:/strategy/P");
   Name nameQ("ndn:/strategy/Q");
-  shared_ptr<Strategy> strategyP = make_shared<DummyStrategy>(ref(forwarder), nameP);
-  shared_ptr<Strategy> strategyQ = make_shared<DummyStrategy>(ref(forwarder), nameQ);
+  install<DummyStrategy>(forwarder, nameP);
+  install<DummyStrategy>(forwarder, nameQ);
+
   StrategyChoice& table = forwarder.getStrategyChoice();
-  table.install(strategyP);
-  table.install(strategyQ);
 
   BOOST_CHECK(table.insert("/A", nameP));
   BOOST_CHECK(table.insert("/A/B/C", nameQ));
@@ -183,12 +173,10 @@
   Forwarder forwarder;
   Name nameP("ndn:/strategy/P");
   Name nameQ("ndn:/strategy/Q");
-  shared_ptr<Strategy> strategyP = make_shared<DummyStrategy>(ref(forwarder), nameP);
-  shared_ptr<Strategy> strategyQ = make_shared<DummyStrategy>(ref(forwarder), nameQ);
+  install<DummyStrategy>(forwarder, nameP);
+  install<DummyStrategy>(forwarder, nameQ);
 
   StrategyChoice& table = forwarder.getStrategyChoice();
-  table.install(strategyP);
-  table.install(strategyQ);
 
   table.insert("ndn:/",      nameP);
   table.insert("ndn:/A/B",   nameQ);
@@ -226,16 +214,12 @@
   Forwarder forwarder;
   Name nameP("ndn:/strategy/P");
   Name nameQ("ndn:/strategy/Q");
-  shared_ptr<Strategy> strategyP = make_shared<DummyStrategy>(ref(forwarder), nameP);
-  shared_ptr<Strategy> strategyQ = make_shared<DummyStrategy>(ref(forwarder), nameQ);
+  install<DummyStrategy>(forwarder, nameP);
+  install<DummyStrategy>(forwarder, nameQ);
 
   StrategyChoice& table = forwarder.getStrategyChoice();
   Measurements& measurements = forwarder.getMeasurements();
 
-  // install
-  table.install(strategyP);
-  table.install(strategyQ);
-
   BOOST_CHECK(table.insert("ndn:/", nameP));
   // { '/'=>P }
   measurements.get("ndn:/")     ->getOrCreateStrategyInfo<PStrategyInfo>();
@@ -245,38 +229,36 @@
 
   BOOST_CHECK(table.insert("ndn:/A/B", nameP));
   // { '/'=>P, '/A/B'=>P }
-  BOOST_CHECK( static_cast<bool>(measurements.get("ndn:/")   ->getStrategyInfo<PStrategyInfo>()));
-  BOOST_CHECK( static_cast<bool>(measurements.get("ndn:/A")  ->getStrategyInfo<PStrategyInfo>()));
-  BOOST_CHECK( static_cast<bool>(measurements.get("ndn:/A/B")->getStrategyInfo<PStrategyInfo>()));
-  BOOST_CHECK( static_cast<bool>(measurements.get("ndn:/A/C")->getStrategyInfo<PStrategyInfo>()));
+  BOOST_CHECK(measurements.get("ndn:/")   ->getStrategyInfo<PStrategyInfo>() != nullptr);
+  BOOST_CHECK(measurements.get("ndn:/A")  ->getStrategyInfo<PStrategyInfo>() != nullptr);
+  BOOST_CHECK(measurements.get("ndn:/A/B")->getStrategyInfo<PStrategyInfo>() != nullptr);
+  BOOST_CHECK(measurements.get("ndn:/A/C")->getStrategyInfo<PStrategyInfo>() != nullptr);
 
   BOOST_CHECK(table.insert("ndn:/A", nameQ));
   // { '/'=>P, '/A/B'=>P, '/A'=>Q }
-  BOOST_CHECK( static_cast<bool>(measurements.get("ndn:/")   ->getStrategyInfo<PStrategyInfo>()));
-  BOOST_CHECK(!static_cast<bool>(measurements.get("ndn:/A")  ->getStrategyInfo<PStrategyInfo>()));
-  BOOST_CHECK( static_cast<bool>(measurements.get("ndn:/A/B")->getStrategyInfo<PStrategyInfo>()));
-  BOOST_CHECK(!static_cast<bool>(measurements.get("ndn:/A/C")->getStrategyInfo<PStrategyInfo>()));
+  BOOST_CHECK(measurements.get("ndn:/")   ->getStrategyInfo<PStrategyInfo>() != nullptr);
+  BOOST_CHECK(measurements.get("ndn:/A")  ->getStrategyInfo<PStrategyInfo>() == nullptr);
+  BOOST_CHECK(measurements.get("ndn:/A/B")->getStrategyInfo<PStrategyInfo>() != nullptr);
+  BOOST_CHECK(measurements.get("ndn:/A/C")->getStrategyInfo<PStrategyInfo>() == nullptr);
 
   table.erase("ndn:/A/B");
   // { '/'=>P, '/A'=>Q }
-  BOOST_CHECK( static_cast<bool>(measurements.get("ndn:/")   ->getStrategyInfo<PStrategyInfo>()));
-  BOOST_CHECK(!static_cast<bool>(measurements.get("ndn:/A")  ->getStrategyInfo<PStrategyInfo>()));
-  BOOST_CHECK(!static_cast<bool>(measurements.get("ndn:/A/B")->getStrategyInfo<PStrategyInfo>()));
-  BOOST_CHECK(!static_cast<bool>(measurements.get("ndn:/A/C")->getStrategyInfo<PStrategyInfo>()));
+  BOOST_CHECK(measurements.get("ndn:/")   ->getStrategyInfo<PStrategyInfo>() != nullptr);
+  BOOST_CHECK(measurements.get("ndn:/A")  ->getStrategyInfo<PStrategyInfo>() == nullptr);
+  BOOST_CHECK(measurements.get("ndn:/A/B")->getStrategyInfo<PStrategyInfo>() == nullptr);
+  BOOST_CHECK(measurements.get("ndn:/A/C")->getStrategyInfo<PStrategyInfo>() == nullptr);
 }
 
 BOOST_AUTO_TEST_CASE(EraseNameTreeEntry)
 {
   Forwarder forwarder;
-  NameTree& nameTree = forwarder.getNameTree();
-  StrategyChoice& table = forwarder.getStrategyChoice();
-
   Name nameP("ndn:/strategy/P");
   Name nameQ("ndn:/strategy/Q");
-  shared_ptr<Strategy> strategyP = make_shared<DummyStrategy>(ref(forwarder), nameP);
-  shared_ptr<Strategy> strategyQ = make_shared<DummyStrategy>(ref(forwarder), nameQ);
-  table.install(strategyP);
-  table.install(strategyQ);
+  install<DummyStrategy>(forwarder, nameP);
+  install<DummyStrategy>(forwarder, nameQ);
+
+  NameTree& nameTree = forwarder.getNameTree();
+  StrategyChoice& table = forwarder.getStrategyChoice();
 
   table.insert("ndn:/", nameP);
 
@@ -297,27 +279,38 @@
   Name name4("ndn:/%FD%04");
   Name nameQ("ndn:/strategy/Q");
   Name nameQ5("ndn:/strategy/Q/%FD%05");
-  shared_ptr<Strategy> strategyP1 = make_shared<DummyStrategy>(ref(forwarder), nameP1);
-  shared_ptr<Strategy> strategyP2 = make_shared<DummyStrategy>(ref(forwarder), nameP2);
-  shared_ptr<Strategy> strategy3  = make_shared<DummyStrategy>(ref(forwarder), name3);
-  shared_ptr<Strategy> strategy4  = make_shared<DummyStrategy>(ref(forwarder), name4);
-  shared_ptr<Strategy> strategyQ  = make_shared<DummyStrategy>(ref(forwarder), nameQ);
-  shared_ptr<Strategy> strategyQ5 = make_shared<DummyStrategy>(ref(forwarder), nameQ5);
 
   StrategyChoice& table = forwarder.getStrategyChoice();
 
   // install
-  BOOST_CHECK_EQUAL(table.install(strategyP1), true);
-  BOOST_CHECK_EQUAL(table.install(strategyP1), false);
+  auto strategyP1 = make_unique<DummyStrategy>(ref(forwarder), nameP1);
+  Strategy* instanceP1 = strategyP1.get();
+  auto strategyP1b = make_unique<DummyStrategy>(ref(forwarder), nameP1);
+  auto strategyP2 = make_unique<DummyStrategy>(ref(forwarder), nameP2);
+  auto strategy3 = make_unique<DummyStrategy>(ref(forwarder), name3);
+  auto strategy4 = make_unique<DummyStrategy>(ref(forwarder), name4);
+  auto strategyQ = make_unique<DummyStrategy>(ref(forwarder), nameQ);
+  auto strategyQ5 = make_unique<DummyStrategy>(ref(forwarder), nameQ5);
+
+  bool isInstalled = false;
+  Strategy* installed = nullptr;
+
+  std::tie(isInstalled, installed) = table.install(std::move(strategyP1));
+  BOOST_CHECK_EQUAL(isInstalled, true);
+  BOOST_CHECK_EQUAL(installed, instanceP1);
+  std::tie(isInstalled, installed) = table.install(std::move(strategyP1b));
+  BOOST_CHECK_EQUAL(isInstalled, false);
+  BOOST_CHECK_EQUAL(installed, instanceP1);
+
   BOOST_CHECK_EQUAL(table.hasStrategy(nameP,  false), true);
   BOOST_CHECK_EQUAL(table.hasStrategy(nameP,  true),  false);
   BOOST_CHECK_EQUAL(table.hasStrategy(nameP1, true),  true);
 
-  BOOST_CHECK_EQUAL(table.install(strategyP2), true);
-  BOOST_CHECK_EQUAL(table.install(strategy3),  true);
-  BOOST_CHECK_EQUAL(table.install(strategy4),  true);
-  BOOST_CHECK_EQUAL(table.install(strategyQ),  true);
-  BOOST_CHECK_EQUAL(table.install(strategyQ5), true);
+  BOOST_CHECK_EQUAL(table.install(std::move(strategyP2)).first, true);
+  BOOST_CHECK_EQUAL(table.install(std::move(strategy3)).first, true);
+  BOOST_CHECK_EQUAL(table.install(std::move(strategy4)).first, true);
+  BOOST_CHECK_EQUAL(table.install(std::move(strategyQ)).first, true);
+  BOOST_CHECK_EQUAL(table.install(std::move(strategyQ5)).first, true);
 
   BOOST_CHECK(table.insert("ndn:/", nameQ));
   // exact match, { '/'=>Q }