Lumiera  0.pre.03
»edit your freedom«
symbol-impl.cpp
Go to the documentation of this file.
1 /*
2  Symbol(impl) - helpers for working with literal string IDs
3 
4  Copyright (C) Lumiera.org
5  2009, 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 
37 #include "lib/symbol.hpp"
38 #include "lib/symbol-table.hpp"
39 
40 #include <boost/functional/hash.hpp>
41 #include <cstddef>
42 #include <cstring>
43 #include <string>
44 
45 using std::size_t;
46 using std::string;
47 using std::forward;
48 using boost::hash_combine;
49 
50 
51 
52 
53 namespace lib {
54 
55  const size_t STRING_MAX_RELEVANT = LUMIERA_IDSTRING_MAX_RELEVANT;
56 
57 
58  namespace { // global symbol table
59 
61  symbolTable()
62  {
63  static SymbolTable theSymbolTable;
64  return theSymbolTable; // Meyer's Singleton
65  }
66 
67  inline int
68  strNcmp (CStr a, CStr b, size_t len)
69  {
70  return a == b ? 0 : std::strncmp (a?a:"", b?b:"", len);
71  }
72  }
73 
74 
82  Symbol::Symbol (string&& definition)
83  : Literal{symbolTable().internedString (forward<string> (definition))}
84  { }
85 
86 
87  /* == predefined marker Symbols == */
88  Symbol Symbol::ANY = "*";
89  Symbol Symbol::EMPTY = "";
90  Symbol Symbol::BOTTOM = "⟂";
91  Symbol Symbol::FAILURE = "↯";
92 
93  // see also: lib/format-obj.cpp
94  // We can not share these definitions due to undefined static init order
95 
96 
97 
98 
101  bool
102  Literal::operator== (CStr charPtr) const
103  {
104  return 0 == strNcmp (this->str_, charPtr, STRING_MAX_RELEVANT);
105  }
106 
107 
112  size_t
113  hash_value (Literal literal)
114  {
115  size_t hash=0;
116  if (literal)
117  {
118  size_t cnt = 1;
119  const char *pos = literal;
120  for ( ; cnt <= STRING_MAX_RELEVANT and *pos ; ++cnt, ++pos )
121  hash_combine(hash, *pos);
122  }
123 
124  return hash;
125  }
126 
128  size_t
130  {
131  return sym? boost::hash_value (sym.c())
132  : 0;
133  }
134 
135 
136 
137 } // namespace lib
size_t hash_value(Symbol sym)
hash value for Symbols is directly based on the symbol table entry
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.
Token or Atom with distinct identity.
Definition: symbol.hpp:126
Registry table for automatically interned strings.
Marker types to indicate a literal string and a Symbol.
HashVal hash_value(QueryText const &entry)
support using queries in hashtables.
Definition: query-text.cpp:61
Table for automatically interned strings.