From aa43265e5cc026a612fe9b36a42da55dcffc48f4 Mon Sep 17 00:00:00 2001 From: Xiaoxi Chen Date: Fri, 19 Apr 2024 14:20:12 +0800 Subject: [PATCH] add formattr and >> operator for SM command line parsing. cxxopts use >> to parse opts. Signed-off-by: Xiaoxi Chen --- conanfile.py | 2 +- src/include/homeobject/homeobject.hpp | 53 ++++++++++++++++++++++++++- 2 files changed, 52 insertions(+), 3 deletions(-) diff --git a/conanfile.py b/conanfile.py index cedda22f..9d1ef6e7 100644 --- a/conanfile.py +++ b/conanfile.py @@ -9,7 +9,7 @@ class HomeObjectConan(ConanFile): name = "homeobject" - version = "1.1.1" + version = "2.0.1" homepage = "https://github.com/eBay/HomeObject" description = "Blob Store built on HomeReplication" topics = ("ebay") diff --git a/src/include/homeobject/homeobject.hpp b/src/include/homeobject/homeobject.hpp index 7d98f43b..1b49125c 100644 --- a/src/include/homeobject/homeobject.hpp +++ b/src/include/homeobject/homeobject.hpp @@ -15,8 +15,24 @@ class PGManager; class ShardManager; ENUM(DevType, uint8_t, AUTO_DETECT = 1, HDD, NVME, UNSUPPORTED); struct device_info_t { - explicit device_info_t(std::string name, DevType dtype = DevType::AUTO_DETECT) : path{std::filesystem::canonical(name)}, type{dtype} {} - bool operator ==(device_info_t const& rhs) const { return path == rhs.path && type == rhs.type; } + explicit device_info_t(std::string name, DevType dtype = DevType::AUTO_DETECT) : + path{std::filesystem::canonical(name)}, type{dtype} {} + device_info_t() = default; + bool operator==(device_info_t const& rhs) const { return path == rhs.path && type == rhs.type; } + friend std::istream& operator>>(std::istream& input, device_info_t& di) { + std::string i_path, i_type; + std::getline(input, i_path, ':'); + std::getline(input, i_type); + di.path = std::filesystem::canonical(i_path); + if (i_type == "HDD") { + di.type = DevType::HDD; + } else if (i_type == "NVME") { + di.type = DevType::NVME; + } else { + di.type = DevType::AUTO_DETECT; + } + return input; + } std::filesystem::path path; DevType type; }; @@ -60,3 +76,36 @@ class HomeObject { extern std::shared_ptr< HomeObject > init_homeobject(std::weak_ptr< HomeObjectApplication >&& application); } // namespace homeobject + // + +namespace fmt { +template <> +struct formatter< homeobject::device_info_t > { + template < typename ParseContext > + constexpr auto parse(ParseContext& ctx) { + return ctx.begin(); + } + + template < typename FormatContext > + auto format(homeobject::device_info_t const& device, FormatContext& ctx) { + std::string type; + switch (device.type) { + case homeobject::DevType::HDD: + type = "HDD"; + break; + case homeobject::DevType::NVME: + type = "NVME"; + break; + case homeobject::DevType::UNSUPPORTED: + type = "UNSUPPORTED"; + break; + case homeobject::DevType::AUTO_DETECT: + type = "AUTO_DETECT"; + break; + default: + type = "UNKNOWN"; + } + return fmt::format_to(ctx.out(), "Path: {}, Type: {}", device.path.string(), type); + } +}; +} // namespace fmt