libabigail
Loading...
Searching...
No Matches
abg-interned-str.h
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
2// -*- Mode: C++ -*-
3//
4// Copyright (C) 2016-2026 Red Hat, Inc.
5//
6// Author: Dodji Seketeli
7
8/// @file
9///
10/// Declaration of types pertaining to the interned string pool used
11/// throughout Libabigail, for performance reasons.
12///
13/// For the record, the concept of the String Interning method is
14/// explained at https://en.wikipedia.org/wiki/String_interning.
15
16#ifndef __ABG_INTERNED_STR_H__
17#define __ABG_INTERNED_STR_H__
18
19#include <functional>
20#include <memory>
21#include <ostream>
22#include <string>
23#include <unordered_set>
24
25
26namespace abigail
27{
28// Inject some std types into this namespace.
29using std::unordered_set;
30using std::string;
31using std::ostream;
32
33/// The abstraction of an interned string.
34///
35/// It's a wrapper around a pointer to a std::string, along with a set
36/// of method that helps make this string integrate with std::string
37/// seamlessly. For instance, the type provides equality operators
38/// that help compare it against std::string.
39///
40/// Note that this @ref interned_string type is design to have the
41/// same size as a pointer to a string.
42class interned_string
43{
44 interned_string(string* raw);
45
46public:
47 struct priv;
48 std::unique_ptr<priv> priv_;
49
50 interned_string();
51
52 ~interned_string();
53
54 interned_string(const interned_string& o);
55
56 interned_string&
57 operator=(const interned_string& o);
58
59 void
60 clear();
61
62 bool
63 empty() const;
64
65 const string*
66 raw() const;
67
68 bool
69 operator==(const interned_string& o) const;
70
71 bool
72 operator!=(const interned_string& o) const;
73
74 bool
75 operator==(const string& o) const;
76
77 bool
78 operator!=(const string& o) const;
79
80 bool
81 operator<(const interned_string& o) const;
82
83 operator string() const;
84
85 friend class interned_string_pool;
86}; // end class interned_string
87
88/// A functor to hash instances of @ref interned_string.
90{
91 /// The hash operator.
92 ///
93 /// It's super fast because hashing an interned string amounts to
94 /// hashing the pointer to it's underlying string. It's because
95 /// every distinct string is present only in one copy in the
96 /// environment.
97 ///
98 /// @param s the instance of @ref interned_string to hash.
99 ///
100 /// @return the returned hash value.
101 size_t
103 {
104 std::hash<size_t> hash_size_t;
105 return hash_size_t(reinterpret_cast<size_t>(s.raw()));
106 }
107}; // end struct hash_interned_string
108
109/// Convenience typedef for a set of @ref interned_string
110typedef unordered_set<interned_string,
112
113/// The interned string pool.
114///
115/// This is where all the distinct strings represented by the interned
116/// strings leave. The pool is the actor responsible for creating
117/// interned strings.
119{
120 struct priv;
121
122public:
123 std::unique_ptr<priv> priv_;
125
127 create_string(const std::string&);
128
130 create_string() const;
131
132 bool
133 has_string(const char* s) const;
134
135 const char*
136 get_string(const char* s) const;
137
139}; // end class interned_string_pool
140
141bool
142operator==(const string& l, const interned_string& r);
143
144bool
145operator!=(const string& l, const interned_string& r);
146
147ostream&
148operator<<(ostream& o, const interned_string& s);
149
150string
151operator+(const interned_string& s1,const string& s2);
152
153string
154operator+(const string& s1, const interned_string& s2);
155
156} // end namespace abigail
157
158#endif // __ABG_INTERNED_STR_H__
bool has_string(const char *s) const
Test if the interned string pool already contains a string with a given value.
Definition abg-ir.cc:105
const char * get_string(const char *s) const
Get a pointer to the interned string which has a given value.
Definition abg-ir.cc:118
interned_string create_string(const std::string &)
Create an interned string with a given value.
Definition abg-ir.cc:136
interned_string_pool()
Default constructor.
Definition abg-ir.cc:91
~interned_string_pool()
Destructor.
Definition abg-ir.cc:155
The abstraction of an interned string.
bool operator<(const interned_string &o) const
"Less than" operator.
Definition abg-ir.cc:318
const string * raw() const
Return the underlying pointer to std::string that this interned_string wraps.
Definition abg-ir.cc:244
bool empty() const
Test if the current instance of interned_string is empty.
Definition abg-ir.cc:232
interned_string & operator=(const interned_string &o)
Assignment operator.
Definition abg-ir.cc:211
void clear()
Clear the string.
Definition abg-ir.cc:221
bool operator!=(const interned_string &o) const
Inequality operator.
Definition abg-ir.cc:273
bool operator==(const interned_string &o) const
Compare the current instance of interned_string against another instance of interned_string.
Definition abg-ir.cc:260
ostream & operator<<(ostream &o, diff_category c)
Serialize an instance of diff_category to an output stream.
Toplevel namespace for libabigail.
bool operator==(const std::string &l, const interned_string &r)
Equality operator.
Definition abg-ir.cc:341
unordered_set< interned_string, hash_interned_string > interned_string_set_type
Convenience typedef for a set of interned_string.
std::string operator+(const interned_string &s1, const std::string &s2)
Concatenation operator.
Definition abg-ir.cc:375
A functor to hash instances of interned_string.
size_t operator()(const interned_string &s) const
The hash operator.