Debian Codegraph @

src/Base/BindingManager.cpp

raw
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2
3 /***************************************************************************
4 * Copyright (c) 2021 Werner Mayer <wmayer[at]users.sourceforge.net> *
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 #include <unordered_map>
26
27 #include "BindingManager.h"
28
29 using namespace Base;
30
31 struct BindingManager::BindingManagerPrivate
32 {
33 std::unordered_map<const void*, PyObject*> wrapperMapper;
34
35 bool hasWrapper(const void* cptr)
36 {
37 auto it = wrapperMapper.find(cptr);
38 return it != wrapperMapper.end();
39 }
40
41 void registerWrapper(const void* cptr, PyObject* pyObj)
42 {
43 wrapperMapper[cptr] = pyObj;
44 }
45
46 void releaseWrapper(const void* cptr, PyObject* pyObj)
47 {
48 auto it = wrapperMapper.find(cptr);
49 if (it != wrapperMapper.end() && (!pyObj || it->second == pyObj)) {
50 wrapperMapper.erase(it);
51 }
52 }
53
54 PyObject* retrieveWrapper(const void* cptr)
55 {
56 auto it = wrapperMapper.find(cptr);
57 if (it != wrapperMapper.end()) {
58 return it->second;
59 }
60
61 return nullptr;
62 }
63 };
64
65 BindingManager& BindingManager::instance()
66 {
67 static BindingManager singleton;
68 return singleton;
69 }
70
71 BindingManager::BindingManager()
72 : p(new BindingManagerPrivate)
73 {}
74
75 BindingManager::~BindingManager() = default;
76
77 bool BindingManager::hasWrapper(const void* cptr)
78 {
79 return p->hasWrapper(cptr);
80 }
81
82 void BindingManager::registerWrapper(const void* cptr, PyObject* pyObj)
83 {
84 p->registerWrapper(cptr, pyObj);
85 }
86
87 void BindingManager::releaseWrapper(const void* cptr, PyObject* pyObj)
88 {
89 p->releaseWrapper(cptr, pyObj);
90 }
91
92 PyObject* BindingManager::retrieveWrapper(const void* cptr)
93 {
94 return p->retrieveWrapper(cptr);
95 }