dmlite  0.4
p/io.h
Go to the documentation of this file.
1 /// @file include/dmlite/cpp/io.h
2 /// @brief I/O API. Abstracts how to write or read to/from a disk within
3 /// a pool.
4 /// @author Alejandro Álvarez Ayllón <aalvarez@cern.ch>
5 #ifndef DMLITE_CPP_IO_H
6 #define DMLITE_CPP_IO_H
7 
8 #include <fcntl.h>
9 #include <map>
10 #include "base.h"
11 #include "exceptions.h"
12 #include "utils/extensible.h"
13 
14 namespace dmlite {
15 
16  // Forward declarations.
17  class PluginManager;
18  class StackInstance;
19 
20  /// IO interface
21  class IOHandler {
22  public:
23  enum Whence { kSet = SEEK_SET, ///< Beginning of the file
24  kCur = SEEK_CUR, ///< Current position
25  kEnd = SEEK_END ///< End of file
26  };
27 
28  /// Virtual destructor
29  virtual ~IOHandler();
30 
31  /// Close
32  virtual void close(void) throw (DmException) = 0;
33 
34  /// Read.
35  /// @param buffer Where to store the data.
36  /// @param count Number of bytes to read.
37  /// @return Number of bytes actually read.
38  virtual size_t read(char* buffer, size_t count) throw (DmException) = 0;
39 
40  /// Write.
41  /// @param buffer Data to write.
42  /// @param count Number of bytes to write.
43  /// @return Number of bytes actually written.
44  virtual size_t write(const char* buffer, size_t count) throw (DmException) = 0;
45 
46  /// Move the cursor.
47  /// @param offset The offset.
48  /// @param whence Reference.
49  virtual void seek(off_t offset, Whence whence) throw (DmException) = 0;
50 
51  /// Return the cursor position.
52  virtual off_t tell(void) throw (DmException) = 0;
53 
54  /// Flush the buffer.
55  virtual void flush(void) throw (DmException) = 0;
56 
57  /// Return true if end of file.
58  virtual bool eof(void) throw (DmException) = 0;
59  };
60 
61  /// IO Driver
62  class IODriver: public virtual BaseInterface {
63  public:
64  /// Virtual destructor
65  virtual ~IODriver();
66 
67  /// Instantiate a implementation of IOHandler
68  /// @param pfn The file name.
69  /// @param flags The open mode.
70  /// @param extras As was given by the PoolHandler.
71  virtual IOHandler* createIOHandler(const std::string& pfn,
72  int flags,
73  const Extensible& extras) throw (DmException) = 0;
74 
75  /// Must be called when the front-end is done writing.
76  /// @param pfn The file name.
77  /// @param params The extra parameters as was returned by whereToWrite
78  virtual void doneWriting(const std::string& pfn,
79  const Extensible& params) throw (DmException) = 0;
80  };
81 
82  /// Plug-ins must implement a concrete factory to be instantiated.
83  class IOFactory: public virtual BaseFactory {
84  public:
85  /// Virtual destructor
86  virtual ~IOFactory();
87 
88  protected:
89  friend class StackInstance;
90 
91  /// Create a IODriver
92  virtual IODriver* createIODriver(PluginManager* pm) throw (DmException) = 0;
93  };
94 
95 };
96 
97 #endif // DMLITE_CPP_IO_H