diff options
author | bubnikv <bubnikv@gmail.com> | 2020-04-01 10:48:56 +0300 |
---|---|---|
committer | bubnikv <bubnikv@gmail.com> | 2020-04-01 10:49:04 +0300 |
commit | b843a094f657e987e845d5b7a0c6a72b0849a2cf (patch) | |
tree | 0fc922483b9cab12c1ac2e2eb041a743b2933366 /src/slic3r/GUI/format.hpp | |
parent | daa6dd0a254b09b6beba449fc0b3a12b9bb183fa (diff) |
Slic3r::format(), Slic3r::GUI::format_wxstr():
wrappers around boost::format using C++17 variadic templates,
replacing the ugly and verbose
(boost::format("template") % arg1 % arg2).str()
syntax.
The wrappers also implictely convert input parameters including the template
from wxString to UTF8.
The new format wrapper has been applied at multiple places as a start,
also some double macros _(L()) with new single macro _L().
Diffstat (limited to 'src/slic3r/GUI/format.hpp')
-rw-r--r-- | src/slic3r/GUI/format.hpp | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/src/slic3r/GUI/format.hpp b/src/slic3r/GUI/format.hpp new file mode 100644 index 000000000..1bc4b93d2 --- /dev/null +++ b/src/slic3r/GUI/format.hpp @@ -0,0 +1,65 @@ +#ifndef slic3r_GUI_format_hpp_ +#define slic3r_GUI_format_hpp_ + +// Functional wrapper around boost::format. +// One day we may replace this wrapper with C++20 format +// https://en.cppreference.com/w/cpp/utility/format/format +// though C++20 format uses a different template pattern for position independent parameters. +// This wrapper also manages implicit conversion from wxString to UTF8 and format_wxstr() variants are provided to format into wxString. + +#include <libslic3r/format.hpp> + +namespace Slic3r { +namespace GUI { + +// Format input mixing UTF8 encoded strings (const char*, std::string) and wxStrings, return a wxString. +template<typename... TArgs> +inline wxString format_wxstr(const char* fmt, TArgs&&... args) { + boost::format message(fmt); + return wxString::FromUTF8(Slic3r::internal::format::format_recursive(message, std::forward<TArgs>(args)...).c_str()); +} +template<typename... TArgs> +inline wxString format_wxstr(const std::string& fmt, TArgs&&... args) { + boost::format message(fmt); + return wxString::FromUTF8(Slic3r::internal::format::format_recursive(message, std::forward<TArgs>(args)...).c_str()); +} +template<typename... TArgs> +inline wxString format_wxstr(const wxString& fmt, TArgs&&... args) { + return format_wxstr(fmt.ToUTF8().data()); +} +template<typename... TArgs> +inline std::string format(const wxString& fmt, TArgs&&... args) { + return format(fmt.ToUTF8().data()); +} + +} // namespace GUI + +namespace internal { + namespace format { + // Wrapper around wxScopedCharBuffer to indicate that the content is UTF8 formatted. + struct utf8_buffer { + // wxScopedCharBuffer is reference counted, therefore copying by value is cheap. + wxScopedCharBuffer data; + }; + // Accept wxString and convert it to UTF8 to be processed by Slic3r::format(). + inline const utf8_buffer cook(const wxString &arg) { + return utf8_buffer { arg.ToUTF8() }; + } + } +} + +} // namespace Slic3r + +namespace boost { + namespace io { + namespace detail { + // Adaptor for boost::format to accept wxString converted to UTF8. + inline std::ostream& operator<<(std::ostream& os, const Slic3r::internal::format::utf8_buffer& str) { + os << str.data.data(); + return os; + } + } + } +} + +#endif /* slic3r_GUI_format_hpp_ */ |