My Project
Public Member Functions | Related Functions | List of all members
bsl::fmt< V > Class Template Referencefinal

The bsl::fmt implements a similar syntax to that of std::format, We adopt a similar approach with some tweaks of course to ensure AUTOSAR compliance include: More...

#include <fmt.hpp>

Public Member Functions

constexpr fmt (fmt_options const &ops, V const &val) noexcept
 Creates a bsl::fmt, which when passed to an outputter will output the provided value given the provided format string. More...
 
constexpr fmt (fmt_options const &ops, V const &val, bsl::uintmax width) noexcept
 Creates a bsl::fmt, which when passed to an outputter will output the provided value given the provided format string. Note that this version also accepts a dynamic width, meaning the width can be determined at runtime. If the width is provided, the width in the format string is ignored. More...
 
constexpr fmt (cstr_type const str, V const &val) noexcept
 Creates a bsl::fmt, which when passed to an outputter will output the provided value given the provided format string. More...
 
constexpr fmt (cstr_type const str, V const &val, bsl::uintmax const width) noexcept
 Creates a bsl::fmt, which when passed to an outputter will output the provided value given the provided format string. Note that this version also accepts a dynamic width, meaning the width can be determined at runtime. If the width is provided, the width in the format string is ignored. More...
 

Related Functions

(Note that these are not member functions.)

template<typename T , typename U >
constexpr out< T > operator<< (out< T > const o, fmt< U > &&arg) noexcept
 Outputs the provided formatted argument to the provided output type. More...
 
template<typename T , typename U >
constexpr out< T > operator<< (out< T > const o, fmt< U > &&arg) noexcept
 Outputs the provided formatted argument to the provided output type. If you want to provide your own custom outputter, DO NOT overload this function. Instead, overload the fmt_impl function. More...
 
template<typename T , typename U , enable_if_t<!is_bool< U >::value, bool > = true, enable_if_t<!is_same< U, char_type >::value, bool > = true, enable_if_t<!is_pointer< U >::value, bool > = true, enable_if_t<!is_integral< U >::value, bool > = true, enable_if_t<!is_null_pointer< U >::value, bool > = true>
constexpr out< T > operator<< (out< T > const o, U const &arg) noexcept
 Outputs the provided argument to the provided output type. If you want to provide your own custom outputter, DO NOT overload this function. Instead, overload the fmt_impl function. More...
 

Detailed Description

template<typename V>
class bsl::fmt< V >

The bsl::fmt implements a similar syntax to that of std::format, We adopt a similar approach with some tweaks of course to ensure AUTOSAR compliance include:

General Syntax:

fill-and-align(optional) sign(optional) #(optional) 0(optional) width(optional) type(optional)

The sign, # and 0 options are only valid when an integer type is being formatted.

Rules for Optional Field [fill-and-align]:
The fill-and-align option tells bsl::fmt how to align the resulting output. The fill states what character should be used as padding (defaults to a space), and can be any character (except for '\0' which would result in UB). The align character determines the type of justification (either left, right or center). If the width field is missing, the fill-and-align field has no effect. In addition, if this field is combined with the '0' sign aware field, this field has no effect.

#include <bsl/debug.hpp>
namespace bsl
{
inline void
example_fmt_align() noexcept
{
bsl::print() << bsl::fmt{"<10", "42"} << bsl::endl;
bsl::print() << bsl::fmt{">10", "42"} << bsl::endl;
bsl::print() << bsl::fmt{"^10", "42"} << bsl::endl;
bsl::print() << bsl::fmt{".<10", "42"} << bsl::endl;
bsl::print() << bsl::fmt{".>10", "42"} << bsl::endl;
bsl::print() << bsl::fmt{".^10", "42"} << bsl::endl;
bsl::print() << bsl::fmt{"=<30", '='} << bsl::endl;
bsl::print() << bsl::fmt{"-<30", '-'} << bsl::endl;
bsl::print() << bsl::fmt{"_<30", '_'} << bsl::endl;
}
}

Results in the following output:

42
42
42
42........
........42
....42....
==============================
------------------------------
______________________________

Rules for Optional Field [sign]:
The sign option states how an integer type should display its positive or negative sign.

#include <bsl/debug.hpp>
namespace bsl
{
inline void
example_fmt_sign() noexcept
{
constexpr bsl::int32 val1{42};
constexpr bsl::int32 val2{-42};
bsl::print() << bsl::fmt{"+", val1} << bsl::endl;
bsl::print() << bsl::fmt{"+", val2} << bsl::endl;
bsl::print() << bsl::fmt{"-", val1} << bsl::endl;
bsl::print() << bsl::fmt{"-", val2} << bsl::endl;
bsl::print() << bsl::fmt{" ", val1} << bsl::endl;
bsl::print() << bsl::fmt{" ", val2} << bsl::endl;
}
}

Results in the following output:

+42
-42
42
-42
42
-42

Rules for Optional Field [#]:
The # option enables the alternative form for integer types. If the type field is missing, this option is ignored.

#include <bsl/debug.hpp>
namespace bsl
{
inline void
example_fmt_alt_form() noexcept
{
constexpr bsl::int32 val{42};
bsl::print() << bsl::fmt{"#b", val} << bsl::endl;
bsl::print() << bsl::fmt{"#d", val} << bsl::endl;
bsl::print() << bsl::fmt{"#x", val} << bsl::endl;
}
}

Results in the following output:

0b101010
42
0x2A

Rules for Optional Field [0]:
The 0 option enables the use of sign aware 0 padding. If this field is combined with the fill-and-align field, the fill-and-align field is ignored. The problem with fill-and-align is that if you want to, for example, output the hexidecimal number 0x2A as 0x002A, there is no way to do that with fill-and-align as the 0s would be on the wrong side of '0x'. Therefore, this option ignores fill-and-align and instead uses the width field to determine how many 0s to add to the output in the correct position within the output. Like fill-and-align, if the width field is missing, this option has no effect.

#include <bsl/debug.hpp>
namespace bsl
{
inline void
example_fmt_sign_aware() noexcept
{
constexpr bsl::int32 val{42};
bsl::print() << bsl::fmt{"#10b", val} << bsl::endl;
bsl::print() << bsl::fmt{"#10x", val} << bsl::endl;
bsl::print() << bsl::fmt{"#010b", val} << bsl::endl;
bsl::print() << bsl::fmt{"#010x", val} << bsl::endl;
}
}

Results in the following output:

0b101010
0x2A
0b00101010
0x0000002A

Rules for Optional Field [width]:
Unlike std::format, negative numbers are not supported. The width field determines the total length of the resulting output, with all options included. In addition, the bsl::fmt library supports dynamic width as well. If the dynamic width is provided, this field is ignored and the dynamic width is used instead.

#include <bsl/debug.hpp>
namespace bsl
{
inline void
example_fmt_width() noexcept
{
constexpr bsl::int32 val{42};
constexpr bsl::uintmax width{10U};
bsl::print() << bsl::fmt{"<10", val} << bsl::endl;
bsl::print() << bsl::fmt{">10", val} << bsl::endl;
bsl::print() << bsl::fmt{"^10", val} << bsl::endl;
bsl::print() << bsl::fmt{"#010b", val} << bsl::endl;
bsl::print() << bsl::fmt{"#010x", val} << bsl::endl;
bsl::print() << bsl::fmt{"#0b", val, width} << bsl::endl;
bsl::print() << bsl::fmt{"#0x", val, width} << bsl::endl;
}
}

Results in the following output:

42
42
42
0b00101010
0x0000002A
0b00101010
0x0000002A

type rules [bool]:
If bsl::fmt{} is given a boolean, the type field indicates the following:

#include <bsl/debug.hpp>
namespace bsl
{
inline void
example_fmt_bool() noexcept
{
bsl::print() << bsl::fmt{"", true} << bsl::endl;
bsl::print() << bsl::fmt{"", false} << bsl::endl;
bsl::print() << bsl::fmt{"d", true} << bsl::endl;
bsl::print() << bsl::fmt{"d", false} << bsl::endl;
}
}

Results in the following output:

true
false
1
0

type rules [bsl::char_type]:
If bsl::fmt{} is given a bsl::char_type, the type field indicates the following:

#include <bsl/debug.hpp>
namespace bsl
{
inline void
example_fmt_char_type() noexcept
{
constexpr char_type val{'*'};
bsl::print() << bsl::fmt{"", val} << bsl::endl;
bsl::print() << bsl::fmt{"b", val} << bsl::endl;
bsl::print() << bsl::fmt{"d", val} << bsl::endl;
bsl::print() << bsl::fmt{"x", val} << bsl::endl;
}
}

Results in the following output:

*
101010
42
2A

type rules [bsl::cstr_type]:
If bsl::fmt{} is given a bsl::cstr_type, the type field indicates the following:

#include <bsl/debug.hpp>
namespace bsl
{
inline void
example_fmt_cstr_type() noexcept
{
bsl::print() << bsl::fmt{"", "success"} << bsl::endl;
}
}

Results in the following output:

success

type rules [integral]:
If bsl::fmt{} is given an integral type, the type field indicates the following:

#include <bsl/debug.hpp>
namespace bsl
{
inline void
example_fmt_integral() noexcept
{
constexpr bsl::int32 val1{42};
constexpr bsl::int32 val2{-42};
bsl::print() << bsl::fmt{"", val1} << bsl::endl;
bsl::print() << bsl::fmt{"b", val1} << bsl::endl;
bsl::print() << bsl::fmt{"c", val1} << bsl::endl;
bsl::print() << bsl::fmt{"x", val1} << bsl::endl;
bsl::print() << bsl::fmt{"#b", val1} << bsl::endl;
bsl::print() << bsl::fmt{"#x", val1} << bsl::endl;
bsl::print() << bsl::fmt{"#08b", val1} << bsl::endl;
bsl::print() << bsl::fmt{"#08x", val1} << bsl::endl;
bsl::print() << bsl::fmt{"+", val1} << bsl::endl;
bsl::print() << bsl::fmt{"+", val2} << bsl::endl;
bsl::print() << bsl::fmt{"-", val1} << bsl::endl;
bsl::print() << bsl::fmt{"-", val2} << bsl::endl;
}
}

Results in the following output:

42
101010
*
2A
0b101010
0x2A
0b101010
0x00002A
+42
-42
42
-42

For all other types, the BSL provides the "<<" syntax, but the use of bsl::fmt is not supported.

If you wish to implement support for your own types, you can do so why overloading the following function in the "bsl" namespace

template<typename OUT>
constexpr void
fmt_impl(OUT &&o, fmt_options const &ops, <type> const &val) noexcept
{
...
}

You can also overload the following if you wish to provide output support but do not wish to provide bsl::fmt{} support (this option will result in more efficient code):

template<typename T>
[[maybe_unused]] constexpr out<T>
operator<<(out<T> const o, <type> const &val) noexcept
{
if constexpr (o.empty()) {
return o;
}
...
}
Template Parameters
Vthe type of value being formatted for output

Constructor & Destructor Documentation

◆ fmt() [1/4]

template<typename V >
constexpr bsl::fmt< V >::fmt ( fmt_options const &  ops,
V const &  val 
)
inlinenoexcept

Creates a bsl::fmt, which when passed to an outputter will output the provided value given the provided format string.

#include <bsl/debug.hpp>
namespace bsl
{
inline void
example_fmt_constructor_f_val() noexcept
{
bsl::print() << bsl::fmt{".<23", '.'} << bsl::endl;
bsl::print() << bsl::fmt{".<42", '.'} << bsl::endl;
}
}
Parameters
opsthe format options used to format the output of val
valthe value to output given the provided format string

◆ fmt() [2/4]

template<typename V >
constexpr bsl::fmt< V >::fmt ( fmt_options const &  ops,
V const &  val,
bsl::uintmax  width 
)
inlinenoexcept

Creates a bsl::fmt, which when passed to an outputter will output the provided value given the provided format string. Note that this version also accepts a dynamic width, meaning the width can be determined at runtime. If the width is provided, the width in the format string is ignored.

#include <bsl/debug.hpp>
namespace bsl
{
inline void
example_fmt_constructor_f_val_width() noexcept
{
constexpr bsl::uintmax width1{23U};
constexpr bsl::uintmax width2{42U};
bsl::print() << bsl::fmt{".<", '.', width1} << bsl::endl;
bsl::print() << bsl::fmt{".<", '.', width2} << bsl::endl;
}
}
Parameters
opsthe format options used to format the output of val
valthe value to output given the provided format string
widtha dynamic width which overrides the width field in the format string (used to set the width field at runtime).

◆ fmt() [3/4]

template<typename V >
constexpr bsl::fmt< V >::fmt ( cstr_type const  str,
V const &  val 
)
inlinenoexcept

Creates a bsl::fmt, which when passed to an outputter will output the provided value given the provided format string.

#include <bsl/debug.hpp>
namespace bsl
{
inline void
example_fmt_constructor_f_val() noexcept
{
bsl::print() << bsl::fmt{".<23", '.'} << bsl::endl;
bsl::print() << bsl::fmt{".<42", '.'} << bsl::endl;
}
}
Parameters
strthe format options used to format the output of val
valthe value to output given the provided format string

◆ fmt() [4/4]

template<typename V >
constexpr bsl::fmt< V >::fmt ( cstr_type const  str,
V const &  val,
bsl::uintmax const  width 
)
inlinenoexcept

Creates a bsl::fmt, which when passed to an outputter will output the provided value given the provided format string. Note that this version also accepts a dynamic width, meaning the width can be determined at runtime. If the width is provided, the width in the format string is ignored.

#include <bsl/debug.hpp>
namespace bsl
{
inline void
example_fmt_constructor_f_val_width() noexcept
{
constexpr bsl::uintmax width1{23U};
constexpr bsl::uintmax width2{42U};
bsl::print() << bsl::fmt{".<", '.', width1} << bsl::endl;
bsl::print() << bsl::fmt{".<", '.', width2} << bsl::endl;
}
}
Parameters
strthe format options used to format the output of val
valthe value to output given the provided format string
widtha dynamic width which overrides the width field in the format string (used to set the width field at runtime).

Friends And Related Function Documentation

◆ operator<< [1/3]

template<typename V >
template<typename T , typename U >
constexpr out<T> operator<< ( out< T > const  o,
fmt< U > &&  arg 
)
friend

Outputs the provided formatted argument to the provided output type.

Note
We make this a friend of bsl::fmt as there is no way to define a stream operator as a member function. As a result, this would require the use of a public member function that would expose the const&, potentially leading to UB.
Template Parameters
Tthe type of outputter provided
Uthe type of value being ouputter using bsl::fmt
Parameters
othe instance of the outputter used to output the value.
arga bsl::fmt that contains the value being outputted as well as any format instructions.
Returns
return o

◆ operator<<() [2/3]

template<typename T , typename U >
constexpr out< T > operator<< ( out< T > const  o,
fmt< U > &&  arg 
)
related

Outputs the provided formatted argument to the provided output type. If you want to provide your own custom outputter, DO NOT overload this function. Instead, overload the fmt_impl function.

Template Parameters
Tthe type of outputter provided
Uthe type of value being ouputter using bsl::fmt
Parameters
othe instance of the outputter used to output the value.
arga bsl::fmt that contains the value being outputted as well as any format instructions.
Returns
return o

◆ operator<<() [3/3]

template<typename T , typename U , enable_if_t<!is_bool< U >::value, bool > = true, enable_if_t<!is_same< U, char_type >::value, bool > = true, enable_if_t<!is_pointer< U >::value, bool > = true, enable_if_t<!is_integral< U >::value, bool > = true, enable_if_t<!is_null_pointer< U >::value, bool > = true>
constexpr out< T > operator<< ( out< T > const  o,
U const &  arg 
)
related

Outputs the provided argument to the provided output type. If you want to provide your own custom outputter, DO NOT overload this function. Instead, overload the fmt_impl function.

Template Parameters
Tthe type of outputter provided
Uthe type of value being ouputter using bsl::fmt
Parameters
othe instance of the outputter used to output the value.
argthe value to output
Returns
return o

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