The Gaudi Framework  master (e68eea06)
Loading...
Searching...
No Matches
PropertyFmt.h File Reference
#include <Gaudi/Property.h>
#include <fmt/format.h>
#include <format>
#include <iomanip>
#include <sstream>
Include dependency graph for PropertyFmt.h:

Go to the source code of this file.

Macros

#define GAUDI_PROPERTY_FORMATTING_IMPL(ns, assert_for_ranges)
 Definitions to allow use of Gaudi::Property<T> with fmtlib.
 
#define assert_for_ranges
 

Macro Definition Documentation

◆ assert_for_ranges

#define assert_for_ranges
Value:
static_assert( !std::ranges::range<T> || std::is_same_v<T, std::string>, \
"Range-valued Property formatting is not supported with std::format in C++20" )

Definition at line 93 of file PropertyFmt.h.

93# define assert_for_ranges \
94 static_assert( !std::ranges::range<T> || std::is_same_v<T, std::string>, \
95 "Range-valued Property formatting is not supported with std::format in C++20" )

◆ GAUDI_PROPERTY_FORMATTING_IMPL

#define GAUDI_PROPERTY_FORMATTING_IMPL ( ns,
assert_for_ranges )
Value:
template <typename T, typename V, typename H> \
struct ns::formatter<Gaudi::Property<T, V, H>> : ns::formatter<T> { \
bool debug = false; \
constexpr auto parse( ns::format_parse_context& ctx ) { \
auto it = ctx.begin(), end = ctx.end(); \
if ( it != end && *it == '?' ) { \
debug = true; \
++it; \
if ( it != end && *it != '}' ) throw ns::format_error( "invalid format" ); \
return it; \
} \
return ns::formatter<T>::parse( ctx ); \
} \
auto format( const Gaudi::Property<T, V, H>& p, ns::format_context& ctx ) const { \
if ( debug ) { \
if constexpr ( std::is_same_v<T, std::string> ) { \
std::stringstream s; \
s << std::quoted( p.value(), '\'' ); \
return ns::format_to( ctx.out(), " '{}':{}", p.name(), s.str() ); \
} else { \
return ns::format_to( ctx.out(), " '{}':{}", p.name(), p.value() ); \
} \
} else { \
return ns::formatter<T>::format( static_cast<const T&>( p ), ctx ); \
} \
} \
};
StatusCode parse(DataObjID &dest, std::string_view src)
Definition DataObjID.cpp:58
GAUDI_API std::string format(const char *,...)
MsgStream format utility "a la sprintf(...)".
Definition MsgStream.cpp:93
#define assert_for_ranges
Definition PropertyFmt.h:93
This file provides a Grammar for the type Gaudi::Accumulators::Axis It allows to use that type from p...
Definition __init__.py:1

Definitions to allow use of Gaudi::Property<T> with fmtlib.

When this header is included, one can pass a Gaudi::Property<T> instance as argument to fmt::format.

For example

#include <Gaudi/Property.h>
#include <fmt/format.h>
int main() {
Gaudi::Property<int> p{ "MyProp", 20 };
fmt::print( "property {} has value {}\n", p.name(), p );
}
int main()
Implementation of property with value of concrete type.
Definition PropertyFwd.h:27

will print

property MyProp has value 20

A special formatting option can be used to print the property name as well as the value, so

#include <Gaudi/Property.h>
#include <fmt/format.h>
#include <iostream>
int main() {
Gaudi::Property<int> p{ "MyProp", 20 };
std::cout << "cout" << p << '\n';
fmt::print( "fmt {:?}\n", p );
}

will print

cout 'MyProp':20
fmt 'MyProp':20

Definition at line 60 of file PropertyFmt.h.

60#define GAUDI_PROPERTY_FORMATTING_IMPL( ns, assert_for_ranges ) \
61 template <typename T, typename V, typename H> \
62 struct ns::formatter<Gaudi::Property<T, V, H>> : ns::formatter<T> { \
63 assert_for_ranges; \
64 bool debug = false; \
65 constexpr auto parse( ns::format_parse_context& ctx ) { \
66 auto it = ctx.begin(), end = ctx.end(); \
67 if ( it != end && *it == '?' ) { \
68 debug = true; \
69 ++it; \
70 if ( it != end && *it != '}' ) throw ns::format_error( "invalid format" ); \
71 return it; \
72 } \
73 return ns::formatter<T>::parse( ctx ); \
74 } \
75 auto format( const Gaudi::Property<T, V, H>& p, ns::format_context& ctx ) const { \
76 if ( debug ) { \
77 if constexpr ( std::is_same_v<T, std::string> ) { \
78 std::stringstream s; \
79 s << std::quoted( p.value(), '\'' ); \
80 return ns::format_to( ctx.out(), " '{}':{}", p.name(), s.str() ); \
81 } else { \
82 return ns::format_to( ctx.out(), " '{}':{}", p.name(), p.value() ); \
83 } \
84 } else { \
85 return ns::formatter<T>::format( static_cast<const T&>( p ), ctx ); \
86 } \
87 } \
88 };