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/InternalOperations.h>
32 #include <string>
33 #include <sstream>
34 
35 namespace Safir
36 {
37 namespace Dob
38 {
39 namespace Typesystem
40 {
47  class InstanceId
48  {
49  public:
53  static InstanceId GenerateRandom() {return InstanceId(Internal::GenerateRandom64Bit());}
54 
60  InstanceId(): m_instanceId(-1) {}
61 
69  InstanceId(const std::wstring& id) :
70  m_instanceId(Internal::Generate64BitHash(id)),
71  m_instanceIdStr(id)
72  {}
73 
81  explicit InstanceId(const Dob::Typesystem::Int64 id):
82  m_instanceId(id) {}
83 
84 
93  InstanceId(const Int64 id, const std::wstring & idStr):
94  m_instanceId(id),
95  m_instanceIdStr(idStr)
96  {
97 #ifndef NDEBUG
98  if (!m_instanceIdStr.empty() && m_instanceId != Internal::Generate64BitHash(idStr))
99  {
100  std::wostringstream ostr;
101  ostr << "InstanceId two-argument constructor got an inconsistent id. Got ("
102  << id << ", '" << idStr << "'), but the string evaluates to " << Internal::Generate64BitHash(idStr) << ".";
103  throw SoftwareViolationException(ostr.str(),__WFILE__,__LINE__);
104  }
105 #endif
106  }
107 
116  void RemoveString() {m_instanceIdStr.clear(); m_CachedUtf8String.clear();}
117 
123  bool operator ==(const InstanceId& other) const
124  {
125  return m_instanceId == other.m_instanceId;
126  }
127 
133  bool operator !=(const InstanceId & other) const
134  {
135  return !(*this==other);
136  }
137 
144  bool operator < (const InstanceId & other) const
145  {
146  return m_instanceId < other.m_instanceId;
147  }
148 
160  DOTS_CPP_API const std::wstring ToString() const;
161 
162 
167 
173  UnderlyingType GetRawValue() const {return m_instanceId;}
174 
182  const std::wstring & GetRawString() const {return m_instanceIdStr;}
183 
191  {
192  if (m_instanceIdStr.empty())
193  {
194  return 0;
195  }
196 
197  if (m_CachedUtf8String.empty())
198  {
199  m_CachedUtf8String = Utilities::ToUtf8(m_instanceIdStr);
200  }
201 
202  return static_cast<Int32>(m_CachedUtf8String.length() + 1);
203  }
204 
212  const std::string & Utf8String() const
213  {
214  if (!m_instanceIdStr.empty() && m_CachedUtf8String.empty())
215  {
216  m_CachedUtf8String = Utilities::ToUtf8(m_instanceIdStr);
217  }
218  return m_CachedUtf8String;
219  }
220 
223  private:
224  UnderlyingType m_instanceId;
225  std::wstring m_instanceIdStr;
226 
227  mutable std::string m_CachedUtf8String;
228  };
229 
230  static inline std::wostream & operator << (std::wostream& out, const InstanceId& instanceId)
231  {return out << instanceId.ToString();}
232 }
233 }
234 }
235 #endif
236 
InstanceId(const Dob::Typesystem::Int64 id)
Constructor.
Definition: InstanceId.h:81
Int32 Utf8StringLength() const
Get the length of the string when converted to UTF-8 encoding.
Definition: InstanceId.h:190
bool operator!=(const InstanceId &other) const
Inequality operator.
Definition: InstanceId.h:133
InstanceId(const Int64 id, const std::wstring &idStr)
Constructor.
Definition: InstanceId.h:93
static InstanceId GenerateRandom()
Returns a random instance id.
Definition: InstanceId.h:53
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:144
Class containing the identity of an instance.
Definition: InstanceId.h:47
DOTS_CPP_API const std::wstring ToString() const
Return a string representation of the instance id.
const std::wstring & GetRawString() const
Get the string that was used to create this id.
Definition: InstanceId.h:182
void RemoveString()
Remove the included string from the instance id.
Definition: InstanceId.h:116
DotsC_Int64 Int64
64 bit integer type.
Definition: Defs.h:70
UnderlyingType GetRawValue() const
Get the raw 64 bit integer identifier.
Definition: InstanceId.h:173
#define __WFILE__
Definition: Exceptions.h:31
InstanceId()
Default constructor.
Definition: InstanceId.h:60
Int64 UnderlyingType
Definition: InstanceId.h:166
bool operator==(const InstanceId &other) const
Equality operator.
Definition: InstanceId.h:123
static std::wostream & operator<<(std::wostream &out, const ChannelId &channelId)
Definition: ChannelId.h:223
InstanceId(const std::wstring &id)
Constructor.
Definition: InstanceId.h:69
const std::string & Utf8String() const
Convert the string to UTF-8.
Definition: InstanceId.h:212
DotsC_Int32 Int32
32 bit integer type.
Definition: Defs.h:67
#define DOTS_CPP_API
Definition: Defs.h:33
DOTS_CPP_API const std::string ToUtf8(const std::wstring &wstr)
Convert a std::wstring to UTF8-encoded std::string.