nlib
AsyncFileIo.h
Go to the documentation of this file.
1 
2 #pragma once
3 #ifndef INCLUDE_NN_NLIB_THREADING_ASYNCFILEIO_H_
4 #define INCLUDE_NN_NLIB_THREADING_ASYNCFILEIO_H_
5 
6 #include "nn/nlib/Config.h"
7 
8 NLIB_NAMESPACE_BEGIN
9 namespace threading {
10 
11 class AsyncFileIoService;
12 template <class R>
13 class Future;
14 
16  public:
17  enum Mode {
18  ASYNCFILEIO_READ = NLIB_FD_O_RDONLY,
19  ASYNCFILEIO_WRITE = NLIB_FD_O_WRONLY | NLIB_FD_O_CREAT | NLIB_FD_O_TRUNC
20  };
21  static bool HasNativeAsyncFileIo() NLIB_NOEXCEPT;
24  errno_t Init() NLIB_NOEXCEPT;
25  NLIB_CHECK_RESULT errno_t Open(const char* path,
26  int flags, int mode) NLIB_NOEXCEPT;
27  NLIB_CHECK_RESULT errno_t Open(const char* path, int flags) NLIB_NOEXCEPT {
28  return Open(path, flags, 0644);
29  }
31  NLIB_ALWAYS_INLINE nlib_fd GetFd() const NLIB_NOEXCEPT { return fd_; }
32  errno_t Close() NLIB_NOEXCEPT;
33  errno_t Read(Future<size_t>* future, void* buf, size_t nbytes, nlib_offset ofs,
35  errno_t Read(size_t* read_bytes, void* buf, size_t nbytes, nlib_offset ofs,
37  errno_t Write(Future<size_t>* future, const void* buf, size_t nbytes, nlib_offset ofs,
39  errno_t Write(size_t* write_bytes, const void* buf, size_t nbytes, nlib_offset ofs,
41  errno_t Cancel(AsyncFileIoService* ioservice) NLIB_NOEXCEPT;
42 
43  private:
44  nlib_fd fd_;
46 };
47 
48 struct AsyncFileIoServiceData;
50  public:
51  AsyncFileIoService() NLIB_NOEXCEPT : data_(NULL) {}
53  errno_t Init() NLIB_NOEXCEPT;
54 
55  private:
56  AsyncFileIoServiceData* data_;
58  friend class AsyncFileIo;
59 };
60 
61 } // namespace threading
62 NLIB_NAMESPACE_END
63 
64 #endif // INCLUDE_NN_NLIB_THREADING_ASYNCFILEIO_H_
#define NLIB_ALWAYS_INLINE
Indicates that the compiler is forced to perform inline expansion of functions.
Definition: Platform_unix.h:69
#define NLIB_DISALLOW_COPY_AND_ASSIGN(TypeName)
Prohibits use of the copy constructor and assignment operator for the class specified by TypeName...
Definition: Config.h:145
#define NLIB_FD_O_CREAT
Used for the flags parameter of the nlib_fd_open function.
Definition: Platform.h:1693
#define NLIB_CHECK_RESULT
Indicates that the caller of the function must check the returned value.
Definition: Platform_unix.h:74
Class that controls asynchronous file I/O.
Definition: AsyncFileIo.h:49
nlib_fd GetFd() const noexcept
Gets nlib_fd.
Definition: AsyncFileIo.h:31
#define NLIB_VIS_PUBLIC
Symbols for functions and classes are made available outside of the library.
Definition: Platform_unix.h:61
bool Write(BinaryWriter *w, T x)
You can write user-defined class objects by specializing this function template.
Definition: BinaryWriter.h:116
#define NLIB_FD_O_WRONLY
Used for the flags parameter of the nlib_fd_open function.
Definition: Platform.h:1672
int nlib_fd
The original file descriptor of nlib (a 32-bit integer value).
Definition: Platform.h:1728
#define NLIB_FD_O_TRUNC
Used for the flags parameter of the nlib_fd_open function.
Definition: Platform.h:1700
#define NLIB_NOEXCEPT
Defines noexcept geared to the environment, or the equivalent.
Definition: Config.h:86
A file that contains the configuration information for each development environment.
Class that gets the output of a different thread executing in a thread safe manner. This class is similar to the std::shared_future class of C++11.
Definition: AsyncFileIo.h:13
#define NLIB_FINAL
Defines final if it is available for use. If not, holds an empty string.
Definition: Config.h:211
bool Read(BinaryReader *r, T *x)
You can read to user-defined class objects by specializing this function template.
Definition: BinaryReader.h:152
int64_t nlib_offset
The offset to the file. A 64-bit integer.
Definition: Platform.h:1727
Class that wraps asynchronous file I/O.
Definition: AsyncFileIo.h:15
AsyncFileIoService() noexcept
Instantiates the object.
Definition: AsyncFileIo.h:51
#define NLIB_FD_O_RDONLY
Used for the flags parameter of the nlib_fd_open function.
Definition: Platform.h:1665
int errno_t
Indicates with an int-type typedef that a POSIX error value is returned as the return value...
Definition: NMalloc.h:24