libabigail
Toggle main menu visibility
Loading...
Searching...
No Matches
include
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
26
namespace
abigail
27
{
28
// Inject some std types into this namespace.
29
using
std::unordered_set;
30
using
std::string;
31
using
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.
42
class
interned_string
43
{
44
interned_string(
string
*
raw
);
45
46
public
:
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.
89
struct
hash_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
102
operator()
(
const
interned_string
& s)
const
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
110
typedef
unordered_set<interned_string,
111
hash_interned_string
>
interned_string_set_type
;
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.
118
class
interned_string_pool
119
{
120
struct
priv;
121
122
public
:
123
std::unique_ptr<priv> priv_;
124
interned_string_pool
();
125
126
interned_string
127
create_string
(
const
std::string&);
128
129
interned_string
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
138
~interned_string_pool
();
139
};
// end class interned_string_pool
140
141
bool
142
operator==
(
const
string
& l,
const
interned_string
& r);
143
144
bool
145
operator!=(
const
string
& l,
const
interned_string
& r);
146
147
ostream&
148
operator<<
(ostream& o,
const
interned_string
& s);
149
150
string
151
operator+
(
const
interned_string
& s1,
const
string
& s2);
152
153
string
154
operator+
(
const
string
& s1,
const
interned_string
& s2);
155
156
}
// end namespace abigail
157
158
#endif
// __ABG_INTERNED_STR_H__
abigail::interned_string_pool::has_string
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
abigail::interned_string_pool::get_string
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
abigail::interned_string_pool::create_string
interned_string create_string(const std::string &)
Create an interned string with a given value.
Definition
abg-ir.cc:136
abigail::interned_string_pool::interned_string_pool
interned_string_pool()
Default constructor.
Definition
abg-ir.cc:91
abigail::interned_string_pool::~interned_string_pool
~interned_string_pool()
Destructor.
Definition
abg-ir.cc:155
abigail::interned_string
The abstraction of an interned string.
Definition
abg-interned-str.h:43
abigail::interned_string::operator<
bool operator<(const interned_string &o) const
"Less than" operator.
Definition
abg-ir.cc:318
abigail::interned_string::raw
const string * raw() const
Return the underlying pointer to std::string that this interned_string wraps.
Definition
abg-ir.cc:244
abigail::interned_string::empty
bool empty() const
Test if the current instance of interned_string is empty.
Definition
abg-ir.cc:232
abigail::interned_string::operator=
interned_string & operator=(const interned_string &o)
Assignment operator.
Definition
abg-ir.cc:211
abigail::interned_string::clear
void clear()
Clear the string.
Definition
abg-ir.cc:221
abigail::interned_string::operator!=
bool operator!=(const interned_string &o) const
Inequality operator.
Definition
abg-ir.cc:273
abigail::interned_string::operator==
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
abigail::comparison::operator<<
ostream & operator<<(ostream &o, diff_category c)
Serialize an instance of diff_category to an output stream.
Definition
abg-comparison.cc:3330
abigail
Toplevel namespace for libabigail.
Definition
abg-comp-filter.cc:27
abigail::operator==
bool operator==(const std::string &l, const interned_string &r)
Equality operator.
Definition
abg-ir.cc:341
abigail::interned_string_set_type
unordered_set< interned_string, hash_interned_string > interned_string_set_type
Convenience typedef for a set of interned_string.
Definition
abg-interned-str.h:111
abigail::operator+
std::string operator+(const interned_string &s1, const std::string &s2)
Concatenation operator.
Definition
abg-ir.cc:375
abigail::hash_interned_string
A functor to hash instances of interned_string.
Definition
abg-interned-str.h:90
abigail::hash_interned_string::operator()
size_t operator()(const interned_string &s) const
The hash operator.
Definition
abg-interned-str.h:102
Generated by
1.17.0