MeVisLab Toolbox Reference
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
35 ConstantStringValue() : _hash(0),_refCount(0) {
36 }
37
38 ConstantStringValue(const std::string& value) :_value(value), _refCount(0) {
39 boost::hash<std::string> hasher;
40 _hash = hasher(_value);
41 }
42
43 void incRefCount() const
44 {
45 _refCount++;
46 }
47
48 void decRefCount() const
49 {
50 _refCount--;
51 if (_refCount == 0) {
52 delete this;
53 }
54 }
55
56 size_t getHash() const { return _hash; }
57
58protected:
61
62private:
63 std::string _value;
64 size_t _hash;
65 mutable MLuint32 _refCount;
66};
67
71{
72public:
74 }
75 ConstantString(const std::string& value) {
76 if (!value.empty()) {
77 _value = new ConstantStringValue(value);
78 _value->incRefCount();
79 } else {
80 _value = nullptr;
81 }
82 }
84 _value = other._value;
85 if (_value) {
86 _value->incRefCount();
87 }
88 }
89
91 if (_value != other._value) {
92 if (_value) {
93 _value->decRefCount();
94 }
95 _value = other._value;
96 if (_value) {
97 _value->incRefCount();
98 }
99 }
100 }
101
103 {
104 if (_value) {
105 _value->decRefCount();
106 _value = nullptr;
107 }
108 }
109
110 bool operator<(const ConstantString& other) const {
111 return toString() < other.toString();
112 }
113
114 bool operator==(const ConstantString& other) const {
115 if (other._value == _value) {
116 // same string pointers
117 return true;
118 } else {
119 if (_value && other._value) {
120 // first compare the hashes
121 if (_value->getHash() == other._value->getHash()) {
122 // compare the real strings...
123 return _value->_value == other._value->_value;
124 } else {
125 return false;
126 }
127 } else if (!_value && !other._value) {
128 // both null
129 return true;
130 } else {
131 // one null, one different
132 return false;
133 }
134 }
135 }
136
137 operator std::string() const {
138 if (_value) {
139 return _value->_value;
140 } else {
141 return std::string();
142 }
143 }
144
145 operator const std::string&() const {
146 if (_value) {
147 return _value->_value;
148 } else {
149 return _emptyString;
150 }
151 }
152
153 const std::string& toString() const {
154 if (_value) {
155 return _value->_value;
156 } else {
157 return _emptyString;
158 }
159 }
160
161 bool empty() const {
162 return _value == nullptr;
163 }
164
165 size_t getHash() const {
166 if (_value) {
167 return _value->getHash();
168 } else {
169 return 0;
170 }
171 }
172
173private:
174 ConstantStringValue* _value;
175
176 static ML_UTILS_EXPORT std::string _emptyString;
177};
178
179inline std::size_t hash_value(ConstantString const& b)
180{
181 return b.getHash();
182}
183
185
186#endif
187
Shared container for the string value and its hash.
ConstantStringValue(const std::string &value)
A ConstantString which 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 has key from last string segment.
Definition mlCallGraph.h:59
Target mlrange_cast(Source arg)
Generic version of checked ML casts.
unsigned int MLuint32
Definition mlTypeDefs.h:185
#define ML_UTILS_EXPORT
Defines platform dependent DLL export macro for mlUtils.
Definition mlUtilities.h:20