Use std::any
Replace holder types with std::any.
Change-Id: I0cd8c2804ad4bff79a0cfe93589e699afb095e5f
Signed-off-by: Brad Bishop <bradleyb@fuzziesquirrel.com>
diff --git a/manager.cpp b/manager.cpp
index 94748ad..83923d9 100644
--- a/manager.cpp
+++ b/manager.cpp
@@ -248,14 +248,15 @@
}
}
-details::holder::Base& Manager::getInterfaceHolder(
+any_ns::any& Manager::getInterfaceHolder(
const char* path, const char* interface)
{
- return const_cast<const Manager*>(
- this)->getInterfaceHolder(path, interface);
+ return const_cast<any_ns::any&>(
+ const_cast<const Manager*>(
+ this)->getInterfaceHolder(path, interface));
}
-details::holder::Base& Manager::getInterfaceHolder(
+const any_ns::any& Manager::getInterfaceHolder(
const char* path, const char* interface) const
{
std::string p{path};
@@ -270,7 +271,7 @@
throw std::runtime_error(
"interface was not found");
- return *iit->second;
+ return iit->second;
}
} // namespace manager
diff --git a/manager.hpp b/manager.hpp
index 9d83ee8..7127ec5 100644
--- a/manager.hpp
+++ b/manager.hpp
@@ -36,18 +36,13 @@
template <typename T>
struct MakeInterface
{
- static std::unique_ptr<details::holder::Base> make(
+ static any_ns::any make(
sdbusplus::bus::bus& bus,
const char* path,
const Interface& props)
{
// TODO: pass props to import constructor...
- using HolderType = holder::Holder<std::unique_ptr<T>>;
- return HolderType::template make_unique<HolderType>(
- std::forward<std::unique_ptr<T>>(
- std::make_unique<T>(
- std::forward<decltype(bus)>(bus),
- std::forward<decltype(path)>(path))));
+ return any_ns::any(std::make_shared<T>(bus, path));
}
};
} // namespace details
@@ -128,8 +123,7 @@
decltype(auto) invokeMethod(const char* path, const char* interface,
U&& member, Args&& ...args)
{
- auto& holder = getInterface<std::unique_ptr<T>>(path, interface);
- auto& iface = *holder.get();
+ auto& iface = getInterface<T>(path, interface);
return (iface.*member)(std::forward<Args>(args)...);
}
@@ -142,8 +136,7 @@
using SigArg = SigArgs::value_type::element_type;
private:
- using HolderPtr = std::unique_ptr<details::holder::Base>;
- using InterfaceComposite = std::map<std::string, HolderPtr>;
+ using InterfaceComposite = std::map<std::string, any_ns::any>;
using ObjectReferences = std::map<std::string, InterfaceComposite>;
using Events = std::vector<EventInfo>;
@@ -165,9 +158,9 @@
*
* @returns A weak reference to the holder instance.
*/
- details::holder::Base& getInterfaceHolder(
+ const any_ns::any& getInterfaceHolder(
const char*, const char*) const;
- details::holder::Base& getInterfaceHolder(
+ any_ns::any& getInterfaceHolder(
const char*, const char*);
/** @brief Provides weak references to interface holders.
@@ -184,15 +177,13 @@
auto& getInterface(const char* path, const char* interface)
{
auto& holder = getInterfaceHolder(path, interface);
- return static_cast <
- details::holder::Holder<T>& >(holder);
+ return *any_ns::any_cast<std::shared_ptr<T> &>(holder);
}
template<typename T>
auto& getInterface(const char* path, const char* interface) const
{
auto& holder = getInterfaceHolder(path, interface);
- return static_cast <
- const details::holder::Holder<T>& >(holder);
+ return *any_ns::any_cast<T>(holder);
}
/** @brief Provided for testing only. */
diff --git a/types.hpp b/types.hpp
index ce43d11..fff52d5 100644
--- a/types.hpp
+++ b/types.hpp
@@ -3,6 +3,7 @@
#include <map>
#include <string>
#include <sdbusplus/message.hpp>
+#include <experimental/any>
namespace phosphor
{
@@ -11,6 +12,8 @@
namespace manager
{
+namespace any_ns = std::experimental;
+
/** @brief Inventory manager supported property types. */
using InterfaceVariantType =
sdbusplus::message::variant<bool, int64_t, std::string>;
diff --git a/utils.hpp b/utils.hpp
index 837c82f..33de439 100644
--- a/utils.hpp
+++ b/utils.hpp
@@ -6,94 +6,7 @@
{
namespace manager
{
-namespace details
-{
-namespace holder
-{
-/** @struct Base
- * @brief Adapt from any type base class.
- *
- * Provides an un-templated base class for use with an adapt to any type
- * adapter to enable containers of mixed types.
- */
-struct Base
-{
- Base() = default;
- virtual ~Base() = default;
- Base(const Base&) = delete;
- Base& operator=(const Base&) = delete;
- Base(Base&&) = default;
- Base& operator=(Base&&) = default;
-};
-
-/** @struct Holder
- * @brief Adapt from any type.
- *
- * Adapts any type to enable containers of mixed types.
- *
- * @tparam T - The adapted type.
- */
-template <typename T>
-struct Holder : public Base
-{
- Holder() = delete;
- virtual ~Holder() = default;
- Holder(const Holder&) = delete;
- Holder& operator=(const Holder&) = delete;
- Holder(Holder&&) = default;
- Holder& operator=(Holder&&) = default;
- explicit Holder(T&& held) : _held(std::forward<T>(held)) {}
-
- /** @brief Construct an adapter.
- *
- * @param[in] held - The object to be adapted.
- *
- * @returns - std::unique pointer to the adapted object.
- *
- * @tparam Ret - The type of the pointer to be returned.
- * @tparam Held - The type of the object to be adapted.
- */
- template <typename Ret, typename Held>
- static auto make_unique(Held&& held)
- {
- return std::make_unique<Ret>(
- std::forward<Held>(held));
- }
-
- /** @brief Construct an adapter.
- *
- * @param[in] held - The object to be adapted.
- *
- * @returns - std::shared pointer to the adapted object.
- *
- * @tparam Ret - The type of the pointer to be returned.
- * @tparam Held - The type of the object to be adapted.
- */
- template <typename Ret, typename Held>
- static auto make_shared(Held&& held)
- {
- return std::make_shared<Ret>(
- std::forward<Held>(held));
- }
-
- /** @brief Provides a weak reference to the held interface. */
- T& get()
- {
- return _held;
- }
-
- /** @brief Provides a weak reference to the held interface. */
- const T& get() const
- {
- return _held;
- }
-
- protected:
- T _held;
-};
-} // namespace holder
-} // namespace details
} // namespace manager
} // namespace inventory
} // namespace phosphor