| 1 |
/*************************************************************************** |
| 2 |
* Copyright (c) 2016 Stefan Tröger <[email protected]> * |
| 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 |
#include <cassert> |
| 24 |
#include <vector> |
| 25 |
#include <map> |
| 26 |
#include <string> |
| 27 |
|
| 28 |
#include <Base/PyObjectBase.h> |
| 29 |
|
| 30 |
#include "Extension.h" |
| 31 |
#include "ExtensionContainer.h" |
| 32 |
#include "ExtensionPython.h" |
| 33 |
#include <ExtensionPy.h> |
| 34 |
|
| 35 |
|
| 36 |
/* We do not use a standard property macro for type initiation. The reason is that we have the first |
| 37 |
* PropertyData in the extension chain, there is no parent property data. |
| 38 |
*/ |
| 39 |
EXTENSION_TYPESYSTEM_SOURCE_P(App::Extension) |
| 40 |
|
| 41 |
const App::PropertyData* App::Extension::extensionGetPropertyDataPtr() |
| 42 |
{ |
| 43 |
return &propertyData; |
| 44 |
} |
| 45 |
|
| 46 |
const App::PropertyData& App::Extension::extensionGetPropertyData() const |
| 47 |
{ |
| 48 |
return propertyData; |
| 49 |
} |
| 50 |
|
| 51 |
App::PropertyData App::Extension::propertyData; |
| 52 |
|
| 53 |
void App::Extension::init() |
| 54 |
{ |
| 55 |
assert(Extension::classTypeId.isBad() && "don't init() twice!"); |
| 56 |
|
| 57 |
/* Set up entry in the type system. */ |
| 58 |
Extension::classTypeId = |
| 59 |
Base::Type::createType(Base::Type::BadType, "App::Extension", Extension::create); |
| 60 |
} |
| 61 |
|
| 62 |
using namespace App; |
| 63 |
|
| 64 |
Extension::~Extension() |
| 65 |
{ |
| 66 |
if (!ExtensionPythonObject.is(Py::_None())) { |
| 67 |
Base::PyGILStateLocker lock; |
| 68 |
// Remark: The API of Py::Object has been changed to set whether the wrapper owns the passed |
| 69 |
// Python object or not. In the constructor we forced the wrapper to own the object so we |
| 70 |
// need not to dec'ref the Python object any more. But we must still invalidate the Python |
| 71 |
// object because it need not to be destructed right now because the interpreter can own |
| 72 |
// several references to it. |
| 73 |
Base::PyObjectBase* obj = static_cast<Base::PyObjectBase*>(ExtensionPythonObject.ptr()); |
| 74 |
// Call before decrementing the reference counter, otherwise a heap error can occur |
| 75 |
obj->setInvalid(); |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
void Extension::initExtensionType(Base::Type type) |
| 80 |
{ |
| 81 |
m_extensionType = type; |
| 82 |
if (m_extensionType.isBad()) { |
| 83 |
throw Base::RuntimeError("Extension: Extension type not set"); |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
void Extension::initExtension(ExtensionContainer* obj) |
| 88 |
{ |
| 89 |
if (m_extensionType.isBad()) { |
| 90 |
throw Base::RuntimeError("Extension: Extension type not set"); |
| 91 |
} |
| 92 |
|
| 93 |
// all properties are initialised without PropertyContainer father. Now that we know it we can |
| 94 |
// finally finish the property initialisation |
| 95 |
std::vector<Property*> list; |
| 96 |
extensionGetPropertyData().getPropertyList(this, list); |
| 97 |
for (Property* prop : list) { |
| 98 |
prop->setContainer(obj); |
| 99 |
} |
| 100 |
|
| 101 |
m_base = obj; |
| 102 |
m_base->registerExtension(m_extensionType, this); |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
PyObject* Extension::getExtensionPyObject() |
| 107 |
{ |
| 108 |
if (ExtensionPythonObject.is(Py::_None())) { |
| 109 |
// ref counter is set to 1 |
| 110 |
auto grp = new ExtensionPy(this); |
| 111 |
ExtensionPythonObject = Py::Object(grp, true); |
| 112 |
} |
| 113 |
return Py::new_reference_to(ExtensionPythonObject); |
| 114 |
} |
| 115 |
|
| 116 |
std::string Extension::name() const |
| 117 |
{ |
| 118 |
if (m_extensionType.isBad()) { |
| 119 |
throw Base::RuntimeError("Extension::name: Extension type not set"); |
| 120 |
} |
| 121 |
|
| 122 |
std::string temp(m_extensionType.getName()); |
| 123 |
std::string::size_type pos = temp.find_last_of(':'); |
| 124 |
|
| 125 |
if (pos != std::string::npos) { |
| 126 |
return temp.substr(pos + 1); |
| 127 |
} |
| 128 |
return {}; |
| 129 |
} |
| 130 |
|
| 131 |
Property* Extension::extensionGetPropertyByName(const char* name) const |
| 132 |
{ |
| 133 |
return extensionGetPropertyData().getPropertyByName(this, name); |
| 134 |
} |
| 135 |
|
| 136 |
short int Extension::extensionGetPropertyType(const Property* prop) const |
| 137 |
{ |
| 138 |
return extensionGetPropertyData().getType(this, prop); |
| 139 |
} |
| 140 |
|
| 141 |
short int Extension::extensionGetPropertyType(const char* name) const |
| 142 |
{ |
| 143 |
return extensionGetPropertyData().getType(this, name); |
| 144 |
} |
| 145 |
|
| 146 |
const char* Extension::extensionGetPropertyName(const Property* prop) const |
| 147 |
{ |
| 148 |
return extensionGetPropertyData().getName(this, prop); |
| 149 |
} |
| 150 |
|
| 151 |
const char* Extension::extensionGetPropertyGroup(const Property* prop) const |
| 152 |
{ |
| 153 |
return extensionGetPropertyData().getGroup(this, prop); |
| 154 |
} |
| 155 |
|
| 156 |
const char* Extension::extensionGetPropertyGroup(const char* name) const |
| 157 |
{ |
| 158 |
return extensionGetPropertyData().getGroup(this, name); |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
const char* Extension::extensionGetPropertyDocumentation(const Property* prop) const |
| 163 |
{ |
| 164 |
return extensionGetPropertyData().getDocumentation(this, prop); |
| 165 |
} |
| 166 |
|
| 167 |
const char* Extension::extensionGetPropertyDocumentation(const char* name) const |
| 168 |
{ |
| 169 |
return extensionGetPropertyData().getDocumentation(this, name); |
| 170 |
} |
| 171 |
|
| 172 |
void Extension::extensionGetPropertyList(std::vector<Property*>& List) const |
| 173 |
{ |
| 174 |
|
| 175 |
extensionGetPropertyData().getPropertyList(this, List); |
| 176 |
} |
| 177 |
|
| 178 |
void Extension::extensionGetPropertyMap(std::map<std::string, Property*>& Map) const |
| 179 |
{ |
| 180 |
extensionGetPropertyData().getPropertyMap(this, Map); |
| 181 |
} |
| 182 |
|
| 183 |
void Extension::extensionVisitProperties(const std::function<void(Property*)>& visitor) const |
| 184 |
{ |
| 185 |
extensionGetPropertyData().visitProperties(this, visitor); |
| 186 |
} |
| 187 |
void Extension::initExtensionSubclass(Base::Type& toInit, |
| 188 |
const char* ClassName, |
| 189 |
const char* ParentName, |
| 190 |
Base::Type::instantiationMethod method) |
| 191 |
{ |
| 192 |
// don't init twice! |
| 193 |
assert(toInit.isBad()); |
| 194 |
// get the parent class |
| 195 |
Base::Type parentType(Base::Type::fromName(ParentName)); |
| 196 |
// forgot init parent! |
| 197 |
assert(!parentType.isBad()); |
| 198 |
|
| 199 |
// create the new type |
| 200 |
toInit = Base::Type::createType(parentType, ClassName, method); |
| 201 |
} |
| 202 |
|
| 203 |
bool Extension::extensionHandleChangedPropertyName(Base::XMLReader& reader, |
| 204 |
const char* TypeName, |
| 205 |
const char* PropName) |
| 206 |
{ |
| 207 |
(void)reader; |
| 208 |
(void)TypeName; |
| 209 |
(void)PropName; |
| 210 |
|
| 211 |
return false; |
| 212 |
} |
| 213 |
|
| 214 |
bool Extension::extensionHandleChangedPropertyType(Base::XMLReader& reader, |
| 215 |
const char* TypeName, |
| 216 |
Property* prop) |
| 217 |
{ |
| 218 |
(void)reader; |
| 219 |
(void)TypeName; |
| 220 |
(void)prop; |
| 221 |
|
| 222 |
return false; |
| 223 |
} |
| 224 |
|
| 225 |
namespace App |
| 226 |
{ |
| 227 |
EXTENSION_PROPERTY_SOURCE_TEMPLATE(App::ExtensionPython, App::ExtensionPython::Inherited) |
| 228 |
|
| 229 |
// explicit template instantiation |
| 230 |
template class AppExport ExtensionPythonT<Extension>; |
| 231 |
} // namespace App |