My Project
fmt.hpp
Go to the documentation of this file.
1 
28 #ifndef BSL_FMT_HPP
29 #define BSL_FMT_HPP
30 
31 #include "details/fmt_impl_char_type.hpp"
32 #include "details/fmt_impl_cstr_type.hpp"
33 #include "details/fmt_impl_bool.hpp"
34 #include "details/fmt_impl_integral.hpp"
35 #include "details/fmt_impl_null_pointer.hpp"
36 #include "details/fmt_impl_void_pointer.hpp"
37 
38 #include "details/out.hpp"
39 
40 #include "cstdint.hpp"
41 #include "enable_if.hpp"
42 #include "forward.hpp"
43 #include "fmt_options.hpp"
44 #include "is_bool.hpp"
45 #include "is_integral.hpp"
46 #include "is_null_pointer.hpp"
47 #include "is_same.hpp"
48 #include "is_pointer.hpp"
49 #include "move.hpp"
50 
51 namespace bsl
52 {
327  template<typename V>
328  class fmt final
329  {
331  fmt_options m_ops;
333  V const &m_val;
334 
335  public:
346  constexpr fmt(fmt_options const &ops, V const &val) noexcept // --
347  : m_ops{ops}, m_val{val}
348  {}
349 
364  constexpr fmt(fmt_options const &ops, V const &val, bsl::uintmax width) noexcept
365  : m_ops{ops}, m_val{val}
366  {
367  constexpr bsl::uintmax max_width{1000U};
368  if (width > max_width) {
369  width = max_width - 1U;
370  }
371 
372  m_ops.set_width(width);
373  }
374 
385  constexpr fmt(cstr_type const str, V const &val) noexcept // --
386  : fmt{fmt_options{str}, val}
387  {}
388 
403  constexpr fmt(cstr_type const str, V const &val, bsl::uintmax const width) noexcept
404  : fmt{fmt_options{str}, val, width}
405  {}
406 
426  template<typename T, typename U>
427  friend constexpr out<T>
428  operator<<(out<T> const o, fmt<U> &&arg) noexcept; // PRQA S 2107 // NOLINT
429  };
430 
446  template<typename T, typename U>
447  [[maybe_unused]] constexpr out<T>
448  operator<<(out<T> const o, fmt<U> &&arg) noexcept
449  {
450  if constexpr (o.empty()) {
451  return o;
452  }
453 
454  fmt_impl(o, arg.m_ops, arg.m_val); // NOLINT
455  return o;
456  }
457 
472  template<
473  typename T,
474  typename U,
475  enable_if_t<!is_bool<U>::value, bool> = true,
477  enable_if_t<!is_pointer<U>::value, bool> = true,
480  [[maybe_unused]] constexpr out<T>
481  operator<<(out<T> const o, U const &arg) noexcept
482  {
483  if constexpr (o.empty()) {
484  return o;
485  }
486 
487  fmt_impl(o, nullops, arg); // NOLINT
488  return o;
489  }
490 }
491 
492 #endif
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 provid...
Definition: fmt.hpp:346
typename enable_if< B, T >::type enable_if_t
a helper that reduces the verbosity of bsl::enable_if
Definition: enable_if.hpp:52
The bsl::fmt implements a similar syntax to that of std::format, We adopt a similar approach with som...
Definition: fmt.hpp:328
Used by fmt to determine how to format the output of an fmt command. See the documentation fo bsl::fm...
Definition: fmt_options.hpp:141
char const * cstr_type
C-style string type.
Definition: cstr_type.hpp:39
constexpr fmt_options nullops
defines no formatting.
Definition: fmt_options.hpp:648
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 provid...
Definition: fmt.hpp:385
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 provid...
Definition: fmt.hpp:364
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 provid...
Definition: fmt.hpp:403
friend constexpr out< T > operator<<(out< T > const o, fmt< U > &&arg) noexcept
Outputs the provided formatted argument to the provided output type.
::uintmax_t uintmax
defines a unsigned integer with the maximum possible size
Definition: cstdint.hpp:97