Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-12-15 10:14:15

0001 /***********************************************************************************\
0002 * (c) Copyright 2023 CERN for the benefit of the LHCb and ATLAS collaborations      *
0003 *                                                                                   *
0004 * This software is distributed under the terms of the Apache version 2 licence,     *
0005 * copied verbatim in the file "LICENSE".                                            *
0006 *                                                                                   *
0007 * In applying this licence, CERN does not waive the privileges and immunities       *
0008 * granted to it by virtue of its status as an Intergovernmental Organization        *
0009 * or submit itself to any jurisdiction.                                             *
0010 \***********************************************************************************/
0011 #pragma once
0012 
0013 /** Definitions to allow use of Gaudi::Property<T> with fmtlib.
0014  *
0015  * When this header is included, one can pass a Gaudi::Property<T> instance as argument to `fmt::format`.
0016  *
0017  * For example
0018  * ```cpp
0019  * #include <Gaudi/Property.h>
0020  * #include <Gaudi/PropertyFmt.h>
0021  * #include <fmt/format.h>
0022  *
0023  * int main() {
0024  *   Gaudi::Property<int> p{ "MyProp", 20 };
0025  *   fmt::print( "property {} has value {}\n", p.name(), p );
0026  * }
0027  * ```
0028  * will print
0029  * ```
0030  * property MyProp has value 20
0031  * ```
0032  *
0033  * A special formatting option can be used to print the property name as well as the value, so
0034  * ```cpp
0035  * #include <Gaudi/Property.h>
0036  * #include <Gaudi/PropertyFmt.h>
0037  * #include <fmt/format.h>
0038  * #include <iostream>
0039  *
0040  * int main() {
0041  *   Gaudi::Property<int> p{ "MyProp", 20 };
0042  *
0043  *   std::cout << "cout" << p << '\n';
0044  *   fmt::print( "fmt {:?}\n", p );
0045  * }
0046  * ```
0047  * will print
0048  * ```
0049  * cout 'MyProp':20
0050  * fmt  'MyProp':20
0051  * ```
0052  */
0053 
0054 #include <Gaudi/Property.h>
0055 #include <fmt/format.h>
0056 #include <iomanip>
0057 #include <sstream>
0058 
0059 template <typename T, typename V, typename H>
0060 struct fmt::formatter<Gaudi::Property<T, V, H>> : formatter<T> {
0061   bool           debug = false;
0062   constexpr auto parse( format_parse_context& ctx ) -> format_parse_context::iterator {
0063     auto it = ctx.begin(), end = ctx.end();
0064     if ( it != end && *it == '?' ) {
0065       debug = true;
0066       ++it;
0067       if ( it != end && *it != '}' )
0068 #if FMT_VERSION >= 110000
0069         report_error( "invalid format" );
0070 #else
0071         detail::throw_format_error( "invalid format" );
0072 #endif
0073       return it;
0074     }
0075     return formatter<T>::parse( ctx );
0076   }
0077   auto format( const Gaudi::Property<T, V, H>& p, format_context& ctx ) const {
0078     if ( debug ) {
0079       if constexpr ( std::is_same_v<T, std::string> ) {
0080         std::stringstream s;
0081         s << std::quoted( p.value(), '\'' );
0082         return fmt::format_to( ctx.out(), " '{}':{}", p.name(), s.str() );
0083       } else {
0084         return fmt::format_to( ctx.out(), " '{}':{}", p.name(), p.value() );
0085       }
0086     } else {
0087       return formatter<T>::format( static_cast<const T&>( p ), ctx );
0088     }
0089   }
0090 };