[mlpack-svn] r11709 - mlpack/trunk/src/mlpack/core/util
fastlab-svn at coffeetalk-1.cc.gatech.edu
fastlab-svn at coffeetalk-1.cc.gatech.edu
Fri Mar 2 18:52:42 EST 2012
Author: mamidon
Date: 2012-03-02 18:52:42 -0500 (Fri, 02 Mar 2012)
New Revision: 11709
Modified:
mlpack/trunk/src/mlpack/core/util/cli.cpp
mlpack/trunk/src/mlpack/core/util/cli.hpp
mlpack/trunk/src/mlpack/core/util/cli_impl.hpp
mlpack/trunk/src/mlpack/core/util/log.cpp
mlpack/trunk/src/mlpack/core/util/log.hpp
mlpack/trunk/src/mlpack/core/util/option.hpp
mlpack/trunk/src/mlpack/core/util/option_impl.hpp
Log:
Modified string types in cli & related sources.
Modified: mlpack/trunk/src/mlpack/core/util/cli.cpp
===================================================================
--- mlpack/trunk/src/mlpack/core/util/cli.cpp 2012-03-02 23:49:42 UTC (rev 11708)
+++ mlpack/trunk/src/mlpack/core/util/cli.cpp 2012-03-02 23:52:42 UTC (rev 11709)
@@ -53,7 +53,7 @@
*
* @param optionsName Name of the module, as far as boost is concerned.
*/
-CLI::CLI(std::string optionsName) :
+CLI::CLI(const std::string& optionsName) :
desc(optionsName.c_str()), didParse(false), doc(&emptyProgramDoc)
{
return;
@@ -109,9 +109,9 @@
* @param alias An alias for the parameter.
* @param required Indicates if parameter must be set on command line.
*/
-void CLI::Add(std::string path,
- std::string description,
- std::string alias,
+void CLI::Add(const std::string& path,
+ const std::string& description,
+ const std::string& alias,
bool required)
{
po::options_description& desc = CLI::GetSingleton().desc;
@@ -150,7 +150,7 @@
* @param alias The alias we will use for the parameter.
* @param original The name of the actual parameter we will be mapping to.
*/
-void CLI::AddAlias(std::string alias, std::string original)
+void CLI::AddAlias(const std::string& alias, const std::string& original)
{
//Conduct the mapping
if (alias.length())
@@ -163,9 +163,9 @@
/*
* @brief Adds a flag parameter to CLI.
*/
-void CLI::AddFlag(std::string identifier,
- std::string description,
- std::string alias)
+void CLI::AddFlag(const std::string& identifier,
+ const std::string& description,
+ const std::string& alias)
{
// Reuse functionality from add
Add(identifier, description, alias, false);
@@ -183,7 +183,7 @@
gmap[data.name] = data;
}
-std::string CLI::AliasReverseLookup(std::string value)
+std::string CLI::AliasReverseLookup(const std::string& value)
{
amap_t& amap = GetSingleton().aliasValues;
amap_t::iterator iter;
@@ -258,23 +258,24 @@
*
* @param identifier The name of the parameter in question.
*/
-bool CLI::HasParam(std::string key)
+bool CLI::HasParam(const std::string& key)
{
+ std::string used_key = key;
po::variables_map vmap = GetSingleton().vmap;
gmap_t& gmap = GetSingleton().globalValues;
// Take any possible alias into account.
amap_t& amap = GetSingleton().aliasValues;
if (amap.count(key))
- key = amap[key];
+ used_key = amap[key];
// Does the parameter exist at all?
- int isInGmap = gmap.count(key);
+ int isInGmap = gmap.count(used_key);
// Check if the parameter is boolean; if it is, we just want to see if it was
// passed.
if (isInGmap)
- return gmap[key].wasPassed;
+ return gmap[used_key].wasPassed;
// The parameter was not passed in; return false.
return false;
@@ -287,7 +288,7 @@
* @param str String to hyphenate (splits are on ' ').
* @param padding Amount of padding on the left for each new line.
*/
-std::string CLI::HyphenateString(std::string str, int padding)
+std::string CLI::HyphenateString(const std::string& str, int padding)
{
size_t margin = 80 - padding;
if (str.length() < margin)
@@ -334,7 +335,7 @@
* @param identifier Name of the node in question.
* @return Description of the node in question.
*/
-std::string CLI::GetDescription(std::string identifier)
+std::string CLI::GetDescription(const std::string& identifier)
{
gmap_t& gmap = GetSingleton().globalValues;
std::string name = std::string(identifier);
@@ -477,22 +478,23 @@
}
/* Prints the descriptions of the current hierarchy. */
-void CLI::PrintHelp(std::string param)
+void CLI::PrintHelp(const std::string& param)
{
+ std::string used_param = param;
gmap_t& gmap = GetSingleton().globalValues;
amap_t& amap = GetSingleton().aliasValues;
gmap_t::iterator iter;
ProgramDoc docs = *GetSingleton().doc;
// If we pass a single param, alias it if necessary.
- if (param != "" && amap.count(param))
- param = amap[param];
+ if (used_param != "" && amap.count(used_param))
+ used_param = amap[used_param];
// Do we only want to print out one value?
- if (param != "" && gmap.count(param))
+ if (used_param != "" && gmap.count(used_param))
{
- ParamData data = gmap[param];
- std::string alias = AliasReverseLookup(param);
+ ParamData data = gmap[used_param];
+ std::string alias = AliasReverseLookup(used_param);
alias = alias.length() ? " (-" + alias + ")" : alias;
// Figure out the name of the type.
@@ -509,7 +511,7 @@
type = " [double]";
// Now, print the descriptions.
- std::string fullDesc = " --" + param + alias + type + " ";
+ std::string fullDesc = " --" + used_param + alias + type + " ";
if (fullDesc.length() <= 32) // It all fits on one line.
std::cout << fullDesc << std::string(32 - fullDesc.length(), ' ');
@@ -519,10 +521,10 @@
std::cout << HyphenateString(data.desc, 32) << std::endl;
return;
}
- else if (param != "")
+ else if (used_param != "")
{
// User passed a single variable, but it doesn't exist.
- std::cerr << "Parameter --" << param << " does not exist." << std::endl;
+ std::cerr << "Parameter --" << used_param << " does not exist." << std::endl;
exit(1); // Nothing left to do.
}
Modified: mlpack/trunk/src/mlpack/core/util/cli.hpp
===================================================================
--- mlpack/trunk/src/mlpack/core/util/cli.hpp 2012-03-02 23:49:42 UTC (rev 11708)
+++ mlpack/trunk/src/mlpack/core/util/cli.hpp 2012-03-02 23:52:42 UTC (rev 11709)
@@ -524,9 +524,9 @@
* ("").
* @param required Indicates if parameter must be set on command line.
*/
- static void Add(std::string path,
- std::string description,
- std::string alias = "",
+ static void Add(const std::string& path,
+ const std::string& description,
+ const std::string& alias = "",
bool required = false);
/**
@@ -541,9 +541,9 @@
* @param required Indicates if parameter must be set on command line.
*/
template<class T>
- static void Add(std::string identifier,
- std::string description,
- std::string alias = "",
+ static void Add(const std::string& identifier,
+ const std::string& description,
+ const std::string& alias = "",
bool required = false);
/**
@@ -553,9 +553,9 @@
* @param description Short string description of the parameter.
* @param alias An alias for the parameter, defaults to "" which is no alias.
*/
- static void AddFlag(std::string identifier,
- std::string description,
- std::string alias = "");
+ static void AddFlag(const std::string& identifier,
+ const std::string& description,
+ const std::string& alias = "");
/**
* Parses the parameters for 'help' and 'info'.
@@ -577,7 +577,7 @@
* @param identifier The name of the parameter in question.
*/
template<typename T>
- static T& GetParam(std::string identifier);
+ static T& GetParam(const std::string& identifier);
/**
* Get the description of the specified node.
@@ -585,7 +585,7 @@
* @param identifier Name of the node in question.
* @return Description of the node in question.
*/
- static std::string GetDescription(std::string identifier);
+ static std::string GetDescription(const std::string& identifier);
/**
* Retrieve the singleton.
@@ -606,7 +606,7 @@
*
* @param identifier The name of the parameter in question.
*/
- static bool HasParam(std::string identifier);
+ static bool HasParam(const std::string& identifier);
/**
* Hyphenate a string or split it onto multiple 80-character lines, with some
@@ -615,7 +615,7 @@
* @param str String to hyphenate (splits are on ' ').
* @param padding Amount of padding on the left for each new line.
*/
- static std::string HyphenateString(std::string str, int padding);
+ static std::string HyphenateString(const std::string& str, int padding);
/**
* Parses the commandline for arguments.
@@ -640,7 +640,7 @@
/**
* Print out the help info of the hierarchy.
*/
- static void PrintHelp(std::string param = "");
+ static void PrintHelp(const std::string& param = "");
/**
* Registers a ProgramDoc object, which contains documentation about the
@@ -697,7 +697,7 @@
* @param alias The name of the alias to be mapped.
* @param original The name of the parameter to be mapped.
*/
- static void AddAlias(std::string alias, std::string original);
+ static void AddAlias(const std::string& alias, const std::string& original);
/**
* Returns an alias, if given the name of the original.
@@ -706,7 +706,7 @@
* is an alias.
* @return The alias associated with value.
*/
- static std::string AliasReverseLookup(std::string value);
+ static std::string AliasReverseLookup(const std::string& value);
#ifdef _WIN32
/**
@@ -731,7 +731,7 @@
* @param str Input string.
* @return Sanitized string.
*/
- static std::string SanitizeString(std::string str);
+ static std::string SanitizeString(const std::string& str);
/**
* Parses the values given on the command line, overriding any default values.
@@ -748,7 +748,7 @@
*
* @param optionsName Name of the module, as far as boost is concerned.
*/
- CLI(std::string optionsName);
+ CLI(const std::string& optionsName);
//! Private copy constructor; we don't want copies floating around.
CLI(const CLI& other);
Modified: mlpack/trunk/src/mlpack/core/util/cli_impl.hpp
===================================================================
--- mlpack/trunk/src/mlpack/core/util/cli_impl.hpp 2012-03-02 23:49:42 UTC (rev 11708)
+++ mlpack/trunk/src/mlpack/core/util/cli_impl.hpp 2012-03-02 23:52:42 UTC (rev 11709)
@@ -28,9 +28,9 @@
* unless the parameter is specified.
*/
template<typename T>
-void CLI::Add(std::string path,
- std::string description,
- std::string alias,
+void CLI::Add(const std::string& path,
+ const std::string& description,
+ const std::string& alias,
bool required)
{
@@ -77,7 +77,7 @@
* valid.
*/
template<typename T>
-T& CLI::GetParam(std::string identifier)
+T& CLI::GetParam(const std::string& identifier)
{
// Used to ensure we have a valid value.
T tmp = T();
Modified: mlpack/trunk/src/mlpack/core/util/log.cpp
===================================================================
--- mlpack/trunk/src/mlpack/core/util/log.cpp 2012-03-02 23:49:42 UTC (rev 11708)
+++ mlpack/trunk/src/mlpack/core/util/log.cpp 2012-03-02 23:52:42 UTC (rev 11709)
@@ -38,7 +38,7 @@
// Only do anything for Assert() if in debugging mode.
#ifdef DEBUG
-void Log::Assert(bool condition, const char* message)
+void Log::Assert(bool condition, const std::string& message)
{
if (!condition)
{
@@ -111,6 +111,6 @@
}
}
#else
-void Log::Assert(bool /* condition */, const char* /* message */)
+void Log::Assert(bool /* condition */, const std::string& /* message */)
{ }
#endif
Modified: mlpack/trunk/src/mlpack/core/util/log.hpp
===================================================================
--- mlpack/trunk/src/mlpack/core/util/log.hpp 2012-03-02 23:49:42 UTC (rev 11708)
+++ mlpack/trunk/src/mlpack/core/util/log.hpp 2012-03-02 23:52:42 UTC (rev 11709)
@@ -7,6 +7,8 @@
#ifndef __MLPACK_CORE_IO_LOG_HPP
#define __MLPACK_CORE_IO_LOG_HPP
+#include <string>
+
#include "prefixedoutstream.hpp"
#include "nulloutstream.hpp"
@@ -53,7 +55,7 @@
* If not, halts program execution and prints a custom error message.
* Does nothing in non-debug mode.
*/
- static void Assert(bool condition, const char* message = "Assert Failed.");
+ static void Assert(bool condition, const std::string& message = std::string("Assert Failed."));
// We only use PrefixedOutStream if the program is compiled with debug
Modified: mlpack/trunk/src/mlpack/core/util/option.hpp
===================================================================
--- mlpack/trunk/src/mlpack/core/util/option.hpp 2012-03-02 23:49:42 UTC (rev 11708)
+++ mlpack/trunk/src/mlpack/core/util/option.hpp 2012-03-02 23:52:42 UTC (rev 11709)
@@ -44,9 +44,9 @@
*/
Option(bool ignoreTemplate,
N defaultValue,
- const char* identifier,
- const char* description,
- const char* parent = NULL,
+ const std::string& identifier,
+ const std::string& description,
+ const std::string& parent = std::string(""),
bool required = false);
/**
@@ -59,9 +59,9 @@
* @param parent Full pathname of the parent module that "owns" this option.
* The default is the root node (an empty string).
*/
- Option(const char* identifier,
- const char* description,
- const char* parent = NULL);
+ Option(const std::string& identifier,
+ const std::string& description,
+ const std::string& parent = std::string(""));
};
/**
Modified: mlpack/trunk/src/mlpack/core/util/option_impl.hpp
===================================================================
--- mlpack/trunk/src/mlpack/core/util/option_impl.hpp 2012-03-02 23:49:42 UTC (rev 11708)
+++ mlpack/trunk/src/mlpack/core/util/option_impl.hpp 2012-03-02 23:52:42 UTC (rev 11709)
@@ -19,23 +19,17 @@
template<typename N>
Option<N>::Option(bool ignoreTemplate,
N defaultValue,
- const char* identifier,
- const char* description,
- const char* alias,
+ const std::string& identifier,
+ const std::string& description,
+ const std::string& alias,
bool required)
{
if (ignoreTemplate)
{
- if (alias == NULL)
- alias = "";
-
CLI::Add(identifier, description, alias, required);
}
else
{
- if (alias == NULL)
- alias = "";
-
CLI::Add<N>(identifier, description, alias, required);
CLI::GetParam<N>(identifier) = defaultValue;
@@ -47,13 +41,10 @@
* Registers a flag parameter with CLI.
*/
template<typename N>
-Option<N>::Option(const char* identifier,
- const char* description,
- const char* alias)
+Option<N>::Option(const std::string& identifier,
+ const std::string& description,
+ const std::string& alias)
{
- if (alias == NULL)
- alias = "";
-
CLI::AddFlag(identifier, description, alias);
}
More information about the mlpack-svn
mailing list