Adonthell 0.4

py_callback.h

Go to the documentation of this file.
00001 /*
00002    $Id: py_callback.h,v 1.4 2002/08/19 19:57:29 ksterker Exp $
00003 
00004    Copyright (C) 2001/2002 Kai Sterker <kaisterker@linuxgames.com>
00005    Part of the Adonthell Project http://adonthell.linuxgames.com
00006 
00007    This program is free software; you can redistribute it and/or modify
00008    it under the terms of the GNU General Public License.
00009    This program is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY.
00011 
00012    See the COPYING file for more details.
00013 */
00014 
00015 #ifndef PY_CALLBACK_H__
00016 #define PY_CALLBACK_H__
00017 
00018 
00019 /**
00020  * @file   py_callback.h
00021  * @author Kai Sterker <kaisterker@linuxgames.com>
00022  * 
00023  * @brief  Declares the py_callback class.
00024  * 
00025  * 
00026  */
00027 
00028  
00029 #include "Python.h"
00030 #include "fileops.h"
00031 
00032 /**
00033  * Stores the C++ <-> Python callback binding
00034  *
00035  */ 
00036 class py_callback
00037 {
00038 public:
00039 
00040     /**
00041      * Default ctor,
00042      */
00043     py_callback ();
00044 
00045     /** 
00046      * Constructor that assigns a function and its arguments to the callback.
00047      * 
00048      * @param func function assigned to this callback.
00049      * @param args Arguments passed to the function.
00050      */
00051     py_callback (PyObject * func, PyObject * args);   
00052 
00053     /** 
00054      * Destructor.
00055      */
00056     ~py_callback ();
00057 
00058     
00059     /**
00060      * @name Executing the callback
00061      */
00062     //@{
00063     
00064     /** 
00065      * Calls the python function without arguments.
00066      */
00067     void callback_func0 ();
00068 
00069     /**
00070      * Calls the python function and returns bool.
00071      */
00072     bool callback_func0ret (); 
00073 
00074     /**
00075      * Calls the python function with an integer.
00076      *
00077      * @param arg Integer value to pass to the callback
00078      */ 
00079     void callback_func1 (int arg);
00080     
00081     //@}
00082     
00083     
00084     /**
00085      * @name Loading / Saving
00086      */
00087     //@{
00088 
00089     /** 
00090      * Saves the callback and it's arguments to file.
00091      * @note Currently, arguments have to be a tuple containing only
00092      *      integers and/or strings.
00093      *
00094      * @param out file where to save the callback.
00095      */ 
00096     void put_state (ogzstream& out) const;
00097     
00098     /** 
00099      * Restores the callback from a file. For that to work, the static
00100      * py_callback::instance member has to point to the python instance 
00101      * containing the callback.
00102      * 
00103      * @param in file to load the callback from.
00104      *
00105      * @return \e true if the callback could be restored, \e false otherwise
00106      *
00107      * @sa instance
00108      */
00109     bool get_state (igzstream& in);
00110 
00111     /**
00112      * When restoring a callback from file, instance has to point to the
00113      * python instance (module or class) containing the callback.
00114      *
00115      * @sa get_state
00116      */
00117     static PyObject *instance;
00118     //@}
00119 
00120 private:
00121 
00122     /**
00123      * The actual function call.
00124      *
00125      * @param args The arguments passed to the callback.
00126      */
00127     PyObject *make_call (PyObject *args);
00128 
00129     /**
00130      * The function to be called.
00131      */ 
00132     PyObject *function;
00133 
00134     /**
00135      * Additional arguments passed to the function.
00136      */ 
00137     PyObject *arguments;
00138 };
00139 
00140 #endif // PY_CALLBACK_H__