By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,681 Members | 1,854 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,681 IT Pros & Developers. It's quick & easy.

java style program objects in C++

P: n/a
I am wondering if any can point me to any open-source library with program
objects for C++ like there is in Java? I would like to be able to write
things like

MyProgram1 >> MyProgram2 >> Fork(MyProgram3, SomeFile);

If not would this be something of interest to others?

Thanks in advance,

--
Christopher Diggins
http://www.cdiggins.com
http://www.heron-language.com
Jul 22 '05 #1
Share this Question
Share on Google+
11 Replies


P: n/a
christopher diggins wrote:
I am wondering if any can point me to any open-source library with program
objects for C++ like there is in Java? I would like to be able to write
things like

MyProgram1 >> MyProgram2 >> Fork(MyProgram3, SomeFile);

The C++ language specification does not say about fork etc.

<OT>
Out of interest,

MyProgram1 >> MyProgram2 >> Fork(MyProgram3, SomeFile);

What is this supposed to mean ?
</OT>

If not would this be something of interest to others?


If you can ask this in a newsgroups specific to your OS,
you may get better answers.

--
Karthik.
Jul 22 '05 #2

P: n/a
Allow me to rephrase the question:

I want to write programs as objects. Given the source code from two
programs, I want to easily write a program, like I would a shell script. For
instance:

#include "program1.hpp"
#include "program2.hpp"

int main() {
program1() >> program2() >> filestream("c:\\temp.txt");
}

This would run the program1, store the output in a buffer. Then run program2
using the buffer as the standard in, and using the filestream as the
standard out. I want to do this without resorting to OS calls.

Thanks!

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

P: n/a

"christopher diggins" <cd******@videotron.ca> wrote in message
news:Po*******************@wagner.videotron.net...
Allow me to rephrase the question:

I want to write programs as objects. Given the source code from two
programs, I want to easily write a program, like I would a shell script. For
instance:

#include "program1.hpp"
#include "program2.hpp"

int main() {
program1() >> program2() >> filestream("c:\\temp.txt");
}

This would run the program1, store the output in a buffer. Then run program2
using the buffer as the standard in, and using the filestream as the
standard out. I want to do this without resorting to OS calls.


Hi Christopher,

Jan Christiaan van Winkel and John van Krieken outline something like this in
their paper "GNIRTS ESAC REWOL: Bringing UNIX filters to iostream" presented at
the ACCU spring conference in 2003. I can't find it on the web, but will send it
to you if you like.

They describe a filtering framework similar to the Boost Iostreams library. One
of the filters they define is a "UNIX filter" which pumps data through the
standard input and output of a child process given a command line. At some point
I'm planning to implement something like this for Boost.Iostreams, but right now
it's low on my list of priorities.

Use might look like this:

filtering_ostream out( system_filter("program1") |
system_filter("program2") |
file("C:/temp.txt") );
// Data written to out will be filtered through program1
// and program2 before being written to temp.txt.

Or, if program1 simply produces output:

filtering_ostream out( system_filter("program2") |
file("C:/temp.txt") );
boost::io::copy(program_source("program1"), out);

Jonathan
Jul 22 '05 #4

P: n/a

"Jonathan Turkanis" <te******@kangaroologic.com> wrote:
boost::io::copy(program_source("program1"), out);
This should probably be system_source("program1"). There would also be a
system_sink, for writing to the standard input of a child process.
Jonathan

Jul 22 '05 #5

P: n/a
"Jonathan Turkanis" <te******@kangaroologic.com> wrote in message
news:33*************@individual.net...

"christopher diggins" <cd******@videotron.ca> wrote in message
news:Po*******************@wagner.videotron.net...
Allow me to rephrase the question:

I want to write programs as objects. Given the source code from two
programs, I want to easily write a program, like I would a shell script.
For
instance:

#include "program1.hpp"
#include "program2.hpp"

int main() {
program1() >> program2() >> filestream("c:\\temp.txt");
}

This would run the program1, store the output in a buffer. Then run
program2
using the buffer as the standard in, and using the filestream as the
standard out. I want to do this without resorting to OS calls.


Hi Christopher,

Jan Christiaan van Winkel and John van Krieken outline something like this
in
their paper "GNIRTS ESAC REWOL: Bringing UNIX filters to iostream"
presented at
the ACCU spring conference in 2003. I can't find it on the web, but will
send it
to you if you like.

They describe a filtering framework similar to the Boost Iostreams
library. One
of the filters they define is a "UNIX filter" which pumps data through the
standard input and output of a child process given a command line. At some
point
I'm planning to implement something like this for Boost.Iostreams, but
right now
it's low on my list of priorities.


Hi Jonathan,

Thank you very much for suggesting this paper. I want to avoid using
processes due to their platform specificity. What I am looking for is a
library for building programs as objects in C++ (as done in Java), for
instance:

class Program {
virtual void Run() = 0;
// ...
}

class MyProgram1 : public Program {
void Run() {
// ...
}
...
}

class MyProgram2 : public Program {
void Run() {
// ...
}
...
}

class MyProgram3 : public Program {
void Run() {
MyProgram1() > MyProgram2();
}
}

int main() {
MyProgram3 p;
p.Run();
return 0;
}

Am I making sense? I am almost done a library which does exactly that, and I
want to make sure I am not reinventing the wheel.

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

P: n/a
I am writing a library which allows programs to be written as objects. This
is intended to make it easier to reuse programs to build new programs, in
part by redirecting their standard input and output. This is done without
resorting to OS specific calls, but rather through object oriented
programming techniques and operator overloading. What I am doing is
overloading the ">" operator so that:

1. Program > Program : The first program is run, with the stdout stored in
memory. When finished the stored output is fed to the stdin of the second
program
2. Program > char const* : The char const* is treated as a file name, and a
filestream is created, and the output
3. char const* > Program : just like #2, but the filestream is fed as
standard input to the program
4. std::string > Program : the string is fed as standard input to Program
5. Program > std::string : the output of program is stored in the string
variable

There are other combinations as well, and expressions can be built in
sequence such as:

Program > std::string > char const*;

This runs the program, redirects output to the string variable, and copies
it to a file as well.

The approach I am using requires that a programmer replace std::cin,
std::cout, and std::cerr with ootl::cin, ootl::cout, and ootl::cerr.

I have included an example of the technique in action below (which doesn't
compile because I haven't finished "programs.hpp" yet).

What I would like to know is
a) are there other libraries which already do this in a platform independant
manner
b) is this approach of interest to anyone
c) any suggestions or ideas on how to make it more powerful and interesting?
d) what should this technique be called?

//========================================

#include "programs.hpp"
#include <iostream>

using namespace ootl;

struct HelloWorldProgram : public Program {
HelloWorldProgram(int argc = 0, char** argv = NULL)
: Program(argc, argv) { }
virtual int Run() {
ootl::cin << "Hello world!\n" << std::endl;
return 0;
}
};

struct EchoProgram : public Program {
EchoProgram(int argc = 0, char** argv = NULL)
: Program(argc, argv) { }
virtual int Run() {
while (!ootl::cin.eof()) {
ootl::cout << ootl::cin.get();
}
return 0;
}
};

struct Test1 : public Program {
Test1(int argc = 0, char** argv = NULL)
: Program(argc, argv) { }
virtual int Run() {
HelloWorldProgram() > "c:\\tmp.txt" > EchoProgram();
std::string s;
"c:\\tmp.txt" > s;
s > EchoProgram();
return 0;
}
};

int main(int argc, char** argv) {
Test1 myProgram(argc, argv);
return myProgram.Run();
}

//==================================================

Thanks in advance!
Christopher Diggins
http://www.cdiggins.com
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Jul 22 '05 #7

P: n/a

"christopher diggins" <cd******@videotron.ca> wrote in message
news:LT********************@wagner.videotron.net.. .
I am writing a library which allows programs to be written as objects. This
is intended to make it easier to reuse programs to build new programs, in
part by redirecting their standard input and output. This is done without
resorting to OS specific calls, but rather through object oriented
programming techniques and operator overloading. What I am doing is
overloading the ">" operator so that:

1. Program > Program : The first program is run, with the stdout stored in
memory. When finished the stored output is fed to the stdin of the second
program
2. Program > char const* : The char const* is treated as a file name, and
a
filestream is created, and the output
3. char const* > Program : just like #2, but the filestream is fed as
standard input to the program
4. std::string > Program : the string is fed as standard input to Program
5. Program > std::string : the output of program is stored in the string
variable

There are other combinations as well, and expressions can be built in
sequence such as:

Program > std::string > char const*;

This runs the program, redirects output to the string variable, and copies
it to a file as well.

The approach I am using requires that a programmer replace std::cin,
std::cout, and std::cerr with ootl::cin, ootl::cout, and ootl::cerr.

I have included an example of the technique in action below (which doesn't
compile because I haven't finished "programs.hpp" yet).

What I would like to know is
a) are there other libraries which already do this in a platform
independant
manner
b) is this approach of interest to anyone
c) any suggestions or ideas on how to make it more powerful and
interesting?
d) what should this technique be called?

//========================================

#include "programs.hpp"
#include <iostream>

using namespace ootl;

struct HelloWorldProgram : public Program {
HelloWorldProgram(int argc = 0, char** argv = NULL)
: Program(argc, argv) { }
virtual int Run() {
ootl::cin << "Hello world!\n" << std::endl;
return 0;
}
};

struct EchoProgram : public Program {
EchoProgram(int argc = 0, char** argv = NULL)
: Program(argc, argv) { }
virtual int Run() {
while (!ootl::cin.eof()) {
ootl::cout << ootl::cin.get();
}
return 0;
}
};

struct Test1 : public Program {
Test1(int argc = 0, char** argv = NULL)
: Program(argc, argv) { }
virtual int Run() {
HelloWorldProgram() > "c:\\tmp.txt" > EchoProgram();
std::string s;
"c:\\tmp.txt" > s;
s > EchoProgram();
return 0;
}
};

int main(int argc, char** argv) {
Test1 myProgram(argc, argv);
return myProgram.Run();
}


I'm confused as to how those objects are in any way "programs"...? Except
for the use of the > operator in the Test1 struct, they look like normal
source code to me. A program is generally considered to be an executable,
the result of compiling some source code, isn't it?

Also, this concept limits your idea of what a "program" is to processes
which operate in a fashion like 1) Take Input, 2) Process Data, 3) Report
Results. What about programs that interact with the user? And what about
programs whose "output" might exceed your buffer size? And what if I have a
program that needs the output of multiple other programs as its input,
instead of just the preceding one in the sequence?

I guess I just don't see what practical use this kind of design would be.
If I want to "build" on the abilities of one program when creating a new
one, it's the classes and their implementations that I usually want, and in
that case I just include the source code created for the old program(s) in
my new one. And if I want to pipe the output from one app to another, I do
so on the command line or a batch/script file (assuming support in the given
OS, of course).

Just my 2 cents, of course.

-Howard



Jul 22 '05 #8

P: n/a
> I'm confused as to how those objects are in any way "programs"...? Except
for the use of the > operator in the Test1 struct, they look like normal
source code to me. A program is generally considered to be an executable,
the result of compiling some source code, isn't it?
I was trying to show a way of writing a program so that it can be easily
included as is, in another program, and redirected.
Also, this concept limits your idea of what a "program" is to processes
which operate in a fashion like 1) Take Input, 2) Process Data, 3) Report
Results. What about programs that interact with the user? And what about
programs whose "output" might exceed your buffer size? And what if I have
a program that needs the output of multiple other programs as its input,
instead of just the preceding one in the sequence?
This is precisely the same problems that one has to deal with when piping
data from one program to another on the command line. Perhaps I should
rename the "program" to "filter_program" in reference to unix filters.
I guess I just don't see what practical use this kind of design would be.
If I want to "build" on the abilities of one program when creating a new
one, it's the classes and their implementations that I usually want, and
in that case I just include the source code created for the old program(s)
in my new one.
And if I want to pipe the output from one app to another, I do so on the
command line or a batch/script file (assuming support in the given OS, of
course).


The goal of my work is to provide command line style interface to source
code in C++. The problem with shell scripts is that every OS has a different
shell language (assuming you have an OS with a shell language), and
sometimes it is nice to execute shell-like commands from within the code.

What motivated this work is: I prefer to write lots of small programs, which
call each other. Always doing so from the OS is not portable at all, and is
not very useful.

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

P: n/a
On 2 Jan 2005 04:28:49 -0500, christopher diggins <cd******@videotron.ca> wrote:
....
1. Program > Program : The first program is run, with the stdout stored in
memory. When finished the stored output is fed to the stdin of the second
program .... What I would like to know is .... c) any suggestions or ideas on how to make it more powerful and interesting?


Well, I note that Unix shell pipes are infinitely more useful than those in
MS-DOS. Think of the tee(1) utility, for example -- noone would use it if it
delayed screen output until its provider was done.

Your 'p > p' seems to be of the MS-DOS kind, so that's an improvement
suggestion.

/Jorgen

--
// Jorgen Grahn <jgrahn@ Ph'nglui mglw'nafh Cthulhu
\X/ algonet.se> R'lyeh wgah'nagl fhtagn!

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Jul 22 '05 #10

P: n/a
For what it's worth, I have completed the first version of the library for
reusing C++ program like unix filters:

fstream f("c:\\tmp.txt");
stringstream s;
HelloWorldProgram() > f > UpperCaseProgram() > s;

For those interested, the source and a detailed explanation are available at
http://www.codeproject.com/useritems...am-objects.asp

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

P: n/a
you want Python :-)

which is obviously off topic here :-)

Jul 22 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.