opynsim
Unofficial C++ Documentation
Loading...
Searching...
No Matches
symbol.h
Go to the documentation of this file.
1#pragma once
2
4
5#include <format>
6#include <functional>
7#include <iosfwd>
8#include <string_view>
9
10namespace opyn
11{
17 class Symbol final {
18 public:
19 Symbol() = default;
20 explicit Symbol(std::string_view sv) : data_{sv} {}
21
22 friend bool operator==(const Symbol&, const Symbol&) = default;
23 friend bool operator==(std::string_view lhs, const Symbol& rhs) { return lhs == rhs.data_; }
24 friend bool operator==(const Symbol& lhs, std::string_view rhs) { return lhs.data_ == rhs; }
25
27 explicit operator std::string () const { return std::string{data_}; }
28
30 explicit operator std::string_view () const { return std::string_view{data_}; }
31
33 std::string_view name() const { return data_.name(); }
34 private:
35 friend struct std::hash<Symbol>;
36
37 osc::StringName data_;
38 };
39}
40
42template<>
43struct std::formatter<opyn::Symbol> final {
44 constexpr auto parse(std::format_parse_context& ctx)
45 {
46 auto it = ctx.begin();
47 if (it != ctx.end() and *it != '}') {
48 throw std::format_error{"Symbol does not accept format specifiers"};
49 }
50 return it;
51 }
52
53 auto format(const opyn::Symbol& s, std::format_context& ctx) const
54 {
55 auto it = std::ranges::copy(std::string_view{"Symbol(\""}, ctx.out()).out;
56 it = std::ranges::copy(s.name(), it).out;
57 return std::ranges::copy(std::string_view{"\")"}, it).out;
58 }
59};
60
61namespace opyn
62{
64 inline std::ostream& operator<<(std::ostream& ostream, const opyn::Symbol& symbol)
65 {
66 return ostream << std::format("{}", symbol);
67 }
68}
69
70template<>
71struct std::hash<opyn::Symbol> final {
72 size_t operator()(const opyn::Symbol& symbol) const noexcept
73 {
74 return hasher_(symbol.data_);
75 }
76private:
77 std::hash<osc::StringName> hasher_;
78};
Definition symbol.h:17
Symbol(std::string_view sv)
Definition symbol.h:20
friend bool operator==(const Symbol &lhs, std::string_view rhs)
Definition symbol.h:24
friend bool operator==(std::string_view lhs, const Symbol &rhs)
Definition symbol.h:23
Symbol()=default
std::string_view name() const
Returns the name (key, content) of this Symbol.
Definition symbol.h:33
friend bool operator==(const Symbol &, const Symbol &)=default
Definition string_name.h:14
std::string_view name() const
Definition string_name.h:62
Definition component_registry.h:14
std::ostream & operator<<(std::ostream &out, const DataFrame &data_frame)
Writes a pretty-printed representation of data_frame to out.
constexpr auto parse(std::format_parse_context &ctx)
Definition symbol.h:44
auto format(const opyn::Symbol &s, std::format_context &ctx) const
Definition symbol.h:53
size_t operator()(const opyn::Symbol &symbol) const noexcept
Definition symbol.h:72