GaitGeneration by Graph Search
読み取り中…
検索中…
一致する文字列を見つけられません
cmdio_util.h
[詳解]
1
3
4// Copyright(c) 2023-2025 Design Engineering Laboratory, Saitama University
5// Released under the MIT license
6// https://opensource.org/licenses/mit-license.php
7
8#ifndef DESIGNLAB_CMDIO_UTIL_H_
9#define DESIGNLAB_CMDIO_UTIL_H_
10
11#include <format>
12#include <string>
13#include <utility>
14
15#include "boot_mode.h"
16#include "output_detail.h"
17
18
19// ----------------------------------------------------------------
20//
21// 色付きで出力をしたくない場合,あるいはうまく動作しない場合は,
22// 以下のマクロをコメントアウトすること.
23//
24// ----------------------------------------------------------------
25
26#define DESIGNLAB_USE_COLOR_OUTPUT
27
28
29namespace designlab
30{
31
45class CmdIOUtil final
46{
47public:
48 CmdIOUtil() = delete;
49 CmdIOUtil(const CmdIOUtil&) = delete;
50 CmdIOUtil& operator=(const CmdIOUtil&) = delete;
51 CmdIOUtil(CmdIOUtil&&) = delete;
52
53
60 static void SetOutputLimit(OutputDetail limit);
61
65 static void DoOutput(bool do_output);
66
67
73 static void Output(const std::string& str, OutputDetail detail);
74
77 static inline void DebugOutput(const std::string& str)
78 {
80 }
81
84 static inline void InfoOutput(const std::string& str)
85 {
87 }
88
91 static inline void WarningOutput(const std::string& str)
92 {
94 }
95
98 static inline void ErrorOutput(const std::string& str)
99 {
101 }
102
105 static inline void SystemOutput(const std::string& str)
106 {
108 }
109
116 template <typename... Args>
117 static void FormatOutput(OutputDetail detail, const std::format_string<Args...> str, Args&&... args)
118 {
119 const std::string formatted_str = std::format(str, std::forward<Args>(args)...);
120 Output(formatted_str, detail);
121 }
122
129 template <typename... Args>
130 static void SpacedFormatOutput(OutputDetail detail, const std::format_string<Args...> str, Args&&... args)
131 {
132 const std::string formatted_str = std::format(str, std::forward<Args>(args)...);
133 SpacedOutput(formatted_str, detail);
134 }
135
140 static void SpacedOutput(const std::string& str, OutputDetail detail);
141
146 static void OutputCenter(const std::string& str, OutputDetail detail);
147
152 static void OutputRight(const std::string& str, OutputDetail detail);
153
154
158 static void OutputNewLine(int num, OutputDetail detail);
159
164 static void OutputHorizontalLine(
165 const std::string& line_visual, OutputDetail detail);
166
172 static void OutputTitle(const std::string& title_name, bool output_copy_right = false);
173
174
178 static void WaitAnyKey(const std::string& str = "Waiting for input.");
179
187 static int InputInt(int min, int max, int default_num, const std::string& str = "Please enter an integer.");
188
194 static bool InputYesNo(const std::string& str = "Are you sure?");
195
203 static std::string InputDirName(const std::string& str = "Enter a directory name. (Japanese is not recommended).");
204
205private:
206 static constexpr int kHorizontalLineLength = 100;
207
209 static OutputDetail output_limit;
210
212 static bool do_output;
213
215 static bool is_initialized;
216};
217
218} // namespace designlab
219
220
221#endif // DESIGNLAB_CMDIO_UTIL_H_
標準入出力の std::cout,std::cinを使った コマンドライン入出力を行うシングルトンクラス.
Definition cmdio_util.h:46
CmdIOUtil(CmdIOUtil &&)=delete
ムーブコンストラクタを禁止.
static void SystemOutput(const std::string &str)
コマンドラインに文字を出力する関数.System用の出力.
Definition cmdio_util.h:105
static std::string InputDirName(const std::string &str="Enter a directory name. (Japanese is not recommended).")
ディレクトリ名を入力させる関数. 出力される文字列は,必ず OutputDetail::kSystem で出力される. ディレクトリ名には次の文字は使えない. \ / : * ?...
static void SpacedOutput(const std::string &str, OutputDetail detail)
コマンドラインに文字を出力する関数. 前と後ろに改行を挿入する.
static void ErrorOutput(const std::string &str)
コマンドラインに文字を出力する関数.Error用の出力.
Definition cmdio_util.h:98
static void Output(const std::string &str, OutputDetail detail)
コマンドラインに文字を出力する関数. SetOutputLimit() で設定した出力の許可範囲内であれば出力される. 必ず SetOutputLimit() を呼び出してから使うこと.
static void WaitAnyKey(const std::string &str="Waiting for input.")
入力待ちをする関数. 出力される文字列は,必ず OutputDetail::kSystem で出力される.
static int InputInt(int min, int max, int default_num, const std::string &str="Please enter an integer.")
整数を入力させる関数. 出力される文字列は,必ず OutputDetail::kSystem で出力される.
static void OutputNewLine(int num, OutputDetail detail)
コマンドラインで改行をする関数.
static void OutputTitle(const std::string &title_name, bool output_copy_right=false)
コマンドラインにこのソフトのタイトルを出力する関数. 出力される文字列は,必ず OutputDetail::kSystem で出力される.
static void DoOutput(bool do_output)
そもそも出力をするかを設定する関数. false に設定しても システムメッセージは出力される.
static void OutputHorizontalLine(const std::string &line_visual, OutputDetail detail)
コマンドラインに水平線を出力する関数.
static bool InputYesNo(const std::string &str="Are you sure?")
yesかnoを入力させる関数.返り値で yes なら true,noなら falseを返す. 出力される文字列は,必ず OutputDetail::kSystem で出力される.
CmdIOUtil()=delete
インスタンス化させない.
static void OutputCenter(const std::string &str, OutputDetail detail)
中央に文字を出力する関数. 文字列が長すぎる場合は普通に左詰めで出力される.
static void InfoOutput(const std::string &str)
コマンドラインに文字を出力する関数.Info用の出力.
Definition cmdio_util.h:84
static void FormatOutput(OutputDetail detail, const std::format_string< Args... > str, Args &&... args)
コマンドラインに文字を出力する関数. SetOutputLimit() で設定した出力の許可範囲内であれば出力される. 必ず SetOutputLimit() を呼び出してから使うこと.
Definition cmdio_util.h:117
static void OutputRight(const std::string &str, OutputDetail detail)
右端に文字を出力する関数. 文字列が長すぎる場合は普通に左詰めで出力される.
static void SetOutputLimit(OutputDetail limit)
出力するメッセージをどこまで許可するかを設定する関数. この関数を呼び出してから出ないと,他の関数を使えない. 例えば kError に設定すると,kError 未満の出力( kInfo とか ...
static void DebugOutput(const std::string &str)
コマンドラインに文字を出力する関数.Debug用の出力.
Definition cmdio_util.h:77
CmdIOUtil & operator=(const CmdIOUtil &)=delete
コピー代入演算子を禁止.
CmdIOUtil(const CmdIOUtil &)=delete
コピーコンストラクタを禁止.
static void SpacedFormatOutput(OutputDetail detail, const std::format_string< Args... > str, Args &&... args)
コマンドラインに文字を出力する関数. SetOutputLimit() で設定した出力の許可範囲内であれば出力される. 必ず SetOutputLimit() を呼び出してから使うこと.
Definition cmdio_util.h:130
static void WarningOutput(const std::string &str)
コマンドラインに文字を出力する関数.Warning用の出力.
Definition cmdio_util.h:91
OutputDetail
コマンドラインに文字を出力する際に,その詳細を指定するための列挙体.
@ kInfo
優先度低めの情報.
@ kSystem
システムメッセージ,常に出力する.
@ kDebug
デバッグ時のみ出力,一番優先度が低い.
@ kError
エラーメッセージ.
@ kWarning
警告メッセージ,エラーではないが注意が必要なメッセージ.