Lumiera  0.pre.03
»edit your freedom«
_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 157 of file format-string.hpp.

Public Member Functions

 _Fmt (string formatString)
 Build a formatter object based on the given format string. More...
 
 operator string () const
 get the formatted result More...
 
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. More...
 

Classes

struct  Converter
 helper to prepare parameters for inclusion More...
 
struct  Converter< bool >
 
struct  Converter< const char * >
 
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 * >
 

Friends

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

Private Types

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

Static Private Member Functions

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

Private Attributes

Implementation formatter_
 

Additional Inherited Members

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

Constructor & Destructor Documentation

◆ _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 123 of file format-string.cpp.

Member Function Documentation

◆ format()

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 163 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 222 of file format-string.cpp.

◆ operator%()

_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 230 of file format-string.hpp.

Member Data Documentation

◆ formatter_

Implementation formatter_
mutableprivate

buffer to hold a boost::format

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

Friends And Related Function 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 248 of file format-string.cpp.

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

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