FreeCAD

Форк
0
/
SmartPtrPy.cpp 
128 строк · 3.0 Кб
1
/***************************************************************************
2
 *   Copyright (c) 2020 Werner Mayer <wmayer[at]users.sourceforge.net>     *
3
 *                                                                         *
4
 *   This file is part of the FreeCAD CAx development system.              *
5
 *                                                                         *
6
 *   This library is free software; you can redistribute it and/or         *
7
 *   modify it under the terms of the GNU Library General Public           *
8
 *   License as published by the Free Software Foundation; either          *
9
 *   version 2 of the License, or (at your option) any later version.      *
10
 *                                                                         *
11
 *   This library  is distributed in the hope that it will be useful,      *
12
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
13
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
14
 *   GNU Library General Public License for more details.                  *
15
 *                                                                         *
16
 *   You should have received a copy of the GNU Library General Public     *
17
 *   License along with this library; see the file COPYING.LIB. If not,    *
18
 *   write to the Free Software Foundation, Inc., 59 Temple Place,         *
19
 *   Suite 330, Boston, MA  02111-1307, USA                                *
20
 *                                                                         *
21
 ***************************************************************************/
22

23

24
#include "PreCompiled.h"
25
#ifndef _PreComp_
26
#endif
27

28
#include "SmartPtrPy.h"
29
#include "Interpreter.h"
30
#include <CXX/Objects.hxx>
31

32

33
namespace Py
34
{
35
void SmartPtr::set(PyObject* pyob, bool owned)
36
{
37
    release();
38
    p = pyob;
39
    if (!owned) {
40
        Py::_XINCREF(p);
41
    }
42
}
43

44
void SmartPtr::release()
45
{
46
    Base::PyGILStateLocker lock;
47
    Py::_XDECREF(p);
48
    p = nullptr;
49
}
50

51
SmartPtr::SmartPtr()
52
    : p(Py::_None())
53
{
54
    Py::_XINCREF(p);
55
}
56

57
SmartPtr::SmartPtr(PyObject* pyob, bool owned)
58
    : p(pyob)
59
{
60
    if (!owned) {
61
        Py::_XINCREF(p);
62
    }
63
}
64

65
SmartPtr::SmartPtr(const SmartPtr& ob)
66
    : p(ob.p)
67
{
68
    Py::_XINCREF(p);
69
}
70

71
SmartPtr& SmartPtr::operator=(const SmartPtr& rhs)
72
{
73
    set(rhs.ptr());
74
    return *this;
75
}
76

77
SmartPtr& SmartPtr::operator=(const Object& rhs)
78
{
79
    set(rhs.ptr());
80
    return *this;
81
}
82

83
SmartPtr& SmartPtr::operator=(PyObject* rhsp)
84
{
85
    if (ptr() != rhsp) {
86
        set(rhsp);
87
    }
88

89
    return *this;
90
}
91

92
SmartPtr::~SmartPtr()
93
{
94
    release();
95
}
96

97
PyObject* SmartPtr::operator*() const
98
{
99
    return p;
100
}
101

102
PyObject* SmartPtr::ptr() const
103
{
104
    return p;
105
}
106

107
bool SmartPtr::is(PyObject* pother) const
108
{  // identity test
109
    return p == pother;
110
}
111

112
bool SmartPtr::is(const SmartPtr& other) const
113
{  // identity test
114
    return p == other.p;
115
}
116

117
bool SmartPtr::isNull() const
118
{
119
    return p == nullptr;
120
}
121

122
BaseExport PyObject* new_reference_to(const SmartPtr& ptr)
123
{
124
    PyObject* py = ptr.ptr();
125
    Py::_XINCREF(py);
126
    return py;
127
}
128
}  // namespace Py
129

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.