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