pythonutils.h
Go to the documentation of this file.
00001 /*************************************************************************** 00002 file : $URL: http://svn.code.sf.net/p/frepple/code/trunk/include/frepple/pythonutils.h $ 00003 version : $LastChangedRevision: 1715 $ $LastChangedBy: jdetaeye $ 00004 date : $LastChangedDate: 2012-07-19 21:37:46 +0200 (Thu, 19 Jul 2012) $ 00005 ***************************************************************************/ 00006 00007 /*************************************************************************** 00008 * * 00009 * Copyright (C) 2007-2012 by Johan De Taeye, frePPLe bvba * 00010 * * 00011 * This library is free software; you can redistribute it and/or modify it * 00012 * under the terms of the GNU Affero General Public License as published * 00013 * by the Free Software Foundation; either version 3 of the License, or * 00014 * (at your option) any later version. * 00015 * * 00016 * This library is distributed in the hope that it will be useful, * 00017 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00018 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 00019 * GNU Affero General Public License for more details. * 00020 * * 00021 * You should have received a copy of the GNU Affero General Public * 00022 * License along with this program. * 00023 * If not, see <http://www.gnu.org/licenses/>. * 00024 * * 00025 ***************************************************************************/ 00026 00027 /** @file pythonutils.h 00028 * @brief Reusable functions for python functionality. 00029 * 00030 * Utility classes for interfacing with the Python language. 00031 */ 00032 00033 #include "frepple/utils.h" 00034 00035 namespace frepple 00036 { 00037 namespace utils 00038 { 00039 00040 /** @brief A template class to expose category classes which use a string 00041 * as the key to Python. */ 00042 template <class T> 00043 class FreppleCategory : public PythonExtension< FreppleCategory<T> > 00044 { 00045 public: 00046 /** Initialization method. */ 00047 static int initialize() 00048 { 00049 // Initialize the type 00050 PythonType& x = PythonExtension< FreppleCategory<T> >::getType(); 00051 x.setName(T::metadata->type); 00052 x.setDoc("frePPLe " + T::metadata->type); 00053 x.supportgetattro(); 00054 x.supportsetattro(); 00055 x.supportstr(); 00056 x.supportcompare(); 00057 x.supportcreate(Object::create<T>); 00058 const_cast<MetaCategory*>(T::metadata)->pythonClass = x.type_object(); 00059 return x.typeReady(); 00060 } 00061 }; 00062 00063 00064 /** @brief A template class to expose classes to Python. */ 00065 template <class ME, class BASE> 00066 class FreppleClass : public PythonExtension< FreppleClass<ME,BASE> > 00067 { 00068 public: 00069 static int initialize() 00070 { 00071 // Initialize the type 00072 PythonType& x = PythonExtension< FreppleClass<ME,BASE> >::getType(); 00073 x.setName(ME::metadata->type); 00074 x.setDoc("frePPLe " + ME::metadata->type); 00075 x.supportgetattro(); 00076 x.supportsetattro(); 00077 x.supportstr(); 00078 x.supportcompare(); 00079 x.supportcreate(Object::create<ME>); 00080 x.setBase(BASE::metadata->pythonClass); 00081 x.addMethod("toXML", ME::toXML, METH_VARARGS, "return a XML representation"); 00082 const_cast<MetaClass*>(ME::metadata)->pythonClass = x.type_object(); 00083 return x.typeReady(); 00084 } 00085 }; 00086 00087 } // end namespace 00088 } // end namespace