formatter.hpp

Go to the documentation of this file.
00001 /* $Id: formatter.hpp 54013 2012-04-29 14:15:48Z mordante $ */
00002 /*
00003    Copyright (C) 2007 - 2012 by David White <dave.net>
00004    Part of the Silver Tree Project
00005 
00006    This program is free software; you can redistribute it and/or modify
00007    it under the terms of the GNU General Public License as published by or later.
00008    This program is distributed in the hope that it will be useful,
00009    but WITHOUT ANY WARRANTY.
00010 
00011    See the COPYING file for more details.
00012 */
00013 
00014 #ifndef FORMATTER_HPP_INCLUDED
00015 #define FORMATTER_HPP_INCLUDED
00016 
00017 #include <sstream>
00018 
00019 /**
00020  * std::ostringstream wrapper.
00021  *
00022  * ostringstream's operator<< doesn't return a ostringstream&. It returns an
00023  * ostream& instead. This is unfortunate, because it means that you can't do
00024  * something like this: (ostringstream() << n).str() to convert an integer to a
00025  * string, all in one line instead you have to use this far more tedious
00026  * approach:
00027  *  ostringstream s;
00028  *  s << n;
00029  *  s.str();
00030  * This class corrects this shortcoming, allowing something like this:
00031  *  string result = (formatter() << "blah " << n << x << " blah").str();
00032  */
00033 class formatter
00034 {
00035 public:
00036     formatter() :
00037         stream_()
00038     {
00039     }
00040 
00041     template<typename T>
00042     formatter& operator<<(const T& o) {
00043         stream_ << o;
00044         return *this;
00045     }
00046 
00047     std::string str() {
00048         return stream_.str();
00049     }
00050 
00051 private:
00052     std::ostringstream stream_;
00053 };
00054 
00055 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated by doxygen 1.7.1 on Thu May 24 2012 01:02:36 for The Battle for Wesnoth
Gna! | Forum | Wiki | CIA | devdocs