The Gaudi Framework  v37r1 (a7f61348)
PropertyId.h
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 2022 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 <functional>
14 #include <string>
15 #include <unordered_set>
16 #include <vector>
17 
18 #if __cplusplus >= 201703
19 # include <string_view>
20 #else
21 # include <experimental/string_view>
22 namespace std {
23  using experimental::string_view;
24 }
25 #endif
26 
27 namespace Gaudi::Details {
35  class SharedString final {
36  public:
39  SharedString( std::string_view s = {} ) : m_s{ SharedString::get( s ) } {}
40 
41  operator std::string() const { return *m_s; }
42  operator std::string_view() const { return *m_s; }
43 
44  bool operator==( std::string_view other ) const { return *m_s == other; }
45  bool operator==( const SharedString& other ) const { return m_s == other.m_s; }
46 
47  template <typename T>
48  bool operator==( const T& other ) const {
49  return *m_s == other;
50  }
51 
52  private:
53  const std::string* m_s;
54 
55  static const std::string* get( std::string_view s ) {
56  if ( s.empty() ) { return {}; }
57  return &*( storage.emplace( s ).first );
58  }
59 
61  };
62 
72  class PropertyId final {
73  public:
74  PropertyId( const std::string& s ) : PropertyId( std::string_view{ s } ) {}
75  PropertyId( std::string_view s ) {
77  if ( !s.empty() ) {
78  m_chunks.reserve( std::count( begin( s ), end( s ), '.' ) + 1 );
79  while ( true ) {
80  if ( auto pos = s.find( '.' ); pos != std::string_view::npos ) {
81  m_chunks.emplace_back( s.substr( 0, pos ) );
82  s.remove_prefix( pos + 1 );
83  } else {
84  m_chunks.emplace_back( s );
85  break;
86  }
87  }
88  }
89  }
90 
91  std::string str() const {
92  auto it = m_chunks.begin();
93  std::string s{ *it++ };
94  while ( it != m_chunks.end() ) {
95  s += '.';
96  s += *it++;
97  }
98  return s;
99  }
100 
101  operator std::string() const { return str(); }
102 
103  std::size_t hash() const noexcept { return m_hash; }
104 
105  private:
108  friend bool operator==( const PropertyId& lhs, const PropertyId& rhs );
109  };
110  inline bool operator==( const PropertyId& lhs, const PropertyId& rhs ) { return lhs.m_chunks == rhs.m_chunks; }
111 } // namespace Gaudi::Details
112 
113 template <>
114 struct std::hash<Gaudi::Details::PropertyId> {
115  std::size_t operator()( Gaudi::Details::PropertyId const& s ) const noexcept { return s.hash(); }
116 };
Gaudi::Details::PropertyId::operator==
friend bool operator==(const PropertyId &lhs, const PropertyId &rhs)
Definition: PropertyId.h:110
std::string
STL class.
Gaudi::Details
Definition: PropertyId.h:27
Gaudi::Details::PropertyId::PropertyId
PropertyId(const std::string &s)
Definition: PropertyId.h:74
Gaudi::Details::SharedString::operator==
bool operator==(std::string_view other) const
Definition: PropertyId.h:44
Gaudi::Details::PropertyId::PropertyId
PropertyId(std::string_view s)
Definition: PropertyId.h:75
Gaudi::Details::SharedString::storage
static std::unordered_set< std::string > storage
Definition: PropertyId.h:60
std::unordered_set< std::string >
gaudirun.s
string s
Definition: gaudirun.py:348
Gaudi::Details::SharedString::operator==
bool operator==(const SharedString &other) const
Definition: PropertyId.h:45
std::vector
STL class.
std::unordered_set::emplace
T emplace(T... args)
Gaudi::Details::SharedString::get
static const std::string * get(std::string_view s)
Definition: PropertyId.h:55
CLHEP::begin
double * begin(CLHEP::HepVector &v)
Definition: TupleAlg.cpp:45
Gaudi::Details::PropertyId::str
std::string str() const
Definition: PropertyId.h:91
std::hash< Gaudi::Details::PropertyId >::operator()
std::size_t operator()(Gaudi::Details::PropertyId const &s) const noexcept
Definition: PropertyId.h:115
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:39
Gaudi::Details::PropertyId::hash
std::size_t hash() const noexcept
Definition: PropertyId.h:103
Gaudi::Details::PropertyId::m_hash
std::size_t m_hash
Definition: PropertyId.h:107
Gaudi::Details::PropertyId::m_chunks
std::vector< SharedString > m_chunks
Definition: PropertyId.h:106
Gaudi
Header file for std:chrono::duration-based Counters.
Definition: __init__.py:1
Gaudi::Details::SharedString::operator==
bool operator==(const T &other) const
Definition: PropertyId.h:48
Gaudi::Details::PropertyId
Helper to record a property identifier as a sequence of SharedString instances.
Definition: PropertyId.h:72
Gaudi::Details::SharedString
std::string wrapper for static strings where identical values actually share the memory.
Definition: PropertyId.h:35
std
STL namespace.
std::count
T count(T... args)
std::size_t
IOTest.end
end
Definition: IOTest.py:123
Gaudi::Details::SharedString::m_s
const std::string * m_s
Definition: PropertyId.h:53
Gaudi::Details::operator==
bool operator==(const PropertyId &lhs, const PropertyId &rhs)
Definition: PropertyId.h:110
std::hash