CMSDK  2.0.1
Types.h
1 #if !defined(__types_h) // Sentry, use file only if it's not already included.
2 #define __types_h
3 
4 #include "Standard.h"
5 #include <stdio.h>
6 #include <string.h>
7 // #define CNSPORT 10000
8 // #define FACTORYPORT 10001
9 
10 #define CNSPRIORITY 90
11 #define FACTORYPRIORITY 90
12 
13 #define BITFREE 1
14 #define BITOCCUPIED 0
15 
16 #ifdef WINDOWS
17 #else
18 #endif
19 
20 #if defined ARCH_32
21 
22  typedef bool bit;
23  typedef signed char int8;
24  typedef unsigned char uint8;
25  typedef short int16;
26  typedef unsigned short uint16;
27  typedef int int32;
28  typedef unsigned int uint32;
29  typedef long long int64;
30  typedef unsigned long long uint64;
31  typedef float float32;
32  typedef double float64;
33 // typedef uint32 paddr;
34 
35 #elif defined ARCH_64
36 
37  typedef bool bit;
38  typedef signed char int8;
39  typedef unsigned char uint8;
40  typedef short int16;
41  typedef unsigned short uint16;
42  typedef int int32;
43  typedef unsigned int uint32;
44  typedef float float32;
45  typedef double float64;
46 // typedef uint32 paddr;
47 
48  #ifdef WINDOWS
49  typedef long long int64;
50  typedef unsigned long long uint64;
51  #else
52  typedef long long int64;
53  typedef unsigned long long uint64;
54  #endif
55 
56 #endif
57 
58 #define INT32_NOVALUE 0x12345678
59 #define INT64_NOVALUE 0x123456789ABCDEF0L
60 #define FLOAT64_NOVALUE 0.123456789123456789
61 
62 #define MAXVALUINT8 ((uint8)~((uint8)0))
63 #define MAXVALINT8 ((int8)(MAXVALUINT8 >> 1))
64 #define MAXVALUINT16 ((uint16)~((uint16)0))
65 #define MAXVALINT16 ((int16)(MAXVALUINT16 >> 1))
66 #define MAXVALUINT32 ((uint32)~((uint32)0))
67 #define MAXVALINT32 ((int32)(MAXVALUINT32 >> 1))
68 #define MAXVALUINT64 ((uint64)~((uint64)0))
69 #define MAXVALINT64 ((int64)(MAXVALUINT64 >> 1))
70 
71 #define MERGEUINT3264(a,b) (((uint64)a)<<32)|((uint64)b)
72 #define GET1UINT3264(a) (uint32)((a & 0xffffffff00000000L)>>32)
73 #define GET2UINT3264(a) (uint32)(a & 0x00000000ffffffffL)
74 #define MERGEUINT1632(a,b) (((uint32)a)<<16)|((uint32)b)
75 #define GET1UINT1632(a) (uint16)((a & 0xffff0000L)>>16)
76 #define GET2UINT1632(a) (uint16)(a & 0x0000ffffL)
77 
78 #define PSYTYPELEVELS 16
79 
80 struct PsyType {
81  uint16 levels[PSYTYPELEVELS];
82  bool isValid() {return (levels[0] != 0);}
83  bool operator==(const PsyType &othertype) const {
84  return memcmp(levels, othertype.levels, sizeof(PsyType)) == 0;
85  }
86  bool operator!=(const PsyType &othertype) const {
87  return memcmp(levels, othertype.levels, sizeof(PsyType)) != 0;
88  }
89  bool operator<(const PsyType &othertype) const {
90  for (uint32 n = 0; n < PSYTYPELEVELS; n++) {
91  if (levels[n] < othertype.levels[n])
92  return true;
93  else if (levels[n] > othertype.levels[n])
94  return false;
95  }
96  return false;
97  }
98  bool operator>(const PsyType &othertype) const {
99  for (uint32 n = 0; n < PSYTYPELEVELS; n++) {
100  if (levels[n] > othertype.levels[n])
101  return true;
102  else if (levels[n] < othertype.levels[n])
103  return false;
104  }
105  return false;
106  }
107  uint16 operator[](int i) const {
108  if ((i < 0) && (i >= PSYTYPELEVELS))
109  return 0;
110  return levels[i];
111  }
112  bool setLevel(int i, uint16 value) {
113  if ((i < 0) && (i >= PSYTYPELEVELS))
114  return false;
115  levels[i] = value;
116  return true;
117  }
118  bool matches(PsyType& otherType) const {
119  for (uint32 n = 0; n < PSYTYPELEVELS; n++) {
120  if ((levels[n] != otherType.levels[n]) && (levels[n] != 0xFFFF) && (otherType.levels[n] != 0xFFFF))
121  return false;
122  }
123  return true;
124 
125  //v = levels[n] & otherType.levels[n];
126  //if ( (v != (levels[n] > otherType.levels[n] ? otherType.levels[n] : levels[n])))
127  // return false;
128  //uint64 *a = (uint64*)&levels;
129  //uint64 *b = (uint64*)&otherType.levels;
130  //uint64 v;
131  //uint32 n, c = sizeof(levels) / sizeof(uint64);
132  //for (n = 0; n < c; n++) {
133  // v = *a++ & *b++;
134  // if ( (v != (*a>*b?*b:*a)))
135  // return false;
136  //}
137  //return true;
138  }
139  std::string toString() {
140  if (!levels[0])
141  return "";
142  std::stringstream ss;
143  ss << levels[0];
144  for (uint32 n = 1; n < PSYTYPELEVELS; n++) {
145  if (levels[n])
146  ss << '.' << levels[n];
147  }
148  return ss.str();
149  }
150  std::string toString(std::map<uint16, std::string>* subtypes) {
151  if (!subtypes)
152  return toString();
153  if (!levels[0])
154  return "";
155 
156  std::map<uint16, std::string>::iterator i, e = subtypes->end();
157  std::stringstream ss;
158  if ((i = subtypes->find(levels[0])) != e)
159  ss << i->second;
160  else
161  ss << levels[0];
162  for (uint32 n = 1; n < PSYTYPELEVELS; n++) {
163  if (levels[n]) {
164  if ((i = subtypes->find(levels[n])) != e)
165  ss << '.' << i->second;
166  else
167  ss << '.' << levels[n];
168  }
169  }
170  return ss.str();
171  }
172  bool fromString(const char* text) {
173  if (!text)
174  return false;
175  memset(levels, 0, sizeof(PsyType));
176 
177  const char* pstr = text;
178  const char* src = text;
179  char *temp = new char[16];
180  uint32 n = 0;
181 
182  while (pstr = strstr(src, ".")) {
183  if (pstr - src) {
184  memcpy(temp, src, pstr - src);
185  temp[pstr - src] = 0;
186  levels[n] = atoi(temp);
187  n++;
188  if (n == PSYTYPELEVELS)
189  return true;
190  }
191  src = pstr + 1;
192  }
193  pstr = src + strlen(src);
194  if (pstr - src) {
195  memcpy(temp, src, pstr - src);
196  temp[pstr - src] = 0;
197  levels[n] = atoi(temp);
198  n++;
199  }
200  delete[] temp;
201  return isValid();
202  }
203 };
204 
205 #define PSYCONTEXTLEVELS 16
206 
207 struct PsyContext {
208  uint16 levels[PSYCONTEXTLEVELS];
209  //bool operator=(const PsyContext &othertype) {
210  // return memcpy(levels, othertype.levels, sizeof(PsyContext));
211  //}
212  bool isValid() {return (levels[0] != 0);}
213  bool operator==(const PsyContext &othercontext) const {
214  return memcmp(levels, othercontext.levels, sizeof(PsyContext)) == 0;
215  }
216  bool operator!=(const PsyContext &othercontext) const {
217  return memcmp(levels, othercontext.levels, sizeof(PsyContext)) != 0;
218  }
219  bool operator<(const PsyContext &othercontext) const {
220  for (uint32 n = 0; n < PSYCONTEXTLEVELS; n++) {
221  if (levels[n] < othercontext.levels[n])
222  return true;
223  else if (levels[n] > othercontext.levels[n])
224  return false;
225  }
226  return false;
227  }
228  bool operator>(const PsyContext &othercontext) const {
229  for (uint32 n = 0; n < PSYCONTEXTLEVELS; n++) {
230  if (levels[n] > othercontext.levels[n])
231  return true;
232  else if (levels[n] < othercontext.levels[n])
233  return false;
234  }
235  return false;
236  }
237  uint16 operator[](int i) const {
238  if ((i < 0) && (i >= PSYCONTEXTLEVELS))
239  return 0;
240  return levels[i];
241  }
242  bool setLevel(int i, uint16 value) {
243  if ((i < 0) && (i >= PSYCONTEXTLEVELS))
244  return false;
245  levels[i] = value;
246  return true;
247  }
248  bool matches(const PsyContext& othercontext) const {
249  for (uint32 n = 0; n < PSYCONTEXTLEVELS; n++) {
250  if ((levels[n] != othercontext.levels[n]) &&
251  (levels[n] != 0xFFFF) &&
252  (othercontext.levels[n] != 0xFFFF) &&
253  (levels[n] != 0) &&
254  (othercontext.levels[n] != 0)
255  )
256  return false;
257  }
258  return true;
259  }
260  std::string toString() {
261  if (!levels[0])
262  return "";
263  std::stringstream ss;
264  ss << levels[0];
265  for (uint32 n = 1; n < PSYCONTEXTLEVELS; n++) {
266  if (levels[n])
267  ss << '.' << levels[n];
268  }
269  return ss.str();
270  }
271  std::string toString(std::map<uint16, std::string>* subcontexts) {
272  if (!subcontexts)
273  return toString();
274  if (!levels[0])
275  return "";
276 
277  std::map<uint16, std::string>::iterator i, e = subcontexts->end();
278  std::stringstream ss;
279  if ((i = subcontexts->find(levels[0])) != e)
280  ss << i->second;
281  else
282  ss << levels[0];
283  for (uint32 n = 1; n < PSYTYPELEVELS; n++) {
284  if (levels[n]) {
285  if ((i = subcontexts->find(levels[n])) != e)
286  ss << '.' << i->second;
287  else
288  ss << '.' << levels[n];
289  }
290  }
291  return ss.str();
292  }
293  bool fromString(const char* text) {
294  if (!text)
295  return false;
296  memset(levels, 0, sizeof(PsyContext));
297 
298  const char* pstr = text;
299  const char* src = text;
300  char *temp = new char[16];
301  uint32 n = 0;
302 
303  while (pstr = strstr(src, ".")) {
304  if (pstr - src) {
305  memcpy(temp, src, pstr - src);
306  temp[pstr - src] = 0;
307  levels[n] = atoi(temp);
308  n++;
309  if (n == PSYCONTEXTLEVELS)
310  return true;
311  }
312  src = pstr + 1;
313  }
314  pstr = src + strlen(src);
315  if (pstr - src) {
316  memcpy(temp, src, pstr - src);
317  temp[pstr - src] = 0;
318  levels[n] = atoi(temp);
319  n++;
320  }
321  delete[] temp;
322  return isValid();
323  }
324 
325  #define CONTEXT_OTHER_ROOT 0
326  #define CONTEXT_EQUAL 1
327  #define CONTEXT_SUPERSEDES 2
328  #define CONTEXT_INCLUDED 3
329 
330  uint8 compare(const PsyContext& othercontext) const {
331  for (uint32 n = 0; n < PSYCONTEXTLEVELS; n++) {
332  if ((levels[n] != othercontext.levels[n]) && (levels[n] != 0xFFFF) && (othercontext.levels[n] != 0xFFFF)) {
333  if (n==0)
334  return CONTEXT_OTHER_ROOT;
335  else if (levels[n] == 0) {
336  if (othercontext.levels[n] == 0)
337  return CONTEXT_EQUAL;
338  else
339  return CONTEXT_INCLUDED;
340  }
341  // included in the statement below
342  // else if (otherContext.levels[n] == 0)
343  // return CONTEXT_SUPERSEDES;
344  else
345  return CONTEXT_SUPERSEDES;
346  }
347  }
348  return CONTEXT_EQUAL;
349  }
350 };
351 
352 static struct PsyType NOTYPE = { { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 } };
353 static struct PsyContext NOCONTEXT = { { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 } };
354 
355 #endif // TYPES_H
Definition: Types.h:207
Definition: Types.h:80