The Gaudi Framework  master (3415b466)
Loading...
Searching...
No Matches
PropertyFmt.h
Go to the documentation of this file.
1/***********************************************************************************\
2* (c) Copyright 2023-2026 CERN for the benefit of the LHCb and ATLAS collaborations *
3* *
4* This software is distributed under the terms of the Apache version 2 licence, *
5* copied verbatim in the file "LICENSE". *
6* *
7* In applying this licence, CERN does not waive the privileges and immunities *
8* granted to it by virtue of its status as an Intergovernmental Organization *
9* or submit itself to any jurisdiction. *
10\***********************************************************************************/
11#pragma once
12
53
54#include <Gaudi/Property.h>
55#include <fmt/format.h>
56#include <fmt/ranges.h>
57#include <format>
58#include <iomanip>
59#include <sstream>
60
61// Disable fmt's automatic range formatting for Gaudi::Property
62// This prevents ambiguity with fmt 11+ which auto-detects ranges
63template <typename T, typename V, typename H, typename Char>
64struct fmt::is_range<Gaudi::Property<T, V, H>, Char> : std::false_type {};
65
66#define GAUDI_PROPERTY_FORMATTING_IMPL( ns, assert_for_ranges ) \
67 template <typename T, typename V, typename H> \
68 struct ns::formatter<Gaudi::Property<T, V, H>> : ns::formatter<T> { \
69 assert_for_ranges; \
70 bool debug = false; \
71 constexpr auto parse( ns::format_parse_context& ctx ) { \
72 auto it = ctx.begin(), end = ctx.end(); \
73 if ( it != end && *it == '?' ) { \
74 debug = true; \
75 ++it; \
76 if ( it != end && *it != '}' ) throw ns::format_error( "invalid format" ); \
77 return it; \
78 } \
79 return ns::formatter<T>::parse( ctx ); \
80 } \
81 auto format( const Gaudi::Property<T, V, H>& p, ns::format_context& ctx ) const { \
82 if ( debug ) { \
83 if constexpr ( std::is_same_v<T, std::string> ) { \
84 std::stringstream s; \
85 s << std::quoted( p.value(), '\'' ); \
86 return ns::format_to( ctx.out(), " '{}':{}", p.name(), s.str() ); \
87 } else { \
88 return ns::format_to( ctx.out(), " '{}':{}", p.name(), p.value() ); \
89 } \
90 } else { \
91 return ns::formatter<T>::format( static_cast<const T&>( p ), ctx ); \
92 } \
93 } \
94 };
95
96GAUDI_PROPERTY_FORMATTING_IMPL( fmt, static_assert( true ) )
97
98#ifndef __cpp_lib_format_ranges
99# define assert_for_ranges \
100 static_assert( !std::ranges::range<T> || std::is_same_v<T, std::string>, \
101 "Range-valued Property formatting is not supported with std::format in C++20" )
102#else
103# define assert_for_ranges static_assert( true )
104#endif
105
107
108#undef assert_for_ranges
109#undef GAUDI_PROPERTY_FORMATTING_IMPL
#define GAUDI_PROPERTY_FORMATTING_IMPL(ns, assert_for_ranges)
Definition PropertyFmt.h:66
#define assert_for_ranges
Definition PropertyFmt.h:99
This file provides a Grammar for the type Gaudi::Accumulators::Axis It allows to use that type from p...
Definition __init__.py:1
STL namespace.