nlib
FileOutputStream.h
Go to the documentation of this file.
1 
2 #pragma once
3 #ifndef INCLUDE_NN_NLIB_FILEOUTPUTSTREAM_H_
4 #define INCLUDE_NN_NLIB_FILEOUTPUTSTREAM_H_
5 
6 #include "nn/nlib/OutputStream.h"
7 
8 NLIB_NAMESPACE_BEGIN
9 
11  void* buf;
12  size_t buffer_size;
13 
14  public:
15  NLIB_CEXPR FileOutputStreamSettings() NLIB_NOEXCEPT : buf(NULL), buffer_size(4096) {}
16  NLIB_CEXPR FileOutputStreamSettings(void* buf_, size_t buffer_size_) NLIB_NOEXCEPT
17  : buf(buf_),
18  buffer_size(buffer_size_) {}
19 };
20 
22  public:
24  buf_(NULL),
25  buf_size_(0),
26  is_internal_buffer_(false) {}
30  return this->Init(settings);
31  }
33  NLIB_CHECK_RESULT errno_t Open(const char* filename,
34  int flags, int mode) NLIB_NOEXCEPT NLIB_NONNULL;
35  NLIB_CHECK_RESULT errno_t Open(const char* filename) NLIB_NOEXCEPT {
36  return Open(filename, NLIB_FD_O_WRONLY | NLIB_FD_O_CREAT | NLIB_FD_O_TRUNC, 0644);
37  }
38  NLIB_CHECK_RESULT errno_t Open(const char* filename, int flags) NLIB_NOEXCEPT {
39  return Open(filename, flags, 0644);
40  }
41  NLIB_CHECK_RESULT errno_t Open(const wchar_t* filename,
42  int flags, int mode) NLIB_NOEXCEPT NLIB_NONNULL;
43  NLIB_CHECK_RESULT errno_t Open(const wchar_t* filename, int flags) NLIB_NOEXCEPT {
44  return Open(filename, flags, 0644);
45  }
46  NLIB_CHECK_RESULT errno_t Open(const wchar_t* filename) NLIB_NOEXCEPT {
47  return Open(filename, NLIB_FD_O_WRONLY | NLIB_FD_O_CREAT | NLIB_FD_O_TRUNC, 0644);
48  }
50  nlib_fd GetFd() const NLIB_NOEXCEPT { return fd_; }
51 
52  private:
53  virtual bool PushBuffer_(const void* p, size_t nbytes,
54  bool do_flush) NLIB_NOEXCEPT NLIB_OVERRIDE;
55  virtual bool Close_() NLIB_NOEXCEPT NLIB_OVERRIDE;
56  virtual bool WriteGather_(const nlib_fd_iovec* iov, int iovcnt) NLIB_NOEXCEPT NLIB_OVERRIDE;
57 
58  private:
59  nlib_fd fd_;
60  unsigned char* buf_;
61  size_t buf_size_;
62  bool is_internal_buffer_;
64 };
65 
66 NLIB_NAMESPACE_END
67 
68 #endif // INCLUDE_NN_NLIB_FILEOUTPUTSTREAM_H_
constexpr FileOutputStreamSettings() noexcept
Instantiates the object with default parameters (default constructor). Sets the default values...
#define NLIB_OVERRIDE
Defines override if it is available for use. If not, holds an empty string.
Definition: Config.h:210
The structure that stores information about the file stream settings.
#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
The class for file output streams.
#define NLIB_CHECK_RESULT
Indicates that the caller of the function must check the returned value.
Definition: Platform_unix.h:74
void * buf
A pointer to a buffer.
nlib_fd GetFd() const noexcept
Returns the file descriptor.
#define NLIB_FD_INVALID
A macro defining invalid file descriptors.
Definition: Platform.h:1729
#define NLIB_VIS_PUBLIC
Symbols for functions and classes are made available outside of the library.
Definition: Platform_unix.h:61
size_t buffer_size
The size of the buffer.
errno_t Init() noexcept
Initializes the stream with the default settings.
#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
#define NLIB_CEXPR
Defines constexpr if it is available for use. If not, holds an empty string.
Definition: Config.h:80
FileOutputStream() noexcept
Instantiates the object with default parameters (default constructor). The buffer must be set and ini...
constexpr FileOutputStreamSettings(void *buf_, size_t buffer_size_) noexcept
Sets the individual data members.
#define NLIB_FINAL
Defines final if it is available for use. If not, holds an empty string.
Definition: Config.h:211
Defines the base class for output streams.
#define NLIB_NONNULL
Indicates that you cannot specify NULL for all arguments.
Definition: Platform_unix.h:76
The base class for output streams. This class cannot be instantiated.
Definition: OutputStream.h:17
int errno_t
Indicates with an int-type typedef that a POSIX error value is returned as the return value...
Definition: NMalloc.h:24