Deepak Kodihalli | 6620e98 | 2017-08-05 13:09:54 -0500 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include <cereal/archives/json.hpp> |
| 4 | #include <experimental/filesystem> |
| 5 | #include <fstream> |
| 6 | #include "config.h" |
| 7 | |
| 8 | namespace cereal |
| 9 | { |
| 10 | |
| 11 | namespace fs = std::experimental::filesystem; |
| 12 | |
| 13 | using Path = std::string; |
| 14 | using Interface = std::string; |
| 15 | |
| 16 | /** @brief Serialize inventory item |
| 17 | * |
| 18 | * @param[in] path - DBus object path |
| 19 | * @param[in] iface - Inventory interface name |
| 20 | * @param[in] object - Object to be serialized |
| 21 | */ |
| 22 | template <typename T> |
| 23 | inline void serialize(const Path& path, const Interface& iface, const T& object) |
| 24 | { |
| 25 | fs::path p(PIM_PERSIST_PATH); |
| 26 | p /= path; |
| 27 | fs::create_directories(p); |
| 28 | p /= iface; |
| 29 | std::ofstream os(p, std::ios::binary); |
| 30 | cereal::JSONOutputArchive oarchive(os); |
| 31 | oarchive(object); |
| 32 | } |
| 33 | |
Deepak Kodihalli | b28990f | 2017-08-08 07:19:34 -0500 | [diff] [blame] | 34 | /** @brief Deserialize inventory item |
| 35 | * |
| 36 | * @param[in] path - DBus object path |
| 37 | * @param[in] iface - Inventory interface name |
| 38 | * @param[in] object - Object to be serialized |
| 39 | */ |
| 40 | template <typename T> |
| 41 | inline void deserialize( |
| 42 | const Path& path, const Interface& iface, T& object) |
| 43 | { |
| 44 | fs::path p(PIM_PERSIST_PATH); |
| 45 | p /= path; |
| 46 | p /= iface; |
| 47 | if (fs::exists(p)) |
| 48 | { |
| 49 | std::ifstream is(p, std::ios::in | std::ios::binary); |
| 50 | cereal::JSONInputArchive iarchive(is); |
| 51 | iarchive(object); |
| 52 | } |
| 53 | } |
| 54 | |
Deepak Kodihalli | 6620e98 | 2017-08-05 13:09:54 -0500 | [diff] [blame] | 55 | } // namespace cereal |