[Orxonox-commit 4376] r9047 - in code/branches/testing/src: libraries/core/command libraries/util modules/notifications/dispatchers
landauf at orxonox.net
landauf at orxonox.net
Sun Mar 18 18:43:24 CET 2012
Author: landauf
Date: 2012-03-18 18:43:24 +0100 (Sun, 18 Mar 2012)
New Revision: 9047
Modified:
code/branches/testing/src/libraries/core/command/ConsoleCommand.h
code/branches/testing/src/libraries/util/SubString.cc
code/branches/testing/src/libraries/util/SubString.h
code/branches/testing/src/libraries/util/VA_NARGS.h
code/branches/testing/src/modules/notifications/dispatchers/CommandNotification.cc
Log:
made SubString more standard compliant
Modified: code/branches/testing/src/libraries/core/command/ConsoleCommand.h
===================================================================
--- code/branches/testing/src/libraries/core/command/ConsoleCommand.h 2012-03-17 18:19:14 UTC (rev 9046)
+++ code/branches/testing/src/libraries/core/command/ConsoleCommand.h 2012-03-18 17:43:24 UTC (rev 9047)
@@ -221,8 +221,6 @@
#include <stack>
#include <vector>
-#include <boost/preprocessor/cat.hpp>
-#include <boost/preprocessor/facilities/expand.hpp>
#include "util/VA_NARGS.h"
#include "ArgumentCompletionFunctions.h"
Modified: code/branches/testing/src/libraries/util/SubString.cc
===================================================================
--- code/branches/testing/src/libraries/util/SubString.cc 2012-03-17 18:19:14 UTC (rev 9046)
+++ code/branches/testing/src/libraries/util/SubString.cc 2012-03-18 17:43:24 UTC (rev 9047)
@@ -86,34 +86,19 @@
@brief creates a new SubString based on a subset of an other SubString.
@param other The other SubString
@param begin The beginning of the subset
+ @param length The length of the subset
- The subset ranges from the token with index @a begin to the end of the tokens.
- If @a begin is greater than the greatest index, the new SubString will be empty.
+ The subset ranges from the token with index @a begin and contains @a length elements.
*/
- SubString::SubString(const SubString& other, unsigned int begin)
+ SubString::SubString(const SubString& other, size_t begin, size_t length)
{
- for (unsigned int i = begin; i < other.size(); ++i)
+ for (size_t i = 0; i < length; ++i)
{
- this->tokens_.push_back(other[i]);
- this->bTokenInSafemode_.push_back(other.isInSafemode(i));
- }
- }
+ if (begin + i >= other.size())
+ break;
- /**
- @brief creates a new SubString based on a subset of an other SubString.
- @param other The other SubString
- @param begin The beginning of the subset
- @param end The end of the subset
-
- The subset ranges from the token with index @a begin until (but not including) the token with index @a end.
- If @a begin or @a end are beyond the allowed index, the resulting SubString will be empty.
- */
- SubString::SubString(const SubString& other, unsigned int begin, unsigned int end)
- {
- for (unsigned int i = begin; i < std::min(other.size(), end); ++i)
- {
- this->tokens_.push_back(other[i]);
- this->bTokenInSafemode_.push_back(other.isInSafemode(i));
+ this->tokens_.push_back(other[begin + i]);
+ this->bTokenInSafemode_.push_back(other.isInSafemode(begin + i));
}
}
@@ -122,9 +107,9 @@
@param argc The number of arguments
@param argv An array of pointers to the arguments
*/
- SubString::SubString(unsigned int argc, const char** argv)
+ SubString::SubString(size_t argc, const char** argv)
{
- for(unsigned int i = 0; i < argc; ++i)
+ for (size_t i = 0; i < argc; ++i)
{
this->tokens_.push_back(std::string(argv[i]));
this->bTokenInSafemode_.push_back(false);
@@ -157,26 +142,19 @@
}
/**
- @copydoc operator==
- */
- bool SubString::compare(const SubString& other) const
- {
- return (*this == other);
- }
-
- /**
@brief Compares this SubString to another SubString and returns true if the first @a length values match.
@param other The other SubString
@param length How many tokens to compare
*/
- bool SubString::compare(const SubString& other, unsigned int length) const
+ bool SubString::compare(const SubString& other, size_t length) const
{
- if (length > this->size() || length > other.size())
+ if (std::min(length, this->size()) != std::min(length, other.size()))
return false;
- for (unsigned int i = 0; i < length; ++i)
+ for (size_t i = 0; i < std::min(length, this->size()); ++i)
if ((this->tokens_[i] != other.tokens_[i]) || (this->bTokenInSafemode_[i] != other.bTokenInSafemode_[i]))
return false;
+
return true;
}
@@ -195,7 +173,7 @@
*/
SubString& SubString::operator+=(const SubString& other)
{
- for (unsigned int i = 0; i < other.size(); ++i)
+ for (size_t i = 0; i < other.size(); ++i)
{
this->tokens_.push_back(other[i]);
this->bTokenInSafemode_.push_back(other.isInSafemode(i));
@@ -206,10 +184,10 @@
/**
@copydoc SubString(const std::string&,const std::string&,const std::string&,bool,char,bool,char,bool,char,char,bool,char)
*/
- unsigned int SubString::split(const std::string& line,
- const std::string& delimiters, const std::string& delimiterNeighbours, bool bAllowEmptyEntries,
- char escapeChar, bool bRemoveEscapeChar, char safemodeChar, bool bRemoveSafemodeChar,
- char openparenthesisChar, char closeparenthesisChar, bool bRemoveParenthesisChars, char commentChar)
+ size_t SubString::split(const std::string& line,
+ const std::string& delimiters, const std::string& delimiterNeighbours, bool bAllowEmptyEntries,
+ char escapeChar, bool bRemoveEscapeChar, char safemodeChar, bool bRemoveSafemodeChar,
+ char openparenthesisChar, char closeparenthesisChar, bool bRemoveParenthesisChars, char commentChar)
{
this->tokens_.clear();
this->bTokenInSafemode_.clear();
@@ -227,7 +205,7 @@
if (!this->tokens_.empty())
{
std::string retVal = this->tokens_[0];
- for (unsigned int i = 1; i < this->tokens_.size(); ++i)
+ for (size_t i = 1; i < this->tokens_.size(); ++i)
retVal += delimiter + this->tokens_[i];
return retVal;
}
@@ -238,37 +216,20 @@
/**
@brief Creates a subset of this SubString.
@param begin The beginning of the subset
+ @param length The length of the subset
@return A new SubString containing the defined subset.
- The subset ranges from the token with index @a begin to the end of the tokens.
- If @a begin is greater than the greatest index, the new SubString will be empty.
+ The subset ranges from the token with index @a begin and contains @a length elements.
This function is added for your convenience, and does the same as
- SubString::SubString(const SubString& other, unsigned int begin)
+ SubString::SubString(const SubString& other, size_t begin, size_t length)
*/
- SubString SubString::subSet(unsigned int begin) const
+ SubString SubString::subSet(size_t begin, size_t length) const
{
- return SubString(*this, begin);
+ return SubString(*this, begin, length);
}
/**
- @brief Creates a subset of this SubString.
- @param begin The beginning of the subset
- @param end The ending of the subset
- @return A new SubString containing the defined subset.
-
- The subset ranges from the token with index @a begin until (but not including) the token with index @a end.
- If @a begin or @a end are beyond the allowed index, the resulting SubString will be empty.
-
- This function is added for your convenience, and does the same as
- SubString::SubString(const SubString& other, unsigned int begin, unsigned int end)
- */
- SubString SubString::subSet(unsigned int begin, unsigned int end) const
- {
- return SubString(*this, begin, end);
- }
-
- /**
@copydoc SubString(const std::string&,const std::string&,const std::string&,bool,char,bool,char,bool,char,char,bool,char)
@param tokens The array, where the splitted strings will be stored in
@param bTokenInSafemode A vector wich stores for each character of the string if it is in safemode or not
@@ -297,8 +258,8 @@
SPLIT_LINE_STATE start_state)
{
SPLIT_LINE_STATE state = start_state;
- unsigned int i = 0;
- unsigned int fallBackNeighbours = 0;
+ size_t i = 0;
+ size_t fallBackNeighbours = 0;
std::string token;
bool inSafemode = false;
@@ -513,7 +474,7 @@
void SubString::debug() const
{
orxout(debug_output) << "Substring-information::count=" << this->tokens_.size() << " ::";
- for (unsigned int i = 0; i < this->tokens_.size(); ++i)
+ for (size_t i = 0; i < this->tokens_.size(); ++i)
orxout(debug_output) << "s" << i << "='" << this->tokens_[i].c_str() << "'::";
orxout(debug_output) << endl;
}
Modified: code/branches/testing/src/libraries/util/SubString.h
===================================================================
--- code/branches/testing/src/libraries/util/SubString.h 2012-03-17 18:19:14 UTC (rev 9046)
+++ code/branches/testing/src/libraries/util/SubString.h 2012-03-18 17:43:24 UTC (rev 9047)
@@ -57,7 +57,7 @@
std::string text = "This is a test, \"Hello \\\" World\" and vector {1, 2, 3}";
SubString tokens(text, SubString::WhiteSpaces, "", false, '\\', true, '"', true, '{', '}', true, '\0');
- for (unsigned int i = 0; i < tokens.size(); ++i)
+ for (size_t i = 0; i < tokens.size(); ++i)
orxout() << i << ": " << tokens[i] << endl;
@endcode
@@ -126,16 +126,14 @@
char closeparenthesisChar = '}',
bool bRemoveParenthesisChars = true,
char commentChar = '\0');
- SubString(unsigned int argc, const char** argv);
- SubString(const SubString& other, unsigned int begin);
- SubString(const SubString& other, unsigned int begin, unsigned int end);
+ SubString(size_t argc, const char** argv);
+ SubString(const SubString& other, size_t begin, size_t length = std::string::npos);
~SubString();
// operate on the SubString
SubString& operator=(const SubString& other);
bool operator==(const SubString& other) const;
- bool compare(const SubString& other) const;
- bool compare(const SubString& other, unsigned int length) const;
+ bool compare(const SubString& other, size_t length = std::string::npos) const;
SubString operator+(const SubString& other) const;
SubString& operator+=(const SubString& other);
/// Appends the tokens of another SubString to this. @return This SubString.
@@ -143,39 +141,38 @@
/////////////////////////////////////////
// Split and Join the any String. ///////
- unsigned int split(const std::string& line,
- const std::string& delimiters = SubString::WhiteSpaces,
- const std::string& delimiterNeighbours = "",
- bool bAllowEmptyEntries = false,
- char escapeChar ='\\',
- bool bRemoveEscapeChar = true,
- char safemodeChar = '"',
- bool bRemoveSafemodeChar = true,
- char openparenthesisChar = '{',
- char closeparenthesisChar = '}',
- bool bRemoveParenthesisChars = true,
- char commentChar = '\0');
+ size_t split(const std::string& line,
+ const std::string& delimiters = SubString::WhiteSpaces,
+ const std::string& delimiterNeighbours = "",
+ bool bAllowEmptyEntries = false,
+ char escapeChar ='\\',
+ bool bRemoveEscapeChar = true,
+ char safemodeChar = '"',
+ bool bRemoveSafemodeChar = true,
+ char openparenthesisChar = '{',
+ char closeparenthesisChar = '}',
+ bool bRemoveParenthesisChars = true,
+ char commentChar = '\0');
std::string join(const std::string& delimiter = " ") const;
////////////////////////////////////////
// retrieve a SubSet from the String
- SubString subSet(unsigned int begin) const;
- SubString subSet(unsigned int begin, unsigned int end) const;
+ SubString subSet(size_t begin, size_t length = std::string::npos) const;
// retrieve Information from within
/// Returns true if the SubString is empty
inline bool empty() const { return this->tokens_.empty(); }
/// Returns the number of tokens stored in this SubString
- inline unsigned int size() const { return this->tokens_.size(); }
+ inline size_t size() const { return this->tokens_.size(); }
/// Returns the i'th token from the subset of strings @param index The index of the requested token
- inline const std::string& operator[](unsigned int index) const { return this->tokens_[index]; }
+ inline const std::string& operator[](size_t index) const { return this->tokens_[index]; }
/// Returns the i'th token from the subset of strings @param index The index of the requested token
- inline const std::string& getString(unsigned int index) const { return (*this)[index]; }
+ inline const std::string& getString(size_t index) const { return (*this)[index]; }
/// Returns all tokens as std::vector
inline const std::vector<std::string>& getAllStrings() const { return this->tokens_; }
/// Returns true if the token is in safemode. @param index The index of the token
- inline bool isInSafemode(unsigned int index) const { return this->bTokenInSafemode_[index]; }
+ inline bool isInSafemode(size_t index) const { return this->bTokenInSafemode_[index]; }
/// Returns the front of the list of tokens.
inline const std::string& front() const { return this->tokens_.front(); }
/// Returns the back of the list of tokens.
Modified: code/branches/testing/src/libraries/util/VA_NARGS.h
===================================================================
--- code/branches/testing/src/libraries/util/VA_NARGS.h 2012-03-17 18:19:14 UTC (rev 9046)
+++ code/branches/testing/src/libraries/util/VA_NARGS.h 2012-03-18 17:43:24 UTC (rev 9047)
@@ -35,6 +35,8 @@
(of course overloading is not possible for different types, as macros are not
type aware, but for different numbers of arguments is still very powerful).
+ Important: The macro can not be overloaded for 0 arguments: ORXONOX_VA_NARGS() returns 1!
+
Example: A macro to call functions
@code
myfunction(); // A static function
@@ -63,6 +65,7 @@
the number of arguments ("1" - "N"). Then all arguments are passed to the right macro.
*/
+#include <boost/preprocessor/cat.hpp>
#include <boost/preprocessor/facilities/expand.hpp>
/**
Modified: code/branches/testing/src/modules/notifications/dispatchers/CommandNotification.cc
===================================================================
--- code/branches/testing/src/modules/notifications/dispatchers/CommandNotification.cc 2012-03-17 18:19:14 UTC (rev 9046)
+++ code/branches/testing/src/modules/notifications/dispatchers/CommandNotification.cc 2012-03-18 17:43:24 UTC (rev 9047)
@@ -116,19 +116,19 @@
//TODO: Move to KeyBinderManager...
const std::string& CommandNotification::bindingNiceifyer(const std::string& binding)
{
- SubString string = SubString(binding, ".");
+ SubString substring = SubString(binding, ".");
std::string name;
std::string group;
- switch(string.size())
+ switch(substring.size())
{
case 0:
return binding;
case 1:
return binding;
case 2:
- group = string[0];
+ group = substring[0];
default:
- name = string.subSet(1, string.size()).join(".");
+ name = substring.subSet(1).join(".");
}
std::stringstream stream;
@@ -147,5 +147,5 @@
return *(new std::string(stream.str()));
}
-
+
}
More information about the Orxonox-commit
mailing list