MeVisLab Toolbox Reference
Loading...
Searching...
No Matches
mlConstantString.h
Go to the documentation of this file.
1/*************************************************************************************
2**
3** Copyright 2016, MeVis Medical Solutions AG
4**
5** The user may use this file in accordance with the license agreement provided with
6** the Software or, alternatively, in accordance with the terms contained in a
7** written agreement between the user and MeVis Medical Solutions AG.
8**
9** For further information use the contact form at https://www.mevislab.de/contact
10**
11**************************************************************************************/
12
13#ifndef ML_CONSTANT_STRING_H
14#define ML_CONSTANT_STRING_H
15
17
18#include "mlUtilsSystem.h"
19
20#include <ThirdPartyWarningsDisable.h>
21#include <string>
22#include <boost/functional/hash.hpp>
23#include <ThirdPartyWarningsRestore.h>
24
26
27class ConstantString;
28
31{
32public:
33 friend class ConstantString;
34
36 : _hash(0)
37 , _refCount(0)
38 {}
39
40 ConstantStringValue(const std::string& value)
41 : _value(value)
42 , _refCount(0)
43 {
44 boost::hash<std::string> hasher;
45 _hash = hasher(_value);
46 }
47
48 void incRefCount() const
49 {
50 _refCount++;
51 }
52
53 void decRefCount() const
54 {
55 _refCount--;
56 if (_refCount == 0) {
57 delete this;
58 }
59 }
60
61 size_t getHash() const
62 {
63 return _hash;
64 }
65
66protected:
68
69private:
70 std::string _value;
71 size_t _hash;
72 mutable MLuint32 _refCount;
73};
74
78{
79public:
81 : _value(nullptr)
82 {}
83 ConstantString(const std::string& value)
84 {
85 if (!value.empty()) {
86 _value = new ConstantStringValue(value);
87 _value->incRefCount();
88 }
89 else {
90 _value = nullptr;
91 }
92 }
94 {
95 _value = other._value;
96 if (_value) {
97 _value->incRefCount();
98 }
99 }
100
102 {
103 if (_value != other._value) {
104 if (_value) {
105 _value->decRefCount();
106 }
107 _value = other._value;
108 if (_value) {
109 _value->incRefCount();
110 }
111 }
112 }
113
115 {
116 if (_value) {
117 _value->decRefCount();
118 _value = nullptr;
119 }
120 }
121
122 bool operator<(const ConstantString& other) const
123 {
124 return toString() < other.toString();
125 }
126
127 bool operator==(const ConstantString& other) const
128 {
129 if (other._value == _value) {
130 // same string pointers
131 return true;
132 }
133 else {
134 if (_value && other._value) {
135 // first compare the hashes
136 if (_value->getHash() == other._value->getHash()) {
137 // compare the real strings...
138 return _value->_value == other._value->_value;
139 }
140 else {
141 return false;
142 }
143 }
144 else if (!_value && !other._value) {
145 // both null
146 return true;
147 }
148 else {
149 // one null, one different
150 return false;
151 }
152 }
153 }
154
155 operator std::string() const
156 {
157 if (_value) {
158 return _value->_value;
159 }
160 else {
161 return std::string();
162 }
163 }
164
165 operator const std::string&() const
166 {
167 if (_value) {
168 return _value->_value;
169 }
170 else {
171 return _emptyString;
172 }
173 }
174
175 const std::string& toString() const
176 {
177 if (_value) {
178 return _value->_value;
179 }
180 else {
181 return _emptyString;
182 }
183 }
184
185 bool empty() const
186 {
187 return _value == nullptr;
188 }
189
190 size_t getHash() const
191 {
192 if (_value) {
193 return _value->getHash();
194 }
195 else {
196 return 0;
197 }
198 }
199
200private:
201 ConstantStringValue* _value;
202
203 static ML_UTILS_EXPORT std::string _emptyString;
204};
205
206inline std::size_t hash_value(ConstantString const& b)
207{
208 return b.getHash();
209}
210
212
213#endif
Shared container for the string value and its hash.
ConstantStringValue(const std::string &value)
A ConstantString that contains a std::string and its hash.
ConstantString(const std::string &value)
bool operator==(const ConstantString &other) const
void operator=(const ConstantString &other)
size_t getHash() const
ConstantString(const ConstantString &other)
bool operator<(const ConstantString &other) const
const std::string & toString() const
size_t hash_value(MLGlobalFunctionKey const &key)
Calculates the hash key from last string segment.
Definition mlCallGraph.h:63
Target mlrange_cast(Source arg)
Generic version of checked ML casts.
unsigned int MLuint32
Definition mlTypeDefs.h:185
#define ML_UTILS_EXPORT
Definition mlUtilities.h:18