-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
72 lines (62 loc) · 1.62 KB
/
main.cpp
File metadata and controls
72 lines (62 loc) · 1.62 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <iostream>
#include <cstdio>
#include <cstring>
#include "CWrapper.hpp"
using namespace CW;
class stdc_error : public std::exception
{
int error_number = errno;
public:
virtual const char * what() const noexcept override
{
return strerror(error_number);
}
};
// this one is here only to test detecting copy_func.
//FILE * copy_file(const FILE *)
//{
// return nullptr;
//}
// This controller class is enough to provide
// RAII, move semantics, .get() and throws bad_alloc.
struct FileWrapperBasic
{
static constexpr auto ctor_func = fopen;
static constexpr auto dtor_func = fclose;
};
// This class shows the flexibility of CWrapper,
// using all the features of controller class.
class FileWrapperAdvanced
{
friend CWrapperFriend<FILE*, FileWrapperAdvanced>;
static FILE* ctor_func(const char* filename)
{
return fopen(filename, "rt");
}
static FILE* ctor_func(const char* filename, const char* mode)
{
return fopen(filename, mode);
}
static constexpr auto dtor_func = fclose;
// static constexpr auto copy_func = copy_file;
using exception = stdc_error;
static constexpr FILE* invalid_value = nullptr;
static bool validate_func(FILE* ptr)
{
std::cout << "Validating" << std::endl;
return ptr != nullptr;
}
};
int main() try
{
using FileWrapper = CWrapper<FILE*, FileWrapperAdvanced>;
FileWrapper file("you/shall/not/path", "rt");
fprintf(file.get(), "Hello Unified CWrapper!");
// FileWrapper file2(file);
// (void)file2;
return 0;
}
catch(std::exception& x)
{
std::cout << x.what();
}