-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwrappers.h
66 lines (52 loc) · 1.44 KB
/
wrappers.h
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
58
59
60
61
62
63
64
65
66
#ifndef REWRITE_TBD_WRAPPERS_H
#define REWRITE_TBD_WRAPPERS_H
#include <iostream>
#include <stdexcept>
#include <memory>
#include <yaml.h>
#include "yaml.h"
/**
* These wrappers are intended to be the small things to let use write nicer C++
* lets us use C++ resource management. This is far from a libyaml wrapper.
*/
struct yaml_parser_wrapper {
yaml_parser_t yaml_parser{};
yaml_parser_wrapper() {
if (!yaml_parser_initialize(&yaml_parser)) {
throw std::runtime_error{"Failed to initialize yaml parser"};
}
}
~yaml_parser_wrapper() {
yaml_parser_delete(&yaml_parser);
}
};
struct yaml_document_wrapper {
yaml_document_t yaml_document{};
explicit yaml_document_wrapper(yaml_parser_t *parser) {
if (!yaml_parser_load(parser, &yaml_document)) {
throw std::runtime_error{"Failed to parse yaml document"};
}
}
~yaml_document_wrapper() {
yaml_document_delete(&yaml_document);
}
};
struct yaml_emitter_wrapper {
yaml_emitter_t yaml_emitter{};
yaml_emitter_wrapper() {
if (!yaml_emitter_initialize(&yaml_emitter)) {
throw std::runtime_error{"Failed to initialize yaml emitter"};
}
}
~yaml_emitter_wrapper() {
yaml_emitter_delete(&yaml_emitter);
}
};
struct close_file {
void operator()(FILE *f) {
if (f) {
fclose(f);
}
}
};
#endif //REWRITE_TBD_WRAPPERS_H