-
Notifications
You must be signed in to change notification settings - Fork 0
/
noexcept_io.hpp
57 lines (47 loc) · 1.42 KB
/
noexcept_io.hpp
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
55
56
57
#pragma once
#include <string>
#include <vector>
#include <fstream>
#include <stdint.h>
#include <iostream>
#include "utility.hpp"
using std::string;
namespace noexcept_io {
const int BYTE_SIZE = 8;
class noexcept_stream {
protected:
public:
std::fstream stream;
virtual ~noexcept_stream();
virtual maybe_error open(const string& filename) noexcept = 0;
};
class noexcept_reader : noexcept_stream {
std::ifstream stream;
std::vector<char> raw;
int cur_ind = 0;
public:
maybe_error open(const string& filename) noexcept override;
or_error<char> read_char() noexcept;
or_error<int> read_int() noexcept;
void skip_spaces() noexcept;
};
class noexcept_writer : noexcept_stream {
std::ofstream stream;
public:
maybe_error open(const string& filename) noexcept override;
template<typename T>
maybe_error write(T val) noexcept {
if (!(stream << val).good())
return error("Couldn't write requeted type.");
return ok();
}
template<typename T>
maybe_error writeln(T val) noexcept {
if (!(stream << val).good())
return error("Couldn't write requeted type.");
if (!stream.put(char(10)).good())
return error("Couldn't write next line.");
return ok();
}
};
}