table: StrategyInfo getter/setter

refs #1180

Change-Id: I8ac487b2b372ceb33d71ebeb14d4fbdeefca9e8e
diff --git a/daemon/common.hpp b/daemon/common.hpp
index fc37c02..3d1a53a 100644
--- a/daemon/common.hpp
+++ b/daemon/common.hpp
@@ -33,6 +33,7 @@
 using boost::shared_ptr;
 using boost::enable_shared_from_this;
 using boost::make_shared;
+using boost::static_pointer_cast;
 using boost::function;
 using boost::bind;
 
diff --git a/daemon/fw/strategy-info.hpp b/daemon/fw/strategy-info.hpp
new file mode 100644
index 0000000..1e9e1a8
--- /dev/null
+++ b/daemon/fw/strategy-info.hpp
@@ -0,0 +1,34 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (C) 2014 Named Data Networking Project
+ * See COPYING for copyright and distribution information.
+ */
+
+#ifndef NFD_FW_STRATEGY_INFO_HPP
+#define NFD_FW_STRATEGY_INFO_HPP
+
+#include "common.hpp"
+
+namespace nfd {
+namespace fw {
+
+/** \class StrategyInfo
+ *  \brief contains arbitrary information forwarding strategy places on table entries
+ */
+class StrategyInfo
+{
+public:
+  virtual
+  ~StrategyInfo();
+};
+
+
+inline
+StrategyInfo::~StrategyInfo()
+{
+}
+
+} // namespace fw
+} // namespace nfd
+
+#endif // NFD_FW_STRATEGY_INFO_HPP
diff --git a/daemon/table/strategy-info-host.cpp b/daemon/table/strategy-info-host.cpp
new file mode 100644
index 0000000..7c70b9f
--- /dev/null
+++ b/daemon/table/strategy-info-host.cpp
@@ -0,0 +1,17 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (C) 2014 Named Data Networking Project
+ * See COPYING for copyright and distribution information.
+ */
+
+#include "strategy-info-host.hpp"
+
+namespace nfd {
+
+void
+StrategyInfoHost::clearStrategyInfo()
+{
+  m_strategyInfo.reset();
+}
+
+} // namespace nfd
diff --git a/daemon/table/strategy-info-host.hpp b/daemon/table/strategy-info-host.hpp
new file mode 100644
index 0000000..41199b2
--- /dev/null
+++ b/daemon/table/strategy-info-host.hpp
@@ -0,0 +1,52 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (C) 2014 Named Data Networking Project
+ * See COPYING for copyright and distribution information.
+ */
+
+#ifndef NFD_TABLE_STRATEGY_INFO_HOST_HPP
+#define NFD_TABLE_STRATEGY_INFO_HOST_HPP
+
+#include "fw/strategy-info.hpp"
+
+namespace nfd {
+
+/** \class StrategyInfoHost
+ *  \brief base class for an entity onto which StrategyInfo may be placed
+ */
+class StrategyInfoHost
+{
+public:
+  template<typename T>
+  void
+  setStrategyInfo(shared_ptr<T> strategyInfo);
+  
+  template<typename T>
+  shared_ptr<T>
+  getStrategyInfo();
+  
+  void
+  clearStrategyInfo();
+
+private:
+  shared_ptr<fw::StrategyInfo> m_strategyInfo;
+};
+
+
+template<typename T>
+void
+StrategyInfoHost::setStrategyInfo(shared_ptr<T> strategyInfo)
+{
+  m_strategyInfo = strategyInfo;
+}
+
+template<typename T>
+shared_ptr<T>
+StrategyInfoHost::getStrategyInfo()
+{
+  return static_pointer_cast<T, fw::StrategyInfo>(m_strategyInfo);
+}
+
+} // namespace nfd
+
+#endif // NFD_TABLE_STRATEGY_INFO_HOST_HPP
diff --git a/tests/table/strategy-info-host.cpp b/tests/table/strategy-info-host.cpp
new file mode 100644
index 0000000..af3351e
--- /dev/null
+++ b/tests/table/strategy-info-host.cpp
@@ -0,0 +1,59 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (C) 2014 Named Data Networking Project
+ * See COPYING for copyright and distribution information.
+ */
+
+#include "table/strategy-info-host.hpp"
+
+#include <boost/test/unit_test.hpp>
+
+namespace nfd {
+
+static int g_DummyStrategyInfo_count = 0;
+
+class DummyStrategyInfo : public fw::StrategyInfo
+{
+public:
+  DummyStrategyInfo(int id)
+    : m_id(id)
+  {
+    ++g_DummyStrategyInfo_count;
+  }
+  
+  virtual ~DummyStrategyInfo()
+  {
+    --g_DummyStrategyInfo_count;
+  }
+  
+  int m_id;
+};
+
+BOOST_AUTO_TEST_SUITE(TableStrategyInfoHost)
+
+BOOST_AUTO_TEST_CASE(SetGetClear)
+{
+  StrategyInfoHost host;
+  
+  BOOST_CHECK(!static_cast<bool>(host.getStrategyInfo<DummyStrategyInfo>()));
+  
+  g_DummyStrategyInfo_count = 0;
+  
+  shared_ptr<DummyStrategyInfo> info = make_shared<DummyStrategyInfo>(7591);
+  host.setStrategyInfo(info);
+  BOOST_REQUIRE(static_cast<bool>(host.getStrategyInfo<DummyStrategyInfo>()));
+  BOOST_CHECK_EQUAL(host.getStrategyInfo<DummyStrategyInfo>()->m_id, 7591);
+
+  info.reset(); // unlink local reference
+  // host should still have a reference to info
+  BOOST_REQUIRE(static_cast<bool>(host.getStrategyInfo<DummyStrategyInfo>()));
+  BOOST_CHECK_EQUAL(host.getStrategyInfo<DummyStrategyInfo>()->m_id, 7591);
+  
+  host.clearStrategyInfo();
+  BOOST_CHECK(!static_cast<bool>(host.getStrategyInfo<DummyStrategyInfo>()));
+  BOOST_CHECK_EQUAL(g_DummyStrategyInfo_count, 0);
+}
+
+BOOST_AUTO_TEST_SUITE_END()
+
+} // namespace nfd