Inexor
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
cube_tools.hpp
Go to the documentation of this file.
1 
2 
3 #pragma once
4 
5 #include <algorithm>
6 #include <cassert>
7 #include <cstring>
8 
9 #ifdef _DEBUG
10 #define ASSERT(c) assert(c)
11 #else
12 #define ASSERT(c) if(c) {}
13 #endif
14 
15 #if defined(__GNUC__) || (defined(_MSC_VER) && _MSC_VER >= 1400)
16 #define RESTRICT __restrict
17 #else
18 #define RESTRICT
19 #endif
20 
21 #ifdef __GNUC__
22 #define UNUSED __attribute__((unused)) //make sure unused code won't get optimized away.
23 #else
24 #define UNUSED
25 #endif
26 
28 #define DELETEP(p) if(p) { delete p; p = 0; }
29 
31 #define DELETEA(p) if(p) { delete[] p; p = 0; }
32 
34 template<class T> struct isclass
35 {
36  template<class C> static char test(void (C::*)(void));
37  template<class C> static int test(...);
38  enum
39  {
40  yes = sizeof(test<T>(nullptr)) == 1 ? 1 : 0,
41  no = yes^1
42  };
43 };
44 
45 #ifdef WIN32
46 #define strcasecmp _stricmp
48 #define strncasecmp _strnicmp
49 #else
50 // adapt macros to OS specifications
51 #define __cdecl
52 #define _vsnprintf vsnprintf
53 #endif
54 
55 
56 inline char *copystring(char *d, const char *s, size_t len)
57 {
58  size_t slen = std::min(strlen(s), len-1);
59  memcpy(d, s, slen);
60  d[slen] = 0;
61  return d;
62 }
63 template<size_t N> inline char *copystring(char(&d)[N], const char *s) { return copystring(d, s, N); }
64 
66 template<size_t N> inline bool matchstring(const char *s, size_t len, const char(&d)[N])
67 {
68  return len == N-1 && !memcmp(s, d, N-1);
69 }
70 
71 inline char *newstring(size_t l) { return new char[l+1]; }
72 inline char *newstring(const char *s, size_t l) { return copystring(newstring(l), s, l+1); }
73 inline char *newstring(const char *s) { size_t l = strlen(s); char *d = newstring(l); memcpy(d, s, l+1); return d; }
74 
75 
77 {
78  const char *str;
79  int len;
81  stringslice(const char *str, int len) : str(str), len(len) {}
82  stringslice(const char *str, const char *end) : str(str), len(int(end-str)) {}
83 
84  const char *end() const { return &str[len]; }
85 };
86 
87 inline char *newstring(const stringslice &s) { return newstring(s.str, s.len); }
88 inline const char *stringptr(const char *s) { return s; }
89 inline const char *stringptr(const stringslice &s) { return s.str; }
90 inline int stringlen(const char *s) { return int(strlen(s)); }
91 inline int stringlen(const stringslice &s) { return s.len; }
92 
93 inline char *copystring(char *d, const stringslice &s, size_t len)
94 {
95  size_t slen = std::min(size_t(s.len), len-1);
96  memcpy(d, s.str, slen);
97  d[slen] = 0;
98  return d;
99 }
100 template<size_t N> inline char *copystring(char(&d)[N], const stringslice &s) { return copystring(d, s, N); }
101 
Definition: cube_tools.hpp:41
Check if a given type is a class.
Definition: cube_tools.hpp:34
Definition: cube_tools.hpp:40
bool matchstring(const char *s, size_t len, const char(&d)[N])
Compare c-strings using memcmp.
Definition: cube_tools.hpp:66
const char * str
Definition: cube_tools.hpp:78
stringslice(const char *str, const char *end)
Definition: cube_tools.hpp:82
stringslice(const char *str, int len)
Definition: cube_tools.hpp:81
const T & min(const inexor::rpc::SharedVar< T > &a, const T &b)
Definition: SharedVar.hpp:210
const int C[3]
Definition: geom.hpp:1222
static char test(void(C::*)(void))
int d
Definition: octaedit.cpp:1749
Definition: cube_tools.hpp:76
char * copystring(char *d, const char *s, size_t len)
Definition: cube_tools.hpp:56
const char * end() const
Definition: cube_tools.hpp:84
const char * stringptr(const char *s)
Definition: cube_tools.hpp:88
int len
Definition: cube_tools.hpp:79
int stringlen(const char *s)
Definition: cube_tools.hpp:90
char * newstring(size_t l)
Definition: cube_tools.hpp:71
stringslice()
Definition: cube_tools.hpp:80