Lumiera 0.pre.04
»edit your freedom«
Loading...
Searching...
No Matches
_Fmt Class Reference

#include "lib/format-string.hpp"

Description

A front-end for using printf-style formatting.

Values to be formatted can be supplied through the operator%. Custom defined string conversions on objects will be used, any errors while invoking the format operation will be suppressed. The implementation is based on boost::format, but kept opaque to keep code size and compilation times down.

See also
FormatString_test

Definition at line 148 of file format-string.hpp.

Classes

struct  Converter
 helper to prepare parameters for inclusion More...
 
struct  Converter< bool >
 
struct  Converter< CStr >
 
struct  Converter< lib::Literal >
 
struct  Converter< lib::Symbol >
 
struct  Converter< SP, lib::meta::enable_if< _shall_show_smartWrapper< SP > > >
 
struct  Converter< VAL * >
 
struct  Converter< VAL, lib::meta::enable_if< _shall_convert_toString< VAL > > >
 some custom types explicitly provide a string representation More...
 
struct  Converter< VAL, lib::meta::enable_if< _shall_format_directly< VAL > > >
 some basic types are directly forwarded down to the implementation; More...
 
struct  Converter< void * >
 

Public Member Functions

 ~_Fmt ()
 
 _Fmt (string formatString)
 Build a formatter object based on the given format string.
 
 operator string () const
 get the formatted result
 
template<typename VAL >
_Fmtoperator% (VAL const &)
 The percent operator ('' ) is used do feed parameter values to be included into the formatted result, at the positions marked by printf-style placeholders within the format string.
 

Friends

std::ostream & operator<< (std::ostream &os, _Fmt const &)
 send the formatted buffer directly to the output stream.
 
bool operator== (_Fmt const &, _Fmt const &)
 
bool operator== (_Fmt const &, string const &)
 
bool operator== (_Fmt const &, CStr const)
 
bool operator== (string const &, _Fmt const &)
 
bool operator== (CStr const, _Fmt const &)
 
template<typename X >
bool operator!= (_Fmt const &fmt, X const &x)
 
template<typename X >
bool operator!= (X const &x, _Fmt const &fmt)
 

Private Types

enum  { FORMATTER_SIZE = lib::meta::SizeTrait::BOOST_FORMAT }
 size of an opaque implementation Buffer More...
 
typedef std::byte Implementation[FORMATTER_SIZE]
 

Static Private Member Functions

template<typename VAL >
static void format (const VAL, Implementation &)
 call into the opaque implementation
 

Private Attributes

Implementation formatter_
 

Additional Inherited Members

- Private Member Functions inherited from NonCopyable
 ~NonCopyable ()=default
 
 NonCopyable ()=default
 
 NonCopyable (NonCopyable const &)=delete
 
NonCopyableoperator= (NonCopyable const &)=delete
 

Constructor & Destructor Documentation

◆ ~_Fmt()

~_Fmt ( )

Definition at line 133 of file format-string.cpp.

References _Fmt::formatter_.

◆ _Fmt()

_Fmt ( string  formatString)

Build a formatter object based on the given format string.

The actual implementation is delegated to an boost::format object, which is placement-constructed into an opaque buffer embedded into this object. Defining the necessary size for this buffer relies on implementation details of boost::format (and might break)

See also
lib::meta::SizeTrait::BOOST_FORMAT

Definition at line 114 of file format-string.cpp.

References _Fmt::formatter_, and _Fmt::FORMATTER_SIZE.

Member Typedef Documentation

◆ Implementation

typedef std::byte Implementation[FORMATTER_SIZE]
private

Definition at line 154 of file format-string.hpp.

Member Enumeration Documentation

◆ anonymous enum

anonymous enum
private

size of an opaque implementation Buffer

Enumerator
FORMATTER_SIZE 

Definition at line 152 of file format-string.hpp.

Member Function Documentation

◆ format()

template<typename VAL >
template void format ( const VAL  ,
Implementation  
)
staticprivate

call into the opaque implementation

access point for the frontend, allowing to push a single parameter value down into the implementation for the actual formatting. Only selected primitive types are handled directly this way, while all custom conversions, the handling of pointers and the fallback (showing a type string) is done in the frontend.

Note
we need to generate instantiations of this template function explicitly for all basic types to be supported for direct handling, otherwise we'll get linker errors. Lumiera uses the `‘inclusion model’' for template instantiation, which means there is no compiler magic involved and a template function either needs to be defined in a header or explicitly instantiated in some translation unit.

Definition at line 154 of file format-string.cpp.

◆ operator string()

operator string ( ) const

get the formatted result

Remarks
usually the _Fmt helper is used inline at places where a string is expected. The '' operator used to fed parameters has a higher precedence than the assignment or comparison operators, ensuring that all parameters are evaluated and formatted prior to receiving the formatted result
Note
EX_FREE

Definition at line 213 of file format-string.cpp.

◆ operator%()

template<typename VAL >
_Fmt & operator% ( VAL const &  val)
inline

The percent operator ('' ) is used do feed parameter values to be included into the formatted result, at the positions marked by printf-style placeholders within the format string.

type specific treatment
Basic types (numbers, chars, strings) are passed to the implementation (= boost::format) literally. For custom types, we try to use a custom string conversion, if applicable. Non-NULL pointers will be dereferenced, with the exception of C-Strings and void*. Any other type gets just translated into a type-ID (using the mangled RTTI info). In case of errors during the conversion, a string representation of the error is returned
Parameters
valarbitrary value or pointer to be included into the result
Warning
you need to provide exactly the right number of parameters, i.e. matching the number of fields in the format string.
Note
EX_FREE

Definition at line 221 of file format-string.hpp.

References _Fmt::Converter< VAL, SEL >::dump(), and _Fmt::formatter_.

+ Here is the call graph for this function:

Member Data Documentation

◆ formatter_

Implementation formatter_
mutableprivate

buffer to hold a boost::format

Definition at line 158 of file format-string.hpp.

Referenced by _Fmt::_Fmt(), _Fmt::~_Fmt(), and _Fmt::operator%().

Friends And Related Symbol Documentation

◆ operator<<

std::ostream & operator<< ( std::ostream &  os,
_Fmt const &  fmt 
)
friend

send the formatted buffer directly to the output stream.

Note
this is more efficient than creating a string and outputting that, because boost::format internally uses a string-stream to generate the formatted representation, relying on the C++ output framework

Definition at line 238 of file format-string.cpp.

◆ operator== [1/5]

bool operator== ( _Fmt const &  left,
_Fmt const &  right 
)
friend

Definition at line 464 of file format-string.hpp.

◆ operator== [2/5]

bool operator== ( _Fmt const &  fmt,
string const &  str 
)
friend

Definition at line 470 of file format-string.hpp.

◆ operator== [3/5]

bool operator== ( _Fmt const &  fmt,
CStr const  cString 
)
friend

Definition at line 476 of file format-string.hpp.

◆ operator== [4/5]

bool operator== ( string const &  str,
_Fmt const &  fmt 
)
friend

Definition at line 482 of file format-string.hpp.

◆ operator== [5/5]

bool operator== ( CStr const  cString,
_Fmt const &  fmt 
)
friend

Definition at line 488 of file format-string.hpp.

◆ operator!= [1/2]

template<typename X >
bool operator!= ( _Fmt const &  fmt,
X const &  x 
)
friend

Definition at line 192 of file format-string.hpp.

◆ operator!= [2/2]

template<typename X >
bool operator!= ( X const &  x,
_Fmt const &  fmt 
)
friend

Definition at line 194 of file format-string.hpp.

+ Inheritance diagram for _Fmt:
+ Collaboration diagram for _Fmt:

The documentation for this class was generated from the following files: