pythonutils.h
Go to the documentation of this file.
00001 /***************************************************************************
00002   file : $URL: https://frepple.svn.sourceforge.net/svnroot/frepple/tags/0.9.1/include/frepple/pythonutils.h $
00003   version : $LastChangedRevision: 1423 $  $LastChangedBy: jdetaeye $
00004   date : $LastChangedDate: 2011-03-12 18:01:53 +0100 (Sat, 12 Mar 2011) $
00005  ***************************************************************************/
00006 
00007 /***************************************************************************
00008  *                                                                         *
00009  * Copyright (C) 2007 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 Lesser General Public License as published   *
00013  * by the Free Software Foundation; either version 2.1 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 GNU Lesser *
00019  * General Public License for more details.                                *
00020  *                                                                         *
00021  * You should have received a copy of the GNU Lesser General Public        *
00022  * License along with this library; if not, write to the Free Software     *
00023  * Foundation Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 *
00024  * USA                                                                     *
00025  *                                                                         *
00026  ***************************************************************************/
00027 
00028 /** @file pythonutils.h
00029   * @brief Reusable functions for python functionality.
00030   *
00031   * Utility classes for interfacing with the Python language.
00032   */
00033 
00034 #include "frepple/utils.h"
00035 
00036 namespace frepple
00037 {
00038 namespace utils
00039 {
00040 
00041 /** @brief A template class to expose category classes which use a string
00042   * as the key to Python . */
00043 template <class T>
00044 class FreppleCategory : public PythonExtension< FreppleCategory<T> >
00045 {
00046   public:
00047     /** Initialization method. */
00048     static int initialize()
00049     {
00050       // Initialize the type
00051       PythonType& x = PythonExtension< FreppleCategory<T> >::getType();
00052       x.setName(T::metadata->type);
00053       x.setDoc("frePPLe " + T::metadata->type);
00054       x.supportgetattro();
00055       x.supportsetattro();
00056       x.supportstr();
00057       x.supportcompare();
00058       x.supportcreate(Object::create<T>);
00059       const_cast<MetaCategory*>(T::metadata)->pythonClass = x.type_object();
00060       return x.typeReady();
00061     }
00062 };
00063 
00064 
00065 /** @brief A template class to expose classes to Python. */
00066 template <class ME, class BASE>
00067 class FreppleClass  : public PythonExtension< FreppleClass<ME,BASE> >
00068 {
00069   public:
00070     static int initialize()
00071     {
00072       // Initialize the type
00073       PythonType& x = PythonExtension< FreppleClass<ME,BASE> >::getType();
00074       x.setName(ME::metadata->type);
00075       x.setDoc("frePPLe " + ME::metadata->type);
00076       x.supportgetattro();
00077       x.supportsetattro();
00078       x.supportstr();
00079       x.supportcompare();
00080       x.supportcreate(Object::create<ME>);
00081       x.setBase(BASE::metadata->pythonClass);
00082       x.addMethod("toXML", ME::toXML, METH_VARARGS, "return a XML representation");
00083       const_cast<MetaClass*>(ME::metadata)->pythonClass = x.type_object();
00084       return x.typeReady();
00085     }
00086 };
00087 
00088 } // end namespace
00089 } // end namespace

Documentation generated for frePPLe by  doxygen