The Gaudi Framework  master (f31105fd)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
PropertyId.h
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 2022-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 
13 #include <algorithm>
14 #include <functional>
15 #include <string>
16 #include <string_view>
17 #include <unordered_set>
18 #include <vector>
19 
20 namespace Gaudi::Details {
28  class SharedString final {
29  public:
32  SharedString( std::string_view s = {} ) : m_s{ SharedString::get( s ) } {}
33 
34  operator std::string() const { return *m_s; }
35  operator std::string_view() const { return *m_s; }
36 
37  bool operator==( std::string_view other ) const { return *m_s == other; }
38  bool operator==( const SharedString& other ) const { return m_s == other.m_s; }
39 
40  template <typename T>
41  bool operator==( const T& other ) const {
42  return *m_s == other;
43  }
44 
45  private:
46  const std::string* m_s;
47 
48  static const std::string* get( std::string_view s ) {
49  if ( s.empty() ) { return {}; }
50  return &*( storage.emplace( s ).first );
51  }
52 
54  };
55 
65  class PropertyId final {
66  public:
67  PropertyId( const std::string& s ) : PropertyId( std::string_view{ s } ) {}
68  PropertyId( std::string_view s ) {
70  if ( !s.empty() ) {
71  m_chunks.reserve( std::count( begin( s ), end( s ), '.' ) + 1 );
72  while ( true ) {
73  if ( auto pos = s.find( '.' ); pos != std::string_view::npos ) {
74  m_chunks.emplace_back( s.substr( 0, pos ) );
75  s.remove_prefix( pos + 1 );
76  } else {
77  m_chunks.emplace_back( s );
78  break;
79  }
80  }
81  }
82  }
83 
84  std::string str() const {
85  auto it = m_chunks.begin();
86  std::string s{ *it++ };
87  while ( it != m_chunks.end() ) {
88  s += '.';
89  s += *it++;
90  }
91  return s;
92  }
93 
94  operator std::string() const { return str(); }
95 
96  std::size_t hash() const noexcept { return m_hash; }
97 
98  private:
101  friend bool operator==( const PropertyId& lhs, const PropertyId& rhs );
102  };
103  inline bool operator==( const PropertyId& lhs, const PropertyId& rhs ) { return lhs.m_chunks == rhs.m_chunks; }
104 } // namespace Gaudi::Details
105 
106 template <>
107 struct std::hash<Gaudi::Details::PropertyId> {
108  std::size_t operator()( Gaudi::Details::PropertyId const& s ) const noexcept { return s.hash(); }
109 };
Gaudi::Details::PropertyId::operator==
friend bool operator==(const PropertyId &lhs, const PropertyId &rhs)
Definition: PropertyId.h:103
std::string
STL class.
Gaudi::Details
Definition: PropertyId.h:20
Gaudi::Details::PropertyId::PropertyId
PropertyId(const std::string &s)
Definition: PropertyId.h:67
Gaudi::Details::SharedString::operator==
bool operator==(std::string_view other) const
Definition: PropertyId.h:37
Gaudi::Details::PropertyId::PropertyId
PropertyId(std::string_view s)
Definition: PropertyId.h:68
Gaudi::Details::SharedString::storage
static std::unordered_set< std::string > storage
Definition: PropertyId.h:53
std::unordered_set< std::string >
gaudirun.s
string s
Definition: gaudirun.py:346
Gaudi::Details::SharedString::operator==
bool operator==(const SharedString &other) const
Definition: PropertyId.h:38
std::vector
STL class.
std::unordered_set::emplace
T emplace(T... args)
Gaudi::Utils::begin
AttribStringParser::Iterator begin(const AttribStringParser &parser)
Definition: AttribStringParser.h:136
Gaudi::Details::SharedString::get
static const std::string * get(std::string_view s)
Definition: PropertyId.h:48
Gaudi::Details::PropertyId::str
std::string str() const
Definition: PropertyId.h:84
std::hash< Gaudi::Details::PropertyId >::operator()
std::size_t operator()(Gaudi::Details::PropertyId const &s) const noexcept
Definition: PropertyId.h:108
Gaudi::Details::SharedString::SharedString
SharedString(std::string_view s={})
Create a new SharedString checking if the value is already in the shared storage otherwise adding it.
Definition: PropertyId.h:32
Gaudi::Details::PropertyId::hash
std::size_t hash() const noexcept
Definition: PropertyId.h:96
Gaudi::Details::PropertyId::m_hash
std::size_t m_hash
Definition: PropertyId.h:100
Gaudi::Details::PropertyId::m_chunks
std::vector< SharedString > m_chunks
Definition: PropertyId.h:99
Gaudi
This file provides a Grammar for the type Gaudi::Accumulators::Axis It allows to use that type from p...
Definition: __init__.py:1
Gaudi::Details::SharedString::operator==
bool operator==(const T &other) const
Definition: PropertyId.h:41
Gaudi::Details::PropertyId
Helper to record a property identifier as a sequence of SharedString instances.
Definition: PropertyId.h:65
Gaudi::Details::SharedString
std::string wrapper for static strings where identical values actually share the memory.
Definition: PropertyId.h:28
std
STL namespace.
std::count
T count(T... args)
std::size_t
IOTest.end
end
Definition: IOTest.py:125
Gaudi::Details::SharedString::m_s
const std::string * m_s
Definition: PropertyId.h:46
Gaudi::Details::operator==
bool operator==(const PropertyId &lhs, const PropertyId &rhs)
Definition: PropertyId.h:103
std::hash