From 67156bb213a818f008fbf21494062923d98839d6 Mon Sep 17 00:00:00 2001 From: bergmann Date: Fri, 4 Oct 2019 23:21:34 +0200 Subject: [PATCH] * Implemented string builder --- include/cppcore/misc/string_builder.h | 33 +++++++++++++++++++++++++ include/cppcore/misc/string_builder.inl | 23 +++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 include/cppcore/misc/string_builder.h create mode 100644 include/cppcore/misc/string_builder.inl diff --git a/include/cppcore/misc/string_builder.h b/include/cppcore/misc/string_builder.h new file mode 100644 index 0000000..de6cd18 --- /dev/null +++ b/include/cppcore/misc/string_builder.h @@ -0,0 +1,33 @@ +#pragma once + +#include + +namespace cppcore +{ + + struct string_builder + { + private: + std::ostringstream os; + + public: + /** + * @brief Write something to the string builder + */ + template + string_builder& operator<<(TArg&& arg); + + /** + * @brief Get the constructed string. + */ + inline std::string str() const; + + /** + * @brief Get the constructed string. + */ + inline operator std::string() const; + }; + +} + +#include "string_builder.inl" diff --git a/include/cppcore/misc/string_builder.inl b/include/cppcore/misc/string_builder.inl new file mode 100644 index 0000000..f868fdd --- /dev/null +++ b/include/cppcore/misc/string_builder.inl @@ -0,0 +1,23 @@ +#pragma once + +#include "string_builder.h" + +namespace cppcore +{ + + /* string_builder */ + + template + string_builder& string_builder::operator<<(TArg&& arg) + { + os << std::forward(arg); + return *this; + } + + std::string string_builder::str() const + { return os.str(); } + + string_builder::operator std::string() const + { return str(); } + +}