root/trunk/whisperlib/common/io/file/fd.h

Revision 7, 3.5 kB (checked in by whispercastorg, 2 years ago)

version 0.2.0

Line 
1 // Copyright (c) 2009, Whispersoft s.r.l.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
13 // distribution.
14 // * Neither the name of Whispersoft s.r.l. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 //// Copyright 2009 WhisperSoft s.r.l.
30 // Author: Cosmin Tudorache
31
32 #ifndef __COMMON_IO_FILE_FD_H__
33 #define __COMMON_IO_FILE_FD_H__
34
35 #include <whisperlib/common/base/types.h>
36
37 namespace io {
38
39 class FileDescriptor {
40  public:
41   FileDescriptor();
42   virtual ~FileDescriptor();
43
44   int fd() const {
45     return fd_;
46   }
47   bool is_open() const {
48     return fd_ != INVALID_FD_VALUE;
49   }
50   bool is_eof() const {
51     return is_eof_;
52   }
53   bool is_live() const {
54     return is_live_;
55   }
56
57   //  Use the given fd. This function fails if another fd is already opened.
58   //  fd: is the file descriptor to use for our operations.
59   //  live: if true specifies the fd stream never ends. The data in stream
60   //        may exhaust temporarily, but new data arrives continuously.
61   // duplicate: if true specifies the "fd" is duplicated first. The new fd
62   //            is stored locally and closed on destructor or on Close() call.
63   //            If the fd is not duplicated, it won't be closed on destructor
64   //            nor on Close() call.
65   virtual bool OpenFD(int fd, bool live = false, bool duplicate = false);
66
67
68   // Closes the underline fd.
69   void Close();
70
71   // Reads len data bytes from the file to given buffer.
72   // Returns # of bytes read / negative on error.
73   virtual int32 Read(void* buf, int32 len);
74
75   // Skips len data bytes in the file.
76   // Returns # of skiped bytes / negative on error.
77   virtual int32 Skip(int32 len);
78
79   // Writes len data bytes from buffer to the file.
80   // Returns # of written bytes / negative on error.
81   virtual int32 Write(const void* buf, int32 len);
82
83  protected:
84   int fd_;                 // file descriptor
85   bool is_live_;           // true = stream never ends (i.e. IsEos() returns
86                            // always false)
87   bool is_eof_;            // true = end of file was reached
88   bool close_on_exit_;     // true = the fd is owned and will be closed
89                            //        on destructor or on Close() call;
90
91   DISALLOW_EVIL_CONSTRUCTORS(FileDescriptor);
92 };
93 }
94 #endif  // __COMMON_IO_FILE_FD_H__
Note: See TracBrowser for help on using the browser.