From 986a4acb2b449adf2324757ae0de2287f974b035 Mon Sep 17 00:00:00 2001 From: Neil Brown Date: Mon, 24 Mar 2008 23:46:05 +0000 Subject: [PATCH] Added support for a StreamReader (to support the input channel from stdin) to the C++CSP backend --- backends/GenerateCPPCSP.hs | 21 +++++---- support/tock_support_cppcsp.h | 81 ++++++++++++++++++++++++++++++++--- 2 files changed, 89 insertions(+), 13 deletions(-) diff --git a/backends/GenerateCPPCSP.hs b/backends/GenerateCPPCSP.hs index 678fc62..5d5b8b7 100644 --- a/backends/GenerateCPPCSP.hs +++ b/backends/GenerateCPPCSP.hs @@ -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 diff --git a/support/tock_support_cppcsp.h b/support/tock_support_cppcsp.h index a1ebd34..361d786 100644 --- a/support/tock_support_cppcsp.h +++ b/support/tock_support_cppcsp.h @@ -43,12 +43,10 @@ public: #include #include #include -#include #include -#include -#include -#include -#include + +#include +#include inline unsigned TimeDiffHelper(unsigned now,unsigned waitFor) { @@ -109,6 +107,48 @@ public: } }; +class StreamReader : public csp::CSProcess +{ +private: + std::istream& in; + csp::Chanout 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& _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 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& _out) + : in(_in),out(_out) + { + } +}; + template class tockList {