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/InternalOperations.h>
32 #include <string>
33 #include <sstream>
34 
35 namespace Safir
36 {
37 namespace Dob
38 {
39 namespace Typesystem
40 {
44  class HandlerId
45  {
46  public:
49 
56  m_handlerId(Internal::DEFAULT_HANDLER_ID),
57  m_handlerIdStr(L"DEFAULT_HANDLER")
58  {}
59 
67  HandlerId(const std::wstring& id):
68  m_handlerId(Internal::Generate64BitHash(id)),
69  m_handlerIdStr(id)
70  {}
71 
79  explicit HandlerId(const Int64 id):
80  m_handlerId(id)
81  {}
82 
91  HandlerId(const Int64 id, const std::wstring & idStr):
92  m_handlerId(id),
93  m_handlerIdStr(idStr)
94  {
95 #ifndef NDEBUG
96  if (!m_handlerIdStr.empty() && m_handlerId != Internal::Generate64BitHash(idStr))
97  {
98  std::wostringstream ostr;
99  ostr << "HandlerId two-argument constructor got an inconsistent id. Got ("
100  << id << ", '" << idStr << "'), but the string evaluates to " << Internal::Generate64BitHash(idStr) << ".";
101  throw SoftwareViolationException(ostr.str(),__WFILE__,__LINE__);
102  }
103 #endif
104  }
105 
114  void RemoveString() {m_handlerIdStr.clear(); m_CachedUtf8String.clear();}
115 
121  bool operator ==(const HandlerId & other) const
122  {
123  return m_handlerId == other.m_handlerId;
124  }
125 
131  bool operator !=(const HandlerId & other) const
132  {
133  return !(*this==other);
134  }
135 
142  bool operator < (const HandlerId & other) const
143  {
144  return m_handlerId < other.m_handlerId;
145  }
146 
150  DOTS_CPP_API const std::wstring ToString() const;
151 
156 
162  UnderlyingType GetRawValue() const {return m_handlerId;}
163 
171  const std::wstring & GetRawString() const {return m_handlerIdStr;}
172 
180  {
181  if (m_handlerIdStr.empty())
182  {
183  return 0;
184  }
185 
186  if (m_CachedUtf8String.empty())
187  {
188  m_CachedUtf8String = Utilities::ToUtf8(m_handlerIdStr);
189  }
190 
191  return static_cast<Int32>(m_CachedUtf8String.length() + 1);
192  }
193 
201  const std::string & Utf8String() const
202  {
203  if (!m_handlerIdStr.empty() && m_CachedUtf8String.empty())
204  {
205  m_CachedUtf8String = Utilities::ToUtf8(m_handlerIdStr);
206  }
207  return m_CachedUtf8String;
208  }
209 
212  private:
213  UnderlyingType m_handlerId;
214  std::wstring m_handlerIdStr;
215 
216  mutable std::string m_CachedUtf8String;
217  };
218 
219  static inline std::wostream & operator << (std::wostream& out, const HandlerId& handlerId)
220  {return out << handlerId.ToString();}
221 
222 }
223 }
224 }
225 #endif
226 
const std::wstring & GetRawString() const
Get the string that was used to create this id.
Definition: HandlerId.h:171
Int32 Utf8StringLength() const
Get the length of the string when converted to UTF-8 encoding.
Definition: HandlerId.h:179
Class containing the identity of a handler.
Definition: HandlerId.h:44
Meant to be used when something goes very wrong.
Definition: Exceptions.h:360
DotsC_Int64 Int64
64 bit integer type.
Definition: Defs.h:70
Int64 UnderlyingType
Definition: HandlerId.h:155
HandlerId()
Default constructor.
Definition: HandlerId.h:55
static DOTS_CPP_API const HandlerId ALL_HANDLERS
Constant representing all handlers.
Definition: HandlerId.h:48
bool operator!=(const HandlerId &other) const
Inequality operator.
Definition: HandlerId.h:131
HandlerId(const Int64 id, const std::wstring &idStr)
Constructor.
Definition: HandlerId.h:91
#define __WFILE__
Definition: Exceptions.h:31
bool operator==(const HandlerId &other) const
Equality operator.
Definition: HandlerId.h:121
HandlerId(const Int64 id)
Constructor.
Definition: HandlerId.h:79
UnderlyingType GetRawValue() const
Get the raw 64 bit integer identifier.
Definition: HandlerId.h:162
static std::wostream & operator<<(std::wostream &out, const ChannelId &channelId)
Definition: ChannelId.h:223
const std::string & Utf8String() const
Convert the string to UTF-8.
Definition: HandlerId.h:201
bool operator<(const HandlerId &other) const
Less-than operator.
Definition: HandlerId.h:142
void RemoveString()
Remove the included string from the handler id.
Definition: HandlerId.h:114
DotsC_Int32 Int32
32 bit integer type.
Definition: Defs.h:67
HandlerId(const std::wstring &id)
Constructor.
Definition: HandlerId.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.
DOTS_CPP_API const std::wstring ToString() const
Return a string representation of the handler id.