diff --git a/src/tools.h b/src/tools.h index 4776b0d0f..d2971b045 100644 --- a/src/tools.h +++ b/src/tools.h @@ -27,6 +27,7 @@ #include #include #include "config.h" +#include #include @@ -70,6 +71,36 @@ namespace zim { return count; } + /** + * @brief Stringstream Class to use itself as the stream object + * returned by << operator. (std::stringstream returns an std::ostream). + * Allows a one-line stringstream to str conversion, e.g. use_str(Formatter() + * << "foo" << variable); + * + */ + class Formatter + { + public: + Formatter() {} + ~Formatter() {} + + template Formatter &operator<<(const Type &value) + { + stream_ << value; + return *this; + } + + operator std::string() const { return stream_.str(); } + + private: + /* Disable copy and assignment constructors */ + Formatter(const Formatter &) = delete; + Formatter &operator=(Formatter &) = delete; + + /* Simple composition with std::stringstream */ + std::stringstream stream_; + }; + // Xapian based tools #if defined(ENABLE_XAPIAN) std::string removeAccents(const std::string& text);