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(&stream)) { 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, m_Stream, m_ErrorValue); // NOLINT
25  bool Init(OutputStream* stream) NLIB_NOEXCEPT NLIB_NONNULL;
26  bool Write(wchar_t c) NLIB_NOEXCEPT;
27  bool Write(const wchar_t* str) NLIB_NOEXCEPT NLIB_NONNULL;
28  bool Write(char c) NLIB_NOEXCEPT;
29  bool Write(const char* str) NLIB_NOEXCEPT NLIB_NONNULL;
31  bool Write(const nlib_utf16_t* str) NLIB_NOEXCEPT NLIB_NONNULL;
33  bool Write(const nlib_utf32_t* str) NLIB_NOEXCEPT NLIB_NONNULL;
34  bool Flush() NLIB_NOEXCEPT;
35  bool Close() NLIB_NOEXCEPT;
36  errno_t GetErrorValue() const NLIB_NOEXCEPT { return m_ErrorValue; }
37  OutputStream* GetStream() NLIB_NOEXCEPT { return m_Stream; }
38  bool WriteFormat(_Printf_format_string_ const wchar_t* fmt, ...) NLIB_NOEXCEPT;
39  bool WriteFormat(_Printf_format_string_ const char* fmt, ...) NLIB_NOEXCEPT;
40  void SetError(errno_t e) const NLIB_NOEXCEPT {
41  if (m_ErrorValue == 0) m_ErrorValue = e;
42  }
43 
44  void swap(TextWriter& rhs) NLIB_NOEXCEPT {
45  using std::swap;
46  swap(m_Stream, rhs.m_Stream);
47  swap(m_ErrorValue, rhs.m_ErrorValue);
48  swap(m_Cr, rhs.m_Cr);
49  }
50  NLIB_SAFE_BOOL(TextWriter, GetErrorValue() == 0)
51 
52  private:
53  NLIB_VIS_HIDDEN bool Write_(const char* str, size_t len) NLIB_NOEXCEPT;
54 
55  private:
56  OutputStream* m_Stream;
57  mutable errno_t m_ErrorValue;
58  unsigned char m_Cr;
59  unsigned char m_Utf8Idx;
60  unsigned char m_Utf16Idx;
61  union {
62  char utf8[5];
63  nlib_utf16_t utf16[3];
64  } m_Buf;
65 
67 };
68 
69 NLIB_NAMESPACE_END
70 
71 NLIB_DEFINE_STD_SWAP(::nlib_ns::TextWriter)
72 
73 #endif // INCLUDE_NN_NLIB_TEXTWRITER_H_
#define NLIB_NOEXCEPT
Defines noexcept geared to the environment, or the equivalent.
Definition: Platform.h:2151
OutputStream * GetStream() noexcept
Gets the stream for the text writer to write to.
Definition: TextWriter.h:37
#define NLIB_FINAL
Defines final if it is available for use. If not, holds an empty string.
#define NLIB_NONNULL
Indicates that you cannot specify NULL for all arguments.
Definition: Platform_unix.h:66
#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:126
#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:141
#define NLIB_VIS_HIDDEN
Symbols for functions and classes are not made available outside of the library.
Definition: Platform_unix.h:50
bool Write(BinaryWriter *w, T x)
You can write user-defined class objects by specializing this function template.
Definition: BinaryWriter.h:121
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: Platform.h:2161
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: Platform.h:2160
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:44
~TextWriter() noexcept
Destructor. The stream is not closed.
Definition: TextWriter.h:23
#define NLIB_VIS_PUBLIC
Symbols for functions and classes are made available outside of the library.
Definition: Platform_unix.h:51
The class for writing text to streams.
Definition: TextWriter.h:20
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