Skip to content

Commit df91dd6

Browse files
committed
Merge remote-tracking branch 'origin/feature/use-std-views' into feature/clang-tidy-19
Signed-off-by: Martijn Govers <Martijn.Govers@Alliander.com>
2 parents 5dc8be6 + b1378c6 commit df91dd6

File tree

1 file changed

+59
-52
lines changed
  • power_grid_model_c/power_grid_model/include/power_grid_model/common

1 file changed

+59
-52
lines changed

power_grid_model_c/power_grid_model/include/power_grid_model/common/exception.hpp

+59-52
Original file line numberDiff line numberDiff line change
@@ -40,74 +40,77 @@ class InvalidArguments : public PowerGridError {
4040

4141
template <std::same_as<TypeValuePair>... Options>
4242
InvalidArguments(std::string const& method, std::string const& arguments) {
43-
append_msg(method + " is not implemented for " + arguments + "!\n");
43+
append_msg(std::format("{} is not implemented for {}!\n", method, arguments));
4444
}
4545

4646
template <class... Options>
4747
requires(std::same_as<std::remove_cvref_t<Options>, TypeValuePair> && ...)
4848
InvalidArguments(std::string const& method, Options const&... options)
4949
: InvalidArguments{method, "the following combination of options"} {
50-
(append_msg(" " + options.name + ": " + options.value + "\n"), ...);
50+
(append_msg(std::format(" {}: {}\n", options.name, options.value)), ...);
5151
}
5252
};
5353

5454
class MissingCaseForEnumError : public InvalidArguments {
5555
public:
5656
template <typename T>
5757
MissingCaseForEnumError(std::string const& method, T const& value)
58-
: InvalidArguments{method, std::string{typeid(T).name()} + " #" + detail::to_string(static_cast<IntS>(value))} {
59-
}
58+
: InvalidArguments{method,
59+
std::format("{} #{}", typeid(T).name(), detail::to_string(static_cast<IntS>(value)))} {}
6060
};
6161

6262
class ConflictVoltage : public PowerGridError {
6363
public:
6464
ConflictVoltage(ID id, ID id1, ID id2, double u1, double u2) {
65-
append_msg("Conflicting voltage for line " + detail::to_string(id) + "\n voltage at from node " +
66-
detail::to_string(id1) + " is " + detail::to_string(u1) + "\n voltage at to node " +
67-
detail::to_string(id2) + " is " + detail::to_string(u2) + '\n');
65+
append_msg(std::format(
66+
"Conflicting voltage for line {}\n voltage at from node {} is {}\n voltage at to node {} is {}\n",
67+
detail::to_string(id), detail::to_string(id1), detail::to_string(u1), detail::to_string(id2),
68+
detail::to_string(u2)));
6869
}
6970
};
7071

7172
class InvalidBranch : public PowerGridError {
7273
public:
7374
InvalidBranch(ID branch_id, ID node_id) {
74-
append_msg("Branch " + detail::to_string(branch_id) + " has the same from- and to-node " +
75-
detail::to_string(node_id) + ",\n This is not allowed!\n");
75+
append_msg(std::format("Branch {} has the same from- and to-node {},\n This is not allowed!\n",
76+
detail::to_string(branch_id), detail::to_string(node_id)));
7677
}
7778
};
7879

7980
class InvalidBranch3 : public PowerGridError {
8081
public:
8182
InvalidBranch3(ID branch3_id, ID node_1_id, ID node_2_id, ID node_3_id) {
82-
append_msg("Branch3 " + detail::to_string(branch3_id) +
83-
" is connected to the same node at least twice. Node 1/2/3: " + detail::to_string(node_1_id) + "/" +
84-
detail::to_string(node_2_id) + "/" + detail::to_string(node_3_id) + ",\n This is not allowed!\n");
83+
append_msg(std::format(
84+
"Branch3 {} is connected to the same node at least twice. Node 1/2/3: {}/{}/{},\n This is not allowed!\n",
85+
detail::to_string(branch3_id), detail::to_string(node_1_id), detail::to_string(node_2_id),
86+
detail::to_string(node_3_id)));
8587
}
8688
};
8789

8890
class InvalidTransformerClock : public PowerGridError {
8991
public:
9092
InvalidTransformerClock(ID id, IntS clock) {
91-
append_msg("Invalid clock for transformer " + detail::to_string(id) + ", clock " + detail::to_string(clock) +
92-
'\n');
93+
append_msg(std::format("Invalid clock for transformer {}, clock {}\n", detail::to_string(id),
94+
detail::to_string(clock)));
9395
}
9496
};
9597

9698
class SparseMatrixError : public PowerGridError {
9799
public:
98100
SparseMatrixError(Idx err, std::string const& msg = "") {
99-
append_msg("Sparse matrix error with error code #" + detail::to_string(err) + " (possibly singular)\n");
101+
append_msg(
102+
std::format("Sparse matrix error with error code #{} (possibly singular)\n", detail::to_string(err)));
100103
if (!msg.empty()) {
101-
append_msg(msg + "\n");
104+
append_msg(std::format("{}\n", msg));
102105
}
103106
append_msg("If you get this error from state estimation, ");
104107
append_msg("it usually means the system is not fully observable, i.e. not enough measurements.");
105108
}
106109
SparseMatrixError() {
107-
append_msg("Sparse matrix error, possibly singular matrix!\n" +
108-
std::string("If you get this error from state estimation, ") +
109-
"it might mean the system is not fully observable, i.e. not enough measurements.\n" +
110-
"It might also mean that you are running into a corner case where PGM cannot resolve yet." +
110+
append_msg("Sparse matrix error, possibly singular matrix!\n"
111+
"If you get this error from state estimation, "
112+
"it might mean the system is not fully observable, i.e. not enough measurements.\n"
113+
"It might also mean that you are running into a corner case where PGM cannot resolve yet."
111114
"See https://github.com/PowerGridModel/power-grid-model/issues/864.");
112115
}
113116
};
@@ -117,7 +120,7 @@ class NotObservableError : public PowerGridError {
117120
NotObservableError(std::string const& msg = "") {
118121
append_msg("Not enough measurements available for state estimation.\n");
119122
if (!msg.empty()) {
120-
append_msg(msg + "\n");
123+
append_msg(std::format("{}\n", msg));
121124
}
122125
}
123126
};
@@ -126,58 +129,59 @@ class IterationDiverge : public PowerGridError {
126129
public:
127130
IterationDiverge() = default;
128131
IterationDiverge(Idx num_iter, double max_dev, double err_tol) {
129-
append_msg("Iteration failed to converge after " + detail::to_string(num_iter) +
130-
" iterations! Max deviation: " + detail::to_string(max_dev) +
131-
", error tolerance: " + detail::to_string(err_tol) + ".\n");
132+
append_msg(
133+
std::format("Iteration failed to converge after {} iterations! Max deviation: {}, error tolerance: {}.\n",
134+
detail::to_string(num_iter), detail::to_string(max_dev), detail::to_string(err_tol)));
132135
}
133136
};
134137

135138
class MaxIterationReached : public IterationDiverge {
136139
public:
137140
MaxIterationReached(std::string const& msg = "") {
138-
append_msg("Maximum number of iterations reached" + msg + "\n");
141+
append_msg(std::format("Maximum number of iterations reached{}\n", msg));
139142
}
140143
};
141144

142145
class ConflictID : public PowerGridError {
143146
public:
144-
explicit ConflictID(ID id) { append_msg("Conflicting id detected: " + detail::to_string(id) + '\n'); }
147+
explicit ConflictID(ID id) { append_msg(std::format("Conflicting id detected: {}\n", detail::to_string(id))); }
145148
};
146149

147150
class IDNotFound : public PowerGridError {
148151
public:
149-
explicit IDNotFound(ID id) { append_msg("The id cannot be found: " + detail::to_string(id) + '\n'); }
152+
explicit IDNotFound(ID id) { append_msg(std::format("The id cannot be found: {}\n", detail::to_string(id))); }
150153
};
151154
class Idx2DNotFound : public PowerGridError {
152155
public:
153156
explicit Idx2DNotFound(Idx2D id) {
154-
append_msg("The idx 2d cannot be found: {" + detail::to_string(id.group) + ", " + detail::to_string(id.pos) +
155-
"}.\n");
157+
append_msg(std::format("The idx 2d cannot be found: {{{}, {}}}.\n", detail::to_string(id.group),
158+
detail::to_string(id.pos)));
156159
}
157160
};
158161

159162
class InvalidMeasuredObject : public PowerGridError {
160163
public:
161164
InvalidMeasuredObject(std::string const& object, std::string const& sensor) {
162-
append_msg(sensor + " measurement is not supported for object of type " + object);
165+
append_msg(std::format("{} measurement is not supported for object of type {}", sensor, object));
163166
}
164167
};
165168

166169
class InvalidMeasuredTerminalType : public PowerGridError {
167170
public:
168171
InvalidMeasuredTerminalType(MeasuredTerminalType const terminal_type, std::string const& sensor) {
169-
append_msg(sensor + " measurement is not supported for object of type " +
170-
detail::to_string(static_cast<IntS>(terminal_type)));
172+
append_msg(std::format("{} measurement is not supported for object of type {}", sensor,
173+
detail::to_string(static_cast<IntS>(terminal_type))));
171174
}
172175
};
173176

174177
class InvalidRegulatedObject : public PowerGridError {
175178
public:
176179
InvalidRegulatedObject(std::string const& object, std::string const& regulator) {
177-
append_msg(regulator + " regulator is not supported for object of type " + object);
180+
append_msg(std::format("{} regulator is not supported for object of type {}", regulator, object));
178181
}
179182
InvalidRegulatedObject(ID id, std::string const& regulator) {
180-
append_msg(regulator + " regulator is not supported for object with ID " + detail::to_string(id));
183+
append_msg(
184+
std::format("{} regulator is not supported for object with ID {}", regulator, detail::to_string(id)));
181185
}
182186
};
183187

@@ -191,8 +195,9 @@ class DuplicativelyRegulatedObject : public PowerGridError {
191195
class AutomaticTapCalculationError : public PowerGridError {
192196
public:
193197
AutomaticTapCalculationError(ID id) {
194-
append_msg("Automatic tap changing regulator with tap_side at LV side is not supported. Found at id " +
195-
detail::to_string(id)); // NOSONAR
198+
append_msg(
199+
std::format("Automatic tap changing regulator with tap_side at LV side is not supported. Found at id {}",
200+
detail::to_string(id)));
196201
}
197202
};
198203

@@ -205,7 +210,9 @@ class AutomaticTapInputError : public PowerGridError {
205210

206211
class IDWrongType : public PowerGridError {
207212
public:
208-
explicit IDWrongType(ID id) { append_msg("Wrong type for object with id " + detail::to_string(id) + '\n'); }
213+
explicit IDWrongType(ID id) {
214+
append_msg(std::format("Wrong type for object with id {}\n", detail::to_string(id)));
215+
}
209216
};
210217

211218
class CalculationError : public PowerGridError {
@@ -235,22 +242,22 @@ class InvalidCalculationMethod : public CalculationError {
235242
class InvalidShortCircuitType : public PowerGridError {
236243
public:
237244
explicit InvalidShortCircuitType(FaultType short_circuit_type) {
238-
append_msg("The short circuit type (" + detail::to_string(static_cast<IntS>(short_circuit_type)) +
239-
") is invalid!\n");
245+
append_msg(std::format("The short circuit type ({}) is invalid!\n",
246+
detail::to_string(static_cast<IntS>(short_circuit_type))));
240247
}
241248
InvalidShortCircuitType(bool sym, FaultType short_circuit_type) {
242-
append_msg("The short circuit type (" + detail::to_string(static_cast<IntS>(short_circuit_type)) +
243-
") does not match the calculation type (symmetric=" + detail::to_string(static_cast<int>(sym)) +
244-
")\n");
249+
append_msg(std::format("The short circuit type ({}) does not match the calculation type (symmetric={})\n",
250+
detail::to_string(static_cast<IntS>(short_circuit_type)),
251+
detail::to_string(static_cast<int>(sym))));
245252
}
246253
};
247254

248255
class InvalidShortCircuitPhases : public PowerGridError {
249256
public:
250257
InvalidShortCircuitPhases(FaultType short_circuit_type, FaultPhase short_circuit_phases) {
251-
append_msg("The short circuit phases (" + detail::to_string(static_cast<IntS>(short_circuit_phases)) +
252-
") do not match the short circuit type (" +
253-
detail::to_string(static_cast<IntS>(short_circuit_type)) + ")\n");
258+
append_msg(std::format("The short circuit phases ({}) do not match the short circuit type ({})\n",
259+
detail::to_string(static_cast<IntS>(short_circuit_phases)),
260+
detail::to_string(static_cast<IntS>(short_circuit_type))));
254261
}
255262
};
256263

@@ -268,7 +275,7 @@ class SerializationError : public PowerGridError {
268275

269276
class DatasetError : public PowerGridError {
270277
public:
271-
explicit DatasetError(std::string const& msg) { append_msg("Dataset error: " + msg); }
278+
explicit DatasetError(std::string const& msg) { append_msg(std::format("Dataset error: {}", msg)); }
272279
};
273280

274281
class ExperimentalFeature : public InvalidArguments {
@@ -283,19 +290,19 @@ class NotImplementedError : public PowerGridError {
283290
class UnreachableHit : public PowerGridError {
284291
public:
285292
UnreachableHit(std::string const& method, std::string const& reason_for_assumption) {
286-
append_msg("Unreachable code hit when executing " + method +
287-
".\n The following assumption for unreachability was not met: " + reason_for_assumption +
288-
".\n This may be a bug in the library\n");
293+
append_msg(std::format("Unreachable code hit when executing {}.\n The following assumption for unreachability "
294+
"was not met: {}.\n This may be a bug in the library\n",
295+
method, reason_for_assumption));
289296
}
290297
};
291298

292299
class TapSearchStrategyIncompatibleError : public InvalidArguments {
293300
public:
294301
template <typename T1, typename T2>
295302
TapSearchStrategyIncompatibleError(std::string const& method, T1 const& value1, T2 const& value2)
296-
: InvalidArguments{
297-
method, std::string{typeid(T1).name()} + " #" + detail::to_string(static_cast<IntS>(value1)) + " and " +
298-
std::string{typeid(T2).name()} + " #" + detail::to_string(static_cast<IntS>(value2))} {}
303+
: InvalidArguments{method, std::format("{} #{} and {} #{}", typeid(T1).name(),
304+
detail::to_string(static_cast<IntS>(value1)), typeid(T2).name(),
305+
detail::to_string(static_cast<IntS>(value2)))} {}
299306
};
300307

301308
} // namespace power_grid_model

0 commit comments

Comments
 (0)