PLaSK library
Loading...
Searching...
No Matches
exceptions.hpp
Go to the documentation of this file.
1/*
2 * This file is part of PLaSK (https://plask.app) by Photonics Group at TUL
3 * Copyright (c) 2022 Lodz University of Technology
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, version 3.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 */
14#ifndef PLASK__EXCEPTIONS_H
15#define PLASK__EXCEPTIONS_H
16
21#include <stdexcept>
22#include "utils/format.hpp"
23#include "utils/string.hpp"
24
25#include <plask/config.hpp>
26
27namespace plask {
28
29#ifdef _MSC_VER
30#pragma warning(push)
31#pragma warning( disable : 4275 ) // Disable MSVC warnings "non - DLL-interface classkey 'identifier' used as base for DLL-interface classkey 'identifier'"
32#endif
33
37struct PLASK_API Exception: public std::runtime_error {
38
40 Exception(const std::string& msg);
41
45 template <typename... T>
46 Exception(const std::string& msg, const T&... args): Exception(format(msg, args...)) {}
47};
48
49#ifdef _MSC_VER
50#pragma warning(pop)
51#endif
52
57
59 CriticalException(const std::string& msg): Exception("Critical exception: " + msg) {}
60
61 template <typename... T>
62 CriticalException(const std::string& msg, const T&... args): Exception("Critical exception: " + msg, args...) {}
63};
64
69 //std::string methodName;
70
72 NotImplemented(const std::string& method_name)
73 : Exception("Method not implemented: " + method_name)/*, methodName(method_name)*/ {}
74
79 NotImplemented(const std::string& where, const std::string& method_name)
80 : Exception(where + ": Method not implemented: " + method_name)/*, methodName(method_name)*/ {}
81};
82
87
88 OutOfBoundsException(const std::string& where, const std::string& argname)
89 : Exception("{0}: argument {1} out of bounds", where, argname) {}
90
91 template <typename BoundTypeWas, typename BoundTypeLo, typename BoundTypeHi>
92 OutOfBoundsException(const std::string& where, const std::string& argname, const BoundTypeWas& was, const BoundTypeLo& lo, const BoundTypeHi& hi)
93 : Exception("{0}: argument {1} out of bounds, should be between {2} and {3}, but was {4}", where, argname, lo, hi, was) {}
94};
95
100 template <typename... T>
102};
103
108
114 template <typename... Params>
115 BadInput(const std::string& where, const std::string& msg, Params... params)
116 : Exception("{0}: {1}", where, format(msg, params...)) {};
117};
118
123
128 template <typename... Params>
129 DataError(const std::string& msg, Params... params)
130 : Exception("{0}", format(msg, params...)) {};
131};
132
133
138
144 template <typename... Params>
145 ComputationError(const std::string& where, const std::string& msg, Params... params)
146 : Exception("{0}: {1}", where, format(msg, params...)) {};
147};
148
152struct PLASK_API BadId: public Exception {
153
154 BadId(const std::string& where, const char* str_to_check)
155 : Exception("\"{0}\" is a bad name for a {1} (must be letters, digits, or '_' and cannot start with a digit) ", str_to_check, where) {};
156
157 static void throwIfBad(const std::string& where, const char* str_to_check) {
158 if (!isCid(str_to_check))
159 throw BadId(where, str_to_check);
160 }
161
162 static void throwIfBad(const std::string& where, const std::string& str_to_check) {
163 throwIfBad(where, str_to_check.c_str());
164 }
165
166};
167
168//-------------- Connected with providers/receivers: -----------------------
169
176 NoProvider(const char* provider_name): Exception("No provider nor value for {0}", provider_name) {}
177};
178
180 NoValue(const char* provider_name): Exception("{0} cannot be provided now", [](std::string s)->std::string{s[0]=char(std::toupper(s[0]));return s;}(provider_name) ) {}
181};
182
183
184/*
185 * Exceptions of this class are thrownwhen some string parser find errors.
186 */
187/*struct ParseException: public Exception {
188 ParseException(): Exception("Parse error") {}
189 ParseException(std::string& msg): Exception("Parse error: " + msg) {}
190};*/
191
192//-------------- Connected with materials: -----------------------
193
197class PLASK_API NoSuchMaterial: public Exception {
198
199 template <typename ComponentMap>
200 std::string constructMsg(const ComponentMap& comp, const std::string dopant_name) {
201 std::string result = "No material with composition consisting of:";
202 for (auto c: comp) (result += ' ') += c.first;
203 if (!dopant_name.empty()) (result += ", doped with: ") += dopant_name;
204 return result;
205 }
206
207public:
209 NoSuchMaterial(const std::string& material_name)
210 : Exception("No such material: {0}", material_name)/*, materialName(material_name)*/ {}
211
212 NoSuchMaterial(const std::string& material_name, const std::string& dopant_name)
213 : Exception("No such material: {0}{1}{2}", material_name, dopant_name.empty() ? "" : ":", dopant_name)/*, materialName(material_name)*/ {}
214
215 template <typename ComponentMap>
216 NoSuchMaterial(const ComponentMap& comp, const std::string dopant_name)
217 : Exception(constructMsg(comp, dopant_name)) {}
218
219};
220
225
230 MaterialMethodNotImplemented(const std::string& material_name, const std::string& method_name)
231 : NotImplemented("Material " + material_name, method_name) {
232 }
233
234};
235
239struct PLASK_API MaterialParseException: public Exception {
240 MaterialParseException(): Exception("Material parse error") {}
242 MaterialParseException(const std::string& msg): Exception(msg) {}
243
244 template <typename... T>
245 MaterialParseException(const std::string& msg, const T&... args): Exception(msg, args...) {
246 }
247};
248
250
251 /*template <typename ComponentMap>
252 std::string constructMsg(const ComponentMap& comp, const std::string dopant_name) {
253 std::string result = "material with composition consisting of:";
254 for (auto c: comp) (result += ' ') += c.first;
255 if (!dopant_name.empty()) (result += ", doped with: ") += dopant_name;
256 return result;
257 }*/
258
259public:
260 MaterialCantBeMixedException(const std::string& material1name_with_components, const std::string& material2name_with_components, const std::string& common_dopant = "")
261 : Exception("Material \"{0}{2}\" can not be mixed with material \"{1}{2}\"",
262 material1name_with_components, material2name_with_components, common_dopant.empty() ? "" : ':' + common_dopant)
263 {}
264
265};
266
267
268//-------------- Connected with geometry: -----------------------
269
273struct PLASK_API GeometryException: public Exception {
274 template <typename... T>
275 GeometryException(const T&... args): Exception(args...) {}
276};
277
281struct PLASK_API NoGeometryException: public GeometryException {
282 NoGeometryException(const std::string& where): GeometryException("{0}: No geometry specified", where) {}
283};
284
288struct PLASK_API NoChildException: public GeometryException {
289 NoChildException(): GeometryException("Incomplete geometry tree") {}
290};
291
296 NotUniqueObjectException(): GeometryException("Unique object instance required") {}
297 NotUniqueObjectException(const std::string& msg): GeometryException(msg) {}
298
299 template <typename... T>
300 NotUniqueObjectException(const std::string& msg, const T&... args): GeometryException(msg, args...) {}
301};
302
307 CyclicReferenceException(): GeometryException("Detected cycle in geometry tree") {}
308};
309
314 //std::string materialName;
315
319 NoSuchGeometryObjectType(const std::string& object_type_name)
320 : GeometryException("No geometry object with given type name \"" + object_type_name + "\"")/*, materialName(material_name)*/ {}
321};
322
327
332 NamesConflictException(const std::string& what, const std::string& object_name):
333 GeometryException(what + " with name \"" + object_name + "\" already exists") {}
334};
335
339struct PLASK_API NoSuchGeometryObject: public GeometryException {
340 //std::string materialName;
341
345 NoSuchGeometryObject(const std::string& object_name)
346 : GeometryException("No geometry object with name \"" + object_name + "\"") {}
347
349 : GeometryException("No geometry object found") {}
350};
351
355struct PLASK_API NoSuchGeometry: public GeometryException {
359 NoSuchGeometry(const std::string& object_name)
360 : GeometryException("No geometry of required type with name \"" + object_name + "\"") {}
361};
362
366struct PLASK_API NoSuchPath: public GeometryException {
370 NoSuchPath(const std::string& object_name)
371 : GeometryException("No path with name \"" + object_name + "\"") {}
372};
373
378 UnexpectedGeometryObjectTypeException(): GeometryException("Geometry object has unexpected type") {}
379};
380
384struct PLASK_API NoSuchAxisNames: public GeometryException {
385 //std::string materialName;
386
388 NoSuchAxisNames(const std::string& axis_names)
389 : GeometryException("No such axis names \"{0}\"", axis_names) {}
390};
391
392
393//-------------- Connected with meshes: -----------------------
394
398struct PLASK_API NoMeshException: public Exception {
399 NoMeshException(const std::string& where): Exception("{0}: No mesh specified", where) {}
400};
401
402
406struct PLASK_API BadMesh: public Exception {
407
413 template <typename... Params>
414 BadMesh(const std::string& where, const std::string& msg, Params... params)
415 : Exception("{0}: Bad mesh: {1}", where, format(msg, params...)) {};
416};
417
418
419
420
421} // namespace plask
422
423#endif //PLASK__EXCEPTIONS_H