Lumiera  0.pre.03
»edityourfreedom«
entry-id.hpp
Go to the documentation of this file.
1 /*
2  ENTRY-ID.hpp - plain symbolic and hash ID used for accounting
3 
4  Copyright (C) Lumiera.org
5  2010, 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 
23 
42 #ifndef LIB_IDI_ENTRY_ID_H
43 #define LIB_IDI_ENTRY_ID_H
44 
45 
46 #include "lib/error.hpp"
47 #include "lib/hash-indexed.hpp"
48 #include "lib/idi/genfunc.hpp"
49 #include "lib/util.hpp"
50 
51 #include <boost/functional/hash.hpp>
52 #include <boost/operators.hpp>
53 #include <string>
54 
55 
56 namespace lib {
57 
65 namespace idi {
66 
67  namespace error = lumiera::error;
68 
69  using std::string;
70  using std::ostream;
71 
75  using lib::hash::LuidH;
76  using lib::HashVal;
77 
78  namespace {
85  const size_t KNUTH_MAGIC = 2654435761;
86 
87 
109  inline LuidH
110  buildHash (string const& sym, HashVal seed =0)
111  {
112  size_t l = sym.length();
113  if (l > 1) boost::hash_combine(seed, KNUTH_MAGIC * sym[l-1]);
114  if (l > 2) boost::hash_combine(seed, KNUTH_MAGIC * sym[l-2]);
115  if (l > 3) boost::hash_combine(seed, KNUTH_MAGIC * sym[l-3]);
116  if (l > 4) boost::hash_combine(seed, KNUTH_MAGIC * sym[l-4]);
117 
118  boost::hash_combine(seed, sym);
119  lumiera_uid tmpLUID;
120  lumiera_uid_set_ptr (&tmpLUID, reinterpret_cast<void*> (seed));
121  return reinterpret_cast<LuidH&> (tmpLUID);
122  }
123  }
124 
125 
126 
127  template<class TY>
128  struct EntryID;
129 
130 
136  : public boost::equality_comparable<BareEntryID>
137  {
138 
139  string symbol_;
141 
142  protected:
149  BareEntryID (string const& symbolID, HashVal seed =0)
150  : symbol_(symbolID)
151  , hash_(buildHash (symbol_, seed))
152  { }
153 
154  public:
155  /* default copyable and assignable */
156 
157 
158  bool
159  isValid() const
160  {
161  return bool(hash_);
162  }
163 
164  string const&
165  getSym() const
166  {
167  return symbol_;
168  }
169 
170  LuidH const&
171  getHash() const
172  {
173  return hash_;
174  }
175 
176  operator string() const;
177 
178 
181  : public std::unary_function<BareEntryID, size_t>
182  {
183  size_t operator() (BareEntryID const& obj) const { return obj.getHash(); }
184  };
185 
186 
187  template<typename TAR>
188  EntryID<TAR> const& recast() const;
189 
190  };
191 
192 
213  template<class TY>
214  struct EntryID
215  : BareEntryID
216  , boost::totally_ordered< EntryID<TY> >
217  {
218 
222  { }
223 
228  explicit
229  EntryID (string const& symbolID)
230  : BareEntryID (util::sanitise(symbolID), getTypeHash<TY>())
231  { }
232  explicit
233  EntryID (const char* symbolID)
234  : BareEntryID (util::sanitise(symbolID), getTypeHash<TY>())
235  { }
236 
241  explicit
242  EntryID (Symbol const& internalSymbol)
243  : BareEntryID (string(internalSymbol), getTypeHash<TY>())
244  { }
245 
246 
252  static bool
253  canRecast (BareEntryID const& bID)
254  {
255  return bID.getHash() == buildHash (bID.getSym(), getTypeHash<TY>());
256  }
257 
258  static EntryID const&
259  recast (BareEntryID const& bID)
260  {
261  if (!canRecast(bID))
262  throw error::Logic ("unable to recast EntryID: desired type "
263  "doesn't match original definition"
264  , error::LUMIERA_ERROR_WRONG_TYPE);
265  return static_cast<EntryID const&> (bID);
266  }
267 
268 
269  operator string() const;
270 
271  friend bool operator< (EntryID const& i1, EntryID const& i2) { return i1.getSym() < i2.getSym(); }
272  };
273 
274 
275  inline bool
276  operator== (BareEntryID const& i1, BareEntryID const& i2)
277  {
278  return i1.getHash() == i2.getHash();
279  }
280 
281 
282 
283 
295  template<typename TAR>
296  EntryID<TAR> const&
298  {
299  return EntryID<TAR>::recast(*this);
300  }
301 
302 
303  inline
304  BareEntryID::operator string() const
305  {
306  return "bID-"+lib::idi::format::instance_hex_format(symbol_, hash_);
307  }
308 
309  template<class TY>
310  inline
311  EntryID<TY>::operator string() const
312  {
313  return "ID<"+typeSymbol<TY>()+">-"+EntryID::getSym();
314  }
315 
316 
317 
318 }} // namespace lib::idi
319 #endif /*LIB_IDI_ENTRY_ID_H*/
type erased baseclass for building a combined hash and symbolic ID.
Definition: entry-id.hpp:135
static EntryID const & recast(BareEntryID const &bID)
Definition: entry-id.hpp:259
BareEntryID(string const &symbolID, HashVal seed=0)
Not to be created stand-alone.
Definition: entry-id.hpp:149
string instance_hex_format(string const &prefix, size_t instanceNr)
Definition: genfunc.cpp:56
EntryID(string const &symbolID)
case-2: explicitly specify a symbolic ID to use.
Definition: entry-id.hpp:229
EntryID(const char *symbolID)
Definition: entry-id.hpp:233
typed symbolic and hash ID for asset-like position accounting.
Definition: entry-id.hpp:128
EntryID()
case-1: auto generated symbolic ID
Definition: entry-id.hpp:220
bool isValid() const
Definition: entry-id.hpp:159
Implementation namespace for support and library code.
Generic functions to build identification schemes.
static bool canRecast(BareEntryID const &bID)
Definition: entry-id.hpp:253
string const & getSym() const
Definition: entry-id.hpp:165
Token or Atom with distinct identity.
Definition: symbol.hpp:116
EntryID< TAR > const & recast() const
try to upcast this BareEntryID to a fully typed EntryID.
Definition: entry-id.hpp:297
bool operator==(BareEntryID const &i1, BareEntryID const &i2)
Definition: entry-id.hpp:276
Tiny helper functions and shortcuts to be used everywhere Consider this header to be effectively incl...
string sanitise(string const &org)
produce an identifier based on the given string.
Definition: util.cpp:65
bool operator<(Goal::QueryID const &id1, Goal::QueryID const &id2)
Definition: query.hpp:198
LumieraError< LERR_(LOGIC)> Logic
Definition: error.hpp:212
HashVal getTypeHash()
Definition: genfunc.hpp:168
A template for generating hash based ID tags carrying compile-time type info.
Lumiera error handling (C++ interface).
string typeSymbol()
Short readable type identifier, not necessarily unique or complete.
Definition: genfunc.hpp:87
Hash implementation based on a lumiera unique object id (LUID) When invoking the default ctor...
size_t HashVal
a STL compatible hash value
Definition: hash-value.h:56
EntryID(Symbol const &internalSymbol)
case-2b: rely on an internal, already sanitised symbol.
Definition: entry-id.hpp:242
string generateSymbolicID()
build a per-type identifier, with type prefix and running counter.
Definition: genfunc.hpp:142
unsigned char lumiera_uid[16]
storage for a Lumiera unique ID, based on a 128bit random number
Definition: hash-value.h:45
LuidH const & getHash() const
Definition: entry-id.hpp:171
using BareEntryID derived objects as keys within std::unordered_map
Definition: entry-id.hpp:180
void lumiera_uid_set_ptr(lumiera_uid *luid, void *ptr)
Store a generic pointer in a luid.
Definition: luid.c:42