Author: mordante
Date: Tue Jun 17 19:29:53 2008
New Revision: 27260
URL: http://svn.gna.org/viewcvs/wesnoth?rev=27260&view=rev
Log:
Added comment to the formatter class, based on what Sirp said on IRC.
Removed the implicit string conversion and fixed the users.
Added svn $id$.
Modified:
trunk/src/formatter.hpp
trunk/src/variant.cpp
Modified: trunk/src/formatter.hpp
URL:
http://svn.gna.org/viewcvs/wesnoth/trunk/src/formatter.hpp?rev=27260&r1=27259&r2=27260&view=diff
==============================================================================
--- trunk/src/formatter.hpp (original)
+++ trunk/src/formatter.hpp Tue Jun 17 19:29:53 2008
@@ -1,4 +1,4 @@
-
+/* $Id$ */
/*
Copyright (C) 2007 by David White <dave.net>
Part of the Silver Tree Project
@@ -10,11 +10,26 @@
See the COPYING file for more details.
*/
+
#ifndef FORMATTER_HPP_INCLUDED
#define FORMATTER_HPP_INCLUDED
#include <sstream>
+/**
+ * std::ostringstream wrapper.
+ *
+ * ostringstream's operator<< doesn't return a ostringstream&. It returns an
+ * ostream& instead. This is unfortunate, because it means that you can't do
+ * something like this: (ostringstream() << n).str() to convert an integer to a
+ * string, all in one line instead you have to use this far more tedious
+ * approach:
+ * ostringstream s;
+ * s << n;
+ * s.str();
+ * This class corrects this shortcoming, allowing something like this:
+ * string result = (formatter() << "blah " << n << x << " blah").str();
+ */
class formatter
{
public:
@@ -32,9 +47,6 @@
return str().c_str();
}
- operator std::string() {
- return stream_.str();
- }
private:
std::ostringstream stream_;
};
Modified: trunk/src/variant.cpp
URL:
http://svn.gna.org/viewcvs/wesnoth/trunk/src/variant.cpp?rev=27260&r1=27259&r2=27260&view=diff
==============================================================================
--- trunk/src/variant.cpp (original)
+++ trunk/src/variant.cpp Tue Jun 17 19:29:53 2008
@@ -8,7 +8,6 @@
#include "boost/lexical_cast.hpp"
-//#include "foreach.hpp"
#include "formatter.hpp"
#include "formula.hpp"
#include "formula_callable.hpp"
@@ -235,7 +234,10 @@
} else if(type_ == TYPE_LIST) {
return operator[](v.as_int());
} else {
- throw type_error(formatter() << "type error: " << " expected a
list or a map but found " << variant_type_to_string(type_) << " (" <<
to_debug_string() << ")");
+ throw type_error((formatter() << "type error: "
+ << " expected a list or a map but found "
+ << variant_type_to_string(type_)
+ << " (" << to_debug_string() << ")").str());
}
}
@@ -280,7 +282,10 @@
assert(map_);
return map_->elements.size();
} else {
- throw type_error(formatter() << "type error: " << " expected a
list or a map but found " << variant_type_to_string(type_) << " (" <<
to_debug_string() << ")");
+ throw type_error((formatter() << "type error: "
+ << " expected a list or a map but found "
+ << variant_type_to_string(type_)
+ << " (" << to_debug_string() << ")").str());
}
}
@@ -374,7 +379,7 @@
const int numerator = as_int();
const int denominator = v.as_int();
if(denominator == 0) {
- throw type_error(formatter() << "divide by zero error");
+ throw type_error((formatter() << "divide by zero
error").str());;
}
return variant(numerator/denominator);
@@ -385,7 +390,7 @@
const int numerator = as_int();
const int denominator = v.as_int();
if(denominator == 0) {
- throw type_error(formatter() << "divide by zero error");
+ throw type_error((formatter() << "divide by zero error").str());
}
return variant(numerator%denominator);
@@ -514,7 +519,10 @@
void variant::must_be(variant::TYPE t) const
{
if(type_ != t) {
- throw type_error(formatter() << "type error: " << " expected "
<< variant_type_to_string(t) << " but found " << variant_type_to_string(type_)
<< " (" << to_debug_string() << ")");
+ throw type_error((formatter() << "type error: " << " expected "
+ << variant_type_to_string(t) << " but found "
+ << variant_type_to_string(type_)
+ << " (" << to_debug_string() << ")").str());
}
}
_______________________________________________
Wesnoth-commits mailing list
[email protected]
https://mail.gna.org/listinfo/wesnoth-commits