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 |
Brad Bishop | 893b348 | 2017-05-23 18:17:25 -0400 | [diff] [blame] | 19 | { |
Brad Bishop | 893b348 | 2017-05-23 18:17:25 -0400 | [diff] [blame] | 20 | }; |
Patrick Venture | 3d6d318 | 2018-08-31 09:33:09 -0700 | [diff] [blame] | 21 | template <> |
| 22 | struct GetFormatType<bool> |
Brad Bishop | ec2ed2f | 2017-05-31 21:10:43 -0400 | [diff] [blame] | 23 | { |
| 24 | static constexpr auto format = "%d"; |
| 25 | }; |
Patrick Venture | 3d6d318 | 2018-08-31 09:33:09 -0700 | [diff] [blame] | 26 | template <> |
| 27 | struct GetFormatType<char> |
Brad Bishop | 893b348 | 2017-05-23 18:17:25 -0400 | [diff] [blame] | 28 | { |
| 29 | static constexpr auto format = "=%hhd"; |
| 30 | }; |
Patrick Venture | 3d6d318 | 2018-08-31 09:33:09 -0700 | [diff] [blame] | 31 | template <> |
| 32 | struct GetFormatType<short int> |
Brad Bishop | 893b348 | 2017-05-23 18:17:25 -0400 | [diff] [blame] | 33 | { |
| 34 | static constexpr auto format = "=%hd"; |
| 35 | }; |
Patrick Venture | 3d6d318 | 2018-08-31 09:33:09 -0700 | [diff] [blame] | 36 | template <> |
| 37 | struct GetFormatType<int> |
Brad Bishop | 893b348 | 2017-05-23 18:17:25 -0400 | [diff] [blame] | 38 | { |
| 39 | static constexpr auto format = "=%d"; |
| 40 | }; |
Patrick Venture | 3d6d318 | 2018-08-31 09:33:09 -0700 | [diff] [blame] | 41 | template <> |
| 42 | struct GetFormatType<long int> |
Brad Bishop | 893b348 | 2017-05-23 18:17:25 -0400 | [diff] [blame] | 43 | { |
| 44 | static constexpr auto format = "=%ld"; |
| 45 | }; |
Patrick Venture | 3d6d318 | 2018-08-31 09:33:09 -0700 | [diff] [blame] | 46 | template <> |
| 47 | struct GetFormatType<long long int> |
Brad Bishop | 893b348 | 2017-05-23 18:17:25 -0400 | [diff] [blame] | 48 | { |
| 49 | static constexpr auto format = "=%lld"; |
| 50 | }; |
Patrick Venture | 3d6d318 | 2018-08-31 09:33:09 -0700 | [diff] [blame] | 51 | template <> |
| 52 | struct GetFormatType<unsigned char> |
Brad Bishop | 893b348 | 2017-05-23 18:17:25 -0400 | [diff] [blame] | 53 | { |
| 54 | static constexpr auto format = "=%hhd"; |
| 55 | }; |
Patrick Venture | 3d6d318 | 2018-08-31 09:33:09 -0700 | [diff] [blame] | 56 | template <> |
| 57 | struct GetFormatType<unsigned short int> |
Brad Bishop | 893b348 | 2017-05-23 18:17:25 -0400 | [diff] [blame] | 58 | { |
| 59 | static constexpr auto format = "=%hd"; |
| 60 | }; |
Patrick Venture | 3d6d318 | 2018-08-31 09:33:09 -0700 | [diff] [blame] | 61 | template <> |
| 62 | struct GetFormatType<unsigned int> |
Brad Bishop | 893b348 | 2017-05-23 18:17:25 -0400 | [diff] [blame] | 63 | { |
| 64 | static constexpr auto format = "=%d"; |
| 65 | }; |
Patrick Venture | 3d6d318 | 2018-08-31 09:33:09 -0700 | [diff] [blame] | 66 | template <> |
| 67 | struct GetFormatType<unsigned long int> |
Brad Bishop | 893b348 | 2017-05-23 18:17:25 -0400 | [diff] [blame] | 68 | { |
| 69 | static constexpr auto format = "=%ld"; |
| 70 | }; |
Patrick Venture | 3d6d318 | 2018-08-31 09:33:09 -0700 | [diff] [blame] | 71 | template <> |
| 72 | struct GetFormatType<unsigned long long int> |
Brad Bishop | 893b348 | 2017-05-23 18:17:25 -0400 | [diff] [blame] | 73 | { |
| 74 | static constexpr auto format = "=%lld"; |
| 75 | }; |
Patrick Venture | 3d6d318 | 2018-08-31 09:33:09 -0700 | [diff] [blame] | 76 | template <> |
| 77 | struct GetFormatType<std::string> |
Brad Bishop | 893b348 | 2017-05-23 18:17:25 -0400 | [diff] [blame] | 78 | { |
| 79 | static constexpr auto format = "=%s"; |
| 80 | }; |
Patrick Venture | 3d6d318 | 2018-08-31 09:33:09 -0700 | [diff] [blame] | 81 | template <> |
| 82 | struct GetFormatType<char*> |
Brad Bishop | 893b348 | 2017-05-23 18:17:25 -0400 | [diff] [blame] | 83 | { |
| 84 | static constexpr auto format = "=%s"; |
| 85 | }; |
Patrick Venture | 3d6d318 | 2018-08-31 09:33:09 -0700 | [diff] [blame] | 86 | template <> |
| 87 | struct GetFormatType<const char*> |
Brad Bishop | 893b348 | 2017-05-23 18:17:25 -0400 | [diff] [blame] | 88 | { |
| 89 | static constexpr auto format = "=%s"; |
| 90 | }; |
| 91 | |
| 92 | } // namespace detail |
| 93 | |
| 94 | /** @brief Get the format string for a C++ type. */ |
Patrick Venture | 3d6d318 | 2018-08-31 09:33:09 -0700 | [diff] [blame] | 95 | template <typename T> |
| 96 | struct GetFormat |
Brad Bishop | 893b348 | 2017-05-23 18:17:25 -0400 | [diff] [blame] | 97 | { |
| 98 | static constexpr auto format = |
| 99 | detail::GetFormatType<DowncastType<T>>::format; |
| 100 | }; |
| 101 | |
| 102 | } // namespace monitoring |
| 103 | } // namespace dbus |
| 104 | } // namespace phosphor |