I'm currently writing a wrapper for STL stream to synchronize write calls from multiple threads. I have the following (simplified) code:
class Synchronize {
private:
std::stringstream ss;
public:
void write(std::string& str) {
// locking ...
ss << str;
// unlocking ...
};
// other stuff ..
};
Synchronize& operator<<(Synchronize& o, std::string& str) {
o.write(str);
return o;
}
Synchronize& operator<<(Synchronize* o, std::string& str) {
o->write(str);
return *o;
}
Its now possible to call the write()
method by using the <<
operator on an object of the Synchronize
class, but only by using a std::string
. And std::stringstream
also takes a lot of other stuff like int
s and float
s.
Is it possible to add this functionality to my Synchronize
class without a ton of own operator<<
functions? Would templates help? Or should I extend some class from the iostream
library?