table: StrategyChoice::insert return value includes error description
The error description is used in the response to a strategy-choice/set
management command failure.
refs #3868
Change-Id: I14e0eb4dc311806a90ebfe60fa17177d2809d104
diff --git a/daemon/table/strategy-choice.cpp b/daemon/table/strategy-choice.cpp
index 7e2f5bf..77f0bf5 100644
--- a/daemon/table/strategy-choice.cpp
+++ b/daemon/table/strategy-choice.cpp
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/**
- * Copyright (c) 2014-2016, Regents of the University of California,
+ * Copyright (c) 2014-2017, Regents of the University of California,
* Arizona Board of Regents,
* Colorado State University,
* University Pierre & Marie Curie, Sorbonne University,
@@ -66,7 +66,7 @@
++m_nItems;
}
-bool
+StrategyChoice::InsertResult
StrategyChoice::insert(const Name& prefix, const Name& strategyName)
{
unique_ptr<Strategy> strategy;
@@ -75,12 +75,12 @@
}
catch (const std::invalid_argument& e) {
NFD_LOG_ERROR("insert(" << prefix << "," << strategyName << ") cannot create strategy: " << e.what());
- return false;
+ return InsertResult(InsertResult::EXCEPTION, e.what());
}
if (strategy == nullptr) {
NFD_LOG_ERROR("insert(" << prefix << "," << strategyName << ") strategy not registered");
- return false;
+ return InsertResult::NOT_REGISTERED;
}
name_tree::Entry& nte = m_nameTree.lookup(prefix);
@@ -89,7 +89,7 @@
if (entry != nullptr) {
if (entry->getStrategyInstanceName() == strategy->getInstanceName()) {
NFD_LOG_TRACE("insert(" << prefix << ") not changing " << strategy->getInstanceName());
- return true;
+ return InsertResult::OK;
}
oldStrategy = &entry->getStrategy();
NFD_LOG_TRACE("insert(" << prefix << ") changing from " << oldStrategy->getInstanceName() <<
@@ -106,7 +106,27 @@
this->changeStrategy(*entry, *oldStrategy, *strategy);
entry->setStrategy(std::move(strategy));
- return true;
+ return InsertResult::OK;
+}
+
+StrategyChoice::InsertResult::InsertResult(Status status, const std::string& exceptionMessage)
+ : m_status(status)
+ , m_exceptionMessage(exceptionMessage)
+{
+}
+
+std::ostream&
+operator<<(std::ostream& os, const StrategyChoice::InsertResult& res)
+{
+ switch (res.m_status) {
+ case StrategyChoice::InsertResult::OK:
+ return os << "OK";
+ case StrategyChoice::InsertResult::NOT_REGISTERED:
+ return os << "Strategy not registered";
+ case StrategyChoice::InsertResult::EXCEPTION:
+ return os << "Error instantiating strategy: " << res.m_exceptionMessage;
+ }
+ return os;
}
void
diff --git a/daemon/table/strategy-choice.hpp b/daemon/table/strategy-choice.hpp
index e007acf..4e59716 100644
--- a/daemon/table/strategy-choice.hpp
+++ b/daemon/table/strategy-choice.hpp
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/**
- * Copyright (c) 2014-2016, Regents of the University of California,
+ * Copyright (c) 2014-2017, Regents of the University of California,
* Arizona Board of Regents,
* Colorado State University,
* University Pierre & Marie Curie, Sorbonne University,
@@ -68,17 +68,46 @@
setDefaultStrategy(const Name& strategyName);
public: // Strategy Choice table
- /** \brief set strategy of prefix to be strategyName
- * \param prefix the name prefix for which \p strategyName should be used
- * \param strategyName the strategy to be used
- * \return true on success
- *
- * This method set a strategy onto a Name prefix.
- * The strategy must have been installed.
- * The strategyName can either be exact (contains version component),
- * or omit the version component to pick the latest version.
+ class InsertResult
+ {
+ public:
+ explicit
+ operator bool() const
+ {
+ return m_status == OK;
+ }
+
+ bool
+ isRegistered() const
+ {
+ return m_status != NOT_REGISTERED;
+ }
+
+ private:
+ enum Status {
+ OK,
+ NOT_REGISTERED,
+ EXCEPTION
+ };
+
+ // implicitly constructible from Status
+ InsertResult(Status status, const std::string& exceptionMessage = "");
+
+ private:
+ Status m_status;
+ std::string m_exceptionMessage;
+
+ friend class StrategyChoice;
+ friend std::ostream& operator<<(std::ostream&, const InsertResult&);
+ };
+
+ /** \brief set strategy of \p prefix to be \p strategyName
+ * \param prefix the name prefix to change strategy
+ * \param strategyName strategy instance name, may contain version and parameters;
+ * strategy must have been registered
+ * \return convertible to true on success; convertible to false on failure
*/
- bool
+ InsertResult
insert(const Name& prefix, const Name& strategyName);
/** \brief make prefix to inherit strategy from its parent
@@ -159,6 +188,9 @@
size_t m_nItems;
};
+std::ostream&
+operator<<(std::ostream& os, const StrategyChoice::InsertResult& res);
+
} // namespace strategy_choice
using strategy_choice::StrategyChoice;