Deepak Kodihalli | 6620e98 | 2017-08-05 13:09:54 -0500 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Patrick Venture | a680d1e | 2018-10-14 13:34:26 -0700 | [diff] [blame] | 3 | #include "config.h" |
| 4 | |
Deepak Kodihalli | 6620e98 | 2017-08-05 13:09:54 -0500 | [diff] [blame] | 5 | #include <cereal/archives/json.hpp> |
| 6 | #include <experimental/filesystem> |
| 7 | #include <fstream> |
Jayanth Othayoth | 86ad3c6 | 2017-09-19 22:18:45 -0500 | [diff] [blame] | 8 | #include <phosphor-logging/log.hpp> |
| 9 | |
Deepak Kodihalli | 6620e98 | 2017-08-05 13:09:54 -0500 | [diff] [blame] | 10 | namespace cereal |
| 11 | { |
| 12 | |
| 13 | namespace fs = std::experimental::filesystem; |
| 14 | |
| 15 | using Path = std::string; |
| 16 | using Interface = std::string; |
Jayanth Othayoth | 86ad3c6 | 2017-09-19 22:18:45 -0500 | [diff] [blame] | 17 | using namespace phosphor::logging; |
Deepak Kodihalli | 6620e98 | 2017-08-05 13:09:54 -0500 | [diff] [blame] | 18 | |
| 19 | /** @brief Serialize inventory item |
| 20 | * |
| 21 | * @param[in] path - DBus object path |
| 22 | * @param[in] iface - Inventory interface name |
| 23 | * @param[in] object - Object to be serialized |
| 24 | */ |
| 25 | template <typename T> |
| 26 | inline void serialize(const Path& path, const Interface& iface, const T& object) |
| 27 | { |
| 28 | fs::path p(PIM_PERSIST_PATH); |
| 29 | p /= path; |
| 30 | fs::create_directories(p); |
| 31 | p /= iface; |
| 32 | std::ofstream os(p, std::ios::binary); |
| 33 | cereal::JSONOutputArchive oarchive(os); |
| 34 | oarchive(object); |
| 35 | } |
| 36 | |
Marri Devender Rao | fa23d70 | 2017-09-02 04:43:42 -0500 | [diff] [blame] | 37 | /** @brief Serialize inventory item path |
| 38 | * Serializing only path for an empty interface to be consistent |
| 39 | * interfaces. |
| 40 | * @param[in] path - DBus object path |
| 41 | * @param[in] iface - Inventory interface name |
| 42 | */ |
| 43 | inline void serialize(const Path& path, const Interface& iface) |
| 44 | { |
| 45 | fs::path p(PIM_PERSIST_PATH); |
| 46 | p /= path; |
| 47 | fs::create_directories(p); |
| 48 | p /= iface; |
| 49 | std::ofstream os(p, std::ios::binary); |
| 50 | } |
| 51 | |
Deepak Kodihalli | b28990f | 2017-08-08 07:19:34 -0500 | [diff] [blame] | 52 | /** @brief Deserialize inventory item |
| 53 | * |
| 54 | * @param[in] path - DBus object path |
| 55 | * @param[in] iface - Inventory interface name |
| 56 | * @param[in] object - Object to be serialized |
| 57 | */ |
| 58 | template <typename T> |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame] | 59 | inline void deserialize(const Path& path, const Interface& iface, T& object) |
Deepak Kodihalli | b28990f | 2017-08-08 07:19:34 -0500 | [diff] [blame] | 60 | { |
| 61 | fs::path p(PIM_PERSIST_PATH); |
| 62 | p /= path; |
| 63 | p /= iface; |
Jayanth Othayoth | 86ad3c6 | 2017-09-19 22:18:45 -0500 | [diff] [blame] | 64 | try |
Deepak Kodihalli | b28990f | 2017-08-08 07:19:34 -0500 | [diff] [blame] | 65 | { |
Jayanth Othayoth | 86ad3c6 | 2017-09-19 22:18:45 -0500 | [diff] [blame] | 66 | if (fs::exists(p)) |
| 67 | { |
| 68 | std::ifstream is(p, std::ios::in | std::ios::binary); |
| 69 | cereal::JSONInputArchive iarchive(is); |
| 70 | iarchive(object); |
| 71 | } |
| 72 | } |
Brad Bishop | 615b2a8 | 2018-03-29 10:32:41 -0400 | [diff] [blame] | 73 | catch (cereal::Exception& e) |
Jayanth Othayoth | 86ad3c6 | 2017-09-19 22:18:45 -0500 | [diff] [blame] | 74 | { |
| 75 | log<level::ERR>(e.what()); |
| 76 | fs::remove(p); |
Deepak Kodihalli | b28990f | 2017-08-08 07:19:34 -0500 | [diff] [blame] | 77 | } |
| 78 | } |
| 79 | |
Deepak Kodihalli | 6620e98 | 2017-08-05 13:09:54 -0500 | [diff] [blame] | 80 | } // namespace cereal |