470,586 Members | 1,358 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,586 developers. It's quick & easy.

Piping between Functions

I just wanted to share this technique for piping the cout from one function
to the cin of another, using the pipe operator:

#include <iostream>
#include <sstream>

using namespace std;

typedef void(*procedure)();

class filter {
public:
filter(procedure x) : proc(x) { }
void operator()(istream& in, ostream& out) {
streambuf* inbuf = cin.rdbuf();
streambuf* outbuf = cout.rdbuf();
cin.rdbuf(in.rdbuf());
cout.rdbuf(out.rdbuf());
proc();
cin.rdbuf(inbuf);
cout.rdbuf(outbuf);
}
private:
procedure proc;
};

void operator|(filter f1, filter f2) {
stringstream s;
f1(cin, s);
s.seekg(0);
f2(s, cout);
}

void HelloWorld() {
cout << "hello world" << endl;
}

void CountChars() {
string s;
s = cin.getline();
cout << static_cast<int>(s.size()) << endl;
}

int main() {
filter(HelloWorld) | filter(CountChars);
return 0;
}

Any comments or suggestions?

I am working on a version which allows passing of arguments to functions,
and which allows arbitrary chaining.

--
Christopher Diggins
http://www.cdiggins.com
http://www.heron-language.com
Jul 22 '05 #1
4 2355

"christopher diggins" <cd******@videotron.ca> wrote in message news:sA*********************@wagner.videotron.net. ..
I just wanted to share this technique for piping the cout from one function
to the cin of another, using the pipe operator:

#include <iostream>
#include <sstream>

using namespace std;

typedef void(*procedure)();

class filter {
public:
filter(procedure x) : proc(x) { }
void operator()(istream& in, ostream& out) {
streambuf* inbuf = cin.rdbuf();
streambuf* outbuf = cout.rdbuf();
cin.rdbuf(in.rdbuf());
cout.rdbuf(out.rdbuf());
proc();
cin.rdbuf(inbuf);
cout.rdbuf(outbuf);
}
private:
procedure proc;
};

void operator|(filter f1, filter f2) {
stringstream s;
f1(cin, s);
s.seekg(0);
f2(s, cout);
}

void HelloWorld() {
cout << "hello world" << endl;
}

void CountChars() {
string s; -------------------------------- s = cin.getline(); // Compiler g++ 3.3.3
In function `void CountChars()':
error: no matching function for call to `std::basic_istream<char,
std::char_traits<char> >::getline()'
/usr/include/c++/3.3.3/bits/istream.tcc:594: error: candidates are:
std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT,
_Traits>::getline(_CharT*, int, _CharT) [with _CharT = char, _Traits =
std::char_traits<char>]
/usr/include/c++/3.3.3/istream:401: error:
std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT,
_Traits>::getline(_CharT*, int) [with _CharT = char, _Traits =
std::char_traits<char>]
-------------------------------- cout << static_cast<int>(s.size()) << endl;
}

int main() {
filter(HelloWorld) | filter(CountChars);
return 0;
}

Any comments or suggestions?

I am working on a version which allows passing of arguments to functions,
and which allows arbitrary chaining.

[snip]

--
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn

Jul 22 '05 #2
Thanks for pointing that out Alex. My apologies for the error. Please
update:

void CountChars() {
string s;
s = cin.getline();
cout << static_cast<int>(s.size()) << endl;
}

to

void CountChars() {
string s;
getline(cin, s);
cout << static_cast<int>(s.size()) << endl;
}

--
Christopher Diggins
http://www.cdiggins.com
http://www.heron-language.com
Jul 22 '05 #3

"christopher diggins" <cd******@videotron.ca> wrote in message news:LN********************@wagner.videotron.net.. .
Thanks for pointing that out Alex. My apologies for the error. Please
update:

void CountChars() {
string s;
s = cin.getline();
cout << static_cast<int>(s.size()) << endl;
}

to

void CountChars() {
string s;
getline(cin, s);
cout << static_cast<int>(s.size()) << endl;
}

[snip]

Piping between functions looks very nice.

How do you mean to use that?

Are there any list of samples/applications?
--
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn



Jul 22 '05 #4
"Alex Vinokur" <al****@big-foot.com> wrote in message
news:34*************@individual.net...

"christopher diggins" <cd******@videotron.ca> wrote in message
news:LN********************@wagner.videotron.net.. .
Thanks for pointing that out Alex. My apologies for the error. Please
update:

void CountChars() {
string s;
s = cin.getline();
cout << static_cast<int>(s.size()) << endl;
}

to

void CountChars() {
string s;
getline(cin, s);
cout << static_cast<int>(s.size()) << endl;
}

[snip]

Piping between functions looks very nice.

How do you mean to use that?

Are there any list of samples/applications?


I am using the techniuq to develop my applications in a more reusable
manner. For instance I can write many of my applications (currently
primarily text parsing tools) as filters. I can then string them together to
create new functions/applications. My plan is to code C++ as if I was
writing unix shell scripts. It basically reduces the amount of code I need
for my applications, which I always like.

--
Christopher Diggins
Object Oriented Template Library (OOTL)
http://www.ootl.org
Jul 22 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Csaba Henk | last post: by
2 posts views Thread by Apple Grew | last post: by
3 posts views Thread by Russ Schneider | last post: by
1 post views Thread by saibotorama | last post: by
2 posts views Thread by moti | last post: by
3 posts views Thread by noob2008 | last post: by
4 posts views Thread by samit | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.