diff options
author | Brad Bishop <bradleyb@fuzziesquirrel.com> | 2017-05-23 18:17:25 -0400 |
---|---|---|
committer | Brad Bishop <bradleyb@fuzziesquirrel.com> | 2017-06-19 16:25:59 -0400 |
commit | 893b348890781dba2603ead61cf6f724350d0b47 (patch) | |
tree | 9b5f6d6c4a7a57afd7065ef8ec474a6593e93c5d /src | |
parent | 13fd8722e616dd424cd585187fb0ef65f6316023 (diff) | |
download | phosphor-dbus-monitor-893b348890781dba2603ead61cf6f724350d0b47.tar.gz phosphor-dbus-monitor-893b348890781dba2603ead61cf6f724350d0b47.zip |
Add printf format utility
Add a template for obtaining a format string at build for a
built-in C++ type.
Change-Id: I8e5d26d4481b3ddafb5ffddbb058a1c75b3a2257
Signed-off-by: Brad Bishop <bradleyb@fuzziesquirrel.com>
Diffstat (limited to 'src')
-rw-r--r-- | src/data_types.hpp | 15 | ||||
-rw-r--r-- | src/format.hpp | 83 |
2 files changed, 98 insertions, 0 deletions
diff --git a/src/data_types.hpp b/src/data_types.hpp index af0ef68..439045e 100644 --- a/src/data_types.hpp +++ b/src/data_types.hpp @@ -80,6 +80,21 @@ using PropertyIndex = TupleRefMap < const std::string, const std::string >; // *INDENT-ON* + +/** @brief Convert some C++ types to others. + * + * Remove type decorators to reduce template specializations. + * + * 1. Remove references. + * 2. Remove 'const' and 'volatile'. + */ +template <typename T> struct Downcast +{ + using Type = std::remove_cv_t<std::remove_reference_t<T>>; +}; +template <typename T> using DowncastType = + typename Downcast<T>::Type; + } // namespace monitoring } // namespace dbus } // namespace phosphor diff --git a/src/format.hpp b/src/format.hpp new file mode 100644 index 0000000..9c8341c --- /dev/null +++ b/src/format.hpp @@ -0,0 +1,83 @@ +#pragma once + +#include "data_types.hpp" + +namespace phosphor +{ +namespace dbus +{ +namespace monitoring +{ +namespace detail +{ + +/** @brief Map format strings to undecorated C++ types. */ +template <typename T> struct GetFormatType +{ + +}; +template <> struct GetFormatType<char> +{ + static constexpr auto format = "=%hhd"; +}; +template <> struct GetFormatType<short int> +{ + static constexpr auto format = "=%hd"; +}; +template <> struct GetFormatType<int> +{ + static constexpr auto format = "=%d"; +}; +template <> struct GetFormatType<long int> +{ + static constexpr auto format = "=%ld"; +}; +template <> struct GetFormatType<long long int> +{ + static constexpr auto format = "=%lld"; +}; +template <> struct GetFormatType<unsigned char> +{ + static constexpr auto format = "=%hhd"; +}; +template <> struct GetFormatType<unsigned short int> +{ + static constexpr auto format = "=%hd"; +}; +template <> struct GetFormatType<unsigned int> +{ + static constexpr auto format = "=%d"; +}; +template <> struct GetFormatType<unsigned long int> +{ + static constexpr auto format = "=%ld"; +}; +template <> struct GetFormatType<unsigned long long int> +{ + static constexpr auto format = "=%lld"; +}; +template <> struct GetFormatType<std::string> +{ + static constexpr auto format = "=%s"; +}; +template <> struct GetFormatType<char*> +{ + static constexpr auto format = "=%s"; +}; +template <> struct GetFormatType<const char*> +{ + static constexpr auto format = "=%s"; +}; + +} // namespace detail + +/** @brief Get the format string for a C++ type. */ +template <typename T> struct GetFormat +{ + static constexpr auto format = + detail::GetFormatType<DowncastType<T>>::format; +}; + +} // namespace monitoring +} // namespace dbus +} // namespace phosphor |