-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathStringIdentifier.h
62 lines (50 loc) · 1.85 KB
/
StringIdentifier.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/*
This file is part of the Util library.
Copyright (C) 2007-2012 Benjamin Eikel <[email protected]>
Copyright (C) 2007-2012 Claudius Jähn <[email protected]>
Copyright (C) 2007-2012 Ralf Petring <[email protected]>
This library is subject to the terms of the Mozilla Public License, v. 2.0.
You should have received a copy of the MPL along with this library; see the
file LICENSE. If not, you can obtain one at http://mozilla.org/MPL/2.0/.
*/
#ifndef STRINGIDENTIFIER_H_
#define STRINGIDENTIFIER_H_
#include <cstddef>
#include <cstdint>
#include <string>
#include <unordered_map>
namespace Util {
/**
* String identifiers map a string to a number.
* This mapping is guaranteed to be unique during one execution of the program.
*
* @author Claudius Jähn, Benjamin Eikel
* @ingroup strings
*/
class StringIdentifier {
uint32_t value;
public:
StringIdentifier() : value(0) {}
explicit StringIdentifier( uint32_t _id) : value(_id) {}
/*implicit*/ StringIdentifier( const std::string & str) : value(calcId(str)) {}
uint32_t getValue()const { return value; }
UTILAPI std::string toString()const;
StringIdentifier & operator=(const std::string & str){
value = calcId(str);
return *this;
}
bool empty()const { return value==0; }
bool operator==(const StringIdentifier & other)const { return value == other.value; }
bool operator!=(const StringIdentifier & other)const { return value != other.value; }
bool operator<(const StringIdentifier & other)const { return value < other.value; }
private:
UTILAPI static uint32_t calcId(const std::string & s);
UTILAPI static uint32_t calcHash(const std::string & s);
};
}
namespace std{
template <> struct hash<Util::StringIdentifier> {
size_t operator()(const Util::StringIdentifier & id) const noexcept { return id.getValue(); }
};
}
#endif /* STRINGIDENTIFIER_H_ */