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