Continued ripping up the source.
[aesalon.git] / monitor / src / misc / StreamAsString.h
blob86b6f3a9abf722d0a21463147e099936c260ea54
1 #ifndef AESALON_MISC_STREAM_AS_STRING_H
2 #define AESALON_MISC_STREAM_AS_STRING_H
4 #include <string>
5 #include <sstream>
7 namespace Aesalon {
8 namespace Misc {
10 /** StreamAsString class.
12 Allows for nice inline stringstreams, such as
13 Exception(StreamAsString() << "Exception occured, line " << __line__);
15 class StreamAsString {
16 public:
17 /** Automatic conversion to a std::string.
18 @return The internal stringstream, converted to a std::string.
20 operator std::string() const {
21 return get_stream().str();
23 /** Add something onto the internal std::stringstream.
24 @param data The data to add.
25 @return The new stringstream, with the data added.
27 template<typename Type>
28 StreamAsString &operator<<(const Type &data) {
29 get_stream() << data;
31 return *this;
33 protected:
34 /** Get the internal stringstream.
35 @return The internal stringstream, in non-const form.
37 std::ostringstream &get_stream() { return stream; }
38 /** Get the internal stringstream.
39 @return The internal stringstream, in const form.
41 const std::ostringstream &get_stream() const { return stream; }
42 private:
43 /** The internal stringstream. */
44 std::ostringstream stream;
47 } // namespace Misc
48 } // namespace Aesalon
50 #endif