repr 0.1
Reconstructable string representations and more
Loading...
Searching...
No Matches
fundamental.h
Go to the documentation of this file.
1#pragma once
2#include <string>
3#include <cmath>
5
6namespace librepr {
7
8// Boolean type
9inline std::string repr(bool obj) {
10 return obj ? "true" : "false";
11}
12
13// Integer types
14inline std::string repr(signed short obj) {
15 return REPR_FORMAT("{}", obj);
16}
17
18inline std::string repr(unsigned short obj) {
19 return REPR_FORMAT("{}", obj);
20}
21inline std::string repr(signed int obj) {
22 return REPR_FORMAT("{}", obj);
23}
24inline std::string repr(unsigned int obj) {
25 return REPR_FORMAT("{}U", obj);
26}
27inline std::string repr(signed long obj) {
28 return REPR_FORMAT("{}L", obj);
29}
30inline std::string repr(unsigned long obj) {
31 return REPR_FORMAT("{}UL", obj);
32}
33inline std::string repr(signed long long obj) {
34 return REPR_FORMAT("{}LL", obj);
35}
36inline std::string repr(unsigned long long obj) {
37 return REPR_FORMAT("{}ULL", obj);
38}
39
40// Floating-point types
41inline std::string repr(float obj) {
42 auto is_integer = rintf(obj) == obj;
43 return REPR_FORMAT("{}{}F", obj, is_integer ? ".0" : "");
44}
45
46inline std::string repr(double obj) {
47 auto is_integer = rint(obj) == obj;
48 return REPR_FORMAT("{}{}", obj, is_integer ? ".0" : "");
49}
50
51inline std::string repr(long double obj) {
52 auto is_integer = rintl(obj) == obj;
53 return REPR_FORMAT("{}{}L", obj, is_integer ? ".0" : "");
54}
55
56// Character types
57inline std::string repr(char obj) {
58 return REPR_FORMAT("'{:c}'", obj);
59}
60
61} // namespace librepr
#define REPR_FORMAT(...)
Definition ctvi/ctvi.h:9
std::string code_for()
Definition repr:39
constexpr ::librepr::ReprWrapper repr
Definition repr:59