00001 /*************************************************************************** 00002 * Copyright (c) 2009 Werner Mayer <wmayer@users.sourceforge.net> * 00003 * * 00004 * This file is part of the FreeCAD CAx development system. * 00005 * * 00006 * This library is free software; you can redistribute it and/or * 00007 * modify it under the terms of the GNU Library General Public * 00008 * License as published by the Free Software Foundation; either * 00009 * version 2 of the License, or (at your option) any later version. * 00010 * * 00011 * This library is distributed in the hope that it will be useful, * 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 00014 * GNU Library General Public License for more details. * 00015 * * 00016 * You should have received a copy of the GNU Library General Public * 00017 * License along with this library; see the file COPYING.LIB. If not, * 00018 * write to the Free Software Foundation, Inc., 59 Temple Place, * 00019 * Suite 330, Boston, MA 02111-1307, USA * 00020 * * 00021 ***************************************************************************/ 00022 00023 00024 #ifndef SANDBOX_DOCUMENTPROTECTOR_H 00025 #define SANDBOX_DOCUMENTPROTECTOR_H 00026 00027 #include <string> 00028 #include <boost/signals.hpp> 00029 #include <boost/bind.hpp> 00030 #include <App/DocumentObserver.h> 00031 00032 namespace App { 00033 class Document; 00034 class DocumentObject; 00035 } 00036 00037 namespace Sandbox { 00038 00039 class SandboxAppExport DocumentProtector : public App::DocumentObserver 00040 { 00041 public: 00042 DocumentProtector(App::Document*); 00043 ~DocumentProtector(); 00044 00045 static void init(); 00046 00047 App::DocumentObject *addObject(const std::string& type, const std::string& name=""); 00048 void removeObject(const std::string& name); 00049 void recompute(); 00050 00051 private: 00053 void slotCreatedDocument(const App::Document& Doc); 00055 void slotDeletedDocument(const App::Document& Doc); 00057 void slotCreatedObject(const App::DocumentObject& Obj); 00059 void slotDeletedObject(const App::DocumentObject& Obj); 00061 void slotChangedObject(const App::DocumentObject& Obj, const App::Property& Prop); 00062 void validate(); 00063 }; 00064 00065 class AbstractCallable 00066 { 00067 public: 00068 AbstractCallable() 00069 { 00070 } 00071 virtual ~AbstractCallable() 00072 { 00073 } 00074 00075 virtual void operator()() const = 0; 00076 }; 00077 00078 template <class T, void (T::*method)()> 00079 class Callable : public AbstractCallable 00080 { 00081 public: 00082 Callable(App::DocumentObject* o) : obj(o) 00083 { 00084 } 00085 virtual ~Callable() 00086 { 00087 } 00088 00089 virtual void operator()() const 00090 { 00091 T* v = static_cast<T*>(obj); 00092 (v->*method)(); 00093 } 00094 00095 private: 00096 App::DocumentObject* obj; 00097 }; 00098 00099 template <class T, class Arg, void (T::*method)(Arg)> 00100 class CallableWithArgs : public AbstractCallable 00101 { 00102 public: 00103 CallableWithArgs(App::DocumentObject* o, Arg a) : obj(o), arg(a) 00104 { 00105 } 00106 virtual ~CallableWithArgs() 00107 { 00108 } 00109 00110 virtual void operator()() const 00111 { 00112 T* v = static_cast<T*>(obj); 00113 (v->*method)(arg); 00114 } 00115 00116 private: 00117 App::DocumentObject* obj; 00118 Arg arg; 00119 }; 00120 00121 class SandboxAppExport DocumentObjectProtector 00122 { 00123 public: 00124 DocumentObjectProtector(App::DocumentObject*); 00125 ~DocumentObjectProtector(); 00126 00127 App::DocumentObject* getObject() const; 00128 bool setProperty(const std::string& name, const App::Property& p); 00129 void execute(const AbstractCallable&); 00130 00131 private: 00132 void validate(); 00133 00134 private: 00135 App::DocumentObject* obj; 00136 }; 00137 00138 } 00139 00140 #endif // SANDBOX_DOCUMENTPROTECTOR_H 00141