Lumiera  0.pre.03
»edit your freedom«
symbol.hpp
Go to the documentation of this file.
1 /*
2  SYMBOL.hpp - symbolic constant datatype
3 
4  Copyright (C) Lumiera.org
5  2008, Hermann Vosseler <Ichthyostega@web.de>
6 
7  This program is free software; you can redistribute it and/or
8  modify it under the terms of the GNU General Public License as
9  published by the Free Software Foundation; either version 2 of
10  the License, or (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program; if not, write to the Free Software
19  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 
21 */
22 
55 #ifndef LIB_SYMBOL_H
56 #define LIB_SYMBOL_H
57 
58 #include "lib/hash-standard.hpp"
59 
60 #include <string>
61 #include <cstring>
62 
63 using CStr = const char*;
64 
67 inline CStr
68 cStr (std::string const& rendered)
69 {
70  return rendered.c_str();
71 }
72 
73 
74 namespace lib {
75 
85  class Literal
86  {
87  CStr str_;
88 
89  public:
91  Literal() noexcept;
92 
93  Literal (const char* literal) noexcept
94  : str_(literal)
95  { }
96 
97  Literal (Literal const& o) noexcept
98  : str_(o.str_)
99  { }
100 
101  operator CStr() const { return str_; }
102  const char* c() const { return str_; }
103 
104  bool
105  empty() const
106  {
107  return !str_ || 0 == std::strlen(str_);
108  }
109 
110  bool operator== (CStr cString) const;
111 
112  protected:
114  Literal& operator= (CStr newStr) noexcept
115  {
116  str_ = newStr;
117  return *this;
118  }
119  };
120 
121 
126  class Symbol
127  : public Literal
128  {
129  public:
130  static Symbol ANY;
131  static Symbol EMPTY;
132  static Symbol BOTTOM;
133  static Symbol FAILURE;
134 
135  Symbol (CStr lit =NULL)
136  : Symbol{std::string(lit? lit : BOTTOM.c())}
137  { }
138 
139  explicit
140  Symbol (std::string&& definition);
141 
142  Symbol (std::string const& str)
143  : Symbol{std::string(str)}
144  { }
145 
146  Symbol (Literal const& base, std::string const& ext)
147  : Symbol{std::string(base)+"."+ext}
148  { }
149 
150  Symbol (Literal const& base, CStr ext)
151  : Symbol{base, std::string(ext)}
152  { }
153 
154  Symbol (Symbol const&) = default;
155  Symbol (Symbol &&) = default;
156 
157  Symbol& operator= (Symbol const&) = default;
158  Symbol& operator= (Symbol &&) = default;
159 
160  explicit operator bool() const { return not empty(); }
161  bool empty() const { return *this == BOTTOM or *this == EMPTY; }
162 
163  size_t
164  length() const
165  {
166  return std::strlen(c());
167  }
168  };
169 
170 
174  extern const size_t STRING_MAX_RELEVANT;
175 
177  inline Literal::Literal() noexcept : str_(Symbol::EMPTY) { }
178 
179 
180  /* ===== to be picked up by ADL ===== */
181 
182  size_t hash_value (Literal);
183  size_t hash_value (Symbol);
184 
185 
186  /* === equality comparisons === */
187 
188  inline bool operator== (Literal const& s1, Literal const& s2) { return s1.operator== (s2.c()); }
189  inline bool operator== (Symbol const& s1, Symbol const& s2) { return s1.c() == s2.c(); }
190 
191  /* === mixed comparisons === */
192 
193  inline bool operator== (CStr s1, Literal s2) { return s2.operator== (s1); }
194  inline bool operator== (Symbol s1, CStr s2) { return s1.operator== (s2); }
195  inline bool operator== (CStr s1, Symbol s2) { return s2.operator== (s1); }
196  inline bool operator== (Literal s1, Symbol s2) { return s1.operator== (s2.c()); }
197  inline bool operator== (Symbol s1, Literal s2) { return s2.operator== (s1.c()); }
198  inline bool operator== (Literal s1, std::string s2) { return s1.operator== (s2.c_str()); }
199  inline bool operator== (std::string s1, Literal s2) { return s2.operator== (s1.c_str()); }
200  inline bool operator== (Symbol s1, std::string s2) { return s1.operator== (s2.c_str()); }
201  inline bool operator== (std::string s1, Symbol s2) { return s2.operator== (s1.c_str()); }
202 
203  /* === negations === */
204 
205  inline bool operator!= (Literal const& s1, Literal const& s2) { return not s1.operator== (s2.c()); }
206  inline bool operator!= (Symbol const& s1, Symbol const& s2) { return not (s1.c() == s2.c()); }
207  inline bool operator!= (Literal s1, CStr s2) { return not s1.operator== (s2); }
208  inline bool operator!= (CStr s1, Literal s2) { return not s2.operator== (s1); }
209  inline bool operator!= (Symbol s1, CStr s2) { return not s1.operator== (s2); }
210  inline bool operator!= (CStr s1, Symbol s2) { return not s2.operator== (s1); }
211  inline bool operator!= (Literal s1, Symbol s2) { return not s1.operator== (s2.c()); }
212  inline bool operator!= (Symbol s1, Literal s2) { return not s2.operator== (s1.c()); }
213  inline bool operator!= (Literal s1, std::string s2) { return not s1.operator== (s2.c_str()); }
214  inline bool operator!= (std::string s1, Literal s2) { return not s2.operator== (s1.c_str()); }
215  inline bool operator!= (Symbol s1, std::string s2) { return not s1.operator== (s2.c_str()); }
216  inline bool operator!= (std::string s1, Symbol s2) { return not s2.operator== (s1.c_str()); }
217 
218 
219 
221  inline std::string
222  operator+ (std::string str, Literal const& sym)
223  {
224  CStr symP (sym);
225  return str + symP;
226  }
227 
228  inline std::string
229  operator+ (Literal const& sym, std::string str)
230  {
231  CStr symP (sym);
232  return symP + str;
233  }
234 
235 
236 
237 } // namespace lib
238 #endif /*LIB_SYMBOL_H*/
CStr cStr(std::string const &rendered)
convenience shortcut: forced conversion to c-String via string.
Definition: symbol.hpp:68
inline string literal This is a marker type to indicate that
Definition: symbol.hpp:85
bool operator==(CStr cString) const
equality on Literal and Symbol values is defined based on the content, not the address.
const size_t STRING_MAX_RELEVANT
safety guard: maximum number of chars to process.
Definition: symbol-impl.cpp:55
Implementation namespace for support and library code.
std::string operator+(std::string str, Literal const &sym)
string concatenation
Definition: symbol.hpp:222
Token or Atom with distinct identity.
Definition: symbol.hpp:126
Helper to use a single extension point for specialised hash functions.
Literal() noexcept
empty string by default
Definition: symbol.hpp:177
HashVal hash_value(QueryText const &entry)
support using queries in hashtables.
Definition: query-text.cpp:61
Literal & operator=(CStr newStr) noexcept
Assignment generally prohibited.
Definition: symbol.hpp:114