fix: cmake install and possible conflicting options
[KDIS.git] / src / DataTypes / EntityType.cpp
blobd1c5ef891ff0a5993f6633548d703d70f5fe91c6
1 /*********************************************************************
2 Copyright 2013 Karl Jones
3 All rights reserved.
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
8 1. Redistributions of source code must retain the above copyright notice, this
9 list of conditions and the following disclaimer.
10 2. Redistributions in binary form must reproduce the above copyright notice,
11 this list of conditions and the following disclaimer in the documentation
12 and/or other materials provided with the distribution.
14 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
15 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
18 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
23 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 For Further Information Please Contact me at
26 Karljj1@yahoo.com
27 http://p.sf.net/kdis/UserGuide
28 *********************************************************************/
30 #include "KDIS/DataTypes/EntityType.hpp"
32 #include <cstdlib>
33 #include <vector>
35 using namespace KDIS;
36 using namespace DATA_TYPE;
37 using namespace ENUMS;
38 using namespace std;
40 //////////////////////////////////////////////////////////////////////////
41 // Public:
42 //////////////////////////////////////////////////////////////////////////
44 EntityType::EntityType()
45 : m_ui8EntityKind(0),
46 m_ui8Domain(0),
47 m_ui16Country(0),
48 m_ui8Category(0),
49 m_ui8SubCategory(0),
50 m_ui8Specific(0),
51 m_ui8Extra(0) {}
53 //////////////////////////////////////////////////////////////////////////
55 EntityType::EntityType(EntityKind Kind, KUINT8 Domain, Country Country,
56 KUINT8 Categoy, KUINT8 SubCategory, KUINT8 Specific,
57 KUINT8 Extra)
58 : m_ui8EntityKind(Kind),
59 m_ui8Domain(Domain),
60 m_ui16Country(Country),
61 m_ui8Category(Categoy),
62 m_ui8SubCategory(SubCategory),
63 m_ui8Specific(Specific),
64 m_ui8Extra(Extra) {}
66 //////////////////////////////////////////////////////////////////////////
68 EntityType::EntityType(KUINT8 Kind, KUINT8 Domain, KUINT16 Country,
69 KUINT8 Categoy, KUINT8 SubCategory, KUINT8 Specific,
70 KUINT8 Extra)
71 : m_ui8EntityKind(Kind),
72 m_ui8Domain(Domain),
73 m_ui16Country(Country),
74 m_ui8Category(Categoy),
75 m_ui8SubCategory(SubCategory),
76 m_ui8Specific(Specific),
77 m_ui8Extra(Extra) {}
79 //////////////////////////////////////////////////////////////////////////
81 EntityType::EntityType(KDataStream& stream) { Decode(stream); }
83 //////////////////////////////////////////////////////////////////////////
85 EntityType::~EntityType() {}
87 //////////////////////////////////////////////////////////////////////////
89 void EntityType::SetEntityKind(EntityKind UI) { m_ui8EntityKind = UI; }
91 //////////////////////////////////////////////////////////////////////////
93 EntityKind EntityType::GetEntityKind() const {
94 return (EntityKind)m_ui8EntityKind;
97 //////////////////////////////////////////////////////////////////////////
99 void EntityType::SetDomain(EntityDomain UI) { m_ui8Domain = UI; }
101 //////////////////////////////////////////////////////////////////////////
103 EntityDomain EntityType::GetDomain() const { return (EntityDomain)m_ui8Domain; }
105 //////////////////////////////////////////////////////////////////////////
107 void EntityType::SetCountry(Country UI) { m_ui16Country = UI; }
109 //////////////////////////////////////////////////////////////////////////
111 Country EntityType::GetCountry() const { return (Country)m_ui16Country; }
113 //////////////////////////////////////////////////////////////////////////
115 void EntityType::SetCategory(KUINT8 UI) { m_ui8Category = UI; }
117 //////////////////////////////////////////////////////////////////////////
119 KUINT8 EntityType::GetCategory() const { return m_ui8Category; }
121 //////////////////////////////////////////////////////////////////////////
123 void EntityType::SetSubCategory(KUINT8 UI) { m_ui8SubCategory = UI; }
125 //////////////////////////////////////////////////////////////////////////
127 KUINT8 EntityType::GetSubCategory() const { return m_ui8SubCategory; }
129 //////////////////////////////////////////////////////////////////////////
131 void EntityType::SetSpecific(KUINT8 UI) { m_ui8Specific = UI; }
133 //////////////////////////////////////////////////////////////////////////
135 KUINT8 EntityType::GetSpecific() const { return m_ui8Specific; }
137 //////////////////////////////////////////////////////////////////////////
139 void EntityType::SetExtra(KUINT8 UI) { m_ui8Extra = UI; }
141 //////////////////////////////////////////////////////////////////////////
143 KUINT8 EntityType::GetExtra() const { return m_ui8Extra; }
145 //////////////////////////////////////////////////////////////////////////
147 void EntityType::ReadFromTokenisedString(const KString& String,
148 const KString& Seperator /*= ","*/) {
149 // Copy the string, we don't want to change the string we have been passed.
150 KString sCopy = String;
152 vector<KUINT16> vValues;
154 KCHAR8* token = strtok((KCHAR8*)sCopy.c_str(), Seperator.c_str());
156 while (token != NULL) {
157 KINT16 i = atoi(token);
158 if (i < 0) i = 0; // If the value is less than 0 then we need to make it 0.
160 vValues.push_back(i);
162 // Get next token:
163 token = strtok(NULL, Seperator.c_str());
166 // We need 7 values in total, if not we have a problem.
167 if (vValues.size() != 7) {
168 throw KException(__FUNCTION__, INVALID_DATA,
169 "Token String Must Contain 7 Integer Values Only.");
172 // Set the new type.
173 m_ui8EntityKind = vValues[0];
174 m_ui8Domain = vValues[1];
175 m_ui16Country = vValues[2];
176 m_ui8Category = vValues[3];
177 m_ui8SubCategory = vValues[4];
178 m_ui8Specific = vValues[5];
179 m_ui8Extra = vValues[6];
182 //////////////////////////////////////////////////////////////////////////
184 KString EntityType::CreateTokenisedString(
185 const KString& Seperator /*= ","*/) const {
186 KStringStream ss;
188 ss << (KUINT16)m_ui8EntityKind << Seperator << (KUINT16)m_ui8Domain
189 << Seperator << m_ui16Country << Seperator << (KUINT16)m_ui8Category
190 << Seperator << (KUINT16)m_ui8SubCategory << Seperator
191 << (KUINT16)m_ui8Specific << Seperator << (KUINT16)m_ui8Extra;
193 return ss.str();
196 //////////////////////////////////////////////////////////////////////////
198 KString EntityType::GetAsString() const {
199 KStringStream ss;
201 ss << CreateTokenisedString() << "\n";
203 return ss.str();
206 //////////////////////////////////////////////////////////////////////////
208 void EntityType::Decode(KDataStream& stream) {
209 if (stream.GetBufferSize() < ENTITY_TYPE_SIZE)
210 throw KException(__FUNCTION__, NOT_ENOUGH_DATA_IN_BUFFER);
212 stream >> m_ui8EntityKind >> m_ui8Domain >> m_ui16Country >> m_ui8Category >>
213 m_ui8SubCategory >> m_ui8Specific >> m_ui8Extra;
216 //////////////////////////////////////////////////////////////////////////
218 KDataStream EntityType::Encode() const {
219 KDataStream stream;
221 EntityType::Encode(stream);
223 return stream;
226 //////////////////////////////////////////////////////////////////////////
228 void EntityType::Encode(KDataStream& stream) const {
229 stream << m_ui8EntityKind << m_ui8Domain << m_ui16Country << m_ui8Category
230 << m_ui8SubCategory << m_ui8Specific << m_ui8Extra;
233 //////////////////////////////////////////////////////////////////////////
235 KBOOL EntityType::operator==(const EntityType& Value) const {
236 if (m_ui8EntityKind != Value.m_ui8EntityKind) return false;
237 if (m_ui8Domain != Value.m_ui8Domain) return false;
238 if (m_ui16Country != Value.m_ui16Country) return false;
239 if (m_ui8Category != Value.m_ui8Category) return false;
240 if (m_ui8SubCategory != Value.m_ui8SubCategory) return false;
241 if (m_ui8Specific != Value.m_ui8Specific) return false;
242 if (m_ui8Extra != Value.m_ui8Extra) return false;
243 return true;
246 //////////////////////////////////////////////////////////////////////////
248 KBOOL EntityType::operator!=(const EntityType& Value) const {
249 return !(*this == Value);
252 //////////////////////////////////////////////////////////////////////////
254 KBOOL EntityType::operator<(const EntityType& Value) const {
255 // We will bit shift all 7 fields into a single KUINT64, this will generate a
256 // new unique value which we can then use for comparisons. bits 56-63 =
257 // EntityKind bits 48-55 = Domain bits 32-47 = Country bits 24-31 = Category
258 // bits 16-23 = SubCategory
259 // bits 8-15 = Specific
260 // bits 0-7 = Extra
261 KUINT64 ui64ThisCmpVal = 0, ui64OtherCmpVal = 0;
263 ui64ThisCmpVal = (KUINT64)m_ui8EntityKind << 56 | (KUINT64)m_ui8Domain << 48 |
264 (KUINT64)m_ui16Country << 32 | (KUINT64)m_ui8Category << 24 |
265 (KUINT64)m_ui8SubCategory << 16 |
266 (KUINT64)m_ui8Specific << 8 | (KUINT64)m_ui8Extra;
268 ui64OtherCmpVal =
269 (KUINT64)Value.m_ui8EntityKind << 56 | (KUINT64)Value.m_ui8Domain << 48 |
270 (KUINT64)Value.m_ui16Country << 32 | (KUINT64)Value.m_ui8Category << 24 |
271 (KUINT64)Value.m_ui8SubCategory << 16 |
272 (KUINT64)Value.m_ui8Specific << 8 | (KUINT64)Value.m_ui8Extra;
274 return ui64ThisCmpVal < ui64OtherCmpVal;
277 //////////////////////////////////////////////////////////////////////////