Tekkotsu Homepage
Demos
Overview
Downloads
Dev. Resources
Reference
Credits

FileSystemCommPort.h

Go to the documentation of this file.
00001 //-*-c++-*-
00002 #ifndef INCLUDED_FileSystemCommPort_h_
00003 #define INCLUDED_FileSystemCommPort_h_
00004 
00005 #include "local/CommPort.h"
00006 #include "Shared/plist.h"
00007 #include <fstream>
00008 #include <ios>
00009 
00010 //! Provides CommPort interface to file system devices, such as serial ports
00011 /*! Pass a path to use a file system device, or see NetworkCommPort for a network interface */
00012 class FileSystemCommPort : public CommPort, public virtual plist::PrimitiveListener {
00013 public:
00014   //! constructor (see also sub-class constructor in protected section)
00015   explicit FileSystemCommPort(const std::string& name)
00016     : CommPort(autoRegisterFileSystemCommPort,name),
00017     path(), mode(std::ios_base::in | std::ios_base::out | std::ios_base::trunc | std::ios_base::binary),
00018     rbuf(), wbuf(), curloc(), curmode(), opened(0)
00019   {
00020     addEntry("Path",path,"Path of file system object being accessed");
00021     addEntry("Mode",mode,"Mode bitmask to pass to the open() call, defaults to 'w+b': in|out|trunc|binary (see std::ios_base::openmode)");
00022   }
00023   
00024   //! destructor, checks that the file descriptor has already been closed
00025   virtual ~FileSystemCommPort() {
00026     if(opened>0)
00027       connectionError("File descriptor still open in FileSystemCommPort destructor",true);
00028   }
00029   
00030   virtual std::string getClassName() const { return autoRegisterFileSystemCommPort; }
00031   
00032   virtual std::basic_filebuf<std::ios::char_type>& getReadStreambuf() { return rbuf; }
00033   virtual std::basic_filebuf<std::ios::char_type>& getWriteStreambuf() { return wbuf; }
00034   virtual bool isReadable() { return rbuf.is_open(); }
00035   virtual bool isWriteable() { return wbuf.is_open(); }
00036   
00037   //! tries to have #rbuf and/or #wbuf open #path, subject to #mode 
00038   virtual bool open();
00039   
00040   //! closes #rbuf and #wbuf
00041   virtual bool close();
00042   
00043   //! watches #path, triggers a close() and re-open() if it changes
00044   virtual void plistValueChanged(const plist::PrimitiveBase& pl);
00045   
00046   plist::Primitive<std::string> path; //!< path of file system object being accessed
00047   plist::Primitive<int> mode; //!< mode bitmask to pass to the open() call (see std::ios_base::openmode)
00048   
00049 protected:
00050   explicit FileSystemCommPort(const std::string& classname, const std::string& instancename)
00051     : CommPort(classname,instancename), 
00052     path(), mode(std::ios_base::in | std::ios_base::out | std::ios_base::trunc | std::ios_base::binary),
00053     rbuf(), wbuf(), curloc(), curmode(), opened(0)
00054   {
00055     addEntry("Path",path,"Path of file system object being accessed");
00056     addEntry("Mode",mode,"Mode bitmask to pass to the open() call, defaults to 'w+b': in|out|trunc|binary (see std::ios_base::openmode)");
00057   }
00058   
00059   //! Displays message on stderr and if @a fatal is set, calls closeFD()
00060   virtual void connectionError(const std::string& msg, bool fatal) {
00061     std::cerr << msg << std::endl;
00062     if(fatal && (rbuf.is_open() || wbuf.is_open())) {
00063       opened=1;
00064       close();
00065     }
00066   }
00067   
00068   std::basic_filebuf<std::ios::char_type> rbuf;
00069   std::basic_filebuf<std::ios::char_type> wbuf;
00070   std::string curloc;
00071   std::ios_base::openmode curmode;
00072   unsigned int opened;
00073   
00074   //! holds the class name, set via registration with the CommPort registry
00075   static const std::string autoRegisterFileSystemCommPort;
00076 };
00077 
00078 /*! @file
00079  * @brief Describes FileDescriptorMotionHook, which provides a file descriptor interface for hardware "drivers"
00080  * @author Ethan Tira-Thompson (ejt) (Creator)
00081  *
00082  * $Author: ejt $
00083  * $Name: tekkotsu-4_0 $
00084  * $Revision: 1.2 $
00085  * $State: Exp $
00086  * $Date: 2007/11/09 19:01:17 $
00087  */
00088 
00089 #endif

Tekkotsu Hardware Abstraction Layer 4.0
Generated Thu Nov 22 01:00:53 2007 by Doxygen 1.5.4