Welcome to mirror list, hosted at ThFree Co, Russian Federation.

format.hpp « libslic3r « src - github.com/supermerill/SuperSlicer.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c3747677b090aadbc83113c537b1534c9cf1860c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#ifndef slic3r_format_hpp_
#define slic3r_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.
// 
// Boost::format works around the missing variadic templates by an ugly % chaining operator. The usage of boost::format looks like this:
// (boost::format("template") % arg1 %arg2).str()
// This wrapper allows for a nicer syntax:
// Slic3r::format("template", arg1, arg2)
// One can also override Slic3r::internal::format::cook() function to convert a Slic3r::format() argument to something that
// boost::format may convert to string, see slic3r/GUI/I18N.hpp for a "cook" function to convert wxString to UTF8.

#include <boost/format.hpp>

namespace Slic3r {

// https://gist.github.com/gchudnov/6a90d51af004d97337ec
namespace internal {
	namespace format {
		// Default "cook" function - just forward.
		template<typename T>
		inline T&& cook(T&& arg) {
		  	return std::forward<T>(arg);
		}

		// End of the recursive chain.
		inline std::string format_recursive(boost::format& message) {
		  	return message.str();
		}

		template<typename TValue, typename... TArgs>
		std::string format_recursive(boost::format& message, TValue&& arg, TArgs&&... args) {
			// Format, possibly convert the argument by the "cook" function.
		  	message % cook(std::forward<TValue>(arg));
		  	return format_recursive(message, std::forward<TArgs>(args)...);
		}
	}
};

template<typename... TArgs>
inline std::string format(const char* fmt, TArgs&&... args) {
	boost::format message(fmt);
	return internal::format::format_recursive(message, std::forward<TArgs>(args)...);
}

template<typename... TArgs>
inline std::string format(const std::string& fmt, TArgs&&... args) {
	boost::format message(fmt);
	return internal::format::format_recursive(message, std::forward<TArgs>(args)...);
}

} // namespace Slic3r

#endif // slic3r_format_hpp_