Lumiera 0.pre.04
»edit your freedom«
Loading...
Searching...
No Matches
symbol.hpp
Go to the documentation of this file.
1/*
2 SYMBOL.hpp - symbolic constant datatype
3
4 Copyright (C)
5 2008,2017 Hermann Vosseler <Ichthyostega@web.de>
6
7  **Lumiera** is free software; you can redistribute it and/or modify it
8  under the terms of the GNU General Public License as published by the
9  Free Software Foundation; either version 2 of the License, or (at your
10  option) any later version. See the file COPYING for further details.
11
12*/
13
47#ifndef LIB_SYMBOL_H
48#define LIB_SYMBOL_H
49
50#include "lib/hash-standard.hpp"
51
52#include <string>
53#include <cstring>
54
55using CStr = const char*;
56
59inline CStr
60cStr (std::string const& rendered)
61{
62 return rendered.c_str();
63}
64
65
66namespace lib {
67
77 class Literal
78 {
80
81 public:
83 constexpr Literal() noexcept;
84
85 constexpr Literal (CStr literal) noexcept
86 : str_(literal)
87 { }
88
89 constexpr Literal (Literal const&) noexcept = default;
90 constexpr Literal& operator= (Literal const&) noexcept = default;
91
92 constexpr operator CStr() const { return str_; }
93 constexpr const char* c() const { return str_; }
94
95 constexpr bool
96 empty() const
97 {
98 return not str_ or 0 == std::strlen(str_);
99 }
100
101 constexpr bool operator== (CStr cString) const;
102 constexpr size_t length() const;
103
104 protected:
106 Literal& operator= (CStr newStr) noexcept
107 {
108 str_ = newStr;
109 return *this;
110 }
111 };
112
113
118 class Symbol
119 : public Literal
120 {
121 public:
122 static Symbol ANY;
123 static Symbol EMPTY;
126
128 : Symbol{std::string(lit? lit : BOTTOM.c())}
129 { }
130
131 explicit
132 Symbol (std::string&& definition);
133
134 Symbol (std::string const& str)
135 : Symbol{std::string(str)}
136 { }
137
138 Symbol (Literal const& base, std::string const& ext)
139 : Symbol{std::string(base)+"."+ext}
140 { }
141
142 Symbol (Literal const& base, CStr ext)
143 : Symbol{base, std::string(ext)}
144 { }
145
146 Symbol (Symbol const&) = default;
147 Symbol (Symbol &&) = default;
148
149 Symbol& operator= (Symbol const&) = default;
150 Symbol& operator= (Symbol &&) = default;
151
152 explicit operator bool() const { return not empty(); }
153 bool empty() const { return *this == BOTTOM.c() or *this == EMPTY.c(); }
154 };
155
156
158 constexpr inline Literal::Literal() noexcept : str_(Symbol::EMPTY) { }
159
160 namespace{
161 constexpr inline int
162 strNcmp (CStr a, CStr b, size_t len)
163 {
164 return a == b ? 0 : std::strncmp (a?a:"", b?b:"", len);
165 }
166 }
167
171 extern const size_t STRING_MAX_RELEVANT;
172
175 constexpr inline bool
177 {
178 return 0 == strNcmp (this->str_, charPtr, STRING_MAX_RELEVANT);
179 }
180
181 constexpr inline size_t
183 {
184 return std::strlen(c());
185 }
186
187
188
189
190
191 /* ===== to be picked up by ADL ===== */
192
195
196
197 /* === equality comparisons === */
198
199 constexpr inline bool operator== (Literal const& s1, Literal const& s2) { return s1.operator== (s2.c()); }
200 constexpr inline bool operator== (Symbol const& s1, Symbol const& s2) { return s1.c() == s2.c(); }
201
202 /* === mixed comparisons === */
203
204 constexpr inline bool operator== (CStr s1, Literal s2) { return s2.operator== (s1); }
205 constexpr inline bool operator== (Symbol s1, CStr s2) { return s1.operator== (s2); }
206 constexpr inline bool operator== (Literal s1, Symbol s2) { return s1.operator== (s2.c()); }
207 constexpr inline bool operator== (Literal s1, std::string const& s2) { return s1.operator== (s2.c_str()); }
208 constexpr inline bool operator== (Symbol s1, std::string const& s2) { return s1.operator== (s2.c_str()); }
209
210
211
213 inline std::string
214 operator+ (std::string str, Literal const& sym)
215 {
216 CStr symP (sym);
217 return str + symP;
218 }
219
220 inline std::string
221 operator+ (Literal const& sym, std::string str)
222 {
223 CStr symP (sym);
224 return symP + str;
225 }
226
227
228
229} // namespace lib
230#endif /*LIB_SYMBOL_H*/
Inline string literal.
Definition symbol.hpp:78
constexpr bool operator==(CStr cString) const
equality on Literal and Symbol values is defined based on the content, not the address.
Definition symbol.hpp:176
constexpr Literal(Literal const &) noexcept=default
constexpr const char * c() const
Definition symbol.hpp:93
constexpr Literal & operator=(Literal const &) noexcept=default
constexpr size_t length() const
Definition symbol.hpp:182
constexpr Literal() noexcept
empty string by default
Definition symbol.hpp:158
constexpr bool empty() const
Definition symbol.hpp:96
Token or Atom with distinct identity.
Definition symbol.hpp:120
static Symbol BOTTOM
Definition symbol.hpp:124
Symbol(Literal const &base, std::string const &ext)
Definition symbol.hpp:138
Symbol & operator=(Symbol const &)=default
bool empty() const
Definition symbol.hpp:153
Symbol(std::string const &str)
Definition symbol.hpp:134
Symbol(Literal const &base, CStr ext)
Definition symbol.hpp:142
Symbol(Symbol &&)=default
static Symbol ANY
Definition symbol.hpp:122
static Symbol FAILURE
Definition symbol.hpp:125
static Symbol EMPTY
Definition symbol.hpp:123
Symbol(Symbol const &)=default
Symbol(CStr lit=NULL)
Definition symbol.hpp:127
const char * CStr
Definition error.hpp:42
Helper to use a single extension point for specialised hash functions.
return NULL
Definition llist.h:586
constexpr int strNcmp(CStr a, CStr b, size_t len)
Definition symbol.hpp:162
Implementation namespace for support and library code.
bool operator==(PtrDerefIter< I1 > const &il, PtrDerefIter< I2 > const &ir)
Supporting equality comparisons...
std::string operator+(std::string str, Literal const &sym)
string concatenation
Definition symbol.hpp:214
const size_t STRING_MAX_RELEVANT
safety guard: maximum number of chars to process.
size_t HashVal
a STL compatible hash value
Definition hash-value.h:52
HashVal hash_value(QueryText const &entry)
support using queries in hashtables.
STL namespace.
CStr cStr(std::string const &rendered)
convenience shortcut: forced conversion to c-String via string.
Definition symbol.hpp:60