nlib
TextWriter.h
Go to the documentation of this file.
1 
2 #pragma once
3 #ifndef INCLUDE_NN_NLIB_TEXTWRITER_H_
4 #define INCLUDE_NN_NLIB_TEXTWRITER_H_
5 
6 #include "nn/nlib/Config.h"
7 #include "nn/nlib/Swap.h"
8 
9 NLIB_NAMESPACE_BEGIN
10 
11 class OutputStream;
12 
13 // code snippets:
14 // TextWriter writer;
15 // if (writer.Init() != 0 || writer.Open(&stream) != 0) { error; }
16 // # note that wide characters(UTF16/UTF32) are converted into UTF8
17 // if (!writer.Write(....)) { error; }
18 // if (!writer.Flush()) { error; }
19 // if (!writer.Close()) { error; }
21  public:
24  NLIB_MOVE_MEMBER_HELPER_2(TextWriter, stream_, errno_); // NOLINT
25  errno_t Init() NLIB_NOEXCEPT;
26  errno_t Open(OutputStream* stream) NLIB_NOEXCEPT;
27  bool Write(wchar_t c) NLIB_NOEXCEPT;
28  bool Write(const wchar_t* str) NLIB_NOEXCEPT NLIB_NONNULL;
29  bool Write(char c) NLIB_NOEXCEPT;
30  bool Write(const char* str) NLIB_NOEXCEPT NLIB_NONNULL;
32  bool Write(const nlib_utf16_t* str) NLIB_NOEXCEPT NLIB_NONNULL;
34  bool Write(const nlib_utf32_t* str) NLIB_NOEXCEPT NLIB_NONNULL;
35  bool Flush() NLIB_NOEXCEPT;
36  bool Close() NLIB_NOEXCEPT;
37  errno_t GetErrorValue() const NLIB_NOEXCEPT { return errno_; }
38  OutputStream* GetStream() NLIB_NOEXCEPT { return stream_; }
39  bool WriteFormat(_Printf_format_string_ const wchar_t* fmt, ...) NLIB_NOEXCEPT;
40  bool WriteFormat(_Printf_format_string_ const char* fmt, ...) NLIB_NOEXCEPT;
41  void SetError(errno_t e) const NLIB_NOEXCEPT {
42  if (errno_ == 0) errno_ = e;
43  }
44 
45  void swap(TextWriter& rhs) NLIB_NOEXCEPT {
46  using std::swap;
47  swap(stream_, rhs.stream_);
48  swap(errno_, rhs.errno_);
49  swap(cr_, rhs.cr_);
50  }
51  NLIB_SAFE_BOOL(TextWriter, GetErrorValue() == 0)
52 
53  private:
54  NLIB_VIS_HIDDEN bool Write_(const char* str, size_t len) NLIB_NOEXCEPT;
55 
56  private:
57  OutputStream* stream_;
58  mutable ErrnoT errno_;
59  unsigned char cr_;
60  unsigned char utf8idx_;
61  unsigned char utf16idx_;
62  union {
63  char utf8[5];
64  nlib_utf16_t utf16[3];
65  } buf_;
66 
68 };
69 
70 NLIB_NAMESPACE_END
71 
72 NLIB_DEFINE_STD_SWAP(::nlib_ns::TextWriter)
73 
74 #endif // INCLUDE_NN_NLIB_TEXTWRITER_H_
errno_t GetErrorValue() const noexcept
This function can get the cause of the error when writing has failed.
Definition: TextWriter.h:37
OutputStream * GetStream() noexcept
Gets the stream for the text writer to write to.
Definition: TextWriter.h:38
#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_SAFE_BOOL(class_name, exp)
Defines a safe operator bool function in the class. Uses the C++11 explicit bool if it is available f...
Definition: Config.h:160
#define NLIB_VIS_HIDDEN
Symbols for functions and classes are not made available outside of the library.
Definition: Platform_unix.h:60
#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
uint32_t nlib_utf32_t
Uses typedef to define as char32_t if that can be used. If not, it uses typedef to define as uint32_t...
Definition: Config.h:539
uint16_t nlib_utf16_t
Uses typedef to define as char16_t if that can be used. If not, it uses typedef to define as uint16_t...
Definition: Config.h:538
Class that wraps errno_t. This class improves visual representations in the Visual Studio debugger...
Definition: Config.h:474
#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.
void swap(TextWriter &rhs) noexcept
Swaps the contents of an object.
Definition: TextWriter.h:45
~TextWriter() noexcept
Destructor. The stream is not closed.
Definition: TextWriter.h:23
The class for writing text to streams.
Definition: TextWriter.h:20
#define NLIB_FINAL
Defines final if it is available for use. If not, holds an empty string.
Definition: Config.h:211
void SetError(errno_t e) const noexcept
Sets an error value.
Definition: TextWriter.h:41
#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