Safir SDK Core
Loading...
Searching...
No Matches
ChannelId.h
Go to the documentation of this file.
1/******************************************************************************
2*
3* Copyright Saab AB, 2008-2013 (http://safirsdkcore.com)
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
28#include <functional>
32#include <Safir/Dob/Typesystem/Internal/InternalOperations.h>
33#include <string>
34#include <sstream>
35
36namespace Safir
37{
38namespace Dob
39{
40namespace Typesystem
41{
46 {
47 public:
50
57 m_channelId(Internal::DEFAULT_CHANNEL_ID),
58 m_channelIdStr(L"DEFAULT_CHANNEL")
59 {}
60
68 ChannelId(const std::wstring& id):
69 m_channelId(Internal::Generate64BitHash(id)),
70 m_channelIdStr(id)
71 {}
72
73
81 explicit ChannelId(const Int64 id):
82 m_channelId(id)
83 {}
84
85
94 ChannelId(const Int64 id, const std::wstring & idStr):
95 m_channelId(id),
96 m_channelIdStr(idStr)
97 {
98#ifndef NDEBUG
99 if (!m_channelIdStr.empty() && m_channelId != Internal::Generate64BitHash(idStr))
100 {
101 std::wostringstream ostr;
102 ostr << "ChannelId 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
109
118 void RemoveString() {m_channelIdStr.clear(); m_CachedUtf8String.clear();}
119
125 bool operator ==(const ChannelId & other) const
126 {
127 return m_channelId == other.m_channelId;
128 }
129
135 bool operator !=(const ChannelId & other) const
136 {
137 return !(*this==other);
138 }
139
146 bool operator < (const ChannelId & other) const
147 {
148 return m_channelId < other.m_channelId;
149 }
150
154 DOTS_CPP_API const std::wstring ToString() const;
155
156
161
167 UnderlyingType GetRawValue() const {return m_channelId;}
168
176 const std::wstring & GetRawString() const {return m_channelIdStr;}
177
185 {
186 if (m_channelIdStr.empty())
187 {
188 return 0;
189 }
190
191 if (m_CachedUtf8String.empty())
192 {
193 m_CachedUtf8String = Utilities::ToUtf8(m_channelIdStr);
194 }
195
196 return static_cast<Int32>(m_CachedUtf8String.length() + 1);
197 }
198
206 const std::string & Utf8String() const
207 {
208 if (!m_channelIdStr.empty() && m_CachedUtf8String.empty())
209 {
210 m_CachedUtf8String = Utilities::ToUtf8(m_channelIdStr);
211 }
212 return m_CachedUtf8String;
213 }
214
217 private:
218 UnderlyingType m_channelId;
219 std::wstring m_channelIdStr;
220
221 mutable std::string m_CachedUtf8String;
222 };
223
224 static inline std::wostream & operator << (std::wostream& out, const ChannelId& channelId)
225 {return out << channelId.ToString();}
226
227 //Make it possible to use ChannelId as key in a dictionaries.
228 inline std::size_t hash_value(const Safir::Dob::Typesystem::ChannelId& val)
229 {return std::hash<ChannelId::UnderlyingType>()(val.GetRawValue());}
230}
231}
232}
233#endif
234
#define __WFILE__
Definition Exceptions.h:31
This namespace contains all the functionality and definitions of the SAFIR SDK.
Definition Backdoor.h:31
std::size_t hash_value(const Safir::Dob::Typesystem::ChannelId &val)
Definition ChannelId.h:228
DotsC_Int32 Int32
32 bit integer type.
Definition Defs.h:66
DotsC_Int64 Int64
64 bit integer type.
Definition Defs.h:69
static std::wostream & operator<<(std::wostream &out, const ChannelId &channelId)
Definition ChannelId.h:224
DOTS_CPP_API const std::string ToUtf8(const std::wstring &wstr)
Convert a std::wstring to UTF8-encoded std::string.
Class containing the identity of a channel.
Definition ChannelId.h:46
static DOTS_CPP_API const ChannelId ALL_CHANNELS
Constant representing all channels.
Definition ChannelId.h:49
Int64 UnderlyingType
Definition ChannelId.h:160
Int32 Utf8StringLength() const
Get the length of the string when converted to UTF-8 encoding.
Definition ChannelId.h:184
const std::wstring & GetRawString() const
Get the string that was used to create this id.
Definition ChannelId.h:176
ChannelId(const Int64 id, const std::wstring &idStr)
Constructor.
Definition ChannelId.h:94
ChannelId()
Default constructor.
Definition ChannelId.h:56
bool operator<(const ChannelId &other) const
Less-than operator.
Definition ChannelId.h:146
UnderlyingType GetRawValue() const
Get the raw 64 bit integer identifier.
Definition ChannelId.h:167
bool operator==(const ChannelId &other) const
Equality operator.
Definition ChannelId.h:125
bool operator!=(const ChannelId &other) const
Inequality operator.
Definition ChannelId.h:135
ChannelId(const Int64 id)
Constructor.
Definition ChannelId.h:81
const std::string & Utf8String() const
Convert the string to UTF-8.
Definition ChannelId.h:206
void RemoveString()
Remove the included string from the channel id.
Definition ChannelId.h:118
ChannelId(const std::wstring &id)
Constructor.
Definition ChannelId.h:68
DOTS_CPP_API const std::wstring ToString() const
Return a string representation of the channel id.
Meant to be used when something goes very wrong.
Definition Exceptions.h:364
#define DOTS_CPP_API
Definition Defs.h:33