Added support for a StreamReader (to support the input channel from stdin) to the C++CSP backend
This commit is contained in:
parent
e8adfb51fa
commit
986a4acb2b
|
@ -136,20 +136,25 @@ cppgenTopLevel s
|
||||||
call genStructured s (\m _ -> tell ["\n#error Invalid top-level item: ",show m])
|
call genStructured s (\m _ -> tell ["\n#error Invalid top-level item: ",show m])
|
||||||
(name, chans) <- tlpInterface
|
(name, chans) <- tlpInterface
|
||||||
tell ["int main (int argc, char** argv) { csp::Start_CPPCSP();"]
|
tell ["int main (int argc, char** argv) { csp::Start_CPPCSP();"]
|
||||||
(chanType,writer) <-
|
(chanType, writer, reader) <-
|
||||||
do st <- getCompState
|
do st <- getCompState
|
||||||
case csFrontend st of
|
case csFrontend st of
|
||||||
FrontendOccam -> return ("tockSendableArrayOfBytes","StreamWriterByteArray")
|
FrontendOccam -> return ("tockSendableArrayOfBytes",
|
||||||
_ -> return ("uint8_t","StreamWriter")
|
"StreamWriterByteArray",
|
||||||
|
"StreamReaderByteArray")
|
||||||
|
_ -> return ("uint8_t", "StreamWriter", "StreamReader")
|
||||||
|
|
||||||
tell ["csp::One2OneChannel<",chanType,"> in,out,err;"] --TODO add streamreader
|
tell ["csp::One2OneChannel<",chanType,"> in,out,err;"]
|
||||||
tell [" csp::Run( csp::InParallel (new ",writer,"(std::cout,out.reader())) (new ",writer,"(std::cerr,err.reader())) (csp::InSequenceOneThread ( new proc_"]
|
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
|
genName name
|
||||||
tell ["("]
|
tell ["("]
|
||||||
infixComma $ map tlpChannel chans
|
infixComma $ map tlpChannel chans
|
||||||
tell [")) (new csp::common::ChannelPoisoner< csp::Chanout<"
|
tell [")) (new LethalProcess()) ) );",
|
||||||
,chanType,">/**/> (out.writer())) (new csp::common::ChannelPoisoner< csp::Chanout<"
|
"csp::End_CPPCSP(); return 0;}\n"]
|
||||||
,chanType,">/**/> (err.writer())) ) ); csp::End_CPPCSP(); return 0;}\n"]
|
|
||||||
where
|
where
|
||||||
tlpChannel :: (A.Direction,TLPChannel) -> CGen()
|
tlpChannel :: (A.Direction,TLPChannel) -> CGen()
|
||||||
tlpChannel (dir,c) = case dir of
|
tlpChannel (dir,c) = case dir of
|
||||||
|
|
|
@ -43,12 +43,10 @@ public:
|
||||||
#include <cppcsp/cppcsp.h>
|
#include <cppcsp/cppcsp.h>
|
||||||
#include <cppcsp/common/basic.h>
|
#include <cppcsp/common/basic.h>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <vector>
|
|
||||||
#include <list>
|
#include <list>
|
||||||
#include <boost/type_traits/remove_const.hpp>
|
|
||||||
#include <boost/preprocessor/repetition/repeat.hpp>
|
#include <termios.h>
|
||||||
#include <boost/preprocessor/repetition/repeat_from_to.hpp>
|
#include <unistd.h>
|
||||||
#include <boost/tuple/tuple.hpp>
|
|
||||||
|
|
||||||
inline unsigned TimeDiffHelper(unsigned now,unsigned waitFor)
|
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
|
class tockSendableArrayOfBytes
|
||||||
{
|
{
|
||||||
private:
|
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>
|
template <typename T>
|
||||||
class tockList
|
class tockList
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in New Issue
Block a user