DocumentProtectorPy.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include "PreCompiled.h"
00025 #ifndef _PreComp_
00026 # include <memory>
00027 #endif
00028
00029 #include "DocumentProtectorPy.h"
00030 #include "DocumentProtector.h"
00031
00032 #include <Base/Exception.h>
00033 #include <Base/Interpreter.h>
00034 #include <App/DocumentPy.h>
00035 #include <App/DocumentObject.h>
00036 #include <App/DocumentObjectPy.h>
00037
00038 using namespace Sandbox;
00039
00040
00041 void DocumentProtectorPy::init_type()
00042 {
00043 behaviors().name("DocumentProtectorPy");
00044 behaviors().doc("Python binding class for the document protector class");
00045
00046 behaviors().supportRepr();
00047 behaviors().supportGetattr();
00048 behaviors().supportSetattr();
00049
00050 add_varargs_method("addObject",&DocumentProtectorPy::addObject,"addObject(type,name)");
00051 add_varargs_method("recompute",&DocumentProtectorPy::recompute,"recompute()");
00052 }
00053
00054 DocumentProtectorPy::DocumentProtectorPy(App::DocumentPy *doc)
00055 {
00056 _dp = new DocumentProtector(doc->getDocumentPtr());
00057 }
00058
00059 DocumentProtectorPy::~DocumentProtectorPy()
00060 {
00061 delete _dp;
00062 }
00063
00064 Py::Object DocumentProtectorPy::repr()
00065 {
00066 std::string s;
00067 std::ostringstream s_out;
00068 if (!_dp)
00069 throw Py::RuntimeError("Cannot print representation of deleted object");
00070 s_out << "Document protector";
00071 return Py::String(s_out.str());
00072 }
00073
00074 DocumentProtectorPy::method_varargs_handler DocumentProtectorPy::pycxx_handler = 0;
00075
00076 PyObject *DocumentProtectorPy::method_varargs_ext_handler(PyObject *_self_and_name_tuple, PyObject *_args)
00077 {
00078 try {
00079 return pycxx_handler(_self_and_name_tuple, _args);
00080 }
00081 catch (const Base::Exception& e) {
00082 throw Py::Exception(e.what());
00083 }
00084 catch (const std::exception& e) {
00085 throw Py::Exception(e.what());
00086 }
00087 catch(...) {
00088 throw Py::Exception("Unknown C++ exception");
00089 }
00090 }
00091
00092 Py::Object DocumentProtectorPy::getattr(const char * attr)
00093 {
00094 if (!_dp) {
00095 std::string s;
00096 std::ostringstream s_out;
00097 s_out << "Cannot access attribute '" << attr << "' of deleted object";
00098 throw Py::RuntimeError(s_out.str());
00099 }
00100 else {
00101 Py::Object obj = Py::PythonExtension<DocumentProtectorPy>::getattr(attr);
00102 if (PyCFunction_Check(obj.ptr())) {
00103 PyCFunctionObject* op = reinterpret_cast<PyCFunctionObject*>(obj.ptr());
00104 if (!pycxx_handler)
00105 pycxx_handler = op->m_ml->ml_meth;
00106 op->m_ml->ml_meth = method_varargs_ext_handler;
00107 }
00108 return obj;
00109 }
00110 }
00111
00112 int DocumentProtectorPy::setattr(const char * attr, const Py::Object & value)
00113 {
00114 if (!_dp) {
00115 std::string s;
00116 std::ostringstream s_out;
00117 s_out << "Cannot access attribute '" << attr << "' of deleted object";
00118 throw Py::RuntimeError(s_out.str());
00119 }
00120 else {
00121 return Py::PythonExtension<DocumentProtectorPy>::setattr(attr, value);
00122 }
00123 }
00124
00125 Py::Object DocumentProtectorPy::addObject(const Py::Tuple& args)
00126 {
00127 char* type;
00128 char* name=0;
00129 if (!PyArg_ParseTuple(args.ptr(), "s|s",&type, &name))
00130 throw Py::Exception();
00131 Base::PyGILStateRelease unlock;
00132 if (!name)
00133 name = type;
00134 App::DocumentObject* obj = _dp->addObject(type, name);
00135 if (!obj) {
00136 std::string s;
00137 std::ostringstream s_out;
00138 s_out << "Couldn't create an object of type '" << type << "'";
00139 throw Py::RuntimeError(s_out.str());
00140 }
00141
00142 return Py::asObject(new DocumentObjectProtectorPy(obj));
00143 }
00144
00145 Py::Object DocumentProtectorPy::recompute(const Py::Tuple& args)
00146 {
00147 if (!PyArg_ParseTuple(args.ptr(), ""))
00148 throw Py::Exception();
00149 Base::PyGILStateRelease unlock;
00150 _dp->recompute();
00151 return Py::None();
00152 }
00153
00154
00155
00156 void DocumentObjectProtectorPy::init_type()
00157 {
00158 behaviors().name("DocumentObjectProtectorPy");
00159 behaviors().doc("Python binding class for the document object protector class");
00160
00161 behaviors().supportRepr();
00162 behaviors().supportGetattr();
00163 behaviors().supportSetattr();
00164 }
00165
00166 DocumentObjectProtectorPy::DocumentObjectProtectorPy(App::DocumentObject *obj)
00167 {
00168 _dp = new DocumentObjectProtector(obj);
00169 }
00170
00171 DocumentObjectProtectorPy::DocumentObjectProtectorPy(App::DocumentObjectPy *obj)
00172 {
00173 _dp = new DocumentObjectProtector(obj->getDocumentObjectPtr());
00174 }
00175
00176 DocumentObjectProtectorPy::~DocumentObjectProtectorPy()
00177 {
00178 delete _dp;
00179 }
00180
00181 Py::Object DocumentObjectProtectorPy::repr()
00182 {
00183 std::string s;
00184 std::ostringstream s_out;
00185 if (!_dp)
00186 throw Py::RuntimeError("Cannot print representation of deleted object");
00187 s_out << "Document object protector";
00188 return Py::String(s_out.str());
00189 }
00190
00191 Py::Object DocumentObjectProtectorPy::getattr(const char * attr)
00192 {
00193 if (!_dp) {
00194 std::string s;
00195 std::ostringstream s_out;
00196 s_out << "Cannot access attribute '" << attr << "' of deleted object";
00197 throw Py::RuntimeError(s_out.str());
00198 }
00199 else {
00200 App::DocumentObject* obj = _dp->getObject();
00201 App::Property* prop = obj->getPropertyByName(attr);
00202 if (!prop) {
00203 std::string s;
00204 std::ostringstream s_out;
00205 s_out << "No such attribute '" << attr << "'";
00206 throw Py::AttributeError(s_out.str());
00207 }
00208
00209 return Py::asObject(prop->getPyObject());
00210 }
00211 }
00212
00213 int DocumentObjectProtectorPy::setattr(const char * attr, const Py::Object & value)
00214 {
00215 if (!_dp) {
00216 std::string s;
00217 std::ostringstream s_out;
00218 s_out << "Cannot access attribute '" << attr << "' of deleted object";
00219 throw Py::RuntimeError(s_out.str());
00220 }
00221 else {
00222 App::DocumentObject* obj = _dp->getObject();
00223 App::Property* prop = obj->getPropertyByName(attr);
00224 if (!prop) {
00225 std::string s;
00226 std::ostringstream s_out;
00227 s_out << "No such attribute '" << attr << "'";
00228 throw Py::AttributeError(s_out.str());
00229 }
00230 std::auto_ptr<App::Property> copy(static_cast<App::Property*>
00231 (prop->getTypeId().createInstance()));
00232 copy->setPyObject(value.ptr());
00233 return _dp->setProperty(attr, *copy) ? 0 : -1;
00234 }
00235 }