-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtest_format.cpp
More file actions
110 lines (91 loc) · 2.55 KB
/
Copy pathtest_format.cpp
File metadata and controls
110 lines (91 loc) · 2.55 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
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
#include "format.hpp"
using format::fmt;
using format::Dict;
using format::sequence;
using format::str;
struct P2d
{
double x, y;
P2d(double x, double y)
: x(x), y(y)
{
}
};
namespace format {
template<>
struct Formatter<P2d>
{
static std::string toString(const P2d& p, const Field& field)
{
return fmt("P2d({x:+0.3f}, {y:+0.3f})") % Dict()
("x", p.x)
("y", p.y);
}
};
}
// Silly example, just to test inheritance
struct P3d : P2d
{
double z;
P3d(double x, double y, double z)
: P2d(x, y), z(z)
{
}
};
#include <iostream>
#include <vector>
#include <list>
#include <set>
int main()
{
std::vector<int> v;
std::list<int> L;
std::set<int> S;
std::map<std::string, int> m;
m["I"] = 1;
m["II"] = 2;
m["III"] = 3;
m["IV"] = 4;
m["V"] = 5;
for (int i=0; i<10; i++)
{
v.push_back(i*i);
L.push_back(i*i);
S.insert(i*i);
}
format::Format fs = fmt("p = {p}\nv = [{v:, }]\n"
"L = {L:->:({*})}\n"
"S = ~{{S: :0x{*:04x}}~}");
Dict fd;
fd ("p", P2d(1234, 0xbadf00d))
("v", sequence(v))
("L", sequence(L))
("S", sequence(S));
std::cout << fs % fd << std::endl;
std::cout << fmt("{1}\n{2}\n{3}\n{4}\n") % Dict()
("1", v)
("2", L)
("3", S)
("4", m);
std::string lines[] = {"This", "is a test", "for the string formatting options"};
std::cout << fmt("{L:\n:{*:=60}}") % Dict()("L", sequence(&lines[0], &lines[3])) << std::endl;
std::cout << fmt("{n:@(###)-########}") % Dict()("n", std::string("555123456789012")) << std::endl;
std::cout << fmt("{m:\n:{*::{*1:=8l} => {*2:08/2}}}") % Dict()("m", sequence(m)) << std::endl;
std::cout << fmt("{s:>30=.}") % Dict()("s", "This is a C string") << std::endl;
std::cout << fmt("{v}") % Dict()("v", &v) << std::endl;
P3d p3(11, 21, 31);
try
{
std::cout << fmt("{p}") % Dict()("p", p3) << std::endl;
printf("WE GOT A PROBLEM\n");
}
catch(const std::runtime_error& re)
{
std::cout << fmt("Exception thrown as expected --> {s}\n") % Dict()("s", re.what());
}
std::cout << fmt("{p}") % Dict()("p", static_cast<P2d *>(&p3)) << std::endl; // Formatted as P2d
std::cout << str(3.141592654) << std::endl;
std::cout << str(std::string("This is a test"), "{*:=40==}") << std::endl;
std::cout << str(175676, "09X,4~:") << std::endl;
return 0;
}