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)
5  2008, 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 
46 #ifndef LIB_SYMBOL_H
47 #define LIB_SYMBOL_H
48 
49 #include "lib/hash-standard.hpp"
50 
51 #include <string>
52 #include <cstring>
53 
54 using CStr = const char*;
55 
58 inline CStr
59 cStr (std::string const& rendered)
60 {
61  return rendered.c_str();
62 }
63 
64 
65 namespace lib {
66 
76  class Literal
77  {
78  CStr str_;
79 
80  public:
82  Literal() noexcept;
83 
84  Literal (const char* literal) noexcept
85  : str_(literal)
86  { }
87 
88  Literal (Literal const& o) noexcept
89  : str_(o.str_)
90  { }
91 
92  operator CStr() const { return str_; }
93  const char* c() const { return str_; }
94 
95  bool
96  empty() const
97  {
98  return !str_ || 0 == std::strlen(str_);
99  }
100 
101  bool operator== (CStr cString) const;
102 
103  protected:
105  Literal& operator= (CStr newStr) noexcept
106  {
107  str_ = newStr;
108  return *this;
109  }
110  };
111 
112 
117  class Symbol
118  : public Literal
119  {
120  public:
121  static Symbol ANY;
122  static Symbol EMPTY;
123  static Symbol BOTTOM;
124  static Symbol FAILURE;
125 
126  Symbol (CStr lit =NULL)
127  : Symbol{std::string(lit? lit : BOTTOM.c())}
128  { }
129 
130  explicit
131  Symbol (std::string&& definition);
132 
133  Symbol (std::string const& str)
134  : Symbol{std::string(str)}
135  { }
136 
137  Symbol (Literal const& base, std::string const& ext)
138  : Symbol{std::string(base)+"."+ext}
139  { }
140 
141  Symbol (Literal const& base, CStr ext)
142  : Symbol{base, std::string(ext)}
143  { }
144 
145  Symbol (Symbol const&) = default;
146  Symbol (Symbol &&) = default;
147 
148  Symbol& operator= (Symbol const&) = default;
149  Symbol& operator= (Symbol &&) = default;
150 
151  explicit operator bool() const { return not empty(); }
152  bool empty() const { return *this == BOTTOM or *this == EMPTY; }
153 
154  size_t
155  length() const
156  {
157  return std::strlen(c());
158  }
159  };
160 
161 
165  extern const size_t STRING_MAX_RELEVANT;
166 
168  inline Literal::Literal() noexcept : str_(Symbol::EMPTY) { }
169 
170 
171  /* ===== to be picked up by ADL ===== */
172 
175 
176 
177  /* === equality comparisons === */
178 
179  inline bool operator== (Literal const& s1, Literal const& s2) { return s1.operator== (s2.c()); }
180  inline bool operator== (Symbol const& s1, Symbol const& s2) { return s1.c() == s2.c(); }
181 
182  /* === mixed comparisons === */
183 
184  inline bool operator== (CStr s1, Literal s2) { return s2.operator== (s1); }
185  inline bool operator== (Symbol s1, CStr s2) { return s1.operator== (s2); }
186  inline bool operator== (CStr s1, Symbol s2) { return s2.operator== (s1); }
187  inline bool operator== (Literal s1, Symbol s2) { return s1.operator== (s2.c()); }
188  inline bool operator== (Symbol s1, Literal s2) { return s2.operator== (s1.c()); }
189  inline bool operator== (Literal s1, std::string s2) { return s1.operator== (s2.c_str()); }
190  inline bool operator== (std::string s1, Literal s2) { return s2.operator== (s1.c_str()); }
191  inline bool operator== (Symbol s1, std::string s2) { return s1.operator== (s2.c_str()); }
192  inline bool operator== (std::string s1, Symbol s2) { return s2.operator== (s1.c_str()); }
193 
194  /* === negations === */
195 
196  inline bool operator!= (Literal const& s1, Literal const& s2) { return not s1.operator== (s2.c()); }
197  inline bool operator!= (Symbol const& s1, Symbol const& s2) { return not (s1.c() == s2.c()); }
198  inline bool operator!= (Literal s1, CStr s2) { return not s1.operator== (s2); }
199  inline bool operator!= (CStr s1, Literal s2) { return not s2.operator== (s1); }
200  inline bool operator!= (Symbol s1, CStr s2) { return not s1.operator== (s2); }
201  inline bool operator!= (CStr s1, Symbol s2) { return not s2.operator== (s1); }
202  inline bool operator!= (Literal s1, Symbol s2) { return not s1.operator== (s2.c()); }
203  inline bool operator!= (Symbol s1, Literal s2) { return not s2.operator== (s1.c()); }
204  inline bool operator!= (Literal s1, std::string s2) { return not s1.operator== (s2.c_str()); }
205  inline bool operator!= (std::string s1, Literal s2) { return not s2.operator== (s1.c_str()); }
206  inline bool operator!= (Symbol s1, std::string s2) { return not s1.operator== (s2.c_str()); }
207  inline bool operator!= (std::string s1, Symbol s2) { return not s2.operator== (s1.c_str()); }
208 
209 
210 
212  inline std::string
213  operator+ (std::string str, Literal const& sym)
214  {
215  CStr symP (sym);
216  return str + symP;
217  }
218 
219  inline std::string
220  operator+ (Literal const& sym, std::string str)
221  {
222  CStr symP (sym);
223  return symP + str;
224  }
225 
226 
227 
228 } // namespace lib
229 #endif /*LIB_SYMBOL_H*/
CStr cStr(std::string const &rendered)
convenience shortcut: forced conversion to c-String via string.
Definition: symbol.hpp:59
inline string literal This is a marker type to indicate that
Definition: symbol.hpp:76
bool operator==(CStr cString) const
equality on Literal and Symbol values is defined based on the content, not the address.
Definition: symbol-impl.cpp:93
const size_t STRING_MAX_RELEVANT
safety guard: maximum number of chars to process.
Definition: symbol-impl.cpp:46
Implementation namespace for support and library code.
std::string operator+(std::string str, Literal const &sym)
string concatenation
Definition: symbol.hpp:213
Token or Atom with distinct identity.
Definition: symbol.hpp:117
Helper to use a single extension point for specialised hash functions.
Literal() noexcept
empty string by default
Definition: symbol.hpp:168
HashVal hash_value(QueryText const &entry)
support using queries in hashtables.
Definition: query-text.cpp:52
Literal & operator=(CStr newStr) noexcept
Assignment generally prohibited.
Definition: symbol.hpp:105
size_t HashVal
a STL compatible hash value
Definition: hash-value.h:52