8#ifndef DESIGNLAB_TOML_SERIALIZE_MACRO_H_
9#define DESIGNLAB_TOML_SERIALIZE_MACRO_H_
18#include <magic_enum.hpp>
62std::vector<std::string>
sjis_to_utf8_vec(
const std::vector<std::string>& str_vec);
70typename std::enable_if<impl::is_toml11_available_type<T>::value>::type
71SetTomlValue(::toml::basic_value<toml::preserve_comments, std::map>* v,
const std::string& str,
const T& value)
82typename std::enable_if<!impl::is_toml11_available_type<T>::value&& std::is_enum<T>::value>::type
83SetTomlValue(::toml::basic_value<toml::preserve_comments, std::map>* v,
const std::string& str,
const T& value)
85 (*v)[str] =
static_cast<std::string
>(magic_enum::enum_name(value));
96SetTomlValue(::toml::basic_value<toml::preserve_comments, std::map>* v,
const std::string& str,
const T& value)
100 (*v)[str] = ss.str();
105SetTomlValue(::toml::basic_value<toml::preserve_comments, std::map>* v,
const std::string& str,
const T& value)
107 std::vector<std::string> str_vec(value.size());
109 for (
int i = 0; i < value.size(); ++i)
111 std::stringstream ss;
113 str_vec[i] = ss.str();
120template <
typename T,
typename Enable =
void>
131 static T
Get(::toml::basic_value<toml::preserve_comments, std::map>* v,
const std::string& var_str)
133 return toml::find<T>(*v, var_str);
141struct GetTomlValueImpl<T, typename
std::enable_if<!impl::is_toml11_available_type<T>::value&& std::is_enum<T>::value>::type>
148 static T
Get(::toml::basic_value<toml::preserve_comments, std::map>* v,
const std::string& var_str)
150 std::string str = toml::find<std::string>(*v, var_str);
151 return magic_enum::enum_cast<T>(str).value();
159struct GetTomlValueImpl<T, typename
std::enable_if<!impl::is_toml11_available_type<T>::value&& impl::has_input_operator<T>::value>::type>
166 static T
Get(::toml::basic_value<toml::preserve_comments, std::map>* v,
const std::string& var_str)
168 std::string str = toml::find<std::string>(*v, var_str);
169 std::stringstream ss;
178struct GetTomlValueImpl<T, typename
std::enable_if<!impl::is_toml11_available_type<T>::value&& impl::is_vector_of_has_input_operator<T>::value>::type>
180 static T
Get(::toml::basic_value<toml::preserve_comments, std::map>* v,
const std::string& var_str)
182 std::vector<std::string> str_vec = toml::find<std::vector<std::string>>(*v, var_str);
186 temp.resize(str_vec.size());
188 for (
int i = 0; i < str_vec.size(); ++i)
190 std::stringstream ss;
207T
GetTomlValue(::toml::basic_value<toml::preserve_comments, std::map> v,
const std::string& var_str)
220#define DESIGNLAB_SUB_MACRO_FIND_MEMBER_VARIABLE_FROM_VALUE(VAR_NAME) \
222 const std::string table_str = desc.VAR_NAME.table_name; \
224 if (table_str == ::designlab::toml_func::Toml11Description::kNoTable) \
227 ::designlab::toml_func::GetTomlValue<decltype(obj.VAR_NAME)>( \
229 TOML11_STRINGIZE(VAR_NAME)); \
234 ::designlab::toml_func::GetTomlValue<decltype(obj.VAR_NAME)>( \
236 TOML11_STRINGIZE(VAR_NAME)); \
247#define DESIGNLAB_SUB_MACRO_ASSIGN_MEMBER_VARIABLE_TO_VALUE(VAR) \
248if (desc.VAR.table_name != ::designlab::toml_func::Toml11Description::kNoTable) { \
249 if (v.count(desc.VAR.table_name) == 0) \
251 v[desc.VAR.table_name] = toml::table{}; \
254 ::designlab::toml_func::SetTomlValue(&v[desc.VAR.table_name], \
255 TOML11_STRINGIZE(VAR), obj.VAR); \
259 ::designlab::toml_func::SetTomlValue( \
260 &v, TOML11_STRINGIZE(VAR), obj.VAR); \
270#define DESIGNLAB_SUB_MACRO_ADD_COMMENT(VN) \
271if (desc.VN.description != "") { \
272 if (desc.VN.table_name != ::designlab::toml_func::Toml11Description::kNoTable) \
274 v[desc.VN.table_name][#VN].comments().push_back(desc.VN.description); \
278 v[#VN].comments().push_back(desc.VN.description); \
286#define DESIGNLAB_TOML11_DESCRIPTION_CLASS(CLASS) \
287struct CLASS##Description final
293#define DESIGNLAB_TOML11_FILE_NO_DESCRIPTION() \
294const ::std::vector<::std::string> file_description_vec{};
300#define DESIGNLAB_TOML11_FILE_ADD_DESCRIPTION(DESCRIPTION) \
301const ::std::vector<::std::string> file_description_vec{sjis_to_utf8(DESCRIPTION)};
307#define DESIGNLAB_TOML11_FILE_ADD_DESCRIPTION_MULTI_LINE(DESCRIPTION_VEC) \
308const ::std::vector<::std::string> file_description_vec = \
309 ::designlab::toml_func::sjis_to_utf8_vec(DESCRIPTION_VEC);
316#define DESIGNLAB_TOML11_TABLE_ADD_DESCRIPTION(...) \
317const std::vector<std::string> table_name_description_vec = \
318 ::designlab::toml_func::sjis_to_utf8_vec({ __VA_ARGS__ });
323#define DESIGNLAB_TOML11_TABLE_NO_DESCRIPTION() \
324const std::vector<std::string> table_name_description_vec = {};
332#define DESIGNLAB_TOML11_VARIABLE_ADD_DESCRIPTION(VARIABLE, TABLE, DESCRIPTION) \
333const ::designlab::toml_func::Toml11Description VARIABLE{ \
334 TABLE, sjis_to_utf8(DESCRIPTION) \
341#define DESIGNLAB_TOML11_VARIABLE_NO_DESCRIPTION(VARIABLE, TABLE) \
342const ::designlab::toml_func::Toml11Description VARIABLE{TABLE, ""}
346#define DESIGNLAB_TOML11_NO_TABLE \
347::designlab::toml_func::Toml11Description::kNoTable
395#define DESIGNLAB_TOML11_SERIALIZE(NAME, ...) \
401 static_assert(std::is_class<NAME>::value, \
402 "第1引数はクラスか構造体である必要があります."); \
403 static_assert(std::is_default_constructible<NAME>::value, \
404 "第1引数はデフォルトコンストラクタを持つ必要があります."); \
406 template<typename C, template<typename ...> class T, \
407 template<typename ...> class A> \
408 static NAME from_toml(basic_value<C, T, A>& v) \
410 ::toml::basic_value<toml::preserve_comments, std::map> v_ = v; \
412 NAME##Description desc; \
413 TOML11_FOR_EACH_VA_ARGS( \
414 DESIGNLAB_SUB_MACRO_FIND_MEMBER_VARIABLE_FROM_VALUE, __VA_ARGS__) \
422 static value into_toml(const NAME& obj) \
424 ::toml::basic_value<toml::preserve_comments, std::map> v = ::toml::table{}; \
426 NAME##Description desc; \
428 for (const auto i : desc.file_description_vec) \
430 v.comments().push_back(i); \
433 for (int i = 0; i < desc.table_name_description_vec.size(); ++i) \
435 v[desc.table_name_description_vec[i]] = ::toml::table{}; \
436 v[desc.table_name_description_vec[i]].comments(). \
437 push_back(desc.table_name_description_vec[i + 1]); \
441 TOML11_FOR_EACH_VA_ARGS( \
442 DESIGNLAB_SUB_MACRO_ASSIGN_MEMBER_VARIABLE_TO_VALUE, __VA_ARGS__) \
443 TOML11_FOR_EACH_VA_ARGS( \
444 DESIGNLAB_SUB_MACRO_ADD_COMMENT, __VA_ARGS__) \
tomlファイルのシリアライズ/デシリアライズを行うための関数群.
T GetTomlValue(::toml::basic_value< toml::preserve_comments, std::map > v, const std::string &var_str)
ユーザーが直接呼ぶ関数. GetTomlValueImpl を利用してテンプレートの型を解決し, それに応じたGet関数を呼び出す.
std::vector< std::string > sjis_to_utf8_vec(const std::vector< std::string > &str_vec)
文字列のベクターをShift-jisからUTF-8に変換する.
std::enable_if< impl::is_toml11_available_type< T >::value >::type SetTomlValue(::toml::basic_value< toml::preserve_comments, std::map > *v, const std::string &str, const T &value)
tomlファイルに値を追加するための関数. enum 型と vector3 型と euler_xyz 型以外の型に対応している.
出力ストリームが実装されているか判断するメタ関数.
出力ストリームが実装されている vector 型かどうかを判定するメタ関数.
static T Get(::toml::basic_value< toml::preserve_comments, std::map > *v, const std::string &var_str)
tomlファイルから値を取得するための関数.
static T Get(::toml::basic_value< toml::preserve_comments, std::map > *v, const std::string &var_str)
static T Get(::toml::basic_value< toml::preserve_comments, std::map > *v, const std::string &var_str)
tomlファイルから値を取得するための関数.
static T Get(::toml::basic_value< toml::preserve_comments, std::map > *v, const std::string &var_str)
プライマリ テンプレート.特殊化されない場合はコンパイルエラーになる.
tomlファイルに追加する変数の説明を追加するための構造体.
std::string description
説明,tomlファイルにはコメントとして追加される.
Toml11Description(const std::string &t, const std::string &d)
static const char kNoTable[]
テーブルがない場合に指定する文字列.
std::string table_name
テーブル名.