table: cs::Policy::getPolicyNames

refs #3148

Change-Id: I06f66e9780b9c5c4820ba18cca5fbc9b1061eae0
diff --git a/daemon/table/cs-policy.cpp b/daemon/table/cs-policy.cpp
index 77101ac..4aab572 100644
--- a/daemon/table/cs-policy.cpp
+++ b/daemon/table/cs-policy.cpp
@@ -26,6 +26,8 @@
 #include "cs-policy.hpp"
 #include "cs.hpp"
 #include "core/logger.hpp"
+#include <boost/range/adaptor/map.hpp>
+#include <boost/range/algorithm/copy.hpp>
 
 NFD_LOG_INIT("CsPolicy");
 
@@ -40,13 +42,22 @@
 }
 
 unique_ptr<Policy>
-Policy::create(const std::string& key)
+Policy::create(const std::string& policyName)
 {
   Registry& registry = getRegistry();
-  auto i = registry.find(key);
+  auto i = registry.find(policyName);
   return i == registry.end() ? nullptr : i->second();
 }
 
+std::set<std::string>
+Policy::getPolicyNames()
+{
+  std::set<std::string> policyNames;
+  boost::copy(getRegistry() | boost::adaptors::map_keys,
+              std::inserter(policyNames, policyNames.end()));
+  return policyNames;
+}
+
 Policy::Policy(const std::string& policyName)
   : m_policyName(policyName)
 {
diff --git a/daemon/table/cs-policy.hpp b/daemon/table/cs-policy.hpp
index 2da1979..93e028e 100644
--- a/daemon/table/cs-policy.hpp
+++ b/daemon/table/cs-policy.hpp
@@ -41,18 +41,23 @@
 public: // registry
   template<typename P>
   static void
-  registerPolicy()
+  registerPolicy(const std::string& policyName = P::POLICY_NAME)
   {
-    const std::string& key = P::POLICY_NAME;
     Registry& registry = getRegistry();
-    BOOST_ASSERT(registry.count(key) == 0);
-    registry[key] = [] { return make_unique<P>(); };
+    BOOST_ASSERT(registry.count(policyName) == 0);
+    registry[policyName] = [] { return make_unique<P>(); };
   }
 
-  /** \return a Policy identified by \p key, or nullptr if \p key is unknown
+  /** \return a cs::Policy identified by \p policyName,
+   *          or nullptr if \p policyName is unknown
    */
   static unique_ptr<Policy>
-  create(const std::string& key);
+  create(const std::string& policyName);
+
+  /** \return a list of available policy names
+   */
+  static std::set<std::string>
+  getPolicyNames();
 
 public:
   explicit
@@ -173,7 +178,7 @@
 
 private: // registry
   typedef std::function<unique_ptr<Policy>()> CreateFunc;
-  typedef std::map<std::string, CreateFunc> Registry; // indexed by key
+  typedef std::map<std::string, CreateFunc> Registry; // indexed by policy name
 
   static Registry&
   getRegistry();
diff --git a/tests/daemon/table/cs-policy-lru.t.cpp b/tests/daemon/table/cs-policy-lru.t.cpp
index 71f1764..8813de5 100644
--- a/tests/daemon/table/cs-policy-lru.t.cpp
+++ b/tests/daemon/table/cs-policy-lru.t.cpp
@@ -38,6 +38,12 @@
 BOOST_AUTO_TEST_SUITE(Table)
 BOOST_AUTO_TEST_SUITE(TestCsLru)
 
+BOOST_AUTO_TEST_CASE(Registration)
+{
+  std::set<std::string> policyNames = Policy::getPolicyNames();
+  BOOST_CHECK_EQUAL(policyNames.count("lru"), 1);
+}
+
 BOOST_FIXTURE_TEST_CASE(EvictOne, UnitTestTimeFixture)
 {
   Cs cs(3);
diff --git a/tests/daemon/table/cs-policy-priority-fifo.t.cpp b/tests/daemon/table/cs-policy-priority-fifo.t.cpp
index 01380ef..04a613f 100644
--- a/tests/daemon/table/cs-policy-priority-fifo.t.cpp
+++ b/tests/daemon/table/cs-policy-priority-fifo.t.cpp
@@ -38,6 +38,12 @@
 BOOST_AUTO_TEST_SUITE(Table)
 BOOST_AUTO_TEST_SUITE(TestCsPriorityFifo)
 
+BOOST_AUTO_TEST_CASE(Registration)
+{
+  std::set<std::string> policyNames = Policy::getPolicyNames();
+  BOOST_CHECK_EQUAL(policyNames.count("priority_fifo"), 1);
+}
+
 BOOST_FIXTURE_TEST_CASE(EvictOne, UnitTestTimeFixture)
 {
   Cs cs(3);