Debian Codegraph @

src/Base/ExceptionFactory.cpp

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 #include "ExceptionFactory.h"
27 #include <CXX/Objects.hxx>
28
29
30 using namespace Base;
31
32 ExceptionFactory* ExceptionFactory::_pcSingleton = nullptr; // NOLINT
33
34 ExceptionFactory& ExceptionFactory::Instance()
35 {
36 if (!_pcSingleton) {
37 _pcSingleton = new ExceptionFactory; // NOLINT
38 }
39 return *_pcSingleton;
40 }
41
42 void ExceptionFactory::Destruct()
43 {
44 delete _pcSingleton;
45 _pcSingleton = nullptr;
46 }
47
48 void ExceptionFactory::raiseException(PyObject* pydict) const
49 {
50 std::string classname;
51
52 Py::Dict edict(pydict);
53 if (edict.hasKey("sclassname")) {
54 classname = static_cast<std::string>(Py::String(edict.getItem("sclassname")));
55
56 auto pProd = _mpcProducers.find(classname);
57 if (pProd != _mpcProducers.end()) {
58 static_cast<AbstractExceptionProducer*>(pProd->second)->raiseException(pydict);
59 }
60 }
61 }
62
63 void ExceptionFactory::raiseExceptionByType(const PyExceptionData& data) const
64 {
65 for (const auto& it : _mpcProducers) {
66 static_cast<AbstractExceptionProducer*>(it.second)->raiseExceptionByType(data); // NOLINT
67 }
68 }