nlib
TextWriter.h
Go to the documentation of this file.
1 
2 /*--------------------------------------------------------------------------------*
3  Project: CrossRoad
4  Copyright (C)Nintendo All rights reserved.
5 
6  These coded instructions, statements, and computer programs contain proprietary
7  information of Nintendo and/or its licensed developers and are protected by
8  national and international copyright laws. They may not be disclosed to third
9  parties or copied or duplicated in any form, in whole or in part, without the
10  prior written consent of Nintendo.
11 
12  The content herein is highly confidential and should be handled accordingly.
13  *--------------------------------------------------------------------------------*/
14 
15 #pragma once
16 #ifndef INCLUDE_NN_NLIB_TEXTWRITER_H_
17 #define INCLUDE_NN_NLIB_TEXTWRITER_H_
18 
19 #include "nn/nlib/Config.h"
20 #include "nn/nlib/Swap.h"
21 
22 NLIB_NAMESPACE_BEGIN
23 
24 class OutputStream;
25 
26 // code snippets:
27 // TextWriter writer;
28 // if (writer.Init() != 0 || writer.Open(&stream) != 0) { error; }
29 // # note that wide characters(UTF16/UTF32) are converted into UTF8
30 // if (!writer.Write(....)) { error; }
31 // if (!writer.Flush()) { error; }
32 // if (!writer.Close()) { error; }
34  public:
37  NLIB_MOVE_MEMBER_HELPER_2(TextWriter, stream_, errno_); // NOLINT
38  errno_t Init() NLIB_NOEXCEPT;
39  errno_t Open(OutputStream* stream) NLIB_NOEXCEPT;
40  bool Write(wchar_t c) NLIB_NOEXCEPT;
41  bool Write(const wchar_t* str) NLIB_NOEXCEPT NLIB_NONNULL;
42  bool Write(char c) NLIB_NOEXCEPT;
43  bool Write(const nlib_utf8_t* str) NLIB_NOEXCEPT NLIB_NONNULL;
45  bool Write(const nlib_utf16_t* str) NLIB_NOEXCEPT NLIB_NONNULL;
47  bool Write(const nlib_utf32_t* str) NLIB_NOEXCEPT NLIB_NONNULL;
48  bool Flush() NLIB_NOEXCEPT;
49  bool Close() NLIB_NOEXCEPT;
50  errno_t GetErrorValue() const NLIB_NOEXCEPT { return errno_; }
51  OutputStream* GetStream() NLIB_NOEXCEPT { return stream_; }
52  bool WriteFormat(_Printf_format_string_ const wchar_t* fmt, ...) NLIB_NOEXCEPT;
53  bool WriteFormat(_Printf_format_string_ const nlib_utf8_t* fmt, ...) NLIB_NOEXCEPT;
54  void SetError(errno_t e) const NLIB_NOEXCEPT {
55  if (errno_ == 0) errno_ = e;
56  }
57 
58  void swap(TextWriter& rhs) NLIB_NOEXCEPT {
59  using std::swap;
60  swap(stream_, rhs.stream_);
61  swap(errno_, rhs.errno_);
62  swap(cr_, rhs.cr_);
63  }
64  NLIB_SAFE_BOOL(TextWriter, GetErrorValue() == 0)
65 
66  private:
67  NLIB_VIS_HIDDEN bool Write_(const char* str, size_t len) NLIB_NOEXCEPT;
68 
69  private:
70  OutputStream* stream_;
71  mutable ErrnoT errno_;
72  unsigned char cr_;
73  unsigned char utf8idx_;
74  unsigned char utf16idx_;
75  union {
76  char utf8[5];
77  nlib_utf16_t utf16[3];
78  } buf_;
79 
81 };
82 
83 NLIB_NAMESPACE_END
84 
85 NLIB_DEFINE_STD_SWAP(::nlib_ns::TextWriter)
86 
87 #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:50
OutputStream * GetStream() noexcept
Gets the stream for the text writer to write to.
Definition: TextWriter.h:51
#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:163
#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:178
#define NLIB_VIS_HIDDEN
Symbols for functions and classes are not made available outside of the library.
Definition: Platform_unix.h:88
#define NLIB_VIS_PUBLIC
Symbols for functions and classes are made available outside of the library.
Definition: Platform_unix.h:89
bool Write(BinaryWriter *w, T x)
You can write user-defined class objects by specializing this function template.
Definition: BinaryWriter.h:136
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:286
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:285
Class that wraps errno_t. This class improves visual representations in the Visual Studio debugger...
Definition: Config.h:492
#define NLIB_NOEXCEPT
Defines noexcept geared to the environment, or the equivalent.
Definition: Config.h:99
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:58
~TextWriter() noexcept
Destructor. The stream is not closed.
Definition: TextWriter.h:36
The class for writing text to streams.
Definition: TextWriter.h:33
#define NLIB_FINAL
Defines final if it is available for use. If not, holds an empty string.
Definition: Config.h:229
void SetError(errno_t e) const noexcept
Sets an error value.
Definition: TextWriter.h:54
#define NLIB_NONNULL
Indicates that you cannot specify NULL for all arguments.
The base class for output streams. This class cannot be instantiated.
Definition: OutputStream.h:30
char nlib_utf8_t
Defines char with a typedef. Indicates that it is a UTF-8 string.
Definition: Platform.h:300
int errno_t
Indicates with an int-type typedef that a POSIX error value is returned as the return value...
Definition: NMalloc.h:37