FreeCAD

Форк
0
/
Extension.cpp 
205 строк · 7.1 Кб
1
/***************************************************************************
2
 *   Copyright (c) 2016 Stefan Tröger <stefantroeger@gmx.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

26
#ifndef _PreComp_
27
# include <cassert>
28
#endif
29

30
#include <Base/PyObjectBase.h>
31

32
#include "Extension.h"
33
#include "ExtensionContainer.h"
34
#include "ExtensionPython.h"
35
#include <ExtensionPy.h>
36

37

38
/* We do not use a standard property macro for type initiation. The reason is that we have the first
39
 * PropertyData in the extension chain, there is no parent property data.
40
 */
41
EXTENSION_TYPESYSTEM_SOURCE_P(App::Extension)
42
const App::PropertyData * App::Extension::extensionGetPropertyDataPtr(){return &propertyData;}
43
const App::PropertyData & App::Extension::extensionGetPropertyData() const{return propertyData;}
44
App::PropertyData App::Extension::propertyData;
45
void App::Extension::init(){
46

47
    assert(Extension::classTypeId == Base::Type::badType() && "don't init() twice!");
48

49
    /* Set up entry in the type system. */
50
    Extension::classTypeId = Base::Type::createType(Base::Type::badType(), "App::Extension",
51
                                Extension::create);
52
}
53

54
using namespace App;
55

56
Extension::~Extension()
57
{
58
    if (!ExtensionPythonObject.is(Py::_None())){
59
        // Remark: The API of Py::Object has been changed to set whether the wrapper owns the passed
60
        // Python object or not. In the constructor we forced the wrapper to own the object so we need
61
        // not to dec'ref the Python object any more.
62
        // But we must still invalidate the Python object because it need not to be
63
        // destructed right now because the interpreter can own several references to it.
64
        Base::PyObjectBase* obj = static_cast<Base::PyObjectBase*>(ExtensionPythonObject.ptr());
65
        // Call before decrementing the reference counter, otherwise a heap error can occur
66
        obj->setInvalid();
67
    }
68
}
69

70
void Extension::initExtensionType(Base::Type type) {
71

72
    m_extensionType = type;
73
    if (m_extensionType.isBad())
74
        throw Base::RuntimeError("Extension: Extension type not set");
75
}
76

77
void Extension::initExtension(ExtensionContainer* obj) {
78
    if (m_extensionType.isBad())
79
        throw Base::RuntimeError("Extension: Extension type not set");
80

81
    //all properties are initialised without PropertyContainer father. Now that we know it we can
82
    //finally finish the property initialisation
83
    std::vector<Property*> list;
84
    extensionGetPropertyData().getPropertyList(this, list);
85
    for(Property* prop : list)
86
        prop->setContainer(obj);
87

88
    m_base = obj;
89
    m_base->registerExtension( m_extensionType, this );
90
}
91

92

93
PyObject* Extension::getExtensionPyObject() {
94

95
    if (ExtensionPythonObject.is(Py::_None())){
96
        // ref counter is set to 1
97
        auto grp = new ExtensionPy(this);
98
        ExtensionPythonObject = Py::Object(grp,true);
99
    }
100
    return Py::new_reference_to(ExtensionPythonObject);
101
}
102

103
std::string Extension::name() const {
104

105
    if (m_extensionType.isBad())
106
        throw Base::RuntimeError("Extension::name: Extension type not set");
107

108
    std::string temp(m_extensionType.getName());
109
    std::string::size_type pos = temp.find_last_of(':');
110

111
    if (pos != std::string::npos)
112
        return temp.substr(pos+1);
113
    return {};
114
}
115

116
Property* Extension::extensionGetPropertyByName(const char* name) const {
117

118
    return extensionGetPropertyData().getPropertyByName(this, name);
119
}
120

121
short int Extension::extensionGetPropertyType(const Property* prop) const {
122

123
    return extensionGetPropertyData().getType(this, prop);
124
}
125

126
short int Extension::extensionGetPropertyType(const char* name) const {
127

128
    return extensionGetPropertyData().getType(this, name);
129
}
130

131
const char* Extension::extensionGetPropertyName(const Property* prop) const {
132

133
    return extensionGetPropertyData().getName(this,prop);
134
}
135

136
const char* Extension::extensionGetPropertyGroup(const Property* prop) const {
137

138
    return extensionGetPropertyData().getGroup(this,prop);
139
}
140

141
const char* Extension::extensionGetPropertyGroup(const char* name) const {
142

143
    return extensionGetPropertyData().getGroup(this,name);
144
}
145

146

147
const char* Extension::extensionGetPropertyDocumentation(const Property* prop) const {
148

149
    return extensionGetPropertyData().getDocumentation(this, prop);
150
}
151

152
const char* Extension::extensionGetPropertyDocumentation(const char* name) const {
153

154
    return extensionGetPropertyData().getDocumentation(this, name);
155
}
156

157
void Extension::extensionGetPropertyList(std::vector< Property* >& List) const {
158

159
    extensionGetPropertyData().getPropertyList(this, List);
160
}
161

162
void Extension::extensionGetPropertyMap(std::map< std::string, Property* >& Map) const {
163

164
    extensionGetPropertyData().getPropertyMap(this, Map);
165
}
166

167
void Extension::initExtensionSubclass(Base::Type& toInit, const char* ClassName, const char* ParentName,
168
                             Base::Type::instantiationMethod method) {
169

170
    // don't init twice!
171
    assert(toInit == Base::Type::badType());
172
    // get the parent class
173
    Base::Type parentType(Base::Type::fromName(ParentName));
174
    // forgot init parent!
175
    assert(parentType != Base::Type::badType() );
176

177
    // create the new type
178
    toInit = Base::Type::createType(parentType, ClassName, method);
179
}
180

181

182
bool Extension::extensionHandleChangedPropertyName(Base::XMLReader &reader, const char * TypeName, const char *PropName)
183
{
184
    (void) reader;
185
    (void) TypeName;
186
    (void) PropName;
187

188
    return false;
189
};
190

191
bool Extension::extensionHandleChangedPropertyType(Base::XMLReader &reader, const char * TypeName, Property * prop)
192
{
193
    (void) reader;
194
    (void) TypeName;
195
    (void) prop;
196

197
    return false;
198
};
199

200
namespace App {
201
EXTENSION_PROPERTY_SOURCE_TEMPLATE(App::ExtensionPython, App::ExtensionPython::Inherited)
202

203
// explicit template instantiation
204
template class AppExport ExtensionPythonT<Extension>;
205
}
206

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

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

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

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