-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfmt_ext_test.cpp
111 lines (72 loc) · 2.7 KB
/
fmt_ext_test.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
#include <vector>
#include <array>
#include <map>
#include <gsl/gsl>
#define FMT_HEADER_ONLY
#include <fmt/format.h>
static_assert(FMT_VERSION >= 50000,"Only {fmt} 5.0 or higher is supported.");
#include "fmt_ext.h"
static_assert( fmt_ext::meta::is_range_v<std::vector<int32_t>>);
static_assert( fmt_ext::meta::is_range_v<std::array<int32_t,7>>);
static_assert( fmt_ext::meta::is_tuple_like_v<std::tuple<int32_t,float,std::string>>);
#if 1 /// Configurable formatting.
namespace fmt_ext {
template <typename Char>
struct formatting_range<Char, void> : formatting_base<Char> {
const Char *prefix = "{"; // use c strings for delimiters.
Char delimiter = ','; // use single char for delimiters.
std::string postfix = "}"; // use std::string for delimiters.
bool add_spaces = true;
};
template <typename Char>
struct formatting_tuple<Char, void> : formatting_base<Char> {
Char prefix = '[';
Char delimiter = ',';
Char postfix = ']';
bool add_spaces = true;
};
} // namespace fmt_ext
#endif
struct my_struct {
int32_t i;
std::string str; // can throw
template <std::size_t N>
decltype(auto) get() const noexcept {
if constexpr (N == 0) return i;
else if constexpr (N == 1) return std::string_view{ str };
}
};
template <std::size_t N>
decltype(auto) get(const my_struct& s) noexcept { return s.get<N>(); }
namespace std {
template<>
struct tuple_size<my_struct> : std::integral_constant<std::size_t, 2> {};
template<std::size_t N>
struct tuple_element<N, my_struct> {
using type = decltype(std::declval<my_struct>().get<N>());
};
} // namespace std
static_assert( fmt_ext::meta::is_tuple_like_v<my_struct>);
int main() {
std::vector<int32_t> iv{1,2,3,5,7,11};
auto ivf = fmt::format("{}", iv);
fmt::print(" vector {} = {} \n", iv, ivf);
std::vector<std::vector<int32_t>> ivv{ {1,2},{3,5},{7,11} };
fmt::print(" vector {} \n", ivv);
std::vector<std::tuple<int32_t, int32_t>> ivt{ {1,2},{3,5},{7,11} };
fmt::print(" vector {} \n", ivt);
std::map<std::string, int32_t> simap{ {"one",1}, {"two",2} };
fmt::print(" map<std::string, int32_t> {} \n", simap);
std::pair<int64_t, float> pa1{42, 3.14159265358979f};
fmt::print(" pair {} \n", pa1);
std::tuple<int64_t, float, std::string> tu1{42, 3.14159265358979f, "this is tuple"};
fmt::print(" tuple {} \n", tu1);
fmt::print(" make_tuple {} \n", std::make_tuple(1, 2, 3));
fmt::print(" make_tuple {} \n", std::make_tuple(1, std::string("str"), 3));
my_struct mst{13,"my struct"};
fmt::print(" my_struct {} \n", mst);
std::string sarr[] = { "1","2" };
gsl::span<std::string> sp{sarr};
fmt::print(" span {} \n", sp);
return 0;
}