xref: /openbmc/phosphor-dbus-monitor/src/format.hpp (revision afa54c68455d70960aafb02665da14e2f20332ee)
1 #pragma once
2 
3 #include "data_types.hpp"
4 
5 #include <string>
6 
7 namespace phosphor
8 {
9 namespace dbus
10 {
11 namespace monitoring
12 {
13 namespace detail
14 {
15 
16 /** @brief Map format strings to undecorated C++ types. */
17 template <typename T>
18 struct GetFormatType
19 {
20 };
21 template <>
22 struct GetFormatType<bool>
23 {
24     static constexpr auto format = "=%d";
25 };
26 template <>
27 struct GetFormatType<char>
28 {
29     static constexpr auto format = "=%hhd";
30 };
31 template <>
32 struct GetFormatType<short int>
33 {
34     static constexpr auto format = "=%hd";
35 };
36 template <>
37 struct GetFormatType<int>
38 {
39     static constexpr auto format = "=%d";
40 };
41 template <>
42 struct GetFormatType<long int>
43 {
44     static constexpr auto format = "=%ld";
45 };
46 template <>
47 struct GetFormatType<long long int>
48 {
49     static constexpr auto format = "=%lld";
50 };
51 template <>
52 struct GetFormatType<double>
53 {
54     static constexpr auto format = "=%lf";
55 };
56 template <>
57 struct GetFormatType<unsigned char>
58 {
59     static constexpr auto format = "=%hhd";
60 };
61 template <>
62 struct GetFormatType<unsigned short int>
63 {
64     static constexpr auto format = "=%hd";
65 };
66 template <>
67 struct GetFormatType<unsigned int>
68 {
69     static constexpr auto format = "=%d";
70 };
71 template <>
72 struct GetFormatType<unsigned long int>
73 {
74     static constexpr auto format = "=%ld";
75 };
76 template <>
77 struct GetFormatType<unsigned long long int>
78 {
79     static constexpr auto format = "=%lld";
80 };
81 template <>
82 struct GetFormatType<std::string>
83 {
84     static constexpr auto format = "=%s";
85 };
86 template <>
87 struct GetFormatType<char*>
88 {
89     static constexpr auto format = "=%s";
90 };
91 template <>
92 struct GetFormatType<const char*>
93 {
94     static constexpr auto format = "=%s";
95 };
96 
97 } // namespace detail
98 
99 /** @brief Get the format string for a C++ type. */
100 template <typename T>
101 struct GetFormat
102 {
103     static constexpr auto format =
104         detail::GetFormatType<DowncastType<T>>::format;
105 };
106 
107 } // namespace monitoring
108 } // namespace dbus
109 } // namespace phosphor
110