Brad Bishop | 893b348 | 2017-05-23 18:17:25 -0400 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include "data_types.hpp" |
| 4 | |
Andrew Geissler | ae4c95c | 2020-05-16 13:58:53 -0500 | [diff] [blame] | 5 | #include <string> |
| 6 | |
Brad Bishop | 893b348 | 2017-05-23 18:17:25 -0400 | [diff] [blame] | 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. */ |
Patrick Venture | 3d6d318 | 2018-08-31 09:33:09 -0700 | [diff] [blame] | 17 | template <typename T> |
| 18 | struct GetFormatType |
George Liu | 3fe976c | 2022-06-21 09:37:04 +0800 | [diff] [blame] | 19 | {}; |
Patrick Venture | 3d6d318 | 2018-08-31 09:33:09 -0700 | [diff] [blame] | 20 | template <> |
| 21 | struct GetFormatType<bool> |
Brad Bishop | ec2ed2f | 2017-05-31 21:10:43 -0400 | [diff] [blame] | 22 | { |
Alexander Filippov | b839c3e | 2021-09-02 15:17:39 +0300 | [diff] [blame] | 23 | static constexpr auto format = "=%d"; |
Brad Bishop | ec2ed2f | 2017-05-31 21:10:43 -0400 | [diff] [blame] | 24 | }; |
Patrick Venture | 3d6d318 | 2018-08-31 09:33:09 -0700 | [diff] [blame] | 25 | template <> |
| 26 | struct GetFormatType<char> |
Brad Bishop | 893b348 | 2017-05-23 18:17:25 -0400 | [diff] [blame] | 27 | { |
| 28 | static constexpr auto format = "=%hhd"; |
| 29 | }; |
Patrick Venture | 3d6d318 | 2018-08-31 09:33:09 -0700 | [diff] [blame] | 30 | template <> |
| 31 | struct GetFormatType<short int> |
Brad Bishop | 893b348 | 2017-05-23 18:17:25 -0400 | [diff] [blame] | 32 | { |
| 33 | static constexpr auto format = "=%hd"; |
| 34 | }; |
Patrick Venture | 3d6d318 | 2018-08-31 09:33:09 -0700 | [diff] [blame] | 35 | template <> |
| 36 | struct GetFormatType<int> |
Brad Bishop | 893b348 | 2017-05-23 18:17:25 -0400 | [diff] [blame] | 37 | { |
| 38 | static constexpr auto format = "=%d"; |
| 39 | }; |
Patrick Venture | 3d6d318 | 2018-08-31 09:33:09 -0700 | [diff] [blame] | 40 | template <> |
| 41 | struct GetFormatType<long int> |
Brad Bishop | 893b348 | 2017-05-23 18:17:25 -0400 | [diff] [blame] | 42 | { |
| 43 | static constexpr auto format = "=%ld"; |
| 44 | }; |
Patrick Venture | 3d6d318 | 2018-08-31 09:33:09 -0700 | [diff] [blame] | 45 | template <> |
| 46 | struct GetFormatType<long long int> |
Brad Bishop | 893b348 | 2017-05-23 18:17:25 -0400 | [diff] [blame] | 47 | { |
| 48 | static constexpr auto format = "=%lld"; |
| 49 | }; |
Patrick Venture | 3d6d318 | 2018-08-31 09:33:09 -0700 | [diff] [blame] | 50 | template <> |
Harvey.Wu | afa54c6 | 2022-03-30 18:56:36 +0800 | [diff] [blame] | 51 | struct GetFormatType<double> |
| 52 | { |
| 53 | static constexpr auto format = "=%lf"; |
| 54 | }; |
| 55 | template <> |
Patrick Venture | 3d6d318 | 2018-08-31 09:33:09 -0700 | [diff] [blame] | 56 | struct GetFormatType<unsigned char> |
Brad Bishop | 893b348 | 2017-05-23 18:17:25 -0400 | [diff] [blame] | 57 | { |
| 58 | static constexpr auto format = "=%hhd"; |
| 59 | }; |
Patrick Venture | 3d6d318 | 2018-08-31 09:33:09 -0700 | [diff] [blame] | 60 | template <> |
| 61 | struct GetFormatType<unsigned short int> |
Brad Bishop | 893b348 | 2017-05-23 18:17:25 -0400 | [diff] [blame] | 62 | { |
| 63 | static constexpr auto format = "=%hd"; |
| 64 | }; |
Patrick Venture | 3d6d318 | 2018-08-31 09:33:09 -0700 | [diff] [blame] | 65 | template <> |
| 66 | struct GetFormatType<unsigned int> |
Brad Bishop | 893b348 | 2017-05-23 18:17:25 -0400 | [diff] [blame] | 67 | { |
| 68 | static constexpr auto format = "=%d"; |
| 69 | }; |
Patrick Venture | 3d6d318 | 2018-08-31 09:33:09 -0700 | [diff] [blame] | 70 | template <> |
| 71 | struct GetFormatType<unsigned long int> |
Brad Bishop | 893b348 | 2017-05-23 18:17:25 -0400 | [diff] [blame] | 72 | { |
| 73 | static constexpr auto format = "=%ld"; |
| 74 | }; |
Patrick Venture | 3d6d318 | 2018-08-31 09:33:09 -0700 | [diff] [blame] | 75 | template <> |
| 76 | struct GetFormatType<unsigned long long int> |
Brad Bishop | 893b348 | 2017-05-23 18:17:25 -0400 | [diff] [blame] | 77 | { |
| 78 | static constexpr auto format = "=%lld"; |
| 79 | }; |
Patrick Venture | 3d6d318 | 2018-08-31 09:33:09 -0700 | [diff] [blame] | 80 | template <> |
| 81 | struct GetFormatType<std::string> |
Brad Bishop | 893b348 | 2017-05-23 18:17:25 -0400 | [diff] [blame] | 82 | { |
| 83 | static constexpr auto format = "=%s"; |
| 84 | }; |
Patrick Venture | 3d6d318 | 2018-08-31 09:33:09 -0700 | [diff] [blame] | 85 | template <> |
| 86 | struct GetFormatType<char*> |
Brad Bishop | 893b348 | 2017-05-23 18:17:25 -0400 | [diff] [blame] | 87 | { |
| 88 | static constexpr auto format = "=%s"; |
| 89 | }; |
Patrick Venture | 3d6d318 | 2018-08-31 09:33:09 -0700 | [diff] [blame] | 90 | template <> |
| 91 | struct GetFormatType<const char*> |
Brad Bishop | 893b348 | 2017-05-23 18:17:25 -0400 | [diff] [blame] | 92 | { |
| 93 | static constexpr auto format = "=%s"; |
| 94 | }; |
| 95 | |
| 96 | } // namespace detail |
| 97 | |
| 98 | /** @brief Get the format string for a C++ type. */ |
Patrick Venture | 3d6d318 | 2018-08-31 09:33:09 -0700 | [diff] [blame] | 99 | template <typename T> |
| 100 | struct GetFormat |
Brad Bishop | 893b348 | 2017-05-23 18:17:25 -0400 | [diff] [blame] | 101 | { |
| 102 | static constexpr auto format = |
| 103 | detail::GetFormatType<DowncastType<T>>::format; |
| 104 | }; |
| 105 | |
| 106 | } // namespace monitoring |
| 107 | } // namespace dbus |
| 108 | } // namespace phosphor |