Skip to content

Instantly share code, notes, and snippets.

@aleksihakli
Last active August 29, 2015 14:01
Show Gist options
  • Save aleksihakli/c1ce42244df5b2660b0b to your computer and use it in GitHub Desktop.
Save aleksihakli/c1ce42244df5b2660b0b to your computer and use it in GitHub Desktop.
Atomic printer sketch for threaded C++11
#pragma once
#ifndef PRINTER_HH
#define PRINTER_HH
#include <string>
#include <sstream>
namespace {
/*
* @brief prints objects atomically.
*/
class Printer {
public:
explicit Printer();
~Printer();
void print(const std::string& str);
private:
Mutex m_;
};
/*
* @brief appends list of items into given stringstream.
*/
void buffer(const std::stringstream&) {}
template <typename H, typename... T>
void buffer(std::stringstream& pbuffer, H head, T... tail) {
pbuffer << head;
buffer(pbuffer, tail...);
}
} // anonymous namespace
/**
* @brief print prints a list of objects.
*
* Objects only need to have operator << for ostream defined.
*/
void print(const std::string& str);
template <typename... L>
void print(L... list) {
std::stringstream pbuffer;
buffer(pbuffer, list...);
print(pbuffer.str());
}
#endif // PRINTER_HH
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment