Yume
atom.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "llvm/ADT/StringSet.h"
4#include <string>
5
6namespace yume {
7/// `Atom`s represent strings in a string pool.
8/** This means two atoms created with the same string value will contain
9 * identical pointers to that string. Thus, comparing `Atom`s is extremely cheap, as it only consists of a pointer
10 * equality check.
11 */
12class Atom {
13 std::string_view m_str;
14
15public:
16 constexpr Atom() = delete;
17 explicit constexpr Atom(std::string_view str) : m_str{str} {}
18
19 /* implicit */ operator std::string_view() const { return m_str; }
20 explicit operator std::string() const { return std::string(m_str); }
21 auto constexpr operator<=>(const Atom& other) const noexcept = default;
22
23 static auto inline make_atom(std::string_view value) noexcept -> Atom {
24 const auto& data = Atom::interned.insert(value).first->first();
25 return Atom{data};
26 }
27
28private:
29 static inline llvm::StringSet interned{}; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
30};
31
32/// Create an `Atom` with the given string content.
33/// \sa atom_literal::operator""_a
34auto inline make_atom(std::string_view value) noexcept -> Atom { return Atom::make_atom(value); }
35
36auto inline operator""_a(const char* value, std::size_t len) noexcept -> Atom {
37 return make_atom(std::string_view(value, len));
38}
39} // namespace yume
Atoms represent strings in a string pool.
Definition: atom.hpp:12
static auto make_atom(std::string_view value) noexcept -> Atom
Definition: atom.hpp:23
auto constexpr operator<=>(const Atom &other) const noexcept=default
constexpr Atom()=delete
constexpr Atom(std::string_view str)
Definition: atom.hpp:17
Definition: ast.cpp:8
auto make_atom(std::string_view value) noexcept -> Atom
Create an Atom with the given string content.
Definition: atom.hpp:34