serialize: add concept API
Wrap serialization methods in a struct to match a yet to be consumed
serialization template concept API.
Change-Id: I4be1749f693ea5fe116bbac581428972e7670791
Signed-off-by: Brad Bishop <bradleyb@fuzziesquirrel.com>
diff --git a/manager.hpp b/manager.hpp
index c9f14a0..93957d9 100644
--- a/manager.hpp
+++ b/manager.hpp
@@ -105,14 +105,14 @@
const std::any& holder)
{
const auto& object = *std::any_cast<const std::shared_ptr<T>&>(holder);
- cereal::serialize(path, iface, object);
+ SerialOps::serialize(path, iface, object);
}
template <typename T, std::enable_if_t<!HasProperties<T>::value, bool> = false>
void propSerialize(const std::string& path, const std::string& iface,
const std::any& holder)
{
- cereal::serialize(path, iface);
+ SerialOps::serialize(path, iface);
}
template <typename T, std::enable_if_t<HasProperties<T>::value, bool> = true>
@@ -120,13 +120,14 @@
std::any& holder)
{
auto& object = *std::any_cast<std::shared_ptr<T>&>(holder);
- cereal::deserialize(path, iface, object);
+ SerialOps::deserialize(path, iface, object);
}
template <typename T, std::enable_if_t<!HasProperties<T>::value, bool> = false>
void propDeSerialize(const std::string& path, const std::string& iface,
std::any& holder)
{
+ SerialOps::deserialize(path, iface);
}
/** @struct MakeInterface
diff --git a/serialize.hpp b/serialize.hpp
index e0fc89a..e724919 100644
--- a/serialize.hpp
+++ b/serialize.hpp
@@ -7,74 +7,85 @@
#include <fstream>
#include <phosphor-logging/log.hpp>
-namespace cereal
+namespace phosphor
+{
+namespace inventory
+{
+namespace manager
{
namespace fs = std::experimental::filesystem;
-using Path = std::string;
-using Interface = std::string;
-using namespace phosphor::logging;
-
-/** @brief Serialize inventory item
- *
- * @param[in] path - DBus object path
- * @param[in] iface - Inventory interface name
- * @param[in] object - Object to be serialized
- */
-template <typename T>
-inline void serialize(const Path& path, const Interface& iface, const T& object)
+struct SerialOps
{
- fs::path p(PIM_PERSIST_PATH);
- p /= path;
- fs::create_directories(p);
- p /= iface;
- std::ofstream os(p, std::ios::binary);
- cereal::JSONOutputArchive oarchive(os);
- oarchive(object);
-}
-
-/** @brief Serialize inventory item path
- * Serializing only path for an empty interface to be consistent
- * interfaces.
- * @param[in] path - DBus object path
- * @param[in] iface - Inventory interface name
- */
-inline void serialize(const Path& path, const Interface& iface)
-{
- fs::path p(PIM_PERSIST_PATH);
- p /= path;
- fs::create_directories(p);
- p /= iface;
- std::ofstream os(p, std::ios::binary);
-}
-
-/** @brief Deserialize inventory item
- *
- * @param[in] path - DBus object path
- * @param[in] iface - Inventory interface name
- * @param[in] object - Object to be serialized
- */
-template <typename T>
-inline void deserialize(const Path& path, const Interface& iface, T& object)
-{
- fs::path p(PIM_PERSIST_PATH);
- p /= path;
- p /= iface;
- try
+ /** @brief Serialize inventory item path
+ * Serializing only path for an empty interface to be consistent
+ * interfaces.
+ * @param[in] path - DBus object path
+ * @param[in] iface - Inventory interface name
+ */
+ static void serialize(const std::string& path, const std::string& iface)
{
- if (fs::exists(p))
+ fs::path p(PIM_PERSIST_PATH);
+ p /= path;
+ fs::create_directories(p);
+ p /= iface;
+ std::ofstream os(p, std::ios::binary);
+ }
+
+ /** @brief Serialize inventory item
+ *
+ * @param[in] path - DBus object path
+ * @param[in] iface - Inventory interface name
+ * @param[in] object - Object to be serialized
+ */
+ template <typename T>
+ static void serialize(const std::string& path, const std::string& iface,
+ const T& object)
+ {
+ fs::path p(PIM_PERSIST_PATH);
+ p /= path;
+ fs::create_directories(p);
+ p /= iface;
+ std::ofstream os(p, std::ios::binary);
+ cereal::JSONOutputArchive oarchive(os);
+ oarchive(object);
+ }
+
+ static void deserialize(const std::string&, const std::string&)
+ {
+ // This is intentionally a noop.
+ }
+
+ /** @brief Deserialize inventory item
+ *
+ * @param[in] path - DBus object path
+ * @param[in] iface - Inventory interface name
+ * @param[in] object - Object to be serialized
+ */
+ template <typename T>
+ static void deserialize(const std::string& path, const std::string& iface,
+ T& object)
+ {
+ fs::path p(PIM_PERSIST_PATH);
+ p /= path;
+ p /= iface;
+ try
{
- std::ifstream is(p, std::ios::in | std::ios::binary);
- cereal::JSONInputArchive iarchive(is);
- iarchive(object);
+ if (fs::exists(p))
+ {
+ std::ifstream is(p, std::ios::in | std::ios::binary);
+ cereal::JSONInputArchive iarchive(is);
+ iarchive(object);
+ }
+ }
+ catch (cereal::Exception& e)
+ {
+ phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
+ fs::remove(p);
}
}
- catch (cereal::Exception& e)
- {
- log<level::ERR>(e.what());
- fs::remove(p);
- }
-}
-
-} // namespace cereal
+};
+} // namespace manager
+} // namespace inventory
+} // namespace phosphor