Piping between Functions

  • Thread starter christopher diggins
  • Start date
C

christopher diggins

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.
 
A

Alex Vinokur

christopher diggins said:
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 said:
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]
 
C

christopher diggins

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;
}
 
A

Alex Vinokur

christopher diggins said:
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?
 
C

christopher diggins

Alex Vinokur said:
christopher diggins said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top