blob: 058961308e527773f0d47554e88c7b142ae2d21f [file] [log] [blame]
Lei YU7f4fca52017-02-23 15:15:51 +08001#pragma once
2
3#include <fstream>
4
5namespace phosphor
6{
7namespace time
8{
9namespace 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 */
18template <typename T>
19T 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 */
35template <typename T>
36void 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}