blob: 8fa6dc8813566d4af0f7b345d5ac56a101cd4065 [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
Brad Bishop893b3482017-05-23 18:17:25 -040019{
Brad Bishop893b3482017-05-23 18:17:25 -040020};
Patrick Venture3d6d3182018-08-31 09:33:09 -070021template <>
22struct GetFormatType<bool>
Brad Bishopec2ed2f2017-05-31 21:10:43 -040023{
Alexander Filippovb839c3e2021-09-02 15:17:39 +030024 static constexpr auto format = "=%d";
Brad Bishopec2ed2f2017-05-31 21:10:43 -040025};
Patrick Venture3d6d3182018-08-31 09:33:09 -070026template <>
27struct GetFormatType<char>
Brad Bishop893b3482017-05-23 18:17:25 -040028{
29 static constexpr auto format = "=%hhd";
30};
Patrick Venture3d6d3182018-08-31 09:33:09 -070031template <>
32struct GetFormatType<short int>
Brad Bishop893b3482017-05-23 18:17:25 -040033{
34 static constexpr auto format = "=%hd";
35};
Patrick Venture3d6d3182018-08-31 09:33:09 -070036template <>
37struct GetFormatType<int>
Brad Bishop893b3482017-05-23 18:17:25 -040038{
39 static constexpr auto format = "=%d";
40};
Patrick Venture3d6d3182018-08-31 09:33:09 -070041template <>
42struct GetFormatType<long int>
Brad Bishop893b3482017-05-23 18:17:25 -040043{
44 static constexpr auto format = "=%ld";
45};
Patrick Venture3d6d3182018-08-31 09:33:09 -070046template <>
47struct GetFormatType<long long int>
Brad Bishop893b3482017-05-23 18:17:25 -040048{
49 static constexpr auto format = "=%lld";
50};
Patrick Venture3d6d3182018-08-31 09:33:09 -070051template <>
Harvey.Wuafa54c62022-03-30 18:56:36 +080052struct GetFormatType<double>
53{
54 static constexpr auto format = "=%lf";
55};
56template <>
Patrick Venture3d6d3182018-08-31 09:33:09 -070057struct GetFormatType<unsigned char>
Brad Bishop893b3482017-05-23 18:17:25 -040058{
59 static constexpr auto format = "=%hhd";
60};
Patrick Venture3d6d3182018-08-31 09:33:09 -070061template <>
62struct GetFormatType<unsigned short int>
Brad Bishop893b3482017-05-23 18:17:25 -040063{
64 static constexpr auto format = "=%hd";
65};
Patrick Venture3d6d3182018-08-31 09:33:09 -070066template <>
67struct GetFormatType<unsigned int>
Brad Bishop893b3482017-05-23 18:17:25 -040068{
69 static constexpr auto format = "=%d";
70};
Patrick Venture3d6d3182018-08-31 09:33:09 -070071template <>
72struct GetFormatType<unsigned long int>
Brad Bishop893b3482017-05-23 18:17:25 -040073{
74 static constexpr auto format = "=%ld";
75};
Patrick Venture3d6d3182018-08-31 09:33:09 -070076template <>
77struct GetFormatType<unsigned long long int>
Brad Bishop893b3482017-05-23 18:17:25 -040078{
79 static constexpr auto format = "=%lld";
80};
Patrick Venture3d6d3182018-08-31 09:33:09 -070081template <>
82struct GetFormatType<std::string>
Brad Bishop893b3482017-05-23 18:17:25 -040083{
84 static constexpr auto format = "=%s";
85};
Patrick Venture3d6d3182018-08-31 09:33:09 -070086template <>
87struct GetFormatType<char*>
Brad Bishop893b3482017-05-23 18:17:25 -040088{
89 static constexpr auto format = "=%s";
90};
Patrick Venture3d6d3182018-08-31 09:33:09 -070091template <>
92struct GetFormatType<const char*>
Brad Bishop893b3482017-05-23 18:17:25 -040093{
94 static constexpr auto format = "=%s";
95};
96
97} // namespace detail
98
99/** @brief Get the format string for a C++ type. */
Patrick Venture3d6d3182018-08-31 09:33:09 -0700100template <typename T>
101struct GetFormat
Brad Bishop893b3482017-05-23 18:17:25 -0400102{
103 static constexpr auto format =
104 detail::GetFormatType<DowncastType<T>>::format;
105};
106
107} // namespace monitoring
108} // namespace dbus
109} // namespace phosphor