remage
Simulation framework for HPGe-based experiments
Toggle main menu visibility
Loading...
Searching...
No Matches
RMGTools.hh
1
// Copyright (C) 2022 Luigi Pertoldi <https://orcid.org/0000-0002-0467-2571>
2
//
3
// This program is free software: you can redistribute it and/or modify it under
4
// the terms of the GNU Lesser General Public License as published by the Free
5
// Software Foundation, either version 3 of the License, or (at your option) any
6
// later version.
7
//
8
// This program is distributed in the hope that it will be useful, but WITHOUT
9
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
10
// FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
11
// details.
12
//
13
// You should have received a copy of the GNU Lesser General Public License
14
// along with this program. If not, see <https://www.gnu.org/licenses/>.
15
16
#ifndef _RMG_TOOLS_HH_
17
#define _RMG_TOOLS_HH_
18
19
#include <stdexcept>
20
21
#include "globals.hh"
22
23
#include "RMGLog.hh"
24
25
#include "magic_enum/magic_enum.hpp"
26
27
namespace
RMGTools {
28
42
template
<
typename
T> T ToEnum(
const
std::string name, std::string prop_name =
"property"
) {
43
auto
result = magic_enum::enum_cast<T>(name);
44
if
(!result.has_value()) result = magic_enum::enum_cast<T>(
"k"
+ name);
45
if
(!result.has_value()) {
46
RMGLog::OutFormat(
RMGLog::error
,
"Illegal '{}' {} specified"
, name, prop_name);
47
throw
std::bad_cast();
48
}
else
return
result.value();
49
}
50
61
template
<
typename
T> std::string GetCandidates(
const
char
delim =
' '
) {
62
auto
v = magic_enum::enum_names<T>();
63
std::string cand;
64
for
(
const
auto
& s : v) {
65
auto
name = s[0] ==
'k'
? s.substr(1, std::string::npos) : s;
66
cand += std::string(name) + delim;
67
}
68
return
cand.substr(0, cand.size() - 1);
69
}
70
81
template
<
typename
T> std::string GetCandidate(T t) {
82
auto
s = magic_enum::enum_name<T>(t);
83
auto
name = s[0] ==
'k'
? s.substr(1, std::string::npos) : s;
84
return
std::string(name);
85
}
86
88
struct
ElapsedTime
{
89
long
days;
90
long
hours;
91
long
minutes;
92
long
seconds;
93
};
94
101
inline
ElapsedTime
BreakDownElapsedTime(
long
tot_seconds) {
102
ElapsedTime
t{};
103
t.days = tot_seconds / 86400;
104
tot_seconds -= t.days * 86400;
105
t.hours = tot_seconds / 3600;
106
tot_seconds -= t.hours * 3600;
107
t.minutes = tot_seconds / 60;
108
t.seconds = tot_seconds - t.minutes * 60;
109
return
t;
110
}
111
}
// namespace RMGTools
112
113
#endif
114
115
// vim: shiftwidth=2 tabstop=2 expandtab
RMGLog::error
@ error
Print only errors.
Definition
RMGLog.hh:72
RMGTools::ElapsedTime
A duration broken down into days, hours, minutes and seconds.
Definition
RMGTools.hh:88
include
RMGTools.hh
Generated by
1.17.0