Safir SDK Core
Loading...
Searching...
No Matches
InstanceId.h
Go to the documentation of this file.
1/******************************************************************************
2*
3* Copyright Saab AB, 2008-2013 (http://safirsdkcore.com)
4*
5* Created by: Lars Hagström / stlrha
6*
7*******************************************************************************
8*
9* This file is part of Safir SDK Core.
10*
11* Safir SDK Core is free software: you can redistribute it and/or modify
12* it under the terms of version 3 of the GNU General Public License as
13* published by the Free Software Foundation.
14*
15* Safir SDK Core is distributed in the hope that it will be useful,
16* but WITHOUT ANY WARRANTY; without even the implied warranty of
17* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18* GNU General Public License for more details.
19*
20* You should have received a copy of the GNU General Public License
21* along with Safir SDK Core. If not, see <http://www.gnu.org/licenses/>.
22*
23******************************************************************************/
24
25#ifndef __DOTS_INSTANCE_ID_H__
26#define __DOTS_INSTANCE_ID_H__
27
28#include <functional>
32#include <Safir/Dob/Typesystem/Internal/InternalOperations.h>
33#include <string>
34#include <sstream>
35
36namespace Safir
37{
38namespace Dob
39{
40namespace Typesystem
41{
49 {
50 public:
54 static InstanceId GenerateRandom() {return InstanceId(Internal::GenerateRandom64Bit());}
55
61 InstanceId(): m_instanceId(-1) {}
62
70 InstanceId(const std::wstring& id) :
71 m_instanceId(Internal::Generate64BitHash(id)),
72 m_instanceIdStr(id)
73 {}
74
83 m_instanceId(id) {}
84
85
94 InstanceId(const Int64 id, const std::wstring & idStr):
95 m_instanceId(id),
96 m_instanceIdStr(idStr)
97 {
98#ifndef NDEBUG
99 if (!m_instanceIdStr.empty() && m_instanceId != Internal::Generate64BitHash(idStr))
100 {
101 std::wostringstream ostr;
102 ostr << "InstanceId two-argument constructor got an inconsistent id. Got ("
103 << id << ", '" << idStr << "'), but the string evaluates to " << Internal::Generate64BitHash(idStr) << ".";
104 throw SoftwareViolationException(ostr.str(),__WFILE__,__LINE__);
105 }
106#endif
107 }
108
117 void RemoveString() {m_instanceIdStr.clear(); m_CachedUtf8String.clear();}
118
124 bool operator ==(const InstanceId& other) const
125 {
126 return m_instanceId == other.m_instanceId;
127 }
128
134 bool operator !=(const InstanceId & other) const
135 {
136 return !(*this==other);
137 }
138
145 bool operator < (const InstanceId & other) const
146 {
147 return m_instanceId < other.m_instanceId;
148 }
149
161 DOTS_CPP_API const std::wstring ToString() const;
162
163
168
174 UnderlyingType GetRawValue() const {return m_instanceId;}
175
183 const std::wstring & GetRawString() const {return m_instanceIdStr;}
184
192 {
193 if (m_instanceIdStr.empty())
194 {
195 return 0;
196 }
197
198 if (m_CachedUtf8String.empty())
199 {
200 m_CachedUtf8String = Utilities::ToUtf8(m_instanceIdStr);
201 }
202
203 return static_cast<Int32>(m_CachedUtf8String.length() + 1);
204 }
205
213 const std::string & Utf8String() const
214 {
215 if (!m_instanceIdStr.empty() && m_CachedUtf8String.empty())
216 {
217 m_CachedUtf8String = Utilities::ToUtf8(m_instanceIdStr);
218 }
219 return m_CachedUtf8String;
220 }
221
224 private:
225 UnderlyingType m_instanceId;
226 std::wstring m_instanceIdStr;
227
228 mutable std::string m_CachedUtf8String;
229 };
230
231 static inline std::wostream & operator << (std::wostream& out, const InstanceId& instanceId)
232 {return out << instanceId.ToString();}
233
234 //Make it possible to use InstanceId as key in a dictionaries.
235 inline std::size_t hash_value(const Safir::Dob::Typesystem::InstanceId& val)
236 {return std::hash<InstanceId::UnderlyingType>()(val.GetRawValue());}
237}
238}
239}
240#endif
241
#define __WFILE__
Definition Exceptions.h:31
This namespace contains all the functionality and definitions of the SAFIR SDK.
Definition Backdoor.h:31
std::size_t hash_value(const Safir::Dob::Typesystem::ChannelId &val)
Definition ChannelId.h:228
DotsC_Int32 Int32
32 bit integer type.
Definition Defs.h:66
DotsC_Int64 Int64
64 bit integer type.
Definition Defs.h:69
static std::wostream & operator<<(std::wostream &out, const ChannelId &channelId)
Definition ChannelId.h:224
DOTS_CPP_API const std::string ToUtf8(const std::wstring &wstr)
Convert a std::wstring to UTF8-encoded std::string.
Meant to be used when something goes very wrong.
Definition Exceptions.h:364
Class containing the identity of an instance.
Definition InstanceId.h:49
DOTS_CPP_API const std::wstring ToString() const
Return a string representation of the instance id.
InstanceId(const std::wstring &id)
Constructor.
Definition InstanceId.h:70
bool operator==(const InstanceId &other) const
Equality operator.
Definition InstanceId.h:124
InstanceId()
Default constructor.
Definition InstanceId.h:61
void RemoveString()
Remove the included string from the instance id.
Definition InstanceId.h:117
static InstanceId GenerateRandom()
Returns a random instance id.
Definition InstanceId.h:54
bool operator<(const InstanceId &other) const
Less-than operator.
Definition InstanceId.h:145
InstanceId(const Int64 id, const std::wstring &idStr)
Constructor.
Definition InstanceId.h:94
const std::wstring & GetRawString() const
Get the string that was used to create this id.
Definition InstanceId.h:183
Int64 UnderlyingType
Definition InstanceId.h:167
Int32 Utf8StringLength() const
Get the length of the string when converted to UTF-8 encoding.
Definition InstanceId.h:191
UnderlyingType GetRawValue() const
Get the raw 64 bit integer identifier.
Definition InstanceId.h:174
const std::string & Utf8String() const
Convert the string to UTF-8.
Definition InstanceId.h:213
bool operator!=(const InstanceId &other) const
Inequality operator.
Definition InstanceId.h:134
InstanceId(const Dob::Typesystem::Int64 id)
Constructor.
Definition InstanceId.h:82
#define DOTS_CPP_API
Definition Defs.h:33