Safir SDK Core
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ChannelId.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_CHANNEL_ID_H__
26 #define __DOTS_CHANNEL_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 ChannelId
45  {
46  public:
49 
56  m_channelId(Internal::DEFAULT_CHANNEL_ID),
57  m_channelIdStr(L"DEFAULT_CHANNEL")
58  {}
59 
67  ChannelId(const std::wstring& id):
68  m_channelId(Internal::Generate64BitHash(id)),
69  m_channelIdStr(id)
70  {}
71 
72 
80  explicit ChannelId(const Int64 id):
81  m_channelId(id)
82  {}
83 
84 
93  ChannelId(const Int64 id, const std::wstring & idStr):
94  m_channelId(id),
95  m_channelIdStr(idStr)
96  {
97 #ifndef NDEBUG
98  if (!m_channelIdStr.empty() && m_channelId != Internal::Generate64BitHash(idStr))
99  {
100  std::wostringstream ostr;
101  ostr << "ChannelId 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 
108 
117  void RemoveString() {m_channelIdStr.clear(); m_CachedUtf8String.clear();}
118 
124  bool operator ==(const ChannelId & other) const
125  {
126  return m_channelId == other.m_channelId;
127  }
128 
134  bool operator !=(const ChannelId & other) const
135  {
136  return !(*this==other);
137  }
138 
145  bool operator < (const ChannelId & other) const
146  {
147  return m_channelId < other.m_channelId;
148  }
149 
153  DOTS_CPP_API const std::wstring ToString() const;
154 
155 
160 
166  UnderlyingType GetRawValue() const {return m_channelId;}
167 
175  const std::wstring & GetRawString() const {return m_channelIdStr;}
176 
184  {
185  if (m_channelIdStr.empty())
186  {
187  return 0;
188  }
189 
190  if (m_CachedUtf8String.empty())
191  {
192  m_CachedUtf8String = Utilities::ToUtf8(m_channelIdStr);
193  }
194 
195  return static_cast<Int32>(m_CachedUtf8String.length() + 1);
196  }
197 
205  const std::string & Utf8String() const
206  {
207  if (!m_channelIdStr.empty() && m_CachedUtf8String.empty())
208  {
209  m_CachedUtf8String = Utilities::ToUtf8(m_channelIdStr);
210  }
211  return m_CachedUtf8String;
212  }
213 
216  private:
217  UnderlyingType m_channelId;
218  std::wstring m_channelIdStr;
219 
220  mutable std::string m_CachedUtf8String;
221  };
222 
223  static inline std::wostream & operator << (std::wostream& out, const ChannelId& channelId)
224  {return out << channelId.ToString();}
225 }
226 }
227 }
228 #endif
229 
Class containing the identity of a channel.
Definition: ChannelId.h:44
void RemoveString()
Remove the included string from the channel id.
Definition: ChannelId.h:117
Meant to be used when something goes very wrong.
Definition: Exceptions.h:360
const std::string & Utf8String() const
Convert the string to UTF-8.
Definition: ChannelId.h:205
UnderlyingType GetRawValue() const
Get the raw 64 bit integer identifier.
Definition: ChannelId.h:166
const std::wstring & GetRawString() const
Get the string that was used to create this id.
Definition: ChannelId.h:175
ChannelId(const std::wstring &id)
Constructor.
Definition: ChannelId.h:67
DotsC_Int64 Int64
64 bit integer type.
Definition: Defs.h:70
static DOTS_CPP_API const ChannelId ALL_CHANNELS
Constant representing all channels.
Definition: ChannelId.h:48
bool operator==(const ChannelId &other) const
Equality operator.
Definition: ChannelId.h:124
ChannelId(const Int64 id, const std::wstring &idStr)
Constructor.
Definition: ChannelId.h:93
#define __WFILE__
Definition: Exceptions.h:31
ChannelId(const Int64 id)
Constructor.
Definition: ChannelId.h:80
ChannelId()
Default constructor.
Definition: ChannelId.h:55
Int64 UnderlyingType
Definition: ChannelId.h:159
DOTS_CPP_API const std::wstring ToString() const
Return a string representation of the channel id.
bool operator<(const ChannelId &other) const
Less-than operator.
Definition: ChannelId.h:145
static std::wostream & operator<<(std::wostream &out, const ChannelId &channelId)
Definition: ChannelId.h:223
Int32 Utf8StringLength() const
Get the length of the string when converted to UTF-8 encoding.
Definition: ChannelId.h:183
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.
bool operator!=(const ChannelId &other) const
Inequality operator.
Definition: ChannelId.h:134