The Gaudi Framework  master (9f3f8895)
Loading...
Searching...
No Matches
CommonParsers.h
Go to the documentation of this file.
1/***********************************************************************************\
2* (c) Copyright 1998-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
14#include <GaudiKernel/Map.h>
16#include <list>
17#include <map>
18#include <nlohmann/json_fwd.hpp>
19#include <set>
20#include <string>
21#include <unordered_set>
22#include <vector>
23
24#define PARSERS_DECL_FOR_SINGLE( Type ) GAUDI_API StatusCode parse( Type& result, std::string_view input );
25
26#define PARSERS_DECL_FOR_PAIR( FirstType, SecondType ) \
27 GAUDI_API StatusCode parse( std::pair<FirstType, SecondType>& result, std::string_view input );
28
29#define PARSERS_DECL_FOR_LIST( InnerType ) \
30 GAUDI_API StatusCode parse( std::vector<InnerType>& result, std::string_view input );
31
32#define PARSERS_DECL_FOR_SET( InnerType ) \
33 GAUDI_API StatusCode parse( std::set<InnerType>& result, std::string_view input ); \
34 GAUDI_API StatusCode parse( std::unordered_set<InnerType>& result, std::string_view input );
35
36#define PARSERS_DECL_FOR_STRMAP( ValueType ) \
37 GAUDI_API StatusCode parse( std::map<std::string, ValueType>& result, std::string_view input ); \
38 GAUDI_API StatusCode parse( std::map<std::string, ValueType, std::less<>>& result, std::string_view input );
39
71namespace Gaudi {
72 class Histo1DDef;
73 namespace Parsers {
98 PARSERS_DECL_FOR_SINGLE( unsigned char )
100 PARSERS_DECL_FOR_SINGLE( signed char )
116 PARSERS_DECL_FOR_SINGLE( unsigned short )
118 PARSERS_DECL_FOR_SINGLE( unsigned int )
122 PARSERS_DECL_FOR_SINGLE( unsigned long )
124 PARSERS_DECL_FOR_SINGLE( long long )
126 PARSERS_DECL_FOR_SINGLE( unsigned long long )
142 PARSERS_DECL_FOR_SINGLE( long double )
154 PARSERS_DECL_FOR_SINGLE( std::string )
155 PARSERS_DECL_FOR_SINGLE( nlohmann::json )
156
159 PARSERS_DECL_FOR_LIST( unsigned char )
160 PARSERS_DECL_FOR_LIST( signed char )
161
163 PARSERS_DECL_FOR_LIST( short )
164 PARSERS_DECL_FOR_LIST( unsigned short )
165 PARSERS_DECL_FOR_LIST( unsigned int )
167 PARSERS_DECL_FOR_LIST( unsigned long )
168 PARSERS_DECL_FOR_LIST( long long )
169 PARSERS_DECL_FOR_LIST( unsigned long long )
170
171 PARSERS_DECL_FOR_LIST( double )
172 PARSERS_DECL_FOR_LIST( float )
173 PARSERS_DECL_FOR_LIST( long double )
174
175 PARSERS_DECL_FOR_LIST( std::string )
176
179 PARSERS_DECL_FOR_SET( unsigned char )
180 PARSERS_DECL_FOR_SET( signed char )
181
183 PARSERS_DECL_FOR_SET( short )
184 PARSERS_DECL_FOR_SET( unsigned short )
185 PARSERS_DECL_FOR_SET( unsigned int )
187 PARSERS_DECL_FOR_SET( unsigned long )
188 PARSERS_DECL_FOR_SET( long long )
189 PARSERS_DECL_FOR_SET( unsigned long long )
190
191 PARSERS_DECL_FOR_SET( double )
192 PARSERS_DECL_FOR_SET( float )
193 PARSERS_DECL_FOR_SET( long double )
194
195 PARSERS_DECL_FOR_SET( std::string )
196
197 // Parsers for std::map<std::string, T>
198 PARSERS_DECL_FOR_STRMAP( std::string )
200 PARSERS_DECL_FOR_STRMAP( unsigned int )
202 PARSERS_DECL_FOR_STRMAP( std::vector<std::string> )
203 PARSERS_DECL_FOR_STRMAP( std::vector<int> )
204 PARSERS_DECL_FOR_STRMAP( std::vector<double> )
205 using IntPair = std::pair<int, int>; // needed to avoid macro parsing error due to brackets
206 using DoublePair = std::pair<double, double>;
210
223 PARSERS_DECL_FOR_PAIR( double, double )
236 PARSERS_DECL_FOR_PAIR( int, int )
250 GAUDI_API StatusCode parse( std::vector<std::pair<double, double>>& result, std::string_view input );
264 GAUDI_API StatusCode parse( std::vector<std::pair<int, int>>& result, std::string_view input );
277 GAUDI_API StatusCode parse( std::vector<std::vector<std::string>>& result, std::string_view input );
290 GAUDI_API StatusCode parse( std::vector<std::vector<double>>& result, std::string_view input );
299 GAUDI_API StatusCode parse( std::vector<std::vector<float>>& result, std::string_view input );
308 GAUDI_API StatusCode parse( std::vector<std::vector<int>>& result, std::string_view input );
309 // map< TYPE, TYPE >
322 GAUDI_API StatusCode parse( std::map<int, int>& result, std::string_view input );
336 GAUDI_API StatusCode parse( std::map<int, double>& result, std::string_view input );
344 GAUDI_API StatusCode parse( std::map<int, std::string>& result, std::string_view input );
352 GAUDI_API StatusCode parse( std::map<unsigned int, std::string>& result, std::string_view input );
357 template <typename K, typename V, typename M>
358 GAUDI_API StatusCode parse( GaudiUtils::Map<K, V, M>& result, std::string_view input ) {
359 return parse( static_cast<M&>( result ), input );
360 }
361
387 GAUDI_API StatusCode parse( std::string& name, std::string& value, std::string_view input );
396 GAUDI_API StatusCode parse( Gaudi::Histo1DDef& histo, std::string_view input );
404 template <class T, unsigned int N>
405 StatusCode parse( T ( &result )[N], std::string_view input ) {
406 typedef std::vector<T> _Vct;
407 // create the temporary vector
408 _Vct tmp;
409 StatusCode sc = parse( tmp, input );
410 if ( sc.isFailure() ) { return sc; }
411 if ( N != tmp.size() ) { return StatusCode::FAILURE; }
412 //
413 std::copy( tmp.begin(), tmp.end(), result );
414 //
415 return StatusCode::SUCCESS;
416 }
417
424 template <unsigned int N>
425 StatusCode parse( char ( &result )[N], std::string_view input ) {
426 // clear the string
427 std::fill_n( result, N, ' ' );
428 // create the temporary string
429 std::string tmp;
430 StatusCode sc = parse( tmp, input );
431 if ( sc.isFailure() ) { return sc; }
432 if ( N == tmp.size() ) {
433 std::copy( tmp.begin(), tmp.end(), result );
434 } else if ( N + 2 == tmp.size() && ( '\'' == tmp[0] || '\"' == tmp[0] ) && ( tmp[0] == tmp[tmp.size() - 1] ) ) {
435 std::copy( tmp.begin() + 1, tmp.end() - 1, result );
436 } else {
437 return StatusCode::FAILURE;
438 }
439 //
440 return StatusCode::SUCCESS;
441 }
442 } // namespace Parsers
443} // namespace Gaudi
#define PARSERS_DECL_FOR_SET(InnerType)
#define PARSERS_DECL_FOR_SINGLE(Type)
#define PARSERS_DECL_FOR_PAIR(FirstType, SecondType)
#define PARSERS_DECL_FOR_LIST(InnerType)
#define PARSERS_DECL_FOR_STRMAP(ValueType)
#define GAUDI_API
Definition Kernel.h:49
Simple helper class for description of 1D-histogram The class is targeted to act as the primary "hist...
Definition HistoDef.h:30
This class is used for returning status codes from appropriate routines.
Definition StatusCode.h:64
bool isFailure() const
Definition StatusCode.h:118
constexpr static const auto SUCCESS
Definition StatusCode.h:99
constexpr static const auto FAILURE
Definition StatusCode.h:100
STL class.
std::pair< int, int > IntPair
parse the bool value
StatusCode parse(GaudiUtils::HashMap< K, V > &result, std::string_view input)
Basic parser for the types of HashMap used in DODBasicMapper.
std::pair< double, double > DoublePair
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.