-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFileIOTemplate.hpp
More file actions
54 lines (42 loc) · 1.63 KB
/
FileIOTemplate.hpp
File metadata and controls
54 lines (42 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#pragma once
#include <fstream>
#include <optional>
#include <string>
namespace FileIO {
inline bool writeTextFile(const std::string& path, const std::string& content) {
std::ofstream out(path, std::ios::out | std::ios::trunc);
if (!out.is_open()) {
return false;
}
out << content;
return !out.fail();
}
inline std::optional<std::string> readTextFile(const std::string& path) { // Useful for file reading because reading may fail
std::ifstream in(path, std::ios::in);
if (!in.is_open()) {
return std::nullopt;
}
std::string data;
std::string line;
while (std::getline(in, line)) {
data += line + "\n";
}
if (in.fail() && !in.eof()) {
return std::nullopt;
}
return data;
}
// Customizable serializer save-load interface (specialize in concrete class modules)
template <typename T> // Function Template
struct Serializer { // Generic pattern for saving/loading of specific classes later
static bool save(const T& object, const std::string& filepath) {
static_assert(sizeof(T) == 0, "Serializer<T> is not specialized for this type"); // Compile time check
return false; // condition will always be false for a real type
}
static std::optional<T> load(const std::string& filepath) {
static_assert(sizeof(T) == 0, "Serializer<T> is not specialized for this type");
return std::nullopt;
}
};
} // namespace FileIO : Scope managing mechanism (Grouping related names)
//Here are common text file helper functions, and here is a generic save/load framework that concrete classes can plug into later.