473,320 Members | 2,112 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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 2512

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Csaba Henk | last post by:
For me, one reason for using Python is that with the help of it I can rid of several problems that I faced with during writing scripts in Bourneish shells. Biggest of these problem was the...
2
by: Apple Grew | last post by:
I want to develope a Winboard like application which will support Winboard protocol. For that I require to know how to handle piping in Python. I seperated out module popen2, but when I use...
3
by: Russ Schneider | last post by:
Is there an easy way to pipe a select statement's output to a file? -- http://www.sugapablo.com <--music ] http://www.sugapablo.net <--personal ] sugapablo@12jabber.com <--jabber IM ]...
1
by: saibotorama | last post by:
What is the Python translation for this Bash statement: tar cf - "${files}" | bzip2 "$file".tar.bz2 (Ignoring the fact that "tar cjf" also exists...) In other words, how does one pipe...
2
by: moti | last post by:
I am a newbie to MySQL. I'm confused as to the piping syntax of SERVER in the User DSN in ODBC for a client PC. If I use an ip it does not work, probably because of a firewall. So I tried using...
3
by: noob2008 | last post by:
can anyone explain me what is piping in C++ programming? for example invoice.exe < purchase.dat > statement.txt do i replace that where all the cin statements? i created a text file called...
1
by: AtoZ | last post by:
Hello, I'm looking for some experience translation between the software PDMS to Pro/ENGINEER ( Pro/PIPING ) and reverse. If some engineer know the solutions, their feedback will be very usefull...
4
by: samit | last post by:
I keen to persue coding like yahoo piping system which will include drag and drop and piping features
0
by: jsimps44 | last post by:
Hi, I'm fairly new to c, and very new to piping and file descriptors and can't seem to get past this problem. The piping is very much not working, and I can't figure out for the life of me why. Any...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.