CPN
Computational Process Networks
FileHandle.h
Go to the documentation of this file.
1 //=============================================================================
2 // Computational Process Networks class library
3 // Copyright (C) 1997-2006 Gregory E. Allen and The University of Texas
4 //
5 // This library is free software; you can redistribute it and/or modify it
6 // under the terms of the GNU Library General Public License as published
7 // by the Free Software Foundation; either version 2 of the License, or
8 // (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Library General Public License for more details.
14 //
15 // The GNU Public License is available in the file LICENSE, or you
16 // can write to the Free Software Foundation, Inc., 59 Temple Place -
17 // Suite 330, Boston, MA 02111-1307, USA, or you can find it on the
18 // World Wide Web at http://www.fsf.org.
19 //=============================================================================
24 #ifndef FILEHANDLE_H
25 #define FILEHANDLE_H
26 #pragma once
27 #include <cpn/common.h>
29 #include <cpn/utils/AutoLock.h>
30 #include <cpn/utils/IteratorRef.h>
31 // for the iovec struct
32 #include <sys/uio.h>
33 
41 class FileHandle {
42 public:
44 
49  static int Poll(IteratorRef<FileHandle*> begin, IteratorRef<FileHandle*> end, double timeout);
50 
54  FileHandle();
55 
61  FileHandle(int filed);
62 
66  virtual ~FileHandle();
67 
79  int Poll(double timeout);
80 
86  bool Readable(bool r) { ALock al(file_lock); return readable = r; }
91  bool Readable() const { ALock al(file_lock); return readable; }
97  bool Writeable(bool w) { ALock al(file_lock); return writeable = w; }
102  bool Writeable() const { ALock al(file_lock); return writeable; }
103 
106  int FD() const { ALock al(file_lock); return fd; }
111  int FD(int filed) { ALock al(file_lock); return fd = filed; }
112 
115  bool Eof() const { ALock al(file_lock); return eof; }
120  bool Eof(bool e) { ALock al(file_lock); return eof = e; }
125  bool Good() const { ALock al(file_lock); return !(eof || fd == -1); }
128  bool Closed() const { ALock al(file_lock); return fd == -1; }
129 
137  void SetBlocking(bool blocking);
144  static void SetBlocking(int fd, bool blocking);
149  bool IsBlocking() const;
155  static bool IsBlocking(int fd);
156 
161  void Reset();
162 
166  void Close();
167 
180  unsigned Read(void *ptr, unsigned len);
185  unsigned Readv(const iovec *iov, int iovcnt);
197  unsigned Write(const void *ptr, unsigned len);
201  unsigned Writev(const iovec *iov, int iovcnt);
202 
207  void Flush();
208 protected:
210  virtual void OnReadable() { Readable(true); }
212  virtual void OnWriteable() { Writeable(true); }
214  int fd;
215  bool readable;
216  bool writeable;
217  bool eof;
218 private:
219  FileHandle(const FileHandle&);
221 };
222 #endif
void Flush()
Tell the OS to flush any buffers it has. May not be supported for all file types. ...
Definition: FileHandle.cc:293
int FD(int filed)
Set the current file descriptor.
Definition: FileHandle.h:111
bool IsBlocking() const
Test if the current file is in blocking or non blocking mode.
Definition: FileHandle.cc:128
virtual void OnWriteable()
Called by Poll when it detects that the file is writeable.
Definition: FileHandle.h:212
bool writeable
Definition: FileHandle.h:216
bool Writeable(bool w)
Set that this file is currently writeable or not.
Definition: FileHandle.h:97
PthreadMutex file_lock
Definition: FileHandle.h:213
static int Poll(IteratorRef< FileHandle * > begin, IteratorRef< FileHandle * > end, double timeout)
poll a list of FileHandles for any activity and call the appropriate On method.
Definition: FileHandle.cc:34
bool Closed() const
Definition: FileHandle.h:128
void SetBlocking(bool blocking)
Manipulate how the current file handles blocking.
Definition: FileHandle.cc:111
void Close()
Close the file and reset the internal state.
Definition: FileHandle.cc:146
AutoLock< PthreadMutex > ALock
Definition: FileHandle.h:43
virtual ~FileHandle()
Close the file descriptor. Use Reset if one wants to not close the file descriptor.
Definition: FileHandle.cc:101
unsigned Writev(const iovec *iov, int iovcnt)
scatter gather io version of Write
Definition: FileHandle.cc:261
A reference to an iterator.
Definition: IteratorRef.h:46
bool Eof(bool e)
Set/reset the end of file condition.
Definition: FileHandle.h:120
FileHandle()
Construct a closed FileHandle.
Definition: FileHandle.cc:91
bool Writeable() const
Gives the current writability status of the file.
Definition: FileHandle.h:102
void Reset()
Clear all internal state including the file descriptor! WARNING does not close the file! ...
Definition: FileHandle.cc:138
int FD() const
Definition: FileHandle.h:106
bool Readable(bool r)
Set that the file is currently readable or not.
Definition: FileHandle.h:86
bool readable
Definition: FileHandle.h:215
bool Readable() const
Gives the current readability status of the file.
Definition: FileHandle.h:91
FileHandle & operator=(const FileHandle &)
This allows the encapsulation of an arbitrary iterator of a specific type. This allows for non templa...
bool Good() const
Convenience method for testing if the file this FileHandle has is open and not at end of file...
Definition: FileHandle.h:125
bool Eof() const
Definition: FileHandle.h:115
unsigned Readv(const iovec *iov, int iovcnt)
scatter gather io version of Read
Definition: FileHandle.cc:190
unsigned Read(void *ptr, unsigned len)
Read data from the file descriptor.
Definition: FileHandle.cc:159
Generic file handle could be a file, or a socket or a device.
Definition: FileHandle.h:41
virtual void OnReadable()
Called by Poll when it detects that the file is readable.
Definition: FileHandle.h:210
Automatic locking on the stack.
unsigned Write(const void *ptr, unsigned len)
Write data to the file descriptor.
Definition: FileHandle.cc:225