Created
January 14, 2014 00:29
-
-
Save dgodfrey206/8410778 to your computer and use it in GitHub Desktop.
Manipulator base class
This file contains hidden or 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
#include <iostream> | |
#include <functional> | |
class manipulator | |
{ | |
public: | |
template<typename Stream, typename F, typename... Args> | |
manipulator(Stream& os, F&& f, Args&&... args) | |
: callback(std::bind(std::forward<F>(f), std::ref(os), std::forward<Args>(args)...)) | |
{ callback();} | |
private: | |
std::function<void ()> callback; | |
}; | |
class manipulator_wrapper | |
{ | |
class inner; | |
public: | |
manipulator_wrapper(std::string str) | |
: value(str) | |
{ | |
} | |
template<class Stream> | |
friend manipulator_wrapper::inner operator<<(Stream& os, const manipulator_wrapper& mw); | |
private: | |
class inner : public manipulator | |
{ | |
public: | |
inner(std::ostream& os, std::string str) | |
: manipulator(os, &inner::go, str) | |
{ } | |
private: | |
static void go(std::ostream& os, std::string str) | |
{ | |
os << "Your message is: " << str; | |
} | |
}; | |
std::string value; | |
}; | |
template<class Stream> | |
manipulator_wrapper::inner operator<<(Stream& os, const manipulator_wrapper& mw) | |
{ | |
return manipulator_wrapper::inner(os, mw.value); | |
} | |
manipulator_wrapper display(std::string str) | |
{ | |
return manipulator_wrapper(str); | |
} | |
int main() | |
{ | |
std::cout << display("hello"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment