1
/***************************************************************************
2
* Copyright (c) 2016 Stefan Tröger <stefantroeger@gmx.net> *
4
* This file is part of the FreeCAD CAx development system. *
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. *
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. *
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 *
21
***************************************************************************/
24
#include "PreCompiled.h"
30
#include <Base/PyObjectBase.h>
33
#include "ExtensionContainer.h"
34
#include "ExtensionPython.h"
35
#include <ExtensionPy.h>
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.
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(){
47
assert(Extension::classTypeId == Base::Type::badType() && "don't init() twice!");
49
/* Set up entry in the type system. */
50
Extension::classTypeId = Base::Type::createType(Base::Type::badType(), "App::Extension",
56
Extension::~Extension()
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
70
void Extension::initExtensionType(Base::Type type) {
72
m_extensionType = type;
73
if (m_extensionType.isBad())
74
throw Base::RuntimeError("Extension: Extension type not set");
77
void Extension::initExtension(ExtensionContainer* obj) {
78
if (m_extensionType.isBad())
79
throw Base::RuntimeError("Extension: Extension type not set");
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);
89
m_base->registerExtension( m_extensionType, this );
93
PyObject* Extension::getExtensionPyObject() {
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);
100
return Py::new_reference_to(ExtensionPythonObject);
103
std::string Extension::name() const {
105
if (m_extensionType.isBad())
106
throw Base::RuntimeError("Extension::name: Extension type not set");
108
std::string temp(m_extensionType.getName());
109
std::string::size_type pos = temp.find_last_of(':');
111
if (pos != std::string::npos)
112
return temp.substr(pos+1);
116
Property* Extension::extensionGetPropertyByName(const char* name) const {
118
return extensionGetPropertyData().getPropertyByName(this, name);
121
short int Extension::extensionGetPropertyType(const Property* prop) const {
123
return extensionGetPropertyData().getType(this, prop);
126
short int Extension::extensionGetPropertyType(const char* name) const {
128
return extensionGetPropertyData().getType(this, name);
131
const char* Extension::extensionGetPropertyName(const Property* prop) const {
133
return extensionGetPropertyData().getName(this,prop);
136
const char* Extension::extensionGetPropertyGroup(const Property* prop) const {
138
return extensionGetPropertyData().getGroup(this,prop);
141
const char* Extension::extensionGetPropertyGroup(const char* name) const {
143
return extensionGetPropertyData().getGroup(this,name);
147
const char* Extension::extensionGetPropertyDocumentation(const Property* prop) const {
149
return extensionGetPropertyData().getDocumentation(this, prop);
152
const char* Extension::extensionGetPropertyDocumentation(const char* name) const {
154
return extensionGetPropertyData().getDocumentation(this, name);
157
void Extension::extensionGetPropertyList(std::vector< Property* >& List) const {
159
extensionGetPropertyData().getPropertyList(this, List);
162
void Extension::extensionGetPropertyMap(std::map< std::string, Property* >& Map) const {
164
extensionGetPropertyData().getPropertyMap(this, Map);
167
void Extension::initExtensionSubclass(Base::Type& toInit, const char* ClassName, const char* ParentName,
168
Base::Type::instantiationMethod method) {
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() );
177
// create the new type
178
toInit = Base::Type::createType(parentType, ClassName, method);
182
bool Extension::extensionHandleChangedPropertyName(Base::XMLReader &reader, const char * TypeName, const char *PropName)
191
bool Extension::extensionHandleChangedPropertyType(Base::XMLReader &reader, const char * TypeName, Property * prop)
201
EXTENSION_PROPERTY_SOURCE_TEMPLATE(App::ExtensionPython, App::ExtensionPython::Inherited)
203
// explicit template instantiation
204
template class AppExport ExtensionPythonT<Extension>;