pythonutils.h
Go to the documentation of this file.
1 /***************************************************************************
2  * *
3  * Copyright (C) 2007-2012 by Johan De Taeye, frePPLe bvba *
4  * *
5  * This library is free software; you can redistribute it and/or modify it *
6  * under the terms of the GNU Affero General Public License as published *
7  * by the Free Software Foundation; either version 3 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This library is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU Affero General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU Affero General Public *
16  * License along with this program. *
17  * If not, see <http://www.gnu.org/licenses/>. *
18  * *
19  ***************************************************************************/
20 
21 /** @file pythonutils.h
22  * @brief Reusable functions for python functionality.
23  *
24  * Utility classes for interfacing with the Python language.
25  */
26 
27 #include "frepple/utils.h"
28 
29 namespace frepple
30 {
31 namespace utils
32 {
33 
34 /** @brief A template class to expose category classes which use a string
35  * as the key to Python. */
36 template <class T>
37 class FreppleCategory : public PythonExtension< FreppleCategory<T> >
38 {
39  public:
40  /** Initialization method. */
41  static int initialize()
42  {
43  // Initialize the type
45  x.setName(T::metadata->type);
46  x.setDoc("frePPLe " + T::metadata->type);
47  x.supportgetattro();
48  x.supportsetattro();
49  x.supportstr();
50  x.supportcompare();
51  x.supportcreate(Object::create<T>);
52  const_cast<MetaCategory*>(T::metadata)->pythonClass = x.type_object();
53  return x.typeReady();
54  }
55 };
56 
57 
58 /** @brief A template class to expose classes to Python. */
59 template <class ME, class BASE>
60 class FreppleClass : public PythonExtension< FreppleClass<ME,BASE> >
61 {
62  public:
63  static int initialize()
64  {
65  // Initialize the type
67  x.setName(ME::metadata->type);
68  x.setDoc("frePPLe " + ME::metadata->type);
69  x.supportgetattro();
70  x.supportsetattro();
71  x.supportstr();
72  x.supportcompare();
73  x.supportcreate(Object::create<ME>);
74  x.setBase(BASE::metadata->pythonClass);
75  x.addMethod("toXML", ME::toXML, METH_VARARGS, "return a XML representation");
76  const_cast<MetaClass*>(ME::metadata)->pythonClass = x.type_object();
77  return x.typeReady();
78  }
79 };
80 
81 } // end namespace
82 } // end namespace