From 365c864a587365fe443b11cc0cd7cfc8f8f8eb81 Mon Sep 17 00:00:00 2001
From: lichao <lichao@aiotlink.com>
Date: 星期二, 01 六月 2021 11:19:22 +0800
Subject: [PATCH] refactor, clean up useless code.
---
box/box.cc | 32 ++++++++++++++++++++++----------
1 files changed, 22 insertions(+), 10 deletions(-)
diff --git a/box/box.cc b/box/box.cc
index dab1c42..7bea9a3 100644
--- a/box/box.cc
+++ b/box/box.cc
@@ -41,7 +41,7 @@
}
return false;
}
- void Apply(std::function<void(const std::string &id, const std::string &desc)> f) const
+ void Apply(std::function<void(const std::string &id, const std::string &desc)> const &f) const
{
for (auto &kv : functions_) {
f(kv.second.id_, kv.second.desc_);
@@ -76,13 +76,24 @@
}
const std::string kOrigName("bhshmqbox");
+
+inline void PrintLn() { putchar('\n'); }
+
+template <class... Params>
+void PrintLn(const char *fmt, Params &&...t)
+{
+ printf(fmt, std::forward<decltype(t)>(t)...);
+ PrintLn();
+}
+
void Help()
{
- printf("%s : bhome shared memory message queue box application.\n", kOrigName.c_str());
- printf("usage:\n");
- printf("\t1) %s [function [options]...]\n", kOrigName.c_str());
- printf("\t2) rename or link %s to a function name then run it directly.\n", kOrigName.c_str());
- printf("\nsupported functions:\n");
+ PrintLn("%s : bhome shared memory message queue box application.", kOrigName.c_str());
+ PrintLn("%s", "usage:");
+ PrintLn("\t1) %s [function [options]...]", kOrigName.c_str());
+ PrintLn("\t2) rename or link %s to a function name then run it directly.", kOrigName.c_str());
+ PrintLn();
+ PrintLn("%s", "supported functions:");
std::map<std::string, std::string> funcs;
auto getInfo = [&](const std::string &name, const std::string &desc) {
funcs[name] = desc;
@@ -92,9 +103,10 @@
int max_len = std::max_element(funcs.begin(), funcs.end(), cmp_name_len)->first.size();
for (auto &kv : funcs) {
int npad = max_len - kv.first.size();
- printf("\n\t%s%s : %s\n", kv.first.c_str(), std::string(npad, ' ').c_str(), kv.second.c_str());
+ PrintLn();
+ PrintLn("\t%s%s : %s", kv.first.c_str(), std::string(npad, ' ').c_str(), kv.second.c_str());
}
- printf("\n");
+ PrintLn();
}
int BoxMain(int argc, const char *argv[])
@@ -106,7 +118,7 @@
if (BoxFind(name, func)) {
return func(argc, argv);
} else {
- printf("%s : function not found!\n", name.c_str());
+ PrintLn("%s : function not found!", name.c_str());
// Help();
}
return 0;
@@ -119,7 +131,7 @@
fs::path exe(argv[0]);
auto name = exe.filename().string();
- // printf("name: %s\n", name.c_str());
+ // PrintLn("name: %s", name.c_str());
if (name == kOrigName) {
if (argc > 1) {
--
Gitblit v1.8.0