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)
5  2009, 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 
28 #include "lib/symbol.hpp"
29 #include "lib/symbol-table.hpp"
30 
31 #include <boost/functional/hash.hpp>
32 #include <cstddef>
33 #include <cstring>
34 #include <string>
35 
36 using std::size_t;
37 using std::string;
38 using std::forward;
39 using boost::hash_combine;
40 
41 
42 
43 
44 namespace lib {
45 
46  const size_t STRING_MAX_RELEVANT = LUMIERA_IDSTRING_MAX_RELEVANT;
47 
48 
49  namespace { // global symbol table
50 
52  symbolTable()
53  {
54  static SymbolTable theSymbolTable;
55  return theSymbolTable; // Meyer's Singleton
56  }
57 
58  inline int
59  strNcmp (CStr a, CStr b, size_t len)
60  {
61  return a == b ? 0 : std::strncmp (a?a:"", b?b:"", len);
62  }
63  }
64 
65 
73  Symbol::Symbol (string&& definition)
74  : Literal{symbolTable().internedString (forward<string> (definition))}
75  { }
76 
77 
78  /* == predefined marker Symbols == */
79  Symbol Symbol::ANY = "*";
80  Symbol Symbol::EMPTY = "";
81  Symbol Symbol::BOTTOM = "⟂";
82  Symbol Symbol::FAILURE = "↯";
83 
84  // see also: lib/format-obj.cpp
85  // We can not share these definitions due to undefined static init order
86 
87 
88 
89 
92  bool
93  Literal::operator== (CStr charPtr) const
94  {
95  return 0 == strNcmp (this->str_, charPtr, STRING_MAX_RELEVANT);
96  }
97 
98 
103  HashVal
104  hash_value (Literal literal)
105  {
106  size_t hash=0;
107  if (literal)
108  {
109  size_t cnt = 1;
110  const char *pos = literal;
111  for ( ; cnt <= STRING_MAX_RELEVANT and *pos ; ++cnt, ++pos )
112  hash_combine (hash, *pos);
113  }
114 
115  return hash;
116  }
117 
119  HashVal
121  {
122  return sym? boost::hash_value (sym.c())
123  : 0;
124  }
125 
126 
127 
128 } // namespace lib
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.
Token or Atom with distinct identity.
Definition: symbol.hpp:117
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:52
Table for automatically interned strings.
size_t HashVal
a STL compatible hash value
Definition: hash-value.h:52
HashVal hash_value(Symbol sym)
hash value for Symbols is directly based on the symbol table entry