CMSDK 2.0.1
Cross-platform C++ base library and SDK for the Psyclone AIOS platform
Loading...
Searching...
No Matches
Types.h
Go to the documentation of this file.
1
19
20#if !defined(__types_h) // Sentry, use file only if it's not already included.
21#define __types_h
22
23#include "Standard.h"
24#include <stdio.h>
25#include <cstdint>
26#include <string.h>
27// #define CNSPORT 10000
28// #define FACTORYPORT 10001
29
30#define CNSPRIORITY 90
31#define FACTORYPRIORITY 90
32
33#define BITFREE 1
34#define BITOCCUPIED 0
35
36#ifdef WINDOWS
37#else
38#pragma GCC diagnostic ignored "-Wformat"
39#endif
40
41#if defined ARCH_32
42
43 typedef bool bit;
44 typedef signed char int8;
45 typedef unsigned char uint8;
46 typedef short int16;
47 typedef unsigned short uint16;
48 typedef int int32;
49 typedef unsigned int uint32;
50 typedef long long int64;
51 typedef unsigned long long uint64;
52 typedef float float32;
53 typedef double float64;
54// typedef uint32 paddr;
55
56#elif defined ARCH_64
57
58 typedef bool bit;
59 typedef signed char int8;
60 typedef unsigned char uint8;
61 typedef short int16;
62 typedef unsigned short uint16;
63 typedef int int32;
64 typedef unsigned int uint32;
65 typedef float float32;
66 typedef double float64;
67// typedef uint32 paddr;
68
69 #ifdef WINDOWS
70 typedef long long int64;
71 typedef unsigned long long uint64;
72 #else
73 typedef int64_t int64;
74 typedef uint64_t uint64;
75 #endif
76
77#endif
78
79#define INT32_NOVALUE 0x12345678
80#define INT64_NOVALUE 0x123456789ABCDEF0L
81#define FLOAT64_NOVALUE 0.123456789123456789
82
83#define MAXVALUINT8 ((uint8)~((uint8)0))
84#define MAXVALINT8 ((int8)(MAXVALUINT8 >> 1))
85#define MAXVALUINT16 ((uint16)~((uint16)0))
86#define MAXVALINT16 ((int16)(MAXVALUINT16 >> 1))
87#define MAXVALUINT32 ((uint32)~((uint32)0))
88#define MAXVALINT32 ((int32)(MAXVALUINT32 >> 1))
89#define MAXVALUINT64 ((uint64)~((uint64)0))
90#define MAXVALINT64 ((int64)(MAXVALUINT64 >> 1))
91
92#define MERGEUINT3264(a,b) (((uint64)a)<<32)|((uint64)b)
93#define GET1UINT3264(a) (uint32)((a & 0xffffffff00000000L)>>32)
94#define GET2UINT3264(a) (uint32)(a & 0x00000000ffffffffL)
95#define MERGEUINT1632(a,b) (((uint32)a)<<16)|((uint32)b)
96#define GET1UINT1632(a) (uint16)((a & 0xffff0000L)>>16)
97#define GET2UINT1632(a) (uint16)(a & 0x0000ffffL)
98
100#define PSYTYPELEVELS 16
101
123struct PsyType {
126 bool isValid() {return (levels[0] != 0);}
128 bool operator==(const PsyType &othertype) const {
129 return memcmp(levels, othertype.levels, sizeof(PsyType)) == 0;
130 }
131
132 bool operator!=(const PsyType &othertype) const {
133 return memcmp(levels, othertype.levels, sizeof(PsyType)) != 0;
134 }
135
136 bool operator<(const PsyType &othertype) const {
137 for (uint32 n = 0; n < PSYTYPELEVELS; n++) {
138 if (levels[n] < othertype.levels[n])
139 return true;
140 else if (levels[n] > othertype.levels[n])
141 return false;
142 }
143 return false;
144 }
145 bool operator>(const PsyType &othertype) const {
146 for (uint32 n = 0; n < PSYTYPELEVELS; n++) {
147 if (levels[n] > othertype.levels[n])
148 return true;
149 else if (levels[n] < othertype.levels[n])
150 return false;
151 }
152 return false;
153 }
154 uint16 operator[](int i) const {
155 if ((i < 0) && (i >= PSYTYPELEVELS))
156 return 0;
157 return levels[i];
158 }
159
163 bool setLevel(int i, uint16 value) {
164 if ((i < 0) && (i >= PSYTYPELEVELS))
165 return false;
166 levels[i] = value;
167 return true;
168 }
169
173 bool matches(PsyType& otherType) const {
174 for (uint32 n = 0; n < PSYTYPELEVELS; n++) {
175 if ((levels[n] != otherType.levels[n]) && (levels[n] != 0xFFFF) && (otherType.levels[n] != 0xFFFF))
176 return false;
177 }
178 return true;
179
180 //v = levels[n] & otherType.levels[n];
181 //if ( (v != (levels[n] > otherType.levels[n] ? otherType.levels[n] : levels[n])))
182 // return false;
183 //uint64 *a = (uint64*)&levels;
184 //uint64 *b = (uint64*)&otherType.levels;
185 //uint64 v;
186 //uint32 n, c = sizeof(levels) / sizeof(uint64);
187 //for (n = 0; n < c; n++) {
188 // v = *a++ & *b++;
189 // if ( (v != (*a>*b?*b:*a)))
190 // return false;
191 //}
192 //return true;
193 }
194
195 std::string toString() {
196 if (!levels[0])
197 return "";
198 std::stringstream ss;
199 ss << levels[0];
200 for (uint32 n = 1; n < PSYTYPELEVELS; n++) {
201 if (levels[n])
202 ss << '.' << levels[n];
203 }
204 return ss.str();
205 }
206
208 std::string toString(std::map<uint16, std::string>* subtypes) {
209 if (!subtypes)
210 return toString();
211 if (!levels[0])
212 return "";
213
214 std::map<uint16, std::string>::iterator i, e = subtypes->end();
215 std::stringstream ss;
216 if ((i = subtypes->find(levels[0])) != e)
217 ss << i->second;
218 else
219 ss << levels[0];
220 for (uint32 n = 1; n < PSYTYPELEVELS; n++) {
221 if (levels[n]) {
222 if ((i = subtypes->find(levels[n])) != e)
223 ss << '.' << i->second;
224 else
225 ss << '.' << levels[n];
226 }
227 }
228 return ss.str();
229 }
230
233 bool fromString(const char* text) {
234 if (!text)
235 return false;
236 memset(levels, 0, sizeof(PsyType));
237
238 const char* pstr = text;
239 const char* src = text;
240 char *temp = new char[16];
241 uint32 n = 0;
242
243 while (pstr = strstr(src, ".")) {
244 if (pstr - src) {
245 memcpy(temp, src, pstr - src);
246 temp[pstr - src] = 0;
247 levels[n] = atoi(temp);
248 n++;
249 if (n == PSYTYPELEVELS)
250 return true;
251 }
252 src = pstr + 1;
253 }
254 pstr = src + strlen(src);
255 if (pstr - src) {
256 memcpy(temp, src, pstr - src);
257 temp[pstr - src] = 0;
258 levels[n] = atoi(temp);
259 n++;
260 }
261 delete[] temp;
262 return isValid();
263 }
264};
265
267#define PSYCONTEXTLEVELS 16
268
286 //bool operator=(const PsyContext &othertype) {
287 // return memcpy(levels, othertype.levels, sizeof(PsyContext));
288 //}
289 bool isValid() {return (levels[0] != 0);}
290 bool operator==(const PsyContext &othercontext) const {
291 return memcmp(levels, othercontext.levels, sizeof(PsyContext)) == 0;
292 }
293 bool operator!=(const PsyContext &othercontext) const {
294 return memcmp(levels, othercontext.levels, sizeof(PsyContext)) != 0;
295 }
296 bool operator<(const PsyContext &othercontext) const {
297 for (uint32 n = 0; n < PSYCONTEXTLEVELS; n++) {
298 if (levels[n] < othercontext.levels[n])
299 return true;
300 else if (levels[n] > othercontext.levels[n])
301 return false;
302 }
303 return false;
304 }
305 bool operator>(const PsyContext &othercontext) const {
306 for (uint32 n = 0; n < PSYCONTEXTLEVELS; n++) {
307 if (levels[n] > othercontext.levels[n])
308 return true;
309 else if (levels[n] < othercontext.levels[n])
310 return false;
311 }
312 return false;
313 }
314 uint16 operator[](int i) const {
315 if ((i < 0) && (i >= PSYCONTEXTLEVELS))
316 return 0;
317 return levels[i];
318 }
319 bool setLevel(int i, uint16 value) {
320 if ((i < 0) && (i >= PSYCONTEXTLEVELS))
321 return false;
322 levels[i] = value;
323 return true;
324 }
325
329 bool matches(const PsyContext& othercontext) const {
330 for (uint32 n = 0; n < PSYCONTEXTLEVELS; n++) {
331 if ((levels[n] != othercontext.levels[n]) &&
332 (levels[n] != 0xFFFF) &&
333 (othercontext.levels[n] != 0xFFFF) &&
334 (levels[n] != 0) &&
335 (othercontext.levels[n] != 0)
336 )
337 return false;
338 }
339 return true;
340 }
341 std::string toString() {
342 if (!levels[0])
343 return "";
344 std::stringstream ss;
345 ss << levels[0];
346 for (uint32 n = 1; n < PSYCONTEXTLEVELS; n++) {
347 if (levels[n])
348 ss << '.' << levels[n];
349 }
350 return ss.str();
351 }
352 std::string toString(std::map<uint16, std::string>* subcontexts) {
353 if (!subcontexts)
354 return toString();
355 if (!levels[0])
356 return "";
357
358 std::map<uint16, std::string>::iterator i, e = subcontexts->end();
359 std::stringstream ss;
360 if ((i = subcontexts->find(levels[0])) != e)
361 ss << i->second;
362 else
363 ss << levels[0];
364 for (uint32 n = 1; n < PSYTYPELEVELS; n++) {
365 if (levels[n]) {
366 if ((i = subcontexts->find(levels[n])) != e)
367 ss << '.' << i->second;
368 else
369 ss << '.' << levels[n];
370 }
371 }
372 return ss.str();
373 }
374 bool fromString(const char* text) {
375 if (!text)
376 return false;
377 memset(levels, 0, sizeof(PsyContext));
378
379 const char* pstr = text;
380 const char* src = text;
381 char *temp = new char[16];
382 uint32 n = 0;
383
384 while (pstr = strstr(src, ".")) {
385 if (pstr - src) {
386 memcpy(temp, src, pstr - src);
387 temp[pstr - src] = 0;
388 levels[n] = atoi(temp);
389 n++;
390 if (n == PSYCONTEXTLEVELS)
391 return true;
392 }
393 src = pstr + 1;
394 }
395 pstr = src + strlen(src);
396 if (pstr - src) {
397 memcpy(temp, src, pstr - src);
398 temp[pstr - src] = 0;
399 levels[n] = atoi(temp);
400 n++;
401 }
402 delete[] temp;
403 return isValid();
404 }
405
406 #define CONTEXT_OTHER_ROOT 0
407 #define CONTEXT_EQUAL 1
408 #define CONTEXT_SUPERSEDES 2
409 #define CONTEXT_INCLUDED 3
410
414 uint8 compare(const PsyContext& othercontext) const {
415 for (uint32 n = 0; n < PSYCONTEXTLEVELS; n++) {
416 if ((levels[n] != othercontext.levels[n]) && (levels[n] != 0xFFFF) && (othercontext.levels[n] != 0xFFFF)) {
417 if (n==0)
418 return CONTEXT_OTHER_ROOT;
419 else if (levels[n] == 0) {
420 if (othercontext.levels[n] == 0)
421 return CONTEXT_EQUAL;
422 else
423 return CONTEXT_INCLUDED;
424 }
425 // included in the statement below
426 // else if (otherContext.levels[n] == 0)
427 // return CONTEXT_SUPERSEDES;
428 else
429 return CONTEXT_SUPERSEDES;
430 }
431 }
432 return CONTEXT_EQUAL;
433 }
434};
435
436static struct PsyType NOTYPE = { { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 } };
437static struct PsyContext NOCONTEXT = { { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 } };
438
439#endif // TYPES_H
Platform/compiler detection and base portability macros for all of CMSDK.
#define CONTEXT_OTHER_ROOT
compare(): the contexts differ already at the root level.
Definition Types.h:406
static struct PsyType NOTYPE
The empty/unset message type (isValid() == false).
Definition Types.h:436
#define CONTEXT_EQUAL
compare(): the contexts are equal (up to wildcards).
Definition Types.h:407
static struct PsyContext NOCONTEXT
The empty/unset context (isValid() == false).
Definition Types.h:437
#define CONTEXT_INCLUDED
compare(): this context is more general and includes the other.
Definition Types.h:409
#define CONTEXT_SUPERSEDES
compare(): this context is more specific than / supersedes the other.
Definition Types.h:408
#define PSYCONTEXTLEVELS
Number of hierarchy levels in a PsyContext.
Definition Types.h:267
#define PSYTYPELEVELS
Number of hierarchy levels in a PsyType.
Definition Types.h:100
Hierarchical execution-context identifier.
Definition Types.h:284
bool fromString(const char *text)
Definition Types.h:374
bool operator<(const PsyContext &othercontext) const
Definition Types.h:296
bool operator!=(const PsyContext &othercontext) const
Definition Types.h:293
std::string toString()
Definition Types.h:341
uint8 compare(const PsyContext &othercontext) const
Classify the hierarchical relationship between this context and another.
Definition Types.h:414
bool operator>(const PsyContext &othercontext) const
Definition Types.h:305
bool matches(const PsyContext &othercontext) const
Wildcard-aware context matching: levels are considered matching when equal, or when either side is 0x...
Definition Types.h:329
uint16 levels[PSYCONTEXTLEVELS]
Hierarchy levels, most significant first; 0 terminates (and wildcards in matches()),...
Definition Types.h:285
std::string toString(std::map< uint16, std::string > *subcontexts)
Definition Types.h:352
bool isValid()
Definition Types.h:289
bool setLevel(int i, uint16 value)
Definition Types.h:319
uint16 operator[](int i) const
Definition Types.h:314
bool operator==(const PsyContext &othercontext) const
Definition Types.h:290
Hierarchical message type identifier — the key used for publish/subscribe matching in Psyclone.
Definition Types.h:123
bool matches(PsyType &otherType) const
Wildcard-aware type matching used by the subscription system.
Definition Types.h:173
bool fromString(const char *text)
Parse dotted decimal notation (e.g.
Definition Types.h:233
std::string toString()
Render as dotted decimal notation, e.g. "2.201.3"; empty string if invalid.
Definition Types.h:195
bool setLevel(int i, uint16 value)
Set an individual hierarchy level.
Definition Types.h:163
uint16 levels[PSYTYPELEVELS]
Hierarchy levels, most significant first; 0 terminates, 0xFFFF is a wildcard.
Definition Types.h:124
std::string toString(std::map< uint16, std::string > *subtypes)
Render as dotted notation, substituting readable names for level ids where available.
Definition Types.h:208
bool operator!=(const PsyType &othertype) const
Exact byte-wise inequality (wildcards NOT honoured).
Definition Types.h:132
bool isValid()
A type is valid if its root level is non-zero.
Definition Types.h:126
uint16 operator[](int i) const
Definition Types.h:154
bool operator<(const PsyType &othertype) const
Lexicographic level-by-level ordering, so PsyType can be used as a std::map/stdmultimap key.
Definition Types.h:136
bool operator==(const PsyType &othertype) const
Exact byte-wise equality of all levels (wildcards NOT honoured; use matches() for that).
Definition Types.h:128
bool operator>(const PsyType &othertype) const
Definition Types.h:145