8#ifndef DESIGNLAB_TOML_FILE_IMPORTER_H_
9#define DESIGNLAB_TOML_FILE_IMPORTER_H_
33concept HasFromToml = std::is_default_constructible_v<T> && impl::has_from_toml<T>::value;
40template <HasFromToml T>
52 std::optional<T>
Import(
const std::string& file_path)
const
54 if (do_output_message_)
60 if (!FileIsExist(file_path)) {
return std::nullopt; }
62 toml::value toml_value;
64 if (!ParseTomlFile(file_path, &toml_value)) {
return std::nullopt; }
68 if (!SerializeTomlData(&toml_value, &data)) {
return std::nullopt; }
70 if (!ValidateData(data)) {
return std::nullopt; }
72 if (do_output_message_)
89 const auto data =
Import(file_path);
91 if (data.has_value()) {
return data.value(); }
96 exporter.
Export(file_path, T());
106 bool FileIsExist(
const std::string& file_path)
const
108 if (do_output_message_)
113 if (!std::filesystem::exists(file_path))
115 if (do_output_message_)
124 if (do_output_message_)
132 bool ParseTomlFile(
const std::string& file_path, toml::value* toml_value)
const
134 if (do_output_message_)
142 std::ifstream ifs(file_path, std::ios::binary);
144 *toml_value = toml::parse(ifs, file_path);
146 catch (toml::syntax_error err)
148 if (do_output_message_)
160 if (do_output_message_)
168 bool SerializeTomlData(toml::value* toml_value, T* data)
const
170 if (do_output_message_)
177 *data = toml::from<T>::from_toml(*toml_value);
181 if (do_output_message_)
190 if (do_output_message_)
198 bool ValidateData(
const T& data)
const
200 if (do_output_message_)
205 const auto [is_valid, error_message] = validator_->Validate(data);
209 if (do_output_message_)
221 if (do_output_message_)
229 bool do_output_message_{
true };
231 const std::unique_ptr<ITomlDataValidator<T>> validator_;
static void SystemOutput(const std::string &str)
コマンドラインに文字を出力する関数.System用の出力.
static void ErrorOutput(const std::string &str)
コマンドラインに文字を出力する関数.Error用の出力.
static void OutputNewLine(int num, OutputDetail detail)
コマンドラインで改行をする関数.
static bool InputYesNo(const std::string &str="Are you sure?")
yesかnoを入力させる関数.返り値で yes なら true,noなら falseを返す. 出力される文字列は,必ず OutputDetail::kSystem で出力される.
static void InfoOutput(const std::string &str)
コマンドラインに文字を出力する関数.Info用の出力.
TOMLファイルのデータの検証を行う処理のインターフェース.
常に trueを返す ITomlDataValidator の実装クラス.
void Export(const std::string &file_path, const T &data)
TOMLファイルを出力する.
tomlファイルを読み込んで構造体に変換するテンプレートクラス.
std::optional< T > Import(const std::string &file_path) const
指定したファイルパスのファイルを読み込み,構造体に変換する.
T ImportOrUseDefault(const std::string &file_path) const
指定したファイルパスのファイルを読み込み,構造体に変換する. 読込に失敗した場合は,デフォルトの構造体を返す. また,読込に失敗した場合には, デフォルトの構造体をファイルに出力するかどうかをユーザに問...
TomlFileImporter(std::unique_ptr< ITomlDataValidator< T > > &&validator)
FromTomlを持つか判定するコンセプト. toml::from<T>::from_toml()が定義されているかどうかを判定する. また,デフォルトコンストラクタが実装されているかどうかも判...
@ kSystem
システムメッセージ,常に出力する.