6 #include <initializer_list>
13 template <typename Integer>
14 struct formated_integer;
15 struct formated_string;
18 explicit width_t( unsigned v ): value{v} {}
22 explicit base_t( unsigned v ): value{v} {}
26 explicit fill_t( char v ): value{v} {}
33 inline namespace literals {
34 inline width_t operator"" _w( unsigned long long value ) {
35 return width_t{static_cast<unsigned>( value )};
37 inline base_t operator"" _b( unsigned long long value ) {
38 return base_t{static_cast<unsigned>( value )};
40 inline fill_t operator"" _f( char c ) {
47 std::uint8_t base = 10;
49 bool align_right = false;
51 void set( width_t w ) {
54 void set( base_t b ) {
57 void set( fill_t f ) {
60 void set( align_t a ) {
61 align_right = ( a == align_t::right );
64 formated_string operator()( const std::string& str ) const;
66 template <typename Integer,
67 typename = typename std::enable_if<std::is_integral<Integer>::value>::type>
68 formated_integer<Integer> operator()( Integer i ) const;
71 template <typename Integer>
72 struct formated_integer : public format_data {
73 formated_integer( Integer i, format_data f ) : format_data( f ), value {i} {}
77 struct formated_string : public format_data {
78 formated_string( const std::string& s, format_data f ) :
79 format_data( f ), value( std::move( s ) ) {}
81 const std::string& value;
84 inline formated_string format_data::operator()( const std::string& str ) const {
88 template <typename Integer, typename>
89 inline formated_integer<Integer> format_data::operator()( Integer i ) const {
93 template <typename... Args>
94 formated_string fmt( const std::string& str, const Args& ... args ) {
95 auto format = format_data{};
96 std::ignore = std::initializer_list<int>{( format.set( args ), 0 )...};
100 template <typename Integer, typename... Args>
101 formated_integer<Integer> fmt( const Integer i, const Args& ... args ) {
102 auto format = format_data{};
103 std::ignore = std::initializer_list<int>{( format.set( args ), 0 )...};
107 inline namespace literals {
108 format_data operator"" _fmt( const char*, std::size_t );
111 } // namespace format
115 template <typename Integer>
116 inline std::string to_string( const format::formated_integer<Integer>& arg ) {
117 std::ostringstream stream;
119 std::setbase( arg.base ) <<
120 std::setw( arg.width ) <<
121 std::setfill( arg.fill ) <<
126 std::string to_string( const format::formated_string& arg );
130 } // namespace logger