Safir SDK Core
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
HandlerId.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_HANDLER_ID_H__
26 #define __DOTS_HANDLER_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 {
45  class HandlerId
46  {
47  public:
49  static DOTS_API const HandlerId ALL_HANDLERS;
50 
57  m_handlerId(Internal::DEFAULT_HANDLER_ID),
58  m_handlerIdStr(L"DEFAULT_HANDLER")
59  {}
60 
68  HandlerId(const std::wstring& id):
69  m_handlerId(Internal::Generate64BitHash(id)),
70  m_handlerIdStr(id)
71  {}
72 
80  explicit HandlerId(const Int64 id):
81  m_handlerId(id)
82  {}
83 
92  HandlerId(const Int64 id, const std::wstring & idStr):
93  m_handlerId(id),
94  m_handlerIdStr(idStr)
95  {
96 #ifndef NDEBUG
97  if (!m_handlerIdStr.empty() && m_handlerId != Internal::Generate64BitHash(idStr))
98  {
99  std::wostringstream ostr;
100  ostr << "HandlerId two-argument constructor got an inconsistent id. Got ("
101  << id << ", '" << idStr << "'), but the string evaluates to " << Internal::Generate64BitHash(idStr) << ".";
102  throw SoftwareViolationException(ostr.str(),__WFILE__,__LINE__);
103  }
104 #endif
105  }
106 
115  void RemoveString() {m_handlerIdStr.clear(); m_CachedUtf8String.clear();}
116 
122  bool operator ==(const HandlerId & other) const
123  {
124  return m_handlerId == other.m_handlerId;
125  }
126 
132  bool operator !=(const HandlerId & other) const
133  {
134  return !(*this==other);
135  }
136 
143  bool operator < (const HandlerId & other) const
144  {
145  return m_handlerId < other.m_handlerId;
146  }
147 
151  DOTS_API const std::wstring ToString() const;
152 
157 
163  UnderlyingType GetRawValue() const {return m_handlerId;}
164 
172  const std::wstring & GetRawString() const {return m_handlerIdStr;}
173 
181  {
182  if (m_handlerIdStr.empty())
183  {
184  return 0;
185  }
186 
187  if (m_CachedUtf8String.empty())
188  {
189  m_CachedUtf8String = Utilities::ToUtf8(m_handlerIdStr);
190  }
191 
192  return static_cast<Int32>(m_CachedUtf8String.length() + 1);
193  }
194 
202  const std::string & Utf8String() const
203  {
204  if (!m_handlerIdStr.empty() && m_CachedUtf8String.empty())
205  {
206  m_CachedUtf8String = Utilities::ToUtf8(m_handlerIdStr);
207  }
208  return m_CachedUtf8String;
209  }
210 
213  private:
214  UnderlyingType m_handlerId;
215  std::wstring m_handlerIdStr;
216 
217  mutable std::string m_CachedUtf8String;
218  };
219 
220  static inline std::wostream & operator << (std::wostream& out, const HandlerId& handlerId)
221  {return out << handlerId.ToString();}
222 
223 }
224 }
225 }
226 #endif
227 
const std::wstring & GetRawString() const
Get the string that was used to create this id.
Definition: HandlerId.h:172
Int32 Utf8StringLength() const
Get the length of the string when converted to UTF-8 encoding.
Definition: HandlerId.h:180
Class containing the identity of a handler.
Definition: HandlerId.h:45
Meant to be used when something goes very wrong.
Definition: Exceptions.h:360
DotsC_Int64 Int64
64 bit integer type.
Definition: Dob/Typesystem/Defs.h:72
Int64 UnderlyingType
Definition: HandlerId.h:156
static DOTS_API const HandlerId ALL_HANDLERS
Constant representing all handlers.
Definition: HandlerId.h:49
HandlerId()
Default constructor.
Definition: HandlerId.h:56
bool operator!=(const HandlerId &other) const
Inequality operator.
Definition: HandlerId.h:132
DOTS_API const std::wstring ToString() const
Return a string representation of the handler id.
HandlerId(const Int64 id, const std::wstring &idStr)
Constructor.
Definition: HandlerId.h:92
#define __WFILE__
Definition: Exceptions.h:31
bool operator==(const HandlerId &other) const
Equality operator.
Definition: HandlerId.h:122
DOTS_API const std::string ToUtf8(const std::wstring &wstr)
Convert a std::wstring to UTF8-encoded std::string.
HandlerId(const Int64 id)
Constructor.
Definition: HandlerId.h:80
UnderlyingType GetRawValue() const
Get the raw 64 bit integer identifier.
Definition: HandlerId.h:163
static std::wostream & operator<<(std::wostream &out, const ChannelId &channelId)
Definition: ChannelId.h:224
const std::string & Utf8String() const
Convert the string to UTF-8.
Definition: HandlerId.h:202
bool operator<(const HandlerId &other) const
Less-than operator.
Definition: HandlerId.h:143
void RemoveString()
Remove the included string from the handler id.
Definition: HandlerId.h:115
DotsC_Int32 Int32
32 bit integer type.
Definition: Dob/Typesystem/Defs.h:69
HandlerId(const std::wstring &id)
Constructor.
Definition: HandlerId.h:68