Lei YU | 7f4fca5 | 2017-02-23 15:15:51 +0800 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include <fstream> |
| 4 | |
| 5 | namespace phosphor |
| 6 | { |
| 7 | namespace time |
| 8 | { |
| 9 | namespace utils |
| 10 | { |
| 11 | |
| 12 | /** @brief Read data with type T from file |
| 13 | * |
| 14 | * @param[in] fileName - The name of file to read from |
| 15 | * |
| 16 | * @return The data with type T |
| 17 | */ |
| 18 | template <typename T> |
| 19 | T readData(const char* fileName) |
| 20 | { |
| 21 | T data{}; |
| 22 | std::ifstream fs(fileName); |
| 23 | if (fs.is_open()) |
| 24 | { |
| 25 | fs >> data; |
| 26 | } |
| 27 | return data; |
| 28 | } |
| 29 | |
| 30 | /** @brief Write data with type T to file |
| 31 | * |
| 32 | * @param[in] fileName - The name of file to write to |
| 33 | * @param[in] data - The data with type T to write to file |
| 34 | */ |
| 35 | template <typename T> |
| 36 | void writeData(const char* fileName, T&& data) |
| 37 | { |
| 38 | std::ofstream fs(fileName, std::ios::out); |
| 39 | if (fs.is_open()) |
| 40 | { |
| 41 | fs << std::forward<T>(data); |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | } |
| 46 | } |
| 47 | } |