Added osg::MakeString class to make it easier to create std::string's using std::ostream style << usage.

This commit is contained in:
Robert Osfield 2016-11-18 14:56:05 +00:00
parent f510613d55
commit 62a9f87f45

View File

@ -16,6 +16,7 @@
#include <ostream>
#include <istream>
#include <sstream>
#include <osg/Vec4d>
#include <osg/Vec4ub>
@ -35,6 +36,43 @@
namespace osg {
/** Convinience class for building std::string using stringstream.
* Usage:
* MakeString str;
* std::string s = str<<"Mix strings with numbers "<<0" ;
* std::string s2 = str.clear()<<"and other classes such as ("<<osg::Vec3(0.0,1.0,3.0)<<)" ; */
class MakeString
{
public:
MakeString() {}
std::stringstream sstream;
template<typename T>
MakeString& operator << (const T& t)
{
sstream << t;
return *this;
}
MakeString& operator << (std::ostream& (*fun)(std::ostream&))
{
sstream << fun;
return *this;
}
inline MakeString& clear() { sstream.str("") ; return *this; }
inline operator std::string () const { return sstream.str(); }
inline std::string str() const { return sstream.str(); }
inline const char* c_str() const { return sstream.str().c_str(); }
};
inline std::ostream& operator << (std::ostream& output, const MakeString& str) { output << str.str(); return output; }
//////////////////////////////////////////////////////////////////////////
// Vec2f streaming operators
inline std::ostream& operator << (std::ostream& output, const Vec2f& vec)