blob: 315fce9c32d36677cccbb235952d44f6fa27ebd2 [file] [log] [blame]
Brad Bishop893b3482017-05-23 18:17:25 -04001#pragma once
2
3#include "data_types.hpp"
4
Andrew Geisslerae4c95c2020-05-16 13:58:53 -05005#include <string>
6
Brad Bishop893b3482017-05-23 18:17:25 -04007namespace phosphor
8{
9namespace dbus
10{
11namespace monitoring
12{
13namespace detail
14{
15
16/** @brief Map format strings to undecorated C++ types. */
Patrick Venture3d6d3182018-08-31 09:33:09 -070017template <typename T>
18struct GetFormatType
George Liu3fe976c2022-06-21 09:37:04 +080019{};
Patrick Venture3d6d3182018-08-31 09:33:09 -070020template <>
21struct GetFormatType<bool>
Brad Bishopec2ed2f2017-05-31 21:10:43 -040022{
Alexander Filippovb839c3e2021-09-02 15:17:39 +030023 static constexpr auto format = "=%d";
Brad Bishopec2ed2f2017-05-31 21:10:43 -040024};
Patrick Venture3d6d3182018-08-31 09:33:09 -070025template <>
26struct GetFormatType<char>
Brad Bishop893b3482017-05-23 18:17:25 -040027{
28 static constexpr auto format = "=%hhd";
29};
Patrick Venture3d6d3182018-08-31 09:33:09 -070030template <>
31struct GetFormatType<short int>
Brad Bishop893b3482017-05-23 18:17:25 -040032{
33 static constexpr auto format = "=%hd";
34};
Patrick Venture3d6d3182018-08-31 09:33:09 -070035template <>
36struct GetFormatType<int>
Brad Bishop893b3482017-05-23 18:17:25 -040037{
38 static constexpr auto format = "=%d";
39};
Patrick Venture3d6d3182018-08-31 09:33:09 -070040template <>
41struct GetFormatType<long int>
Brad Bishop893b3482017-05-23 18:17:25 -040042{
43 static constexpr auto format = "=%ld";
44};
Patrick Venture3d6d3182018-08-31 09:33:09 -070045template <>
46struct GetFormatType<long long int>
Brad Bishop893b3482017-05-23 18:17:25 -040047{
48 static constexpr auto format = "=%lld";
49};
Patrick Venture3d6d3182018-08-31 09:33:09 -070050template <>
Harvey.Wuafa54c62022-03-30 18:56:36 +080051struct GetFormatType<double>
52{
53 static constexpr auto format = "=%lf";
54};
55template <>
Patrick Venture3d6d3182018-08-31 09:33:09 -070056struct GetFormatType<unsigned char>
Brad Bishop893b3482017-05-23 18:17:25 -040057{
58 static constexpr auto format = "=%hhd";
59};
Patrick Venture3d6d3182018-08-31 09:33:09 -070060template <>
61struct GetFormatType<unsigned short int>
Brad Bishop893b3482017-05-23 18:17:25 -040062{
63 static constexpr auto format = "=%hd";
64};
Patrick Venture3d6d3182018-08-31 09:33:09 -070065template <>
66struct GetFormatType<unsigned int>
Brad Bishop893b3482017-05-23 18:17:25 -040067{
68 static constexpr auto format = "=%d";
69};
Patrick Venture3d6d3182018-08-31 09:33:09 -070070template <>
71struct GetFormatType<unsigned long int>
Brad Bishop893b3482017-05-23 18:17:25 -040072{
73 static constexpr auto format = "=%ld";
74};
Patrick Venture3d6d3182018-08-31 09:33:09 -070075template <>
76struct GetFormatType<unsigned long long int>
Brad Bishop893b3482017-05-23 18:17:25 -040077{
78 static constexpr auto format = "=%lld";
79};
Patrick Venture3d6d3182018-08-31 09:33:09 -070080template <>
81struct GetFormatType<std::string>
Brad Bishop893b3482017-05-23 18:17:25 -040082{
83 static constexpr auto format = "=%s";
84};
Patrick Venture3d6d3182018-08-31 09:33:09 -070085template <>
86struct GetFormatType<char*>
Brad Bishop893b3482017-05-23 18:17:25 -040087{
88 static constexpr auto format = "=%s";
89};
Patrick Venture3d6d3182018-08-31 09:33:09 -070090template <>
91struct GetFormatType<const char*>
Brad Bishop893b3482017-05-23 18:17:25 -040092{
93 static constexpr auto format = "=%s";
94};
95
96} // namespace detail
97
98/** @brief Get the format string for a C++ type. */
Patrick Venture3d6d3182018-08-31 09:33:09 -070099template <typename T>
100struct GetFormat
Brad Bishop893b3482017-05-23 18:17:25 -0400101{
102 static constexpr auto format =
103 detail::GetFormatType<DowncastType<T>>::format;
104};
105
106} // namespace monitoring
107} // namespace dbus
108} // namespace phosphor