The Gaudi Framework  master (e68eea06)
Loading...
Searching...
No Matches
PropertyFmt.h
Go to the documentation of this file.
1/***********************************************************************************\
2* (c) Copyright 2023-2025 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 <format>
57#include <iomanip>
58#include <sstream>
59
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 };
89
90GAUDI_PROPERTY_FORMATTING_IMPL( fmt, static_assert( true ) )
91
92#ifndef __cpp_lib_format_ranges
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" )
96#else
97# define assert_for_ranges static_assert( true )
98#endif
99
101
102#undef assert_for_ranges
103#undef GAUDI_PROPERTY_FORMATTING_IMPL
#define GAUDI_PROPERTY_FORMATTING_IMPL(ns, assert_for_ranges)
Definitions to allow use of Gaudi::Property<T> with fmtlib.
Definition PropertyFmt.h:60
#define assert_for_ranges
Definition PropertyFmt.h:93
STL namespace.