xref: /openbmc/phosphor-dbus-monitor/src/format.hpp (revision b839c3ea01044e38b7beb37e8920ccc31f3c6519)
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<unsigned char>
53 {
54     static constexpr auto format = "=%hhd";
55 };
56 template <>
57 struct GetFormatType<unsigned short int>
58 {
59     static constexpr auto format = "=%hd";
60 };
61 template <>
62 struct GetFormatType<unsigned int>
63 {
64     static constexpr auto format = "=%d";
65 };
66 template <>
67 struct GetFormatType<unsigned long int>
68 {
69     static constexpr auto format = "=%ld";
70 };
71 template <>
72 struct GetFormatType<unsigned long long int>
73 {
74     static constexpr auto format = "=%lld";
75 };
76 template <>
77 struct GetFormatType<std::string>
78 {
79     static constexpr auto format = "=%s";
80 };
81 template <>
82 struct GetFormatType<char*>
83 {
84     static constexpr auto format = "=%s";
85 };
86 template <>
87 struct GetFormatType<const char*>
88 {
89     static constexpr auto format = "=%s";
90 };
91 
92 } // namespace detail
93 
94 /** @brief Get the format string for a C++ type. */
95 template <typename T>
96 struct GetFormat
97 {
98     static constexpr auto format =
99         detail::GetFormatType<DowncastType<T>>::format;
100 };
101 
102 } // namespace monitoring
103 } // namespace dbus
104 } // namespace phosphor
105