gu_util
gu_util.h
Go to the documentation of this file.
1/*
2 * $Id$
3 *
4 * gu_util.h
5 * GUNao
6 *
7 * Created by Rene Hexel on 24/04/10.
8 * Copyright 2010-2013, 2015, 2021 Rene Hexel. All rights reserved.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 *
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 *
17 * 2. Redistributions in binary form must reproduce the above
18 * copyright notice, this list of conditions and the following
19 * disclaimer in the documentation and/or other materials
20 * provided with the distribution.
21 *
22 * 3. All advertising materials mentioning features or use of this
23 * software must display the following acknowledgement:
24 *
25 * This product includes software developed by Rene Hexel.
26 *
27 * 4. Neither the name of the author nor the names of contributors
28 * may be used to endorse or promote products derived from this
29 * software without specific prior written permission.
30 *
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
34 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
35 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
36 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
37 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
38 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
39 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
40 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
41 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42 *
43 * -----------------------------------------------------------------------
44 * This program is free software; you can redistribute it and/or
45 * modify it under the above terms or under the terms of the GNU
46 * General Public License as published by the Free Software Foundation;
47 * either version 2 of the License, or (at your option) any later version.
48 *
49 * This program is distributed in the hope that it will be useful,
50 * but WITHOUT ANY WARRANTY; without even the implied warranty of
51 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
52 * GNU General Public License for more details.
53 *
54 * You should have received a copy of the GNU General Public License
55 * along with this program; if not, see http://www.gnu.org/licenses/
56 * or write to the Free Software Foundation, Inc., 51 Franklin Street,
57 * Fifth Floor, Boston, MA 02110-1301, USA.
58 *
59 */
60#ifndef GU_UTIL_H_
61#define GU_UTIL_H_
62
63#pragma clang diagnostic push
64#pragma clang diagnostic ignored "-Wc++98-compat-pedantic"
65#pragma clang diagnostic ignored "-Wc++98-compat"
66#pragma clang diagnostic ignored "-Wc++11-long-long"
67#pragma clang diagnostic ignored "-Wunused-macros"
68#pragma clang diagnostic ignored "-Wreserved-id-macro"
69#pragma clang diagnostic ignored "-Wdeprecated"
70#pragma clang diagnostic ignored "-Wold-style-cast"
71
72#ifndef _POSIX_SOURCE
73#define _POSIX_SOURCE 200112L
74#endif
75#ifndef _XOPEN_SOURCE
76#define _XOPEN_SOURCE
77#endif
78#ifdef __APPLE__
79#ifndef _DARWIN_C_SOURCE
80#define _DARWIN_C_SOURCE 200112L
81#ifndef __DARWIN_C_LEVEL
82#define __DARWIN_C_LEVEL 200112L
83#endif
84#endif
85#endif
86
87#ifdef DEBUG
88#ifndef DBG
89#define DBG(x) x
90#endif
91#ifndef DPRINTF
92#define DPRINTF(x) printf x
93#endif
94#else
95#ifndef DBG
96#define DBG(x)
97#endif
98#ifndef DPRINTF
99#define DPRINTF(x)
100#endif
101#endif
102
103#define protected_msleep(ms) protected_usleep(1000LL*(ms))
104#define protected_sleep(s) protected_usleep(1000000LL*(s))
105
106#undef __block
107#define __block _xblock
108#ifdef __cplusplus
109#include <vector>
110#include <string>
111#include <cstring>
112#include <cmath>
113#include <map> // read_config
114#else
115#include <math.h>
116#include <string.h>
117#endif
118#undef __block
119#define __block __attribute__((__blocks__(byref)))
120
121#ifdef __cplusplus
122
123#ifdef bool
124#undef bool
125#endif
126
127#ifdef true
128#undef true
129#undef false
130#endif
131
132#ifndef NULLPTR
133# if __cplusplus >= 201103L
134# define NULLPTR nullptr
135# else
136# define NULLPTR NULL
137# endif
138#endif
139
140#ifndef OVERRIDE
141# if __cplusplus >= 201103L
142# define OVERRIDE override
143# else
144# define OVERRIDE
145# endif
146#endif
147
148#ifndef NOEXCEPT
149# if __cplusplus >= 201103L
150# define NOEXCEPT noexcept
151# else
152# define NOEXCEPT
153# endif
154#endif
155
156#define atoi16(s) static_cast<int16_t>(atoi(s))
157#define atou16(s) static_cast<uint16_t>(atoi(s))
158#define atoi32(s) static_cast<int32_t>(atoi(s))
159#define atou32(s) static_cast<uint32_t>(atoi(s))
160#define atoi64(s) static_cast<int64_t>(atoll(s))
161#define atou64(s) static_cast<uint64_t>(atoll(s))
162
163#define DOXY_MASK(n)
164#define DOXY_S(n)
165#define DOXY_SS(n, s)
166#define DOXY_G(n)
167#define DOXY_SG(n, s)
168#define DOXY_GC(n)
169#define DOXY_SGC(n, s)
170#define DOXY_ARRAY_G(n)
171#define DOXY_ARRAY_GC(n)
172#define DOXY_VERSION_NEW(n)
174#define DOXY_BAPG(n)
175#define DOXY_BAPGC(n)
176#define DOXY_BAPS(n)
178#define PROPERTY_DECLARATION(t, n) protected: t _##n;
179#define PROPERTY_GETTER(t, n) public: DOXY_G(n) t &n () { return _##n ; } DOXY_GC(n) const t &n () const { return _##n ; }
180#define PROPERTY_SETTER(t, n) public: DOXY_S(n) void set_##n (const t &n ) { _##n = n ; }
181#define PROPERTY(type, name) PROPERTY_DECLARATION(type,name) PROPERTY_GETTER(type,name) PROPERTY_SETTER(type,name)
182
183#define BIT_DECLARATION(n) protected: unsigned int _##n : 1;
184#define BIT_GETTER(n) public: DOXY_G(n) unsigned int n () DOXY_GC(n) const { return _##n ; }
185#define BIT_SETTER(n) public: DOXY_S(n) void set_##n (const unsigned int n = true) { _##n = n ; }
186#define BIT_PROPERTY(name) BIT_DECLARATION(name) BIT_GETTER(name) BIT_SETTER(name)
187
188#define ARRAY_PROPERTY_DECLARATION(t, n, s) protected: t _##n[s];
189#define ARRAY_PROPERTY_GETTER(t, n, s) public: DOXY_ARRAY_G(n) t &n (int e) { return _##n[e] ; } DOXY_ARRAY_GC(n) const t &n (int e) const { return _##n[e] ; } const t* n() const { return _##n; } size_t n##_size() { return s; }
190#define ARRAY_PROPERTY_SETTER(t, n) public: DOXY_S(n) void set_##n (const t &v, int e ) { _##n[e] = v ; }
191#define ARRAY_PROPERTY(type, name, size) ARRAY_PROPERTY_DECLARATION(type,name,size) ARRAY_PROPERTY_GETTER(type,name,size) ARRAY_PROPERTY_SETTER(type,name)
192
193#define STRING_PROPERTY_DECLARATION(n, s) protected: char _##n[s];
194#define STRING_PROPERTY_GETTER(n, s) public: DOXY_SG(n,s) char* n () { return &(_##n[0]); } DOXY_SGC(n,s) const char* n () const { return &(_##n[0]); }
195#define STRING_PROPERTY_SETTER(n, s) public: DOXY_SS(n,s) void set_##n (const char* n ) { gu_strlcpy(_##n,n,(s)); }
196#define STRING_PROPERTY(name, size) STRING_PROPERTY_DECLARATION(name, size) STRING_PROPERTY_GETTER(name, size) STRING_PROPERTY_SETTER(name, size)
197
198
199#define BIT_ARRAY_PROPERTY_DECLARATION(n, s) protected: uint8_t _##n[((s-1)/8)+1];
200#define BIT_ARRAY_PROPERTY_GETTER(n) public: DOXY_BAPG(n) uint8_t n (int b) { return ( (_##n[( b/8) ] & (1 << (b%8) ) ) >> (b%8)); } DOXY_BAPGC(n) const uint8_t n (int b) const { return ( (_##n[(b/8)] & (1 << (b%8))) >>(b%8) ); }
201#define BIT_ARRAY_PROPERTY_SETTER(n) public: DOXY_BAPS(n) void set_##n (int b, const unsigned int v = true ) { ( (v)&1 ? _##n[(b/8)] |= (1 << (b%8)) : _##n[(b/8)] &= ~(1 << (b%8)) ); }
202#define BIT_ARRAY_PROPERTY(name, size) BIT_ARRAY_PROPERTY_DECLARATION(name, size) BIT_ARRAY_PROPERTY_GETTER(name) BIT_ARRAY_PROPERTY_SETTER(name)
203
204#define CONTROLLED_SETTER(t, n) public: DOXY_S(n) void set_##n (const t &n ) { _##n = n ; _##n##_mask = 1;}
205#define CONTROLLED_PROPERTY(type, name) PROPERTY_DECLARATION(type, name) PROPERTY_GETTER(type, name) CONTROLLED_SETTER(type, name)
206
207#define CONTROLLED_ARRAY_PROPERTY_SETTER(t, n, s) public: DOXY_S(n) void set_##n(const t* n, size_t size) { _##n##_mask = 1; if(size <= s) memcpy(_##n, n, size); else { memcpy(_##n, n, size); printf("WARNING: wb msg array property setter out of bounds");}}
208#define CONTROLLED_ARRAY_PROPERTY(type, name, size) ARRAY_PROPERTY_DECLARATION(type, name, size) ARRAY_PROPERTY_GETTER(type, name,size) CONTROLLED_ARRAY_PROPERTY_SETTER(type, name, size)
209
210#define CONTROL_BIT(name) DOXY_MASK(n) BIT_DECLARATION(name##_mask) BIT_GETTER(name##_mask) BIT_SETTER(name##_mask) //these should all be declared together to avoid excess padding
211
212#define VERSION_DECLARATION(n) private: int _##n##_version; static int* get_##n##_classVersion() { static int _##n##_classVersion = 0; return &_##n##_classVersion;}
213#define VERSION_PROPERTY_SETTER(t, n) public: DOXY_S(n) void set_##n (const t &n ) { _##n = n; (*get_##n##_classVersion())+=1; _##n##_version = *get_##n##_classVersion();} private: void reset_##n##_version() {(*get_##n##_classVersion())=0;}
214#define VERSION_PROPERTY_GETTER(t, n) public: DOXY_G(n) t &n () {(*get_##n##_classVersion()) = _##n##_version; return _##n ; }
215#define VERSION_PROPERTY_CHECK(n) public: DOXY_VERSION_NEW(n) bool new_##n () {if(_##n##_version > (*get_##n##_classVersion())) return true; return false;}
216#define VERSION_PROPERTY_INITIALIZATION(n) _##n##_version = 0;
217#define VERSIONED_PROPERTY(type, name) PROPERTY_DECLARATION(type,name) VERSION_DECLARATION(name) VERSION_PROPERTY_SETTER(type,name) VERSION_PROPERTY_GETTER(type,name) VERSION_PROPERTY_CHECK(name)
218
219#define SET_CONTROL_VAL(o,a,v) do { (o).set_##a(v); (o)set_##a##_mask(true); } while (0)
220
221#define CHECK_ENUM(s,p,v) if (strstr((s), #v)) set_##p(v)
222#define STREAM_ENUM_CASE(s,v) case v: s << #v; break
223
224
225#define DEG_OVER_RAD (180.0f / static_cast<float>(M_PI))
226#define RAD_OVER_DEG (static_cast<float>(M_PI) / 180.0f)
227#define DEG_OVER_RAD_10 (DEG_OVER_RAD * 10.0f)
228#define RAD_OVER_DEG_10 (RAD_OVER_DEG / 10.0f)
229
230#define RAD2DEG(x) ((x) * DEG_OVER_RAD)
231#define DEG2RAD(x) ((x) * RAD_OVER_DEG)
232
233
234
235extern "C"
236{
237#else // !__cplusplus
238#include <string.h>
239#include <stdbool.h>
240#include <stddef.h>
241
242#ifndef M_PI
243 #define M_PI 3.14159265358979323846
244#endif
245
246#define DEG_OVER_RAD (180.0f / (float)M_PI)
247#define RAD_OVER_DEG ((float)M_PI / 180.0f)
248
249#define RAD2DEG(x) ((x) * DEG_OVER_RAD)
250#define DEG2RAD(x) ((x) * RAD_OVER_DEG)
251
252
253#define NULLPTR NULL
254
255#define PROPERTY_DECLARATION(t, n) t n;
256#define PROPERTY_GETTER(t, n)
257#define PROPERTY_SETTER(t, n)
258#define PROPERTY(type, name) PROPERTY_DECLARATION(type,name)
259#define CONTROLLED_PROPERTY(type, name) PROPERTY_DECLARATION(type, name)
260
261#define ARRAY_PROPERTY_DECLARATION(t, n, s) t n[s];
262#define ARRAY_PROPERTY_GETTER(t, n)
263#define ARRAY_PROPERTY_SETTER(t, n)
264#define ARRAY_PROPERTY(type, name, size) ARRAY_PROPERTY_DECLARATION(type,name,size)
265#define CONTROLLED_ARRAY_PROPERTY(type, name, size) ARRAY_PROPERTY_DECLARATION(type, name, size)
266
267#define STRING_PROPERTY_DECLARATION(n, s) char n[s];
268#define STRING_PROPERTY_GETTER(n, s)
269#define STRING_PROPERTY_SETTER(n, s)
270#define STRING_PROPERTY(name, size) STRING_PROPERTY_DECLARATION(name, size)
271
272#define VERSION_DECLARATION(n) int n##_version = 0; static int* get_##n##_classVersion() { static int n##_classVersion = 0; return &##n##_classVersion;}
273#define VERSION_PROPERTY_SETTER(t, n)
274#define VERSION_PROPERTY_GETTER(t, n)
275#define VERSION_PROPERTY_CHECK(n)
276#define VERSIONED_PROPERTY(type, name) PROPERTY_DECLARATION(type,name) VERSION_DECLARATION(name) VERSION_PROPERTY_SETTER(type,name) VERSION_PROPERTY_GETTER(type,name) VERSION_PROPERTY_CHECK(name)
277#define VERSION_PROPERTY_INITIALIZATION(n) n##_version = 0;
278
279#define BIT_DECLARATION(n) unsigned n : 1;
280#define BIT_GETTER(n)
281#define BIT_SETTER(n)
282#define BIT_PROPERTY(name) BIT_DECLARATION(name) BIT_GETTER(name) BIT_SETTER(name)
283#define CONTROL_BIT(name) BIT_DECLARATION(name##_mask)
284
285#endif // __cplusplus
286
287#include <sys/param.h>
288
289#if defined(BSD) && (BSD >= 199506)
290#define gu_strlcpy(d,s,n) strlcpy((d),(s),(n))
291#define gu_strlcat(d,s,n) strlcat((d),(s),(n))
292#else
293size_t gu_strlcpy(char *dst, const char *src, size_t size);
294size_t gu_strlcat(char *dst, const char *src, size_t size);
295#endif
296
301bool file_exists(const char *fileName);
302
307char *new_string_from_file(const char *fileName);
308
313int int_from_file(const char *fileName);
314
319double double_from_file(const char *fileName);
320
325char *gu_strdup(const char *str);
326
332char *concatenate_path(const char *head, const char *tail);
333
337long long get_utime(void);
338
342void protected_usleep(long long us);
343
349char *gu_strtrim(const char *s);
350
357int mipal_err_file(const char *filename);
358
363void mipal_warn(const char *fmt, ...);
364
368const char *gu_getprogname(void);
369
373int getplayernumber(void);
374
375#ifdef __cplusplus
376} // extern "C"
377
382std::string string_from_file(const char *fileName);
383
384
390std::string string_by_concatenating_path_components(const std::string &head, const std::string &tail);
391
397std::string &gu_trim(std::string &s);
398
403std::string gu_ltos(long val);
404
409std::string gu_ultos(unsigned long val);
410
415std::string gu_dtos(double val);
416
423std::vector<std::string> components_of_string_separated(const std::string &str, char sep = '\n', bool trim = false);
424
425
430std::map<std::string, std::string> read_configuration(const std::string &filename);
431
432
438int inc(void *num);
439
445int dec(void *num);
446
447#endif // __cplusplus
448
449#pragma clang diagnostic pop
450
451#endif // GU_UTIL_H_
452
std::string gu_ltos(long val)
Convert a long value to a string.
Definition: gu_util.cpp:321
char * gu_strdup(const char *str)
Duplicate a string.
Definition: gu_util.cpp:111
std::string gu_ultos(unsigned long val)
Convert an unsigned long value to a string.
Definition: gu_util.cpp:330
std::string string_by_concatenating_path_components(const std::string &head, const std::string &tail)
Concatenate two path components into a new string.
Definition: gu_util.cpp:280
long long get_utime(void)
Get the current time of day in microseconds.
Definition: gu_util.cpp:130
int int_from_file(const char *fileName)
Load the contents of a given file into an int.
Definition: gu_util.cpp:84
double double_from_file(const char *fileName)
Load the contents of a given file into a double.
Definition: gu_util.cpp:98
std::string & gu_trim(std::string &s)
Trim whitespace characters from both ends of a string.
Definition: gu_util.cpp:305
int dec(void *num)
Decrement a number Used to decrement a enum value without warnings.
Definition: gu_util.cpp:395
std::string string_from_file(const char *fileName)
Load the contents of a given file into a string.
Definition: gu_util.cpp:267
char * new_string_from_file(const char *fileName)
Load the contents of a given file into a string.
Definition: gu_util.cpp:60
std::map< std::string, std::string > read_configuration(const std::string &filename)
Read simple key = value pairs into a c++ map.
Definition: gu_util.cpp:363
size_t gu_strlcat(char *dst, const char *src, size_t size)
Definition: gu_util.cpp:252
char * gu_strtrim(const char *s)
Return a trimmed copy of the current string by removing all leading and trailing whitespace character...
Definition: gu_util.cpp:150
char * concatenate_path(const char *head, const char *tail)
Create a new string by concatenating two path components.
Definition: gu_util.cpp:125
bool file_exists(const char *fileName)
Return whether a file exists.
Definition: gu_util.cpp:54
std::vector< std::string > components_of_string_separated(const std::string &str, char sep='\n', bool trim=false)
Split a string based on the token.
Definition: gu_util.cpp:348
int mipal_err_file(const char *filename)
Set the log file for the current module.
Definition: gu_util.cpp:158
const char * gu_getprogname(void)
get the basename of the current program
Definition: gu_util.cpp:210
int getplayernumber(void)
get player number
Definition: gu_util.cpp:228
std::string gu_dtos(double val)
Convert a double value to a string.
Definition: gu_util.cpp:339
void protected_usleep(long long us)
Protected usleep() – guaranteed to sleep for the given time! param us number of microseconds to sleep...
Definition: gu_util.cpp:140
void mipal_warn(const char *fmt,...)
Print a warning to the file mipal_err_file() has been set for (or stderr if none has been set)
Definition: gu_util.cpp:187
size_t gu_strlcpy(char *dst, const char *src, size_t size)
Definition: gu_util.cpp:245
int inc(void *num)
Increment a number Used to increment an enum value without warnings.
Definition: gu_util.cpp:389