8#ifndef DESIGNLAB_TOML_FILE_IMPORTER_H_
9#define DESIGNLAB_TOML_FILE_IMPORTER_H_
33 std::is_default_constructible_v<T> && impl::has_from_toml<T>::value;
39template <HasFromToml T>
46 : validator_(
std::move(validator)) {}
51 std::optional<T>
Import(
const std::string& file_path)
const {
52 if (do_output_message_) {
57 if (!FileIsExist(file_path)) {
61 toml::value toml_value;
63 if (!ParseTomlFile(file_path, &toml_value)) {
69 if (!SerializeTomlData(&toml_value, &data)) {
73 if (!ValidateData(data)) {
77 if (do_output_message_) {
92 const auto data =
Import(file_path);
94 if (data.has_value()) {
100 exporter.
Export(file_path, T());
110 bool FileIsExist(
const std::string& file_path)
const {
111 if (do_output_message_) {
115 if (!std::filesystem::exists(file_path)) {
116 if (do_output_message_) {
124 if (do_output_message_) {
131 bool ParseTomlFile(
const std::string& file_path,
132 toml::value* toml_value)
const {
133 if (do_output_message_) {
139 std::ifstream ifs(file_path, std::ios::binary);
141 *toml_value = toml::parse(ifs, file_path);
142 }
catch (toml::syntax_error err) {
143 if (do_output_message_) {
154 if (do_output_message_) {
161 bool SerializeTomlData(toml::value* toml_value, T* data)
const {
162 if (do_output_message_) {
167 *data = toml::from<T>::from_toml(*toml_value);
169 if (do_output_message_) {
177 if (do_output_message_) {
184 bool ValidateData(
const T& data)
const {
185 if (do_output_message_) {
189 const auto [is_valid, error_message] = validator_->Validate(data);
192 if (do_output_message_) {
203 if (do_output_message_) {
210 bool do_output_message_{
true};
212 const std::unique_ptr<ITomlDataValidator<T>> validator_;
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()が定義されているかどうかを判定する. また,デフォルトコンストラクタが実装されているかどうかも判...
void ErrorOutput(const std::string &str)
コマンドラインに文字を出力する関数. Error 用の出力.
void SystemOutput(const std::string &str)
コマンドラインに文字を出力する関数. System 用の出力.
void InfoOutput(const std::string &str)
コマンドラインに文字を出力する関数. Info 用の出力.
void OutputNewLine(int num, OutputDetail detail)
コマンドラインで改行をする関数.
bool InputYesNo(const std::string &str="Are you sure?")
yesかnoを入力させる関数.返り値で yes なら true, no なら false を返す. 出力される文字列は,必ず OutputDetail::kSystem で出力される.
@ kSystem
システムメッセージ,常に出力する.