-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhello.cpp
211 lines (172 loc) · 5.14 KB
/
hello.cpp
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#include <iostream>
#include <type_traits>
#include <cstring>
#include <memory>
#include <utility>
#include <tuple>
#include <array>
#include <functional>
#include <exception>
#include <stdexcept>
#include <iomanip>
#include <cstddef>
#include <cstdint>
#include <cctype>
#define HELLO_NAMESPACE_BEGIN namespace HelloNS {
#define HELLO_NAMESPACE_END }
#define STRINGIFY_DETAIL(x) #x
#define STRINGIFY(x) STRINGIFY_DETAIL(x)
#ifdef __cplusplus
#define CPP_VER __cplusplus
#else
#define CPP_VER 0
#endif
#if CPP_VER < 201103L
#error "A modern C++ compiler is required!"
#endif
#define HELLO_WORLD_VERSION_MAJOR 1
#define HELLO_WORLD_VERSION_MINOR 0
#define HELLO_WORLD_VERSION_PATCH 0
#define CALL_WITH_HELLO_STRING(f, s) f(s)
#define EXPAND_HELLO_STRING "Hello, World!"
#define EXPAND_HELLO_WRAPPER() EXPAND_HELLO_STRING
HELLO_NAMESPACE_BEGIN
template <typename T>
struct OverlyComplicatedAllocator {
using value_type = T;
OverlyComplicatedAllocator() noexcept {}
template <class U> OverlyComplicatedAllocator(const OverlyComplicatedAllocator<U>&) noexcept {}
T* allocate(std::size_t n) {
if (n > std::numeric_limits<std::size_t>::max() / sizeof(T))
throw std::bad_alloc();
return static_cast<T*>(::operator new(n * sizeof(T)));
}
void deallocate(T* p, std::size_t) noexcept {
::operator delete(p);
}
};
template<typename T>
struct is_char_type : std::false_type {};
template<>
struct is_char_type<char> : std::true_type {};
constexpr std::size_t cstring_length(const char* str, std::size_t count = 0) {
return (str[0] == '\0') ? count : cstring_length(str + 1, count + 1);
}
template <std::size_t N>
struct StaticString {
char data_[N + 1] = {};
constexpr StaticString(const char(&arr)[N+1]) {
for (std::size_t i = 0; i < N+1; ++i)
data_[i] = arr[i];
}
constexpr const char* get() const { return data_; }
};
static constexpr StaticString<cstring_length(EXPAND_HELLO_STRING)> helloStaticStr(EXPAND_HELLO_STRING);
struct BasePrinter {
virtual ~BasePrinter() = default;
virtual void print() const = 0;
};
template <typename CharT, typename = typename std::enable_if<is_char_type<CharT>::value>::type>
struct HelloPrinter : public BasePrinter {
std::basic_ostream<CharT>& os_;
HelloPrinter(std::basic_ostream<CharT>& os) : os_(os) {}
void print() const override {
os_ << helloStaticStr.get() << std::endl;
}
};
struct PrinterInvoker {
const BasePrinter& printerRef;
void invoke() const {
printerRef.print();
}
};
struct PrinterFactory {
template<typename CharT>
static PrinterInvoker create(std::basic_ostream<CharT>& os) {
static HelloPrinter<CharT> printer(os);
return PrinterInvoker{ printer };
}
};
template<typename CharT>
struct PrinterSelector {
static PrinterInvoker selectPrinter(std::basic_ostream<CharT>& os) {
return PrinterFactory::create(os);
}
};
using PrinterFunc = void(*)(void);
template<typename CharT>
void complicatedPrintFunction(std::basic_ostream<CharT>& os) {
auto invoker = PrinterSelector<CharT>::selectPrinter(os);
invoker.invoke();
}
template<typename CharT>
void printHelloWithUnneededAbstraction(std::basic_ostream<CharT>& os) {
complicatedPrintFunction(os);
}
template<typename CharT>
struct PrintOnConstruction {
PrintOnConstruction(std::basic_ostream<CharT>& os) {
printHelloWithUnneededAbstraction(os);
}
};
struct ComplexHolder {
std::unique_ptr<int> ptr_;
ComplexHolder() : ptr_(new int(42)) {}
};
struct GlobalInitializer {
GlobalInitializer() {
volatile int x = 0;
x += 10;
}
};
static GlobalInitializer globalInitInstance;
template<typename T>
struct Identity {
using type = T;
};
template<typename T>
using Identity_t = typename Identity<T>::type;
template<bool B, typename CharT>
struct ConditionalPrinter {
static void doPrint(std::basic_ostream<CharT>& os) {
printHelloWithUnneededAbstraction(os);
}
};
template<typename CharT>
struct ConditionalPrinter<false, CharT> {
static void doPrint(std::basic_ostream<CharT>&) {
}
};
constexpr bool shouldPrintHello = true;
template<typename CharT>
void runConditionalPrint(std::basic_ostream<CharT>& os) {
ConditionalPrinter<shouldPrintHello, CharT>::doPrint(os);
}
template<typename CharT>
void finalHelloPrinter(std::basic_ostream<CharT>& os) {
runConditionalPrint(os);
}
int main(int, char**) {
try {
std::vector<char, OverlyComplicatedAllocator<char>> buffer;
buffer.push_back('X');
buffer.clear();
auto lambdaPrinter = [&](auto& output) {
finalHelloPrinter(output);
};
CALL_WITH_HELLO_STRING(lambdaPrinter, std::cout);
std::unique_ptr<int> dummyPtr(new int(123));
*dummyPtr = *dummyPtr + 1;
auto no_op = [](){};
PrinterFunc pf = reinterpret_cast<PrinterFunc>(+no_op);
(void)pf;
} catch (const std::exception& ex) {
std::cerr << "An error occurred: " << ex.what() << std::endl;
return 1;
} catch (...) {
std::cerr << "Unknown error." << std::endl;
return 1;
}
return 0;
}
HELLO_NAMESPACE_END