table: StrategyInfoHost stores StrategyInfo as unique_ptr
refs #3205
Change-Id: Ia194ca94717347848d257096549cfec5df1ba6c9
diff --git a/daemon/table/strategy-info-host.hpp b/daemon/table/strategy-info-host.hpp
index 23b5fcc..db88558 100644
--- a/daemon/table/strategy-info-host.hpp
+++ b/daemon/table/strategy-info-host.hpp
@@ -40,7 +40,7 @@
* \return an existing StrategyInfo item of type T, or nullptr if it does not exist
*/
template<typename T>
- shared_ptr<T>
+ T*
getStrategyInfo() const
{
static_assert(std::is_base_of<fw::StrategyInfo, T>::value,
@@ -50,44 +50,41 @@
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 nfd::fw::StrategyInfo
- */
- template<typename T>
- void
- 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;
- }
+ return static_cast<T*>(it->second.get());
}
/** \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
+ * \return a new or existing StrategyInfo item of type T,
+ * and true for new item, false for existing item
*/
template<typename T, typename ...A>
- shared_ptr<T>
+ std::pair<T*, bool>
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);
+ unique_ptr<fw::StrategyInfo>& item = m_items[T::getTypeId()];
+ bool isNew = (item == nullptr);
+ if (isNew) {
+ item.reset(new T(std::forward<A>(args)...));
}
- return item;
+ return {static_cast<T*>(item.get()), isNew};
+ }
+
+ /** \brief erase a StrategyInfo item
+ * \tparam T type of StrategyInfo, must be a subclass of fw::StrategyInfo
+ * \return number of items erased
+ */
+ template<typename T>
+ size_t
+ eraseStrategyInfo()
+ {
+ static_assert(std::is_base_of<fw::StrategyInfo, T>::value,
+ "T must inherit from StrategyInfo");
+
+ return m_items.erase(T::getTypeId());
}
/** \brief clear all StrategyInfo items
@@ -96,7 +93,7 @@
clearStrategyInfo();
private:
- std::map<int, shared_ptr<fw::StrategyInfo>> m_items;
+ std::unordered_map<int, unique_ptr<fw::StrategyInfo>> m_items;
};
} // namespace nfd