Last active
August 29, 2015 14:01
-
-
Save aleksihakli/c1ce42244df5b2660b0b to your computer and use it in GitHub Desktop.
Atomic printer sketch for threaded C++11
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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