7 #ifndef BOOST_LOCALE_FORMAT_HPP_INCLUDED 8 #define BOOST_LOCALE_FORMAT_HPP_INCLUDED 10 #include <boost/locale/formatting.hpp> 11 #include <boost/locale/hold_ptr.hpp> 12 #include <boost/locale/message.hpp> 18 # pragma warning(push) 19 # pragma warning(disable : 4275 4251 4231 4660) 22 namespace boost {
namespace locale {
33 template<
typename CharType>
35 typedef std::basic_ostream<CharType> stream_type;
36 typedef void (*writer_type)(stream_type& output,
const void* ptr);
38 formattible() : pointer_(0), writer_(&formattible::void_write) {}
40 formattible(
const formattible&) =
default;
41 formattible(formattible&&) =
default;
42 formattible& operator=(
const formattible&) =
default;
43 formattible& operator=(formattible&&) =
default;
45 template<
typename Type>
46 explicit formattible(
const Type& value)
48 pointer_ = static_cast<const void*>(&value);
49 writer_ = &write<Type>;
52 friend stream_type&
operator<<(stream_type& out,
const formattible& fmt)
54 fmt.writer_(out, fmt.pointer_);
59 static void void_write(stream_type& output,
const void* )
61 CharType empty_string[1] = {0};
62 output << empty_string;
65 template<
typename Type>
66 static void write(stream_type& output,
const void* ptr)
68 output << *static_cast<const Type*>(ptr);
75 class BOOST_LOCALE_DECL format_parser {
77 format_parser(std::ios_base& ios,
void*,
void (*imbuer)(
void*,
const std::locale&));
79 format_parser(
const format_parser&) =
delete;
80 format_parser& operator=(
const format_parser&) =
delete;
82 unsigned get_position();
84 void set_one_flag(
const std::string& key,
const std::string& value);
86 template<
typename CharType>
87 void set_flag_with_str(
const std::string& key,
const std::basic_string<CharType>& value)
89 if(key ==
"ftime" || key ==
"strftime") {
97 void imbue(
const std::locale&);
178 template<
typename CharType>
183 typedef detail::formattible<CharType> formattible_type;
202 message_(std::move(other.message_)), format_(std::move(other.format_)), translate_(other.translate_),
205 if(other.parameters_count_)
206 throw std::invalid_argument(
"Can't move a basic_format with bound parameters");
210 if(other.parameters_count_)
211 throw std::invalid_argument(
"Can't move a basic_format with bound parameters");
212 message_ = std::move(other.message_);
213 format_ = std::move(other.format_);
214 translate_ = other.translate_;
215 parameters_count_ = 0;
234 template<
typename Formattible>
237 add(formattible_type(
object));
244 std::basic_ostringstream<CharType> buffer;
259 format_output(out,
format);
265 format_guard(detail::format_parser& fmt) : fmt_(&fmt), restored_(false) {}
282 detail::format_parser* fmt_;
295 size_t size = sformat.size();
296 const CharType*
format = sformat.c_str();
309 if(pos + 1 < size &&
format[pos + 1] == obrk) {
316 detail::format_parser fmt(out, static_cast<void*>(&out), &basic_format::imbue_locale);
318 format_guard guard(fmt);
324 bool use_svalue =
true;
325 for(;
format[pos]; pos++) {
327 if(c == comma || c == eq || c == cbrk)
330 key += static_cast<char>(c);
336 if(
format[pos] == quote) {
340 if(
format[pos] == quote) {
341 if(
format[pos + 1] == quote) {
355 while((c =
format[pos]) != 0 && c != comma && c != cbrk) {
356 svalue += static_cast<char>(c);
363 fmt.set_one_flag(key, svalue);
365 fmt.set_flag_with_str(key, value);
367 if(
format[pos] == comma) {
370 }
else if(
format[pos] == cbrk) {
371 unsigned position = fmt.get_position();
372 out << get(position);
384 void add(
const formattible_type& param)
386 if(parameters_count_ >= base_params_)
387 ext_params_.push_back(param);
389 parameters_[parameters_count_] = param;
393 formattible_type get(
unsigned id)
const 395 if(
id >= parameters_count_)
396 return formattible_type();
397 else if(
id >= base_params_)
398 return ext_params_[
id - base_params_];
400 return parameters_[id];
403 static void imbue_locale(
void* ptr,
const std::locale& l) { reinterpret_cast<stream_type*>(ptr)->imbue(l); }
405 static constexpr
unsigned base_params_ = 8;
411 formattible_type parameters_[base_params_];
412 unsigned parameters_count_;
413 std::vector<formattible_type> ext_params_;
419 template<
typename CharType>
431 #ifdef BOOST_LOCALE_ENABLE_CHAR16_T 436 #ifdef BOOST_LOCALE_ENABLE_CHAR32_T 445 # pragma warning(pop)
std::ios_base & strftime(std::ios_base &ios)
Definition: formatting.hpp:253
string_type str() const
Translate message to a string in the default global locale, using default domain.
Definition: message.hpp:229
static ios_info & get(std::ios_base &ios)
Get ios_info instance for specific stream object.
std::basic_ostream< CharType > & operator<<(std::basic_ostream< CharType > &out, const date_time &t)
Definition: date_time.hpp:719
void domain_id(int)
Set special message domain identification.
void date_time_pattern(const std::basic_string< CharType > &str)
Set date/time pattern (strftime like)
Definition: formatting.hpp:130