From e3c678215b3df16d01f50325c346b8845930552c Mon Sep 17 00:00:00 2001 From: Arnaud Giersch Date: Wed, 5 Jan 2011 09:58:40 +0100 Subject: [PATCH] Define an opt_helper class for local functions. --- options.cpp | 176 ++++++++++++++++++++++++++-------------------------- 1 file changed, 89 insertions(+), 87 deletions(-) diff --git a/options.cpp b/options.cpp index fe78559..827a064 100644 --- a/options.cpp +++ b/options.cpp @@ -79,87 +79,87 @@ namespace opt { namespace { - // some helper functions for opt::print() + // local helper class + class opt_helper { + public: + static const char* on_off(bool b); + const char* descr(const char* str); + template + const char* val_or_string(const T& val, const char* str, + const T& deflt = 0); + template + static bool nol_find_prefix(const T& nol, const char* descr, + std::string& name); + + private: + std::string descr_str; + std::string val_or_string_str; + }; - std::string* descr_str; - std::string* val_or_string_str; - - void print_helper_init() - { - descr_str = new std::string; - val_or_string_str = new std::string; - } +} // namespace - void print_helper_destroy() - { - delete val_or_string_str; - delete descr_str; - } +const char* opt_helper::on_off(bool b) +{ + return b ? "on" : "off"; +} - const char* descr(const char* str) - { - const int descr_width = 35; - std::string* res = descr_str; - *res = str; - res->resize(descr_width, '.'); - return res->c_str(); - } +const char* opt_helper::descr(const char* str) +{ + const int descr_width = 35; + std::string& res = descr_str; + res = str; + res.resize(descr_width, '.'); + return res.c_str(); +} - const char* on_off(bool b) - { - return b ? "on" : "off"; +template +const char* opt_helper::val_or_string(const T& val, const char* str, + const T& deflt) +{ + std::string& res = val_or_string_str; + if (val != deflt) { + std::ostringstream oss; + oss << val; + res = oss.str(); + } else { + res = str; } + return res.c_str(); +} - template - const char* val_or_string(const T& val, const char* str, const T& deflt = 0) - { - std::string* res = val_or_string_str; - if (val != deflt) { - std::ostringstream oss; - oss << val; - *res = oss.str(); - } else { - *res = str; +template +bool opt_helper::nol_find_prefix(const T& nol, const char* descr, + std::string& name) +{ + bool result = nol.exists(name); + if (!result) { + std::stack candidates; + for (typename T::iterator it = nol.begin() ; it != nol.end() ; ++it) { + const std::string& fullname = nol.get_name(it); + if (fullname.compare(0, name.length(), name) == 0) + candidates.push(fullname); } - return res->c_str(); - } - - // helper function for opt::parse_args() - - template - bool nol_find_prefix(const T& nol, const char* descr, std::string& name) - { - bool result = nol.exists(name); - if (!result) { - std::stack candidates; - for (typename T::iterator it = nol.begin() ; it != nol.end() ; ++it) { - const std::string& fullname = nol.get_name(it); - if (fullname.compare(0, name.length(), name) == 0) - candidates.push(fullname); - } - switch (candidates.size()) { - case 0: - ERROR2("unknownw %s -- %s", descr, name.c_str()); - break; - case 1: - name = candidates.top(); + switch (candidates.size()) { + case 0: + ERROR2("unknownw %s -- %s", descr, name.c_str()); + break; + case 1: + name = candidates.top(); + candidates.pop(); + result = true; + DEBUG2("infered %s -- %s", descr, name.c_str()); + break; + default: + ERROR2("ambiguous %s -- %s", descr, name.c_str()); + while (!candidates.empty()) { + ERROR1(" candidates are -- %s", candidates.top().c_str()); candidates.pop(); - result = true; - DEBUG2("infered %s -- %s", descr, name.c_str()); - break; - default: - ERROR2("ambiguous %s -- %s", descr, name.c_str()); - while (!candidates.empty()) { - ERROR1(" candidates are -- %s", candidates.top().c_str()); - candidates.pop(); - } - break; } + break; } - return result; } - -} // namespace + return result; +} bool opt::parse_args(int* argc, char* argv[]) { @@ -174,9 +174,9 @@ bool opt::parse_args(int* argc, char* argv[]) switch (c) { case 'a': opt::loba_algo = optarg; - result = nol_find_prefix(opt::loba_algorithms, - "load balancing algorithm", - opt::loba_algo); + result = opt_helper::nol_find_prefix(opt::loba_algorithms, + "load balancing algorithm", + opt::loba_algo); break; case 'b': opt::bookkeeping = true; @@ -212,8 +212,8 @@ bool opt::parse_args(int* argc, char* argv[]) break; case 'T': opt::auto_depl::topology = optarg; - result = nol_find_prefix(opt::topologies, "topology", - opt::auto_depl::topology); + result = opt_helper::nol_find_prefix(opt::topologies, "topology", + opt::auto_depl::topology); break; case 'v': // nothing to do: this option is checked at the very @@ -253,34 +253,36 @@ bool opt::parse_args(int* argc, char* argv[]) void opt::print() { - print_helper_init(); + opt_helper h; + #define DESCR(description, format, value) \ - INFO2("| %s: " format, descr(description), value) + INFO2("| %s: " format, h.descr(description), value) + INFO0(",----[ Simulation parameters ]"); - DESCR("log rate", "%s", val_or_string(log_rate, "disabled")); + DESCR("log rate", "%s", h.val_or_string(log_rate, "disabled")); DESCR("platform file", "\"%s\"", platform_file.c_str()); if (auto_depl::enabled) { INFO0("| automatic deployment enabled"); DESCR("- topology", "%s", auto_depl::topology.c_str()); - DESCR("- number of hosts", "%s", val_or_string(auto_depl::nhosts, - "auto")); - DESCR("- initial load", "%s", val_or_string(auto_depl::load, - "auto")); + DESCR("- number of hosts", "%s", h.val_or_string(auto_depl::nhosts, + "auto")); + DESCR("- initial load", "%s", h.val_or_string(auto_depl::load, + "auto")); } else { DESCR("deployment file", "\"%s\"", deployment_file.c_str()); } DESCR("load balancing algorithm", "%s", loba_algo.c_str()); - DESCR("bookkeeping", "%s", on_off(bookkeeping)); + DESCR("bookkeeping", "%s", h.on_off(bookkeeping)); DESCR("computation cost factors", "[%s]", comp_cost.to_string().c_str()); DESCR("communication cost factors", "[%s]", comm_cost.to_string().c_str()); DESCR("maximum number of comp. iterations", "%s", - val_or_string(comp_maxiter, "infinity")); + h.val_or_string(comp_maxiter, "infinity")); DESCR("maximum number of lb. iterations", "%s", - val_or_string(lb_maxiter, "infinity")); - DESCR("exit on close", "%s", on_off(exit_on_close)); + h.val_or_string(lb_maxiter, "infinity")); + DESCR("exit on close", "%s", h.on_off(exit_on_close)); INFO0("`----"); + #undef DESCR - print_helper_destroy(); } void opt::usage() -- 2.39.5