Lumiera 0.pre.04
»edit your freedom«
Loading...
Searching...
No Matches
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)
5 2010, 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
14
40#ifndef LIB_IDI_ENTRY_ID_H
41#define LIB_IDI_ENTRY_ID_H
42
43
44#include "lib/error.hpp"
45#include "lib/hash-indexed.hpp"
46#include "lib/idi/genfunc.hpp"
47#include "lib/util.hpp"
48
49#include <boost/functional/hash.hpp>
50#include <compare>
51#include <string>
52
53
54namespace lib {
55
63namespace idi {
64
65 namespace error = lumiera::error;
66
67 using std::string;
68 using std::ostream;
69
73 using lib::hash::LuidH;
74 using lib::HashVal;
75
76 namespace {
83 const size_t KNUTH_MAGIC = 2654435761;
84
85
107 inline LuidH
108 buildHash (string const& sym, HashVal seed =0)
109 {
110 size_t l = sym.length();
111 if (l > 1) boost::hash_combine(seed, KNUTH_MAGIC * sym[l-1]);
112 if (l > 2) boost::hash_combine(seed, KNUTH_MAGIC * sym[l-2]);
113 if (l > 3) boost::hash_combine(seed, KNUTH_MAGIC * sym[l-3]);
114 if (l > 4) boost::hash_combine(seed, KNUTH_MAGIC * sym[l-4]);
115
116 boost::hash_combine(seed, sym);
117 lumiera_uid tmpLUID;
118 lumiera_uid_set_ptr (&tmpLUID, reinterpret_cast<void*> (seed));
119 return reinterpret_cast<LuidH&> (tmpLUID);
120 }
121 }
122
123
124
125 template<class TY>
126 struct EntryID;
127
128
134 {
135
136 string symbol_;
138
139 protected:
146 BareEntryID (string const& symbolID, HashVal seed)
147 : symbol_(symbolID)
148 , hash_(buildHash (symbol_, seed))
149 { }
150
152 explicit
153 BareEntryID (string const& symbolID)
154 : symbol_(symbolID)
155 , hash_{} // random
156 { }
157
158 public:
159 /* default copyable and assignable */
160
161
162 bool
163 isValid() const
164 {
165 return bool(hash_);
166 }
167
168 string const&
169 getSym() const
170 {
171 return symbol_;
172 }
173
174 LuidH const&
175 getHash() const
176 {
177 return hash_;
178 }
179
180 operator string() const;
181
182
185 {
186 size_t operator() (BareEntryID const& obj) const { return obj.getHash(); }
187 };
188
189
190 template<typename TAR>
191 EntryID<TAR> const& recast() const;
192
193 };
194
195
216 template<class TY>
217 struct EntryID
219 {
220
224 { }
225
230 explicit
231 EntryID (string const& symbolID)
232 : BareEntryID (util::sanitise(symbolID), getTypeHash<TY>())
233 { }
234 explicit
235 EntryID (CStr symbolID)
236 : BareEntryID (util::sanitise(symbolID), getTypeHash<TY>())
237 { }
238
243 explicit
244 EntryID (Symbol const& internalSymbol)
245 : BareEntryID (string(internalSymbol), getTypeHash<TY>())
246 { }
247
248
254 static bool
256 {
257 return bID.getHash() == buildHash (bID.getSym(), getTypeHash<TY>());
258 }
259
260 static EntryID const&
261 recast (BareEntryID const& bID)
262 {
263 if (!canRecast(bID))
264 throw error::Logic ("unable to recast EntryID: desired type "
265 "doesn't match original definition"
266 , error::LUMIERA_ERROR_WRONG_TYPE);
267 return static_cast<EntryID const&> (bID);
268 }
269
270 explicit
271 operator string() const;
272
273 friend auto operator<=> (EntryID const& i1, EntryID const& i2) { return i1.getSym() <=> i2.getSym(); }
274 };
275
276 inline bool
277 operator== (BareEntryID const& i1, BareEntryID const& i2)
278 {
279 return i1.getHash() == i2.getHash();
280 }
281 // Note: since we allow comparison only between EntryIDs of same type
282 // and also feed-down the symbol into the hash value, both equality
283 // and (total) ordering mesh up perfectly.
284
285
286
287
293 struct RandID
295 {
296 RandID (string const& symbolID)
297 : BareEntryID{util::sanitise (symbolID)}
298 { }
299 RandID (CStr symbolID)
300 : BareEntryID{util::sanitise (symbolID)}
301 { }
302 RandID (Symbol const& internalSymbol)
303 : BareEntryID{string{internalSymbol}}
304 { }
305 };
306
307
319 template<typename TAR>
320 EntryID<TAR> const&
322 {
323 return EntryID<TAR>::recast(*this);
324 }
325
326
327 inline
328 BareEntryID::operator string() const
329 {
330 return "bID-"+lib::idi::format::instance_hex_format(symbol_, hash_);
331 }
332
333 template<class TY>
334 inline
336 {
337 return "ID<"+typeSymbol<TY>()+">-"+EntryID::getSym();
338 }
339
340
341
342}} // namespace lib::idi
343#endif /*LIB_IDI_ENTRY_ID_H*/
Token or Atom with distinct identity.
Definition symbol.hpp:120
Hash implementation based on a lumiera unique object id (LUID) When invoking the default ctor,...
type erased baseclass for building a combined hash and symbolic ID.
Definition entry-id.hpp:134
LuidH const & getHash() const
Definition entry-id.hpp:175
EntryID< TAR > const & recast() const
bool isValid() const
Definition entry-id.hpp:163
string const & getSym() const
Definition entry-id.hpp:169
BareEntryID(string const &symbolID, HashVal seed)
Not to be created stand-alone.
Definition entry-id.hpp:146
BareEntryID(string const &symbolID)
store the symbol but use a random hash part
Definition entry-id.hpp:153
Lumiera error handling (C++ interface).
const char * CStr
Definition error.hpp:42
Generic functions to build identification schemes.
A template for generating hash based ID tags carrying compile-time type info.
unsigned char lumiera_uid[16]
storage for a Lumiera unique ID, based on a 128bit random number
Definition hash-value.h:40
void lumiera_uid_set_ptr(lumiera_uid *luid, void *ptr)
Store a generic pointer in a luid.
Definition luid.c:33
const size_t KNUTH_MAGIC
lousy old tinkerer's trick: hash values with poor distribution can be improved by spreading the input...
Definition entry-id.hpp:83
LuidH buildHash(string const &sym, HashVal seed=0)
build up a hash value, packaged as LUID.
Definition entry-id.hpp:108
string instance_hex_format(string const &prefix, size_t instanceNr)
Definition genfunc.cpp:47
bool operator==(BareEntryID const &i1, BareEntryID const &i2)
Definition entry-id.hpp:277
HashVal getTypeHash()
Definition genfunc.hpp:160
string generateSymbolicID()
build a per-type identifier, with type prefix and running counter.
Definition genfunc.hpp:136
string typeSymbol()
Short readable type identifier, not necessarily unique or complete.
Definition genfunc.hpp:78
Implementation namespace for support and library code.
size_t HashVal
a STL compatible hash value
Definition hash-value.h:52
LumieraError< LERR_(LOGIC)> Logic
Definition error.hpp:207
basic_ostream< char, char_traits< char > > ostream
using BareEntryID derived objects as keys within std::unordered_map
Definition entry-id.hpp:185
size_t operator()(BareEntryID const &obj) const
Definition entry-id.hpp:186
typed symbolic and hash ID for asset-like position accounting.
Definition entry-id.hpp:219
friend auto operator<=>(EntryID const &i1, EntryID const &i2)
Definition entry-id.hpp:273
EntryID()
case-1: auto generated symbolic ID
Definition entry-id.hpp:222
EntryID(CStr symbolID)
Definition entry-id.hpp:235
static EntryID const & recast(BareEntryID const &bID)
Definition entry-id.hpp:261
static bool canRecast(BareEntryID const &bID)
Definition entry-id.hpp:255
EntryID(string const &symbolID)
case-2: explicitly specify a symbolic ID to use.
Definition entry-id.hpp:231
EntryID(Symbol const &internalSymbol)
case-2b: rely on an internal, already sanitised symbol.
Definition entry-id.hpp:244
Entry-ID with a symbolic tag but just a plain random hash part.
Definition entry-id.hpp:295
RandID(CStr symbolID)
Definition entry-id.hpp:299
RandID(Symbol const &internalSymbol)
Definition entry-id.hpp:302
RandID(string const &symbolID)
Definition entry-id.hpp:296
Tiny helper functions and shortcuts to be used everywhere Consider this header to be effectively incl...