diff options
author | Brad Bishop <bradleyb@fuzziesquirrel.com> | 2018-12-12 21:40:26 -0500 |
---|---|---|
committer | Brad Bishop <bradleyb@fuzziesquirrel.com> | 2019-01-07 15:59:43 -0500 |
commit | 7dfd08f8c4e4e0fb19378e9098dffc6012ee1dac (patch) | |
tree | 0f82e57bb2887b3c7311ff1d4076c3b76055bc0b | |
parent | 35aba2347fd0ffbbaf704d4cc067304881348182 (diff) | |
download | phosphor-inventory-manager-7dfd08f8c4e4e0fb19378e9098dffc6012ee1dac.tar.gz phosphor-inventory-manager-7dfd08f8c4e4e0fb19378e9098dffc6012ee1dac.zip |
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>
-rw-r--r-- | manager.hpp | 7 | ||||
-rw-r--r-- | serialize.hpp | 131 |
2 files changed, 75 insertions, 63 deletions
diff --git a/manager.hpp b/manager.hpp index c9f14a0..93957d9 100644 --- a/manager.hpp +++ b/manager.hpp @@ -105,14 +105,14 @@ void propSerialize(const std::string& path, const std::string& iface, 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 @@ void propDeSerialize(const std::string& path, const std::string& iface, 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) -{ - 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) +struct SerialOps { - 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 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) + { + 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 + * + * @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) { - if (fs::exists(p)) - { - std::ifstream is(p, std::ios::in | std::ios::binary); - cereal::JSONInputArchive iarchive(is); - iarchive(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); } - catch (cereal::Exception& e) + + static void deserialize(const std::string&, const std::string&) { - log<level::ERR>(e.what()); - fs::remove(p); + // This is intentionally a noop. } -} -} // namespace cereal + /** @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 + { + 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); + } + } +}; +} // namespace manager +} // namespace inventory +} // namespace phosphor |