From 2963d9a25b47c8496e757f48694bcac223cf68b0 Mon Sep 17 00:00:00 2001 From: ToruNiina Date: Fri, 5 Jun 2020 19:43:23 +0900 Subject: [PATCH 1/3] feat: add std::filesystem::path support --- toml/parser.hpp | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/toml/parser.hpp b/toml/parser.hpp index 80fe704..62900ee 100644 --- a/toml/parser.hpp +++ b/toml/parser.hpp @@ -12,6 +12,13 @@ #include #include +#if __cplusplus >= 201703L +#if __has_include() +#define TOML11_HAS_STD_FILESYSTEM +#include +#endif // has_include() +#endif // cplusplus >= C++17 + namespace toml { namespace detail @@ -2094,5 +2101,21 @@ basic_value parse(const std::string& fname) return parse(ifs, fname); } +#ifdef TOML11_HAS_STD_FILESYSTEM +template class Table = std::unordered_map, + template class Array = std::vector> +basic_value parse(const std::filesystem::path& fpath) +{ + std::ifstream ifs(fpath, std::ios_base::binary); + if(!ifs.good()) + { + throw std::runtime_error("toml::parse: file open error -> " + + fpath.string()); + } + return parse(ifs, fname); +} +#endif // TOML11_HAS_STD_FILESYSTEM + } // toml #endif// TOML11_PARSER_HPP From 46ed051740803bb2a81c1d5cdc79a19d8512dd25 Mon Sep 17 00:00:00 2001 From: ToruNiina Date: Fri, 5 Jun 2020 23:15:19 +0900 Subject: [PATCH 2/3] fix: pass path.string as a filename --- toml/parser.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/toml/parser.hpp b/toml/parser.hpp index 62900ee..b2e4755 100644 --- a/toml/parser.hpp +++ b/toml/parser.hpp @@ -2113,7 +2113,7 @@ basic_value parse(const std::filesystem::path& fpath) throw std::runtime_error("toml::parse: file open error -> " + fpath.string()); } - return parse(ifs, fname); + return parse(ifs, fpath.string()); } #endif // TOML11_HAS_STD_FILESYSTEM From defde33544ed949e312a5116375e6b32f29b0840 Mon Sep 17 00:00:00 2001 From: ToruNiina Date: Sat, 6 Jun 2020 17:18:02 +0900 Subject: [PATCH 3/3] fix: avoid ambiguity in overload resolution Since both `std::string` and `std::filesystem::path` can be convertible from `const char &[N]` (like, `parse("file.toml")`), after adding `parse(std::filesystem::path)`, the overload resolution of `parse("file.toml")` becomes ambiguous. By adding `parse(...)` that exactly matches to `parse("file.toml")`, we can remove this ambiguity. --- toml/parser.hpp | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/toml/parser.hpp b/toml/parser.hpp index b2e4755..d919c16 100644 --- a/toml/parser.hpp +++ b/toml/parser.hpp @@ -2102,6 +2102,24 @@ basic_value parse(const std::string& fname) } #ifdef TOML11_HAS_STD_FILESYSTEM +// This function just forwards `parse("filename.toml")` to std::string version +// to avoid the ambiguity in overload resolution. +// +// Both std::string and std::filesystem::path are convertible from const char[]. +// Without this, both parse(std::string) and parse(std::filesystem::path) +// matches to parse("filename.toml"). This breaks the existing code. +// +// This function exactly matches to the invokation with string literal. +// So this function is preferred than others and the ambiguity disappears. +template class Table = std::unordered_map, + template class Array = std::vector, + std::size_t N> +basic_value parse(const char (&fname)[N]) +{ + return parse(std::string(fname)); +} + template class Table = std::unordered_map, template class Array = std::vector>