Skip to content

Commit

Permalink
Introduced Formatter Class to libzim
Browse files Browse the repository at this point in the history
  • Loading branch information
ShaopengLin committed Mar 3, 2024
1 parent 65bf7a8 commit 8c24319
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/tools.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include <map>
#include <vector>
#include "config.h"
#include <sstream>

#include <zim/item.h>

Expand Down Expand Up @@ -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 <typename Type> 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);
Expand Down

0 comments on commit 8c24319

Please sign in to comment.