2009-09-24 19:38:11 +02:00
|
|
|
|
/*
|
2025-06-07 23:59:57 +02:00
|
|
|
|
Symbol(impl) - helpers for working with literal string IDs
|
2010-12-17 23:28:49 +01:00
|
|
|
|
|
Copyright: clarify and simplify the file headers
* Lumiera source code always was copyrighted by individual contributors
* there is no entity "Lumiera.org" which holds any copyrights
* Lumiera source code is provided under the GPL Version 2+
== Explanations ==
Lumiera as a whole is distributed under Copyleft, GNU General Public License Version 2 or above.
For this to become legally effective, the ''File COPYING in the root directory is sufficient.''
The licensing header in each file is not strictly necessary, yet considered good practice;
attaching a licence notice increases the likeliness that this information is retained
in case someone extracts individual code files. However, it is not by the presence of some
text, that legally binding licensing terms become effective; rather the fact matters that a
given piece of code was provably copyrighted and published under a license. Even reformatting
the code, renaming some variables or deleting parts of the code will not alter this legal
situation, but rather creates a derivative work, which is likewise covered by the GPL!
The most relevant information in the file header is the notice regarding the
time of the first individual copyright claim. By virtue of this initial copyright,
the first author is entitled to choose the terms of licensing. All further
modifications are permitted and covered by the License. The specific wording
or format of the copyright header is not legally relevant, as long as the
intention to publish under the GPL remains clear. The extended wording was
based on a recommendation by the FSF. It can be shortened, because the full terms
of the license are provided alongside the distribution, in the file COPYING.
2024-11-17 23:42:55 +01:00
|
|
|
|
Copyright (C)
|
2025-07-02 22:18:39 +02:00
|
|
|
|
2009,2017 Hermann Vosseler <Ichthyostega@web.de>
|
2010-12-17 23:28:49 +01:00
|
|
|
|
|
Copyright: clarify and simplify the file headers
* Lumiera source code always was copyrighted by individual contributors
* there is no entity "Lumiera.org" which holds any copyrights
* Lumiera source code is provided under the GPL Version 2+
== Explanations ==
Lumiera as a whole is distributed under Copyleft, GNU General Public License Version 2 or above.
For this to become legally effective, the ''File COPYING in the root directory is sufficient.''
The licensing header in each file is not strictly necessary, yet considered good practice;
attaching a licence notice increases the likeliness that this information is retained
in case someone extracts individual code files. However, it is not by the presence of some
text, that legally binding licensing terms become effective; rather the fact matters that a
given piece of code was provably copyrighted and published under a license. Even reformatting
the code, renaming some variables or deleting parts of the code will not alter this legal
situation, but rather creates a derivative work, which is likewise covered by the GPL!
The most relevant information in the file header is the notice regarding the
time of the first individual copyright claim. By virtue of this initial copyright,
the first author is entitled to choose the terms of licensing. All further
modifications are permitted and covered by the License. The specific wording
or format of the copyright header is not legally relevant, as long as the
intention to publish under the GPL remains clear. The extended wording was
based on a recommendation by the FSF. It can be shortened, because the full terms
of the license are provided alongside the distribution, in the file COPYING.
2024-11-17 23:42:55 +01:00
|
|
|
|
**Lumiera** is free software; you can redistribute it and/or modify it
|
|
|
|
|
|
under the terms of the GNU General Public License as published by the
|
|
|
|
|
|
Free Software Foundation; either version 2 of the License, or (at your
|
|
|
|
|
|
option) any later version. See the file COPYING for further details.
|
2010-12-17 23:28:49 +01:00
|
|
|
|
|
Copyright: clarify and simplify the file headers
* Lumiera source code always was copyrighted by individual contributors
* there is no entity "Lumiera.org" which holds any copyrights
* Lumiera source code is provided under the GPL Version 2+
== Explanations ==
Lumiera as a whole is distributed under Copyleft, GNU General Public License Version 2 or above.
For this to become legally effective, the ''File COPYING in the root directory is sufficient.''
The licensing header in each file is not strictly necessary, yet considered good practice;
attaching a licence notice increases the likeliness that this information is retained
in case someone extracts individual code files. However, it is not by the presence of some
text, that legally binding licensing terms become effective; rather the fact matters that a
given piece of code was provably copyrighted and published under a license. Even reformatting
the code, renaming some variables or deleting parts of the code will not alter this legal
situation, but rather creates a derivative work, which is likewise covered by the GPL!
The most relevant information in the file header is the notice regarding the
time of the first individual copyright claim. By virtue of this initial copyright,
the first author is entitled to choose the terms of licensing. All further
modifications are permitted and covered by the License. The specific wording
or format of the copyright header is not legally relevant, as long as the
intention to publish under the GPL remains clear. The extended wording was
based on a recommendation by the FSF. It can be shortened, because the full terms
of the license are provided alongside the distribution, in the file COPYING.
2024-11-17 23:42:55 +01:00
|
|
|
|
* *****************************************************************/
|
2009-09-24 19:38:11 +02:00
|
|
|
|
|
2021-01-23 16:45:04 +01:00
|
|
|
|
/** @file symbol-impl.cpp
|
2025-07-02 22:18:39 +02:00
|
|
|
|
** Implementation functionality to support definition of lib::Symbol.
|
|
|
|
|
|
** @see lib::Symbol
|
|
|
|
|
|
** @see control::CommandRegistry for usage example of the hash function.
|
|
|
|
|
|
** @see symbol-table.hpp for the implementation of _interned strings_
|
2009-09-24 19:38:11 +02:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-09-24 23:02:40 +02:00
|
|
|
|
#include "lib/symbol.hpp"
|
2017-04-08 02:27:51 +02:00
|
|
|
|
#include "lib/symbol-table.hpp"
|
2009-09-24 19:38:11 +02:00
|
|
|
|
|
|
|
|
|
|
#include <boost/functional/hash.hpp>
|
|
|
|
|
|
#include <cstddef>
|
2024-03-16 00:01:49 +01:00
|
|
|
|
#include <cstring>
|
2017-03-31 23:44:24 +02:00
|
|
|
|
#include <string>
|
2009-09-24 19:38:11 +02:00
|
|
|
|
|
|
|
|
|
|
using std::size_t;
|
2017-03-31 23:44:24 +02:00
|
|
|
|
using std::string;
|
2017-04-07 06:34:41 +02:00
|
|
|
|
using std::forward;
|
2009-09-24 19:38:11 +02:00
|
|
|
|
using boost::hash_combine;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-09-24 23:02:40 +02:00
|
|
|
|
namespace lib {
|
2009-09-24 19:38:11 +02:00
|
|
|
|
|
2012-12-11 04:07:06 +01:00
|
|
|
|
const size_t STRING_MAX_RELEVANT = LUMIERA_IDSTRING_MAX_RELEVANT;
|
2011-10-15 23:41:31 +02:00
|
|
|
|
|
2017-04-08 02:27:51 +02:00
|
|
|
|
namespace { // global symbol table
|
2017-04-07 19:25:21 +02:00
|
|
|
|
|
2017-05-02 01:37:17 +02:00
|
|
|
|
SymbolTable&
|
|
|
|
|
|
symbolTable()
|
|
|
|
|
|
{
|
|
|
|
|
|
static SymbolTable theSymbolTable;
|
|
|
|
|
|
return theSymbolTable; // Meyer's Singleton
|
|
|
|
|
|
}
|
2017-04-07 19:25:21 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* create Symbol by symbol table lookup.
|
2017-04-08 02:27:51 +02:00
|
|
|
|
* @note identical strings will be mapped to the same Symbol (embedded pointer)
|
|
|
|
|
|
* @warning potential lock contention, since each ctor call needs to do a lookup
|
2017-05-02 01:37:17 +02:00
|
|
|
|
* @remark since lumiera::LifecycleHook entries use a Symbol, the symbol table is
|
|
|
|
|
|
* implemented as Meyer's singleton and pulled up early, way before main()
|
2017-04-07 19:25:21 +02:00
|
|
|
|
*/
|
|
|
|
|
|
Symbol::Symbol (string&& definition)
|
2017-05-02 01:37:17 +02:00
|
|
|
|
: Literal{symbolTable().internedString (forward<string> (definition))}
|
2017-04-07 19:25:21 +02:00
|
|
|
|
{ }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* == predefined marker Symbols == */
|
2017-10-02 23:06:23 +02:00
|
|
|
|
Symbol Symbol::ANY = "*";
|
2017-09-30 02:34:56 +02:00
|
|
|
|
Symbol Symbol::EMPTY = "";
|
2017-04-07 19:25:21 +02:00
|
|
|
|
Symbol Symbol::BOTTOM = "⟂";
|
|
|
|
|
|
Symbol Symbol::FAILURE = "↯";
|
|
|
|
|
|
|
|
|
|
|
|
// see also: lib/format-obj.cpp
|
|
|
|
|
|
// We can not share these definitions due to undefined static init order
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-09-24 19:38:11 +02:00
|
|
|
|
|
2017-04-07 23:34:35 +02:00
|
|
|
|
/** generate hash value based on the Literal's contents.
|
|
|
|
|
|
* This function is intended to be picked up by ADL, and should be usable
|
|
|
|
|
|
* both with `std::hash` and `<boost/functional/hash.hpp>`. It is implemented
|
|
|
|
|
|
* similar as the boost::hash specialisation for std::string */
|
2024-11-03 21:03:34 +01:00
|
|
|
|
HashVal
|
2017-04-07 23:34:35 +02:00
|
|
|
|
hash_value (Literal literal)
|
2009-09-24 19:38:11 +02:00
|
|
|
|
{
|
|
|
|
|
|
size_t hash=0;
|
2017-04-07 23:34:35 +02:00
|
|
|
|
if (literal)
|
2009-10-30 05:15:26 +01:00
|
|
|
|
{
|
2018-10-12 23:43:36 +02:00
|
|
|
|
size_t cnt = 1;
|
2025-07-02 22:18:39 +02:00
|
|
|
|
CStr pos = literal;
|
2018-10-12 23:43:36 +02:00
|
|
|
|
for ( ; cnt <= STRING_MAX_RELEVANT and *pos ; ++cnt, ++pos )
|
2024-11-03 23:58:25 +01:00
|
|
|
|
hash_combine (hash, *pos);
|
2009-10-30 05:15:26 +01:00
|
|
|
|
}
|
2009-09-24 19:38:11 +02:00
|
|
|
|
|
|
|
|
|
|
return hash;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-04-07 23:34:35 +02:00
|
|
|
|
/** hash value for Symbols is directly based on the symbol table entry */
|
2024-11-03 21:03:34 +01:00
|
|
|
|
HashVal
|
2017-04-07 23:34:35 +02:00
|
|
|
|
hash_value (Symbol sym)
|
|
|
|
|
|
{
|
|
|
|
|
|
return sym? boost::hash_value (sym.c())
|
|
|
|
|
|
: 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2009-09-24 19:38:11 +02:00
|
|
|
|
|
|
|
|
|
|
|
2009-09-24 23:02:40 +02:00
|
|
|
|
} // namespace lib
|