Use fmt instead of boost.

This commit is contained in:
adityaruplaha 2020-07-01 23:50:11 +05:30
parent eebb545b40
commit 6ae1d6885f
No known key found for this signature in database
GPG Key ID: 1B24048A1F78845F
2 changed files with 22 additions and 18 deletions

View File

@ -7,7 +7,7 @@
#include <regex> #include <regex>
#include <utility> #include <utility>
#include <vector> #include <vector>
#include <boost/algorithm/string/join.hpp> #include <fmt/format.h>
#include "common/logging/log.h" #include "common/logging/log.h"
#include "common/param_package.h" #include "common/param_package.h"
#include "common/string_util.h" #include "common/string_util.h"
@ -260,8 +260,7 @@ void ParamPackage::Set(const std::string& key, std::string value) {
} }
void ParamPackage::Set(const std::string& key, std::vector<std::string> value) { void ParamPackage::Set(const std::string& key, std::vector<std::string> value) {
data.insert_or_assign(key, data.insert_or_assign(key, fmt::format("[{}]", fmt::join(value, std::string{LIST_SEPARATOR})));
"[" + boost::algorithm::join(value, std::string{LIST_SEPARATOR}) + "]");
} }
void ParamPackage::Set(const std::string& key, int value) { void ParamPackage::Set(const std::string& key, int value) {
@ -269,11 +268,7 @@ void ParamPackage::Set(const std::string& key, int value) {
} }
void ParamPackage::Set(const std::string& key, std::vector<int> value) { void ParamPackage::Set(const std::string& key, std::vector<int> value) {
std::vector<std::string> vec{}; data.insert_or_assign(key, fmt::format("[{}]", fmt::join(value, std::string{LIST_SEPARATOR})));
std::transform(value.begin(), value.end(), std::back_inserter(vec),
[](float a) -> std::string const { return std::to_string(a); });
data.insert_or_assign(key,
"[" + boost::algorithm::join(vec, std::string{LIST_SEPARATOR}) + "]");
} }
void ParamPackage::Set(const std::string& key, float value) { void ParamPackage::Set(const std::string& key, float value) {
@ -281,11 +276,7 @@ void ParamPackage::Set(const std::string& key, float value) {
} }
void ParamPackage::Set(const std::string& key, std::vector<float> value) { void ParamPackage::Set(const std::string& key, std::vector<float> value) {
std::vector<std::string> vec{}; data.insert_or_assign(key, fmt::format("[{}]", fmt::join(value, std::string{LIST_SEPARATOR})));
std::transform(value.begin(), value.end(), std::back_inserter(vec),
[](float a) -> std::string { return std::to_string(a); });
data.insert_or_assign(key,
"[" + boost::algorithm::join(vec, std::string{LIST_SEPARATOR}) + "]");
} }
void ParamPackage::Set(const std::string& key, ParamPackage value) { void ParamPackage::Set(const std::string& key, ParamPackage value) {
@ -293,15 +284,15 @@ void ParamPackage::Set(const std::string& key, ParamPackage value) {
} }
void ParamPackage::Set(const std::string& key, std::vector<ParamPackage> value) { void ParamPackage::Set(const std::string& key, std::vector<ParamPackage> value) {
std::vector<std::string> vec{}; data.insert_or_assign(key, fmt::format("[{}]", fmt::join(value, std::string{LIST_SEPARATOR})));
std::transform(value.begin(), value.end(), std::back_inserter(vec),
[](ParamPackage& a) -> std::string { return a.Serialize(); });
data.insert_or_assign(key,
"[" + boost::algorithm::join(vec, std::string{LIST_SEPARATOR}) + "]");
} }
// Other methods // Other methods
std::ostream& operator<<(std::ostream& os, const ParamPackage& p) {
return os << p.Serialize();
}
bool ParamPackage::Has(const std::string& key) const { bool ParamPackage::Has(const std::string& key) const {
return data.find(key) != data.end(); return data.find(key) != data.end();
} }
@ -362,3 +353,15 @@ std::string ParamPackage::ReplacePlaceholders(const std::string& str,
} }
} // namespace Common } // namespace Common
template <>
struct fmt::formatter<Common::ParamPackage> {
constexpr auto parse(fmt::format_parse_context& ctx) {
return ctx.begin();
}
template <typename FormatContext>
auto format(const Common::ParamPackage& p, FormatContext& ctx) {
return format_to(ctx.out(), "{}", p.Serialize());
}
};

View File

@ -26,6 +26,7 @@ public:
ParamPackage& operator=(ParamPackage&& other) = default; ParamPackage& operator=(ParamPackage&& other) = default;
std::string Serialize() const; std::string Serialize() const;
friend std::ostream& operator<<(std::ostream& os, const ParamPackage& p);
// Getters // Getters
std::string Get(const std::string& key, const std::string& default_value) const; std::string Get(const std::string& key, const std::string& default_value) const;