Xpace
types.h
Go to the documentation of this file.
1 
2 /**************************************************************
3  **
4  ** @file base/types.h
5  **
6  ** Copyright (C) 2012 Xpace, LLC. All rights reserved
7  **
8  ** www.xpace.net
9  **
10  **************************************************************/
11 
12 
13 #ifndef XPACE_TYPES_H
14 #define XPACE_TYPES_H
15 
16 #include <cstdlib> // for size_t
17 #include <memory> // for unique_ptr
18 #include <functional>
19 #include <cassert>
20 #include <cerrno>
21 #include <limits>
22 
23 #include <vector>
24 
25 #define XPACE_RELEASE 1.1.5
26 
27 #if defined _WIN32 && defined _DEBUG
28  #define _CRTDBG_MAP_ALLOC
29  #include <stdlib.h>
30  #include <crtdbg.h>
31 //!!! have to do this bcs Qt uses free & realloc as function names
32  #undef free
33  #undef realloc
34 #endif
35 
36 /// import/export attribute, used only for Windows DLLs
37 #if !defined XPACE_EXPORT
38 # if !defined _WIN32 || defined XPACE_STATICLIB
39 # define XPACE_EXPORT
40 # else
41 # define XPACE_EXPORT __declspec(dllimport)
42 # endif
43 #endif
44 
45 /// this definition requests an 'inline' that the compiler should honor
46 #if !defined FORCEINLINE
47 # if defined NDEBUG
48 # if defined _WIN32
49 # define FORCEINLINE __forceinline
50 # elif defined __GNUC__
51 # define FORCEINLINE inline __attribute__((always_inline))
52 # else
53 # error need to define FORCEINLINE for this compiler
54 # endif
55 # else
56 # define FORCEINLINE inline
57 # endif
58 #endif
59 
60 #if defined __GNUC__
61 #define override // until GCC 4.7
62 #endif
63 
64 // on Windows, these may be defined
65 #undef max
66 #undef min
67 
68 /// %Xpace project main namespace
69 namespace Xpace
70 {
71  /// @name unsigned types
72  //@{
73  typedef unsigned char uchar;
74  typedef uchar byte;
75  typedef unsigned int uint;
76  //@}
77 
78  /// @name explicity-sized types
79  //@{
80  typedef char int8;
81  typedef unsigned char uint8;
82  typedef short int16;
83  typedef unsigned short uint16;
84  typedef int int32;
85  typedef unsigned int uint32;
86  typedef long long int64;
87  typedef unsigned long long uint64;
88  //@}
89 
90  /// @name check an unsigned number against an error value
91  /// @param the (unsigned) number to test
92  /// @return bool if it's invalid
93  template <typename N>
94  inline
95  bool INVALID
96  (N n)
97  {
98  return n == N(~0);
99  }
100 
101  /// @name convert signed type to unsigned type
102  //@{
103  template<typename intType> struct Unsigned { typedef intType Type; };
104 
105  template<> struct Unsigned<int8> { typedef uint8 Type; };
106  template<> struct Unsigned<int16> { typedef uint16 Type; };
107  template<> struct Unsigned<int32> { typedef uint32 Type; };
108  template<> struct Unsigned<int64> { typedef uint64 Type; };
109 
110  #define UNSIGN(T) typename Unsigned<T>::Type
111  //@}
112 
113  /// @name convert unsigned type to signed type
114  //@{
115  template<typename intType> struct Signed { typedef intType Type; };
116 
117  template<> struct Signed<uint8> { typedef int8 Type; };
118  template<> struct Signed<uint16> { typedef int16 Type; };
119  template<> struct Signed<uint32> { typedef int32 Type; };
120  template<> struct Signed<uint64> { typedef int64 Type; };
121 
122  #define SIGN(T) typename Signed<T>::Type
123  #define DIFF(T) typename Signed<T>::Type
124 
125  //@}
126 
127  /// @name convert unsigned type to signed type, unsigned to signed
128  //@{
129  template<typename intType> struct ChangeSign {};
130 
131  template<> struct ChangeSign<int8> { typedef uint8 Type; };
132  template<> struct ChangeSign<uint8> { typedef int8 Type; };
133  template<> struct ChangeSign<int16> { typedef uint16 Type; };
134  template<> struct ChangeSign<uint16> { typedef int16 Type; };
135  template<> struct ChangeSign<int32> { typedef uint32 Type; };
136  template<> struct ChangeSign<uint32> { typedef int32 Type; };
137  template<> struct ChangeSign<int64> { typedef uint64 Type; };
138  template<> struct ChangeSign<uint64> { typedef int64 Type; };
139 
140  #define CHANGE_SIGN(T) typename ChangeSign<T>::Type
141  //@}
142 
143  /// @name UTF char types
144  //@{
145  typedef unsigned char utf8_t;
146  typedef unsigned short utf16_t;
147 
148  //@}
149 
150  /// @name fastest native types of at least 32 bits
151  //@{
152  #if defined _WIN64 || defined __LP64__
153  // native 64-bit
154  typedef int64 int_fast32;
155  typedef uint64 uint_fast32;
156  #else
157  // native 32-bit
158  typedef int32 int_fast32;
159  typedef uint32 uint_fast32;
160  #endif
161  //@}
162 
163  /// A low-level const data holder
164  template <typename T>
165  struct Ref
166  {
167  typedef T baseType;
168 
169  const T* data; ///< first T
170  size_t length; ///< number of Ts
171 
172  /// constructor
173  /// @param d first T
174  /// @param len number of Ts
175  Ref
176  (const T* d = 0,
177  size_t len = 0);
178  };
179 
181 
182  /// A low-level data holder
183  template <typename T>
184  struct Buf
185  {
186  T *const data; ///< first T
187  size_t length; ///< number of Ts
188 
189  /// constructor
190  /// @param d first T
191  /// @param len number of Ts
192  Buf
193  (T* d,
194  size_t len = 0);
195  };
196 
197 
198  // ================================ STRINGS =================
199 
200  /// Raw Strings - just a pointer and length
201  template <typename CH>
202  struct RawString
203  {
204  typedef CH charType;
205 
206  CH* data;
207  size_t length;
208 
209  RawString
210  (CH* d = 0,
211  size_t len = 0) :
212  data(d),
213  length(len)
214  {
215  }
216 
217  const RawString& operator=
218  (const RawString<CH>& rhs)
219  {
220  length = rhs.length;
221  data = rhs.data;
222  return *this;
223  }
224 
225  bool operator==
226  (const RawString<CH>& rhs)
227  const
228  {
229  return (length == rhs.length) &&
230  !memcmp(data, rhs.data, sizeof(CH) * length);
231  }
232 
233  bool operator!=
234  (const RawString<CH>& rhs)
235  const
236  {
237  return !(*this == rhs);
238  }
239 
240  bool operator>
241  (const RawString<CH>& rhs)
242  const
243  {
244  int cmp(memcmp(data, rhs.data, sizeof(CH) * std::min(length, rhs.length)));
245  if (cmp > 0)
246  return true;
247  if (cmp < 0)
248  return false;
249  return length > rhs.length;
250  }
251 
252  bool operator<
253  (const RawString<CH>& rhs)
254  const
255  {
256  int cmp(memcmp(data, rhs.data, sizeof(CH) * std::min(length, rhs.length)));
257  if (cmp < 0)
258  return true;
259  if (cmp > 0)
260  return false;
261  return length < rhs.length;
262  }
263  };
264 
267 
268  /// A string, Unicode UTF-16 and reference-counted
270  {
271  public:
272  /// Construct a String from a UTF-8 array of 8-bit chars
273  /// @param s the array of chars
274  /// @param len if != 0, the length of the array; if 0, array is 0-terminated
275  String
276  (const utf8_t* s,
277  size_t len = 0);
278 
279  /// Construct a String from a UTF-16 array of 16-bit chars
280  /// @param s the array of chars
281  /// @param len if != 0, the length of the array; if 0, array is 0-terminated
282  String
283  (const utf16_t* s,
284  size_t len = 0);
285 
286  /// Construct a String from an array of 8-bit chars
287  /// @param s the array of chars
288  /// @param len if != 0, the length of the array; if 0, array is 0-terminated
289  String
290  (const char* s,
291  size_t len = 0);
292 
293  /// Construct a String from a String8
294  /// @param s the String8
295  String
296  (const String8& s);
297 
298  /// @return the length of the string, in chars
299  size_t getLength
300  ()
301  const;
302 
303  /// convert a String into a (null-terminated) UTF-8 string, writing it into a buffer
304  /// @param buf the buffer into which to write
305  /// @param bufLen the lemgth of the buffer
306  /// @return the buffer
307  utf8_t* toUtf8
308  (utf8_t* buf,
309  size_t bufLen)
310  const;
311 
312  /// convert a String into a (null-terminated) UTF-8 string, writing it into a vector
313  std::vector<utf8_t> toUtf8
314  ()
315  const;
316 
317  /// @return the (null-terminated) UTF-16 representation of the string
318  const utf16_t* toUtf16
319  ()
320  const;
321 
322  #if defined QSTRING_H || defined DOCUMENTATION
323  /// Convert a QString to a String
324  /// @param s the source QString
325  String
326  (const QString& s);
327  /// Convert a (const) String into a (const) QString
328  /// @return the QString
329  operator const QString
330  ()
331  const;
332  /// Convert a String into a QString
333  /// @return the QString
334  operator QString
335  ();
336  #endif
337 
338  #if defined _STRING_ || defined DOCUMENTATION
339  /// Convert a std::string to a String
340  /// param s the std::string
341  String
342  (const std::string& s);
343  /// Convert a std::string to a String
344  /// param s the std::string
345  String
346  (const std::wstring& s);
347  /// Convert a String into a std::string
348  /// return the std::string
349  std::string toStdString
350  ()
351  const;
352  /// Convert a String into a std::string
353  /// return the std::string
354  std::wstring toStdWString
355  ()
356  const;
357  #endif
358 
359  /// Construct an empty string
360  String
361  ();
362  String
363  (const String&);
364  String& operator=
365  (const String&);
366  ~String
367  ();
368 
369  /// Make a string empty
370  void clear
371  ();
372 
373  /// Make a string from a number
374  /// @param n the source number
375  /// @return the string
376  static String setNum
377  (int64 n);
378 
379  /// Get a number from a (base-10) string
380  /// @param fillin true if the string could be converted, false if not
381  /// @return value converted
382  int64 getInt
383  (bool* ok = 0)
384  const;
385 
386  /// @return true is String is empty
387  bool operator!
388  ()
389  const;
390 
391  /// @return true if this String matches rhs
392  bool operator==
393  (const String& rhs)
394  const;
395 
396  /// @return true if this String does not match rhs
397  bool operator!=
398  (const String& rhs)
399  const
400  {
401  return !(*this == rhs);
402  };
403 
404  /// @return true if this String is less than rhs
405  bool operator<
406  (const String& rhs)
407  const;
408 
409  /// @return true if theis String matches rhs w/o case sensitvity
410  bool matchNoCase
411  (const String& rhs)
412  const;
413 
414  /// concatenate rhs to this string
415  /// @param rhs the char to concatenate
416  /// @return *this
417  String& operator+=
418  (utf16_t rhs);
419 
420  /// concatenate rhs to this string
421  /// @param rhs the string to concatenate
422  /// @return *this
423  String& operator+=
424  (const String& rhs);
425 
426  private :
427  friend class access;
428  intptr_t impl;
429  };
430 
431  #if defined _STRING_ || defined DOCUMENTATION
432  /// Output a String to a narrow char stream
433  XPACE_EXPORT std::ostream& operator<<
434  (std::ostream& ostr,
435  const String& str);
436 
437  /// Output a String to a wide char stream
438  XPACE_EXPORT std::wostream& operator<<
439  (std::wostream& ostr,
440  const String& str);
441  #endif
442 
443  // ================================ REVEAL CONTENTS OF A STRING
444 
445  #ifdef NDEBUG
446  #define REVEAL_STRING(s, Str)
447  #elif defined QSTRING_H
448  #define REVEAL_STRING(s, Str) const std::wstring s(reinterpret_cast<const wchar_t*>(QString(Str).utf16()))
449  #endif
450 
451  // ================================ STATUS CALLBACK ---------
452 
453  //
454  /// A status callback passed to operations that could be time-consuming
455  /// @param val a number indicating the operation's progress
456  /// @return the desired interval before the next call to this function, 0 to avoid being called, -1 to abort
457  typedef std::function<int64(uint64 val)> StatusCallback;
458 
459  /// Default status function
460  /// @return 0 so we're never called again
461  static StatusCallback defaultStatus = [](int64 doc){ return 0; };
462 
463 
464  // ================================ POWERS OF 10 ============
465 
466  //inline
467  uint32 XPACE_EXPORT pow10_32
468  (uint p);
469 
470  //inline won't work in VS 2012
471  uint64 XPACE_EXPORT pow10_64
472  (uint p);
473 
474 
475  // ================================ TRACE ===================
476 
477  /// debugging
479  {
480  public:
481  Trace
482  (const char* ID,
483  const char* format = 0,
484  ...);
485  ~Trace
486  ();
487 
488  static void Msg
489  (const char* format = 0,
490  ...);
491 
492  #if defined _TRACE || defined DOCUMENTATION
493  private:
494  String id;
495  #endif
496  };
497 
498  // ================================ CHECKED TYPE CONVERSION ===
499 
500  /// checked loss-of-precision cast
501  template <typename TO, typename FROM>
502  inline TO narrow_to
503  (FROM from)
504  {
505  assert(FROM(TO(from)) == from);
506  return TO(from);
507  }
508 
509  // ============================================================
510  // ============================================================
511  // ============================================================
512 
513  template <typename T>
514  inline
516  (const T* d,
517  size_t len) :
518  data(d),
519  length(len)
520  {
521  }
522 
523  template <typename T>
524  inline
526  (T* d,
527  size_t len) :
528  data(d),
529  length(len)
530  {
531  }
532 
533  #ifndef _TRACE
534  inline
536  (const char* /*ID*/,
537  const char*,
538  ...)
539  {
540  }
541 
542  inline
544  ()
545  {
546  }
547 
548  inline
549  void Trace::Msg
550  (const char*,
551  ...)
552  {
553  }
554  #endif
555 
556 #if !defined(_WIN32)
557  // nonstandard MS library functions supplied in namespace
558  // Xpace on other platforms
559  typedef int errno_t;
560  int64 _atoi64(const char*);
561  int64 _wtoi64(const wchar_t*);
562  int sprintf_s(char*, size_t, const char*, ...);
563  int swprintf_s(wchar_t*, size_t, const wchar_t*, ...);
564  char* strtok_s(char*, const char*, char**);
565  errno_t strncpy_s(char*, size_t, const char*, size_t);
566  errno_t _itoa_s(int, char*, size_t, int);
567  errno_t _itow_s(int, wchar_t*, size_t, int);
568  errno_t _i64toa_s(int64, char*, size_t, int);
569  errno_t _i64tow_s(int64, wchar_t*, size_t, int);
570  errno_t wmemmove_s(wchar_t*, size_t, const wchar_t*, size_t);
571  #define _strdup(s) strdup(s)
572 
573  enum { _CVTBUFSIZE = 349 };
574 #endif
575 
576 } // namespace Xpace
577 
578 #endif
579 
Buf(T *d, size_t len=0)
constructor
Definition: types.h:526
debugging
Definition: types.h:478
A low-level data holder.
Definition: types_c.h:82
int int32
Definition: types.h:84
Ref< byte > BytesRef
Definition: types.h:180
unsigned int uint
Definition: types.h:75
Ref(const T *d=0, size_t len=0)
constructor
Definition: types.h:516
int sprintf_s(char *, size_t, const char *,...)
bool INVALID(N n)
Definition: types.h:96
char int8
Definition: types.h:80
A low-level data holder.
Definition: types.h:184
A string, Unicode UTF-16 and reference-counted.
Definition: types.h:269
uint const Xpace_Char8 size_t len
Definition: table_c.h:180
unsigned short utf16_t
Definition: types.h:146
unsigned char uint8
Definition: types.h:81
A low-level const data holder.
Definition: types.h:165
uint64 XPACE_EXPORT pow10_64(uint p)
int64 _wtoi64(const wchar_t *)
unsigned long long uint64
Definition: types.h:87
int64 _atoi64(const char *)
Raw Strings - just a pointer and length.
Definition: types.h:202
Xpace_StoreAccess access
Definition: table_c.h:42
static void Msg(const char *format=0,...)
Definition: types.h:550
int32 int_fast32
Definition: types.h:158
static StatusCallback defaultStatus
Default status function.
Definition: types.h:461
size_t length
Definition: types.h:207
uint32 XPACE_EXPORT pow10_32(uint p)
errno_t _i64tow_s(int64, wchar_t *, size_t, int)
errno_t _itoa_s(int, char *, size_t, int)
uint bool * ok
Definition: table_c.h:117
uint32 uint_fast32
Definition: types.h:159
short int16
Definition: types.h:82
std::function< int64(uint64 val)> StatusCallback
A status callback passed to operations that could be time-consuming.
Definition: types.h:457
errno_t _i64toa_s(int64, char *, size_t, int)
TO narrow_to(FROM from)
checked loss-of-precision cast
Definition: types.h:503
intType Type
Definition: types.h:103
char * strtok_s(char *, const char *, char **)
errno_t _itow_s(int, wchar_t *, size_t, int)
unsigned char uchar
Definition: types.h:73
errno_t strncpy_s(char *, size_t, const char *, size_t)
int swprintf_s(wchar_t *, size_t, const wchar_t *,...)
RawString< utf8_t > String8
Definition: types.h:265
long long int64
Definition: types.h:86
size_t length
number of Ts
Definition: types.h:170
Trace(const char *ID, const char *format=0,...)
Definition: types.h:536
unsigned int uint32
Definition: types.h:85
RawString< utf16_t > String16
Definition: types.h:266
errno_t wmemmove_s(wchar_t *, size_t, const wchar_t *, size_t)
T baseType
Definition: types.h:167
unsigned short uint16
Definition: types.h:83
uint const Xpace_Char8 * data
Definition: table_c.h:180
unsigned char utf8_t
Definition: types.h:145
intType Type
Definition: types.h:115
Xpace project main namespace
Definition: datetime.h:18
int errno_t
Definition: types.h:559
size_t length
number of Ts
Definition: types.h:187
const T * data
first T
Definition: types.h:169
uchar byte
Definition: types.h:74
T *const data
first T
Definition: types.h:186

current as of Wed Jun 10 2026 12:00:05