DPsim
Loading...
Searching...
No Matches
Utils.h
1/* Copyright 2017-2021 Institute for Automation of Complex Power Systems,
2 * EONERC, RWTH Aachen University
3 *
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at https://mozilla.org/MPL/2.0/.
7 *********************************************************************************/
8
9#pragma once
10
11#if defined(__GNUC__) && !defined(__clang__)
12#include <cxxabi.h>
13#endif
14
15#include <dpsim-models/Definitions.h>
16#include <dpsim-models/Logger.h>
17
18namespace CPS {
19namespace Utils {
20
21template <typename T> String className(T *ptr, const String &prefix = "CPS::") {
22 const char *mangled;
23 mangled = typeid(*ptr).name();
24#if !defined(__GNUC__) || defined(__clang__)
25 String ret(mangled);
26 return ret;
27#else
28 Int status = 1;
29 const char *unmangled;
30 unmangled = abi::__cxa_demangle(mangled, NULL, NULL, &status);
31
32 if (status) {
33 return mangled;
34 } else {
35 String ret(unmangled);
36 free((void *)unmangled);
37 if (ret.find(prefix) == 0)
38 return ret.substr(prefix.size());
39 else
40 return ret;
41 }
42#endif
43}
44
45struct Rgb {
46 double r, g, b;
47
48 static Rgb gradient(double x) {
49 if (x > 1.0)
50 x = 1.0;
51
52 Rgb c = {2.0 * x, 2.0 * (1 - x), 0.0};
53
54 if (c.r > 1.0)
55 c.r = 1.0;
56 if (c.g > 1.0)
57 c.g = 1.0;
58
59 return c;
60 }
61
62 String hex() {
63 return fmt::format("#{:02x}{:02x}{:02x}", (unsigned)(r * 255),
64 (unsigned)(g * 255), (unsigned)(b * 255));
65 }
66};
67} // namespace Utils
68} // namespace CPS