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