Safir SDK Core
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
InstanceId.h
Go to the documentation of this file.
1 /******************************************************************************
2 *
3 * Copyright Saab AB, 2008-2013 (http://safir.sourceforge.net)
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 
31 #include <Safir/Dob/Typesystem/Internal/KernelDefs.h>
32 #include <Safir/Dob/Typesystem/Internal/InternalOperations.h>
33 #include <string>
34 #include <sstream>
35 
36 namespace Safir
37 {
38 namespace Dob
39 {
40 namespace Typesystem
41 {
48  class InstanceId
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 
82  explicit InstanceId(const Dob::Typesystem::Int64 id):
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_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 }
235 }
236 #endif
237 
InstanceId(const Dob::Typesystem::Int64 id)
Constructor.
Definition: InstanceId.h:82
Int32 Utf8StringLength() const
Get the length of the string when converted to UTF-8 encoding.
Definition: InstanceId.h:191
bool operator!=(const InstanceId &other) const
Inequality operator.
Definition: InstanceId.h:134
InstanceId(const Int64 id, const std::wstring &idStr)
Constructor.
Definition: InstanceId.h:94
static InstanceId GenerateRandom()
Returns a random instance id.
Definition: InstanceId.h:54
Meant to be used when something goes very wrong.
Definition: Exceptions.h:360
bool operator<(const InstanceId &other) const
Less-than operator.
Definition: InstanceId.h:145
Class containing the identity of an instance.
Definition: InstanceId.h:48
const std::wstring & GetRawString() const
Get the string that was used to create this id.
Definition: InstanceId.h:183
void RemoveString()
Remove the included string from the instance id.
Definition: InstanceId.h:117
DotsC_Int64 Int64
64 bit integer type.
Definition: Dob/Typesystem/Defs.h:72
UnderlyingType GetRawValue() const
Get the raw 64 bit integer identifier.
Definition: InstanceId.h:174
#define __WFILE__
Definition: Exceptions.h:31
InstanceId()
Default constructor.
Definition: InstanceId.h:61
Int64 UnderlyingType
Definition: InstanceId.h:167
DOTS_API const std::wstring ToString() const
Return a string representation of the instance id.
DOTS_API const std::string ToUtf8(const std::wstring &wstr)
Convert a std::wstring to UTF8-encoded std::string.
bool operator==(const InstanceId &other) const
Equality operator.
Definition: InstanceId.h:124
static std::wostream & operator<<(std::wostream &out, const ChannelId &channelId)
Definition: ChannelId.h:224
InstanceId(const std::wstring &id)
Constructor.
Definition: InstanceId.h:70
const std::string & Utf8String() const
Convert the string to UTF-8.
Definition: InstanceId.h:213
DotsC_Int32 Int32
32 bit integer type.
Definition: Dob/Typesystem/Defs.h:69