The Gaudi Framework  master (b9786168)
Loading...
Searching...
No Matches
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
20namespace 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
53 static std::unordered_set<std::string> storage;
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 ) {
69 m_hash = std::hash<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:
99 std::vector<SharedString> m_chunks;
100 std::size_t m_hash;
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
106template <>
107struct std::hash<Gaudi::Details::PropertyId> {
108 std::size_t operator()( Gaudi::Details::PropertyId const& s ) const noexcept { return s.hash(); }
109};
Helper to record a property identifier as a sequence of SharedString instances.
Definition PropertyId.h:65
friend bool operator==(const PropertyId &lhs, const PropertyId &rhs)
Definition PropertyId.h:103
PropertyId(std::string_view s)
Definition PropertyId.h:68
std::size_t hash() const noexcept
Definition PropertyId.h:96
std::vector< SharedString > m_chunks
Definition PropertyId.h:99
std::string str() const
Definition PropertyId.h:84
PropertyId(const std::string &s)
Definition PropertyId.h:67
std::string wrapper for static strings where identical values actually share the memory.
Definition PropertyId.h:28
static const std::string * get(std::string_view s)
Definition PropertyId.h:48
bool operator==(std::string_view other) const
Definition PropertyId.h:37
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
bool operator==(const SharedString &other) const
Definition PropertyId.h:38
static std::unordered_set< std::string > storage
Definition PropertyId.h:53
const std::string * m_s
Definition PropertyId.h:46
bool operator==(const T &other) const
Definition PropertyId.h:41
bool operator==(const PropertyId &lhs, const PropertyId &rhs)
Definition PropertyId.h:103
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.
std::size_t operator()(Gaudi::Details::PropertyId const &s) const noexcept
Definition PropertyId.h:108