Mountain  1.0.0
Simple C++ 2D Game Framework
formatter.hpp
Go to the documentation of this file.
1 // ReSharper disable CppMemberFunctionMayBeStatic
2 // ReSharper disable CppClangTidyCertDcl58Cpp
3 #pragma once
4 
5 #include <filesystem>
6 #include <format>
7 #include <sstream>
8 
10 
20 
21 // These definitions must be in the std namespace
22 
26 template <>
27 struct std::formatter<std::filesystem::path>
28 {
30  template <class ParseContext>
31  constexpr typename ParseContext::iterator parse(ParseContext& ctx)
32  {
33  auto it = ctx.begin();
34  if (it == ctx.end())
35  return it;
36 
37  if (*it != '}')
38  throw std::format_error("Invalid format args for std::filesystem::path");
39 
40  return it;
41  }
42 
44  template <class FormatContext>
45  typename FormatContext::iterator format(const std::filesystem::path& path, FormatContext& ctx) const
46  {
47  std::ostringstream out;
48 
49  out << path.generic_string();
50 
51  return std::ranges::copy(std::move(out).str(), ctx.out()).out;
52  }
53 };
54 
58 template <Mountain::Concepts::ExceptionT Exception>
59 struct std::formatter<Exception>
60 {
62  template <class ParseContext>
63  constexpr typename ParseContext::iterator parse(ParseContext& ctx)
64  {
65  auto it = ctx.begin();
66  if (it == ctx.end())
67  return it;
68 
69  if (*it != '}')
70  throw std::format_error("Invalid format args for std::exception");
71 
72  return it;
73  }
74 
76  template <class FormatContext>
77  typename FormatContext::iterator format(const Exception& ex, FormatContext& ctx) const
78  {
79  std::ostringstream out;
80 
81  out << ex.what();
82 
83  return std::ranges::copy(std::move(out).str(), ctx.out()).out;
84  }
85 };
constexpr ParseContext::iterator parse(ParseContext &ctx)
Parses the input formatting options.
Definition: formatter.hpp:63
STL namespace.
FormatContext::iterator format(const std::filesystem::path &path, FormatContext &ctx) const
Formats a string using the given instance of std::filesystem::path, according to the given options in...
Definition: formatter.hpp:45
constexpr ParseContext::iterator parse(ParseContext &ctx)
Parses the input formatting options.
Definition: formatter.hpp:31
Defines the Mountain::Concepts namespace which contains useful concepts used in the engine...
FormatContext::iterator format(const Exception &ex, FormatContext &ctx) const
Formats a string using the given instance of std::exception, according to the given options in the pa...
Definition: formatter.hpp:77