Added support for a StreamReader (to support the input channel from stdin) to the C++CSP backend

This commit is contained in:
Neil Brown 2008-03-24 23:46:05 +00:00
parent e8adfb51fa
commit 986a4acb2b
2 changed files with 89 additions and 13 deletions

View File

@ -136,20 +136,25 @@ cppgenTopLevel s
call genStructured s (\m _ -> tell ["\n#error Invalid top-level item: ",show m])
(name, chans) <- tlpInterface
tell ["int main (int argc, char** argv) { csp::Start_CPPCSP();"]
(chanType,writer) <-
(chanType, writer, reader) <-
do st <- getCompState
case csFrontend st of
FrontendOccam -> return ("tockSendableArrayOfBytes","StreamWriterByteArray")
_ -> return ("uint8_t","StreamWriter")
FrontendOccam -> return ("tockSendableArrayOfBytes",
"StreamWriterByteArray",
"StreamReaderByteArray")
_ -> return ("uint8_t", "StreamWriter", "StreamReader")
tell ["csp::One2OneChannel<",chanType,"> in,out,err;"] --TODO add streamreader
tell [" csp::Run( csp::InParallel (new ",writer,"(std::cout,out.reader())) (new ",writer,"(std::cerr,err.reader())) (csp::InSequenceOneThread ( new proc_"]
tell ["csp::One2OneChannel<",chanType,"> in,out,err;"]
tell [" csp::Run( csp::InParallel ",
"(new ",writer,"(std::cout,out.reader())) ",
"(new ",writer,"(std::cerr,err.reader())) ",
"(new ",reader,"(std::cin,in.writer())) ",
"(csp::InSequenceOneThread ( new proc_"]
genName name
tell ["("]
infixComma $ map tlpChannel chans
tell [")) (new csp::common::ChannelPoisoner< csp::Chanout<"
,chanType,">/**/> (out.writer())) (new csp::common::ChannelPoisoner< csp::Chanout<"
,chanType,">/**/> (err.writer())) ) ); csp::End_CPPCSP(); return 0;}\n"]
tell [")) (new LethalProcess()) ) );",
"csp::End_CPPCSP(); return 0;}\n"]
where
tlpChannel :: (A.Direction,TLPChannel) -> CGen()
tlpChannel (dir,c) = case dir of

View File

@ -43,12 +43,10 @@ public:
#include <cppcsp/cppcsp.h>
#include <cppcsp/common/basic.h>
#include <iostream>
#include <vector>
#include <list>
#include <boost/type_traits/remove_const.hpp>
#include <boost/preprocessor/repetition/repeat.hpp>
#include <boost/preprocessor/repetition/repeat_from_to.hpp>
#include <boost/tuple/tuple.hpp>
#include <termios.h>
#include <unistd.h>
inline unsigned TimeDiffHelper(unsigned now,unsigned waitFor)
{
@ -109,6 +107,48 @@ public:
}
};
class StreamReader : public csp::CSProcess
{
private:
std::istream& in;
csp::Chanout<uint8_t> out;
protected:
virtual void run()
{
try
{
tock_configure_terminal(true);
char c;
while (true)
{
in.get(c);
out << (uint8_t)c;
}
}
catch (csp::PoisonException& e)
{
out.poison();
}
}
public:
inline StreamReader(std::istream& _in,const csp::Chanout<uint8_t>& _out)
: in(_in),out(_out)
{
}
};
//Exits the whole program when it is run
class LethalProcess : public csp::CSProcess
{
protected:
void run ()
{
//TODO should probably put this in an exit handler instead:
tock_restore_terminal();
exit(0);
}
};
class tockSendableArrayOfBytes
{
private:
@ -201,6 +241,37 @@ public:
}
};
class StreamReaderByteArray : public csp::CSProcess
{
private:
std::istream& in;
csp::Chanout<tockSendableArrayOfBytes> out;
protected:
virtual void run()
{
try
{
tock_configure_terminal(true);
char c;
while (true)
{
in.get(c);
tockSendableArrayOfBytes aob(&c);
out << aob;
}
}
catch (csp::PoisonException& e)
{
out.poison();
}
}
public:
inline StreamReaderByteArray(std::istream& _in,const csp::Chanout<tockSendableArrayOfBytes>& _out)
: in(_in),out(_out)
{
}
};
template <typename T>
class tockList
{