nlib
TextWriter.h
[詳解]
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  errno_t Init() NLIB_NOEXCEPT;
38  errno_t Open(OutputStream* stream) NLIB_NOEXCEPT;
39  bool Write(wchar_t c) NLIB_NOEXCEPT;
40  bool Write(const wchar_t* str) NLIB_NOEXCEPT NLIB_NONNULL;
41  bool Write(char c) NLIB_NOEXCEPT;
42  bool Write(const nlib_utf8_t* str) NLIB_NOEXCEPT NLIB_NONNULL;
44  bool Write(const nlib_utf16_t* str) NLIB_NOEXCEPT NLIB_NONNULL;
46  bool Write(const nlib_utf32_t* str) NLIB_NOEXCEPT NLIB_NONNULL;
47  bool Flush() NLIB_NOEXCEPT;
48  bool Close() NLIB_NOEXCEPT;
49  errno_t GetErrorValue() const NLIB_NOEXCEPT { return errno_; }
50  OutputStream* GetStream() NLIB_NOEXCEPT { return stream_; }
51  bool WriteFormat(_Printf_format_string_ const wchar_t* fmt, ...) NLIB_NOEXCEPT;
52  bool WriteFormat(_Printf_format_string_ const nlib_utf8_t* fmt, ...) NLIB_NOEXCEPT;
53  void SetError(errno_t e) const NLIB_NOEXCEPT {
54  if (errno_ == 0) errno_ = e;
55  }
56  NLIB_SAFE_BOOL(TextWriter, GetErrorValue() == 0)
57 
58  private:
59  NLIB_VIS_HIDDEN bool Write_(const char* str, size_t len) NLIB_NOEXCEPT;
60 
61  private:
62  OutputStream* stream_;
63  mutable ErrnoT errno_;
64  unsigned char cr_;
65  unsigned char utf8idx_;
66  unsigned char utf16idx_;
67  union {
68  char utf8[5];
69  nlib_utf16_t utf16[3];
70  } buf_;
71 
73 };
74 
75 NLIB_NAMESPACE_END
76 #endif // INCLUDE_NN_NLIB_TEXTWRITER_H_
errno_t GetErrorValue() const noexcept
書き込み等が失敗した際に、エラーの原因を取得できます。
Definition: TextWriter.h:49
OutputStream * GetStream() noexcept
テキストライタが書き込みを行うストリームを取得します。
Definition: TextWriter.h:50
#define NLIB_DISALLOW_COPY_AND_ASSIGN(TypeName)
TypeName で指定されたクラスのコピーコンストラクタと代入演算子を禁止します。
Definition: Config.h:179
#define NLIB_SAFE_BOOL(class_name, exp)
クラス内に安全なoperator bool()を定義します。 可能であればC++11のexplicit boolを利用します。 ...
Definition: Config.h:194
#define NLIB_VIS_HIDDEN
関数やクラス等のシンボルをライブラリの外部に公開しません。
Definition: Platform_unix.h:88
#define NLIB_VIS_PUBLIC
関数やクラス等のシンボルをライブラリの外部に公開します。
Definition: Platform_unix.h:89
bool Write(BinaryWriter *w, T x)
この関数テンプレートを特殊化することで、ユーザー定義クラスを書きこむことができます。 ...
Definition: BinaryWriter.h:136
uint32_t nlib_utf32_t
char32_tが利用できる場合はchar32_tに、そうでない場合はuint32_tにtypedefされます。 ...
Definition: Platform.h:294
uint16_t nlib_utf16_t
char16_tが利用できる場合はchar16_tに、そうでない場合はuint16_tにtypedefされます。 ...
Definition: Platform.h:293
errno_tをラップするクラスです。Visual Studioのデバッガ上での表示を改善します。
Definition: Config.h:406
#define NLIB_NOEXCEPT
環境に合わせてnoexcept 又は同等の定義がされます。
Definition: Config.h:105
開発環境別の設定が書かれるファイルです。
~TextWriter() noexcept
デストラクタです。ストリームはクローズされません。
Definition: TextWriter.h:36
ストリームにテキストを書き込むクラスです。
Definition: TextWriter.h:33
#define NLIB_FINAL
利用可能であればfinalが定義されます。そうでない場合は空文字列です。
Definition: Config.h:245
void SetError(errno_t e) const noexcept
エラー値を設定します。
Definition: TextWriter.h:53
#define NLIB_NONNULL
全ての引数にNULLを指定することができないことを示します。
出力ストリームの基底クラスです。このクラスを実体化することはできません。
Definition: OutputStream.h:30
char nlib_utf8_t
charのtypedefです。文字列がUTF-8であることを示します。
Definition: Platform.h:308
int errno_t
intのtypedefで、戻り値としてPOSIXのエラー値を返すことを示します。
Definition: NMalloc.h:37