Tekkotsu Homepage
Demos
Overview
Downloads
Dev. Resources
Reference
Credits

RedirectionCommPort.h

Go to the documentation of this file.
00001 //-*-c++-*-
00002 #ifndef INCLUDED_RedirectionCommPort_h_
00003 #define INCLUDED_RedirectionCommPort_h_
00004 
00005 #include "local/CommPort.h"
00006 
00007 //! Allows you to recombine the input/output of other CommPorts in various ways
00008 /*! This will take input from one CommPort, and can send output to a different CommPort.
00009  *  It's not a 'pipe' from the input to the output, it just changes where the inputs and outputs
00010  *  are coming from. */
00011 class RedirectionCommPort : public CommPort, public virtual plist::PrimitiveListener {
00012 public:
00013   //! constructor
00014   explicit RedirectionCommPort(const std::string& name)
00015   : CommPort(autoRegisterRedirectionCommPort,name), input(), output(), curin(), curout(), openedCnt(0)
00016   {
00017     addEntry("Input",input,"Name of comm port from which to get input");
00018     addEntry("Output",output,"Name of comm port into which to send output");
00019   }
00020 
00021   //! destructor, checks that the file descriptor has already been closed
00022   virtual ~RedirectionCommPort() {
00023     if(openedCnt>0)
00024       std::cerr << "Still open in RedirectionCommPort destructor" << std::endl;
00025   }
00026 
00027   virtual std::string getClassName() const { return autoRegisterRedirectionCommPort; }
00028 
00029   virtual streambuf& getReadStreambuf() { CommPort * cp=getInputCP(); return (cp!=NULL) ? cp->getReadStreambuf() : invalid; }
00030   virtual streambuf& getWriteStreambuf() { CommPort * cp=getOutputCP(); return (cp!=NULL) ? cp->getWriteStreambuf() : invalid; }
00031   virtual bool isReadable() { CommPort * cp=getInputCP(); return (cp!=NULL) ? cp->isReadable() : false; }
00032   virtual bool isWriteable() { CommPort * cp=getOutputCP(); return (cp!=NULL) ? cp->isWriteable() : false; }
00033 
00034   virtual bool open();
00035   virtual bool close();
00036 
00037   //! watches #input and #output, triggers a close() and re-open() as needed
00038   virtual void plistValueChanged(const plist::PrimitiveBase& pl);
00039 
00040   plist::Primitive<std::string> input; //!< Name of comm port from which to get input
00041   plist::Primitive<std::string> output; //!< Name of comm port into which to send output
00042 
00043 protected:
00044   //! convenience function to lookup the current input comm port instance
00045   CommPort * getInputCP() { return (curin.size()==0) ? NULL : CommPort::getRegistry().getInstance(curin); }
00046   //! convenience function to lookup the current output comm port instance
00047   CommPort * getOutputCP() { return (curout.size()==0) ? NULL : CommPort::getRegistry().getInstance(curout); }
00048   
00049   std::string curin; //!< name of the current input comm port (stored separately from #input so if that changes, we can close the old one)
00050   std::string curout; //!< name of the current output comm port (stored separately from #output so if that changes, we can close the old one)
00051   unsigned int openedCnt; //!< open() call depth, tracks multiple usage
00052   
00053   //! std::streambuf uses a protected constructor, so we have to inherit to make an empty streambuf for #invalid
00054   class invalid_streambuf : public std::streambuf {};
00055   //! needed so we have something to return when the RedirectionCommPort doesn't have a valid external comm port to take a streambuf from
00056   static invalid_streambuf invalid;
00057   
00058   //! holds the class name, set via registration with the CommPort registry
00059   static const std::string autoRegisterRedirectionCommPort;
00060 };
00061 
00062 /*! @file
00063  * @brief 
00064  * @author Ethan Tira-Thompson (ejt) (Creator)
00065  */
00066 
00067 #endif

Tekkotsu Hardware Abstraction Layer 5.1CVS
Generated Mon May 9 05:01:39 2016 by Doxygen 1.6.3