OMSimulatorLib
The OMSimulator project is a FMI-based co-simulation environment.
Loading...
Searching...
No Matches
Util.h
Go to the documentation of this file.
1/*
2 * This file is part of OpenModelica.
3 *
4 * Copyright (c) 1998-2026, Open Source Modelica Consortium (OSMC),
5 * c/o Linköpings universitet, Department of Computer and Information Science,
6 * SE-58183 Linköping, Sweden.
7 *
8 * All rights reserved.
9 *
10 * THIS PROGRAM IS PROVIDED UNDER THE TERMS OF AGPL VERSION 3 LICENSE OR
11 * THIS OSMC PUBLIC LICENSE (OSMC-PL) VERSION 1.8.
12 * ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES
13 * RECIPIENT'S ACCEPTANCE OF THE OSMC PUBLIC LICENSE OR THE GNU AGPL
14 * VERSION 3, ACCORDING TO RECIPIENTS CHOICE.
15 *
16 * The OpenModelica software and the OSMC (Open Source Modelica Consortium)
17 * Public License (OSMC-PL) are obtained from OSMC, either from the above
18 * address, from the URLs:
19 * http://www.openmodelica.org or
20 * https://github.com/OpenModelica/ or
21 * http://www.ida.liu.se/projects/OpenModelica,
22 * and in the OpenModelica distribution.
23 *
24 * GNU AGPL version 3 is obtained from:
25 * https://www.gnu.org/licenses/licenses.html#GPL
26 *
27 * This program is distributed WITHOUT ANY WARRANTY; without
28 * even the implied warranty of MERCHANTABILITY or FITNESS
29 * FOR A PARTICULAR PURPOSE, EXCEPT AS EXPRESSLY SET FORTH
30 * IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE CONDITIONS OF OSMC-PL.
31 *
32 * See the full OSMC Public License conditions for more details.
33 *
34 */
35
36#ifndef _OMS_UTIL_H_
37#define _OMS_UTIL_H_
38
39#include <algorithm>
40#include <cctype>
41#include <locale>
42#include <math.h>
43#include <sstream>
44#include <string>
45#include <random>
46
47// trim from start (in place)
48// https://stackoverflow.com/a/217605/7534030
49static inline void ltrim(std::string &s)
50{
51 s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](int ch) { return !std::isspace(ch); }));
52}
53
54// trim from end (in place)
55// https://stackoverflow.com/a/217605/7534030
56static inline void rtrim(std::string &s)
57{
58 s.erase(std::find_if(s.rbegin(), s.rend(), [](int ch) { return !std::isspace(ch); }).base(), s.end());
59}
60
61// trim from both ends (in place)
62// https://stackoverflow.com/a/217605/7534030
63static inline void trim(std::string &s)
64{
65 ltrim(s);
66 rtrim(s);
67}
68
69const double DOUBLEEQUAL_ABSTOL = 1e-10;
70const double DOUBLEEQUAL_RELTOL = 1e-5;
71
72// http://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/
73static inline bool almostEqualRelativeAndAbs(double a, double b, double reltol=DOUBLEEQUAL_RELTOL, double abstol=DOUBLEEQUAL_ABSTOL)
74{
75 double diff = fabs(a - b);
76 if (diff <= abstol)
77 return true;
78
79 if (diff <= fmax(fabs(a), fabs(b)) * reltol)
80 return true;
81
82 return false;
83}
84
85template <class T>
86void reverseArray(T* array, unsigned int length)
87{
88 T tmp;
89 for (unsigned int start = 0, end = length-1; start < end; start++, end--)
90 {
91 tmp = array[start];
92 array[start] = array[end];
93 array[end] = tmp;
94 }
95}
96
97static inline std::string generateUuid()
98{
99 std::random_device rd;
100 std::mt19937_64 gen(rd());
101 std::uniform_int_distribution<> dis(0, 15);
102 std::uniform_int_distribution<> dis2(8, 11);
103 std::stringstream ss;
104 int i;
105 ss << std::hex;
106 for (i = 0; i < 8; i++) {
107 ss << dis(gen);
108 }
109 ss << "-";
110 for (i = 0; i < 4; i++) {
111 ss << dis(gen);
112 }
113 ss << "-4";
114 for (i = 0; i < 3; i++) {
115 ss << dis(gen);
116 }
117 ss << "-";
118 ss << dis2(gen);
119 for (i = 0; i < 3; i++) {
120 ss << dis(gen);
121 }
122 ss << "-";
123 for (i = 0; i < 12; i++) {
124 ss << dis(gen);
125 }
126 return ss.str().c_str();
127}
128
129#endif
const double DOUBLEEQUAL_RELTOL
Definition Util.h:70
static void ltrim(std::string &s)
Definition Util.h:49
void reverseArray(T *array, unsigned int length)
Definition Util.h:86
static void trim(std::string &s)
Definition Util.h:63
const double DOUBLEEQUAL_ABSTOL
Definition Util.h:69
static std::string generateUuid()
Definition Util.h:97
static bool almostEqualRelativeAndAbs(double a, double b, double reltol=DOUBLEEQUAL_RELTOL, double abstol=DOUBLEEQUAL_ABSTOL)
Definition Util.h:73
static void rtrim(std::string &s)
Definition Util.h:56