Debian Codegraph @

src/Base/ExceptionFactory.h

raw
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2
3 /***************************************************************************
4 * Copyright (c) 2017 Abdullah Tahiri <[email protected]> *
5 * *
6 * This file is part of the FreeCAD CAx development system. *
7 * *
8 * This library is free software; you can redistribute it and/or *
9 * modify it under the terms of the GNU Library General Public *
10 * License as published by the Free Software Foundation; either *
11 * version 2 of the License, or (at your option) any later version. *
12 * *
13 * This library is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU Library General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU Library General Public *
19 * License along with this library; see the file COPYING.LIB. If not, *
20 * write to the Free Software Foundation, Inc., 59 Temple Place, *
21 * Suite 330, Boston, MA 02111-1307, USA *
22 * *
23 ***************************************************************************/
24
25
26 #ifndef BASE_EXCEPTIONFACTORY_H
27 #define BASE_EXCEPTIONFACTORY_H
28
29
30 #include "Factory.h"
31 #include <FCGlobal.h>
32 #include <typeinfo>
33
34 // Python stuff
35 using PyObject = struct _object;
36
37 namespace Base
38 {
39
40 struct PyExceptionData
41 {
42 PyObject* pyexc {};
43 std::string message;
44 bool reported {};
45 };
46
47 /// Abstract base class of all exception producers
48 class BaseExport AbstractExceptionProducer: public AbstractProducer
49 {
50 public:
51 AbstractExceptionProducer() = default;
52 // just implement it
53 void* Produce() const override
54 {
55 return nullptr;
56 }
57 virtual void raiseException(PyObject* pydict) const = 0;
58 virtual void raiseExceptionByType(const PyExceptionData&) const = 0;
59 };
60
61 // --------------------------------------------------------------------
62
63 /** The ExceptionFactory */
64 class BaseExport ExceptionFactory: public Factory
65 {
66 public:
67 static ExceptionFactory& Instance();
68 static void Destruct();
69
70 void raiseException(PyObject* pydict) const;
71 void raiseExceptionByType(const PyExceptionData& data) const;
72
73 private:
74 static ExceptionFactory* _pcSingleton; // NOLINT
75
76 ExceptionFactory() = default;
77 };
78
79 /* Producers */
80
81 template<class CLASS>
82 class ExceptionProducer: public AbstractExceptionProducer
83 {
84 public:
85 ExceptionProducer()
86 {
87 CLASS cls;
88 pyExcType = cls.getPyExceptionType();
89 ExceptionFactory::Instance().AddProducer(typeid(CLASS).name(), this);
90 }
91
92 void raiseException(PyObject* pydict) const override
93 {
94 CLASS cls;
95 cls.setPyObject(pydict);
96
97 throw cls;
98 }
99
100 void raiseExceptionByType(const PyExceptionData& data) const override
101 {
102 if (pyExcType == data.pyexc) {
103 CLASS cls;
104 cls.setMessage(data.message);
105 cls.setReported(data.reported);
106
107 throw cls;
108 }
109 }
110
111 private:
112 PyObject* pyExcType {};
113 };
114
115 } // namespace Base
116
117
118 #endif