-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathratio_to_string.hpp
61 lines (51 loc) · 2.2 KB
/
ratio_to_string.hpp
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
#ifndef talbi_RATIO_TO_STRING_HPP
#define talbi_RATIO_TO_STRING_HPP
#include <string>
#include "static_false.hpp"
#include <ratio>
/*
* Given a ratio type R, ratio_to_string<R>() will return the string representing that ratio,
* e.g.:
* - ratio_to_string<std::micro>() => µ
* - ratio_to_string<std::milli>() => m
*
* > suffixes come from here: https://en.cppreference.com/w/cpp/chrono/duration/operator_ltlt.
*
* It might be handy to `#define talbi__DISABLE_TIME_SPECIALIZATIONS` before including if
* one wishes to disable the template specializations for std::ratio<.>s defining minute, hour, day.
*
* Since for some reason this is not implemented globally yet and only works for std::duration<.> .. :/
* */
template<class R>
constexpr const char* ratio_to_string(void);
#define talbi__define_ratio_to_string(T, L) template<> constexpr const char* ratio_to_string<T>(void) { return L; }
talbi__define_ratio_to_string(std::atto, "a")
talbi__define_ratio_to_string(std::femto, "f")
talbi__define_ratio_to_string(std::pico, "p")
talbi__define_ratio_to_string(std::nano, "n")
talbi__define_ratio_to_string(std::micro, "µ")
talbi__define_ratio_to_string(std::milli, "m")
talbi__define_ratio_to_string(std::centi, "c")
talbi__define_ratio_to_string(std::deci, "d")
talbi__define_ratio_to_string(std::ratio<1>, "")
talbi__define_ratio_to_string(std::deca, "da")
talbi__define_ratio_to_string(std::hecto, "h")
talbi__define_ratio_to_string(std::kilo, "k")
talbi__define_ratio_to_string(std::mega, "M")
talbi__define_ratio_to_string(std::giga, "G")
talbi__define_ratio_to_string(std::tera, "T")
talbi__define_ratio_to_string(std::peta, "P")
talbi__define_ratio_to_string(std::exa, "E")
#ifndef talbi__DISABLE_TIME_SPECIALIZATIONS
talbi__define_ratio_to_string(std::ratio<60>, "min")
talbi__define_ratio_to_string(std::ratio<3600>, "h")
talbi__define_ratio_to_string(std::ratio<86400>, "day")
#endif
#undef talbi__define_ratio_to_string
template<class R>
constexpr const char* ratio_to_string(void) {
static_assert(talbi::static_false<R>::value,
"talbi::ratio_to_string() is not implemented for custom ratio types (i.e., those not defined by <ratio>)");
return nullptr;
}
#endif //talbi_RATIO_TO_STRING_HPP