7 #ifndef BOOST_LOCALE_FORMAT_HPP_INCLUDED 8 #define BOOST_LOCALE_FORMAT_HPP_INCLUDED 10 #include <boost/locale/config.hpp> 11 #include <boost/locale/formatting.hpp> 12 #include <boost/locale/hold_ptr.hpp> 13 #include <boost/locale/message.hpp> 19 # pragma warning(push) 20 # pragma warning(disable : 4275 4251 4231 4660) 23 namespace boost {
namespace locale {
34 template<
typename CharType>
36 typedef std::basic_ostream<CharType> stream_type;
37 typedef void (*writer_type)(stream_type& output,
const void* ptr);
39 formattible() : pointer_(0), writer_(&formattible::void_write) {}
41 formattible(
const formattible& other) =
default;
42 formattible(formattible&& other) =
default;
43 formattible& operator=(
const formattible& other) =
default;
44 formattible& operator=(formattible&& other) =
default;
46 template<
typename Type>
47 explicit formattible(
const Type& value)
49 pointer_ = static_cast<const void*>(&value);
50 writer_ = &write<Type>;
53 friend stream_type&
operator<<(stream_type& out,
const formattible& fmt)
55 fmt.writer_(out, fmt.pointer_);
60 static void void_write(stream_type& output,
const void* )
62 CharType empty_string[1] = {0};
63 output << empty_string;
66 template<
typename Type>
67 static void write(stream_type& output,
const void* ptr)
69 output << *static_cast<const Type*>(ptr);
76 class BOOST_LOCALE_DECL format_parser {
78 format_parser(std::ios_base& ios,
void*,
void (*imbuer)(
void*,
const std::locale&));
80 format_parser(
const format_parser&) =
delete;
81 format_parser& operator=(
const format_parser&) =
delete;
83 unsigned get_position();
85 void set_one_flag(
const std::string& key,
const std::string& value);
87 template<
typename CharType>
88 void set_flag_with_str(
const std::string& key,
const std::basic_string<CharType>& value)
90 if(key ==
"ftime" || key ==
"strftime") {
98 void imbue(
const std::locale&);
179 template<
typename CharType>
184 typedef detail::formattible<CharType> formattible_type;
203 message_(std::move(other.message_)), format_(std::move(other.format_)), translate_(other.translate_),
206 if(other.parameters_count_)
207 throw std::invalid_argument(
"Can't move a basic_format with bound parameters");
211 if(other.parameters_count_)
212 throw std::invalid_argument(
"Can't move a basic_format with bound parameters");
213 message_ = std::move(other.message_);
214 format_ = std::move(other.format_);
215 translate_ = other.translate_;
216 parameters_count_ = 0;
235 template<
typename Formattible>
238 add(formattible_type(
object));
245 std::basic_ostringstream<CharType> buffer;
260 format_output(out,
format);
266 format_guard(detail::format_parser& fmt) : fmt_(&fmt), restored_(false) {}
283 detail::format_parser* fmt_;
296 size_t size = sformat.size();
297 const CharType*
format = sformat.c_str();
310 if(pos + 1 < size &&
format[pos + 1] == obrk) {
317 detail::format_parser fmt(out, static_cast<void*>(&out), &basic_format::imbue_locale);
319 format_guard guard(fmt);
325 bool use_svalue =
true;
326 for(;
format[pos]; pos++) {
328 if(c == comma || c == eq || c == cbrk)
331 key += static_cast<char>(c);
337 if(
format[pos] == quote) {
341 if(
format[pos] == quote) {
342 if(
format[pos + 1] == quote) {
356 while((c =
format[pos]) != 0 && c != comma && c != cbrk) {
357 svalue += static_cast<char>(c);
364 fmt.set_one_flag(key, svalue);
366 fmt.set_flag_with_str(key, value);
368 if(
format[pos] == comma) {
371 }
else if(
format[pos] == cbrk) {
372 unsigned position = fmt.get_position();
373 out << get(position);
385 void add(
const formattible_type& param)
387 if(parameters_count_ >= base_params_)
388 ext_params_.push_back(param);
390 parameters_[parameters_count_] = param;
394 formattible_type get(
unsigned id)
const 396 if(
id >= parameters_count_)
397 return formattible_type();
398 else if(
id >= base_params_)
399 return ext_params_[
id - base_params_];
401 return parameters_[id];
404 static void imbue_locale(
void* ptr,
const std::locale& l) { reinterpret_cast<stream_type*>(ptr)->imbue(l); }
406 static constexpr
unsigned base_params_ = 8;
412 formattible_type parameters_[base_params_];
413 unsigned parameters_count_;
414 std::vector<formattible_type> ext_params_;
420 template<
typename CharType>
432 #ifdef BOOST_LOCALE_ENABLE_CHAR16_T 437 #ifdef BOOST_LOCALE_ENABLE_CHAR32_T 446 # pragma warning(pop)
std::ios_base & strftime(std::ios_base &ios)
Definition: formatting.hpp:254
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:131