PLearn 0.1
|
00001 // -*- C++ -*- 00002 00003 // InjectionTest.cc 00004 // 00005 // Copyright (C) 2005-2006 Olivier Delalleau 00006 // 00007 // Redistribution and use in source and binary forms, with or without 00008 // modification, are permitted provided that the following conditions are met: 00009 // 00010 // 1. Redistributions of source code must retain the above copyright 00011 // notice, this list of conditions and the following disclaimer. 00012 // 00013 // 2. Redistributions in binary form must reproduce the above copyright 00014 // notice, this list of conditions and the following disclaimer in the 00015 // documentation and/or other materials provided with the distribution. 00016 // 00017 // 3. The name of the authors may not be used to endorse or promote 00018 // products derived from this software without specific prior written 00019 // permission. 00020 // 00021 // THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR 00022 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 00023 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN 00024 // NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00025 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 00026 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00027 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00028 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00029 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00030 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00031 // 00032 // This file is part of the PLearn library. For more information on the PLearn 00033 // library, go to the PLearn Web site at www.plearn.org 00034 00035 /* ******************************************************* 00036 * $Id: .pyskeleton_header 544 2003-09-01 00:05:31Z plearner $ 00037 ******************************************************* */ 00038 00039 // Authors: Olivier Delalleau 00040 00044 #include <plearn/python/PythonCodeSnippet.h> 00045 #include <iostream> 00046 00047 #include "InjectionTest.h" 00048 00049 namespace PLearn { 00050 using namespace std; 00051 00052 PLEARN_IMPLEMENT_OBJECT( 00053 InjectionTest, 00054 "Ensure that the Python function PyCFunction_NewEx works correctly", 00055 "" 00056 ); 00057 00059 // InjectionTest // 00061 InjectionTest::InjectionTest() 00062 /* ### Initialize all fields to their default value */ 00063 { 00064 // ... 00065 00066 // ### You may (or not) want to call build_() to finish building the object 00067 // ### (doing so assumes the parent classes' build_() have been called too 00068 // ### in the parent classes' constructors, something that you must ensure) 00069 } 00070 00072 // build // 00074 void InjectionTest::build() 00075 { 00076 inherited::build(); 00077 build_(); 00078 } 00079 00081 // makeDeepCopyFromShallowCopy // 00083 void InjectionTest::makeDeepCopyFromShallowCopy(CopiesMap& copies) 00084 { 00085 inherited::makeDeepCopyFromShallowCopy(copies); 00086 00087 // ### Call deepCopyField on all "pointer-like" fields 00088 // ### that you wish to be deepCopied rather than 00089 // ### shallow-copied. 00090 // ### ex: 00091 // deepCopyField(trainvec, copies); 00092 00093 // ### Remove this line when you have fully implemented this method. 00094 PLERROR("InjectionTest::makeDeepCopyFromShallowCopy not fully (correctly) implemented yet!"); 00095 } 00096 00098 // declareOptions // 00100 void InjectionTest::declareOptions(OptionList& ol) 00101 { 00102 // ### Declare all of this object's options here 00103 // ### For the "flags" of each option, you should typically specify 00104 // ### one of OptionBase::buildoption, OptionBase::learntoption or 00105 // ### OptionBase::tuningoption. Another possible flag to be combined with 00106 // ### is OptionBase::nosave 00107 00108 // ### ex: 00109 // declareOption(ol, "myoption", &InjectionTest::myoption, OptionBase::buildoption, 00110 // "Help text describing this option"); 00111 // ... 00112 00113 // Now call the parent class' declareOptions 00114 inherited::declareOptions(ol); 00115 } 00116 00118 // build_ // 00120 void InjectionTest::build_() 00121 { 00122 // ### This method should do the real building of the object, 00123 // ### according to set 'options', in *any* situation. 00124 // ### Typical situations include: 00125 // ### - Initial building of an object from a few user-specified options 00126 // ### - Building of a "reloaded" object: i.e. from the complete set of all serialised options. 00127 // ### - Updating or "re-building" of an object after a few "tuning" options have been modified. 00128 // ### You should assume that the parent class' build_() has already been called. 00129 } 00130 00131 string InjectionTest::python_code = 00132 "import sys\n" 00133 "\n" 00134 "def trampoline_function_call(x):\n" 00135 " y = injected_c_function(x)\n" 00136 " print >>sys.stderr, 'The C function returned the charming value',y\n" 00137 " sys.stderr.flush()\n" 00138 "\n" 00139 "def trampoline_method_call(x):\n" 00140 " y = injected_c_method1(x)\n" 00141 " z = injected_c_method2(x)\n" 00142 " print >>sys.stderr, 'The C method 1 returned the charming value',y\n" 00143 " print >>sys.stderr, 'The C method 2 returned the charming value',z\n" 00144 " sys.stderr.flush()\n" 00145 ; 00146 00147 PythonObjectWrapper InjectionTest_basic_function(const TVec<PythonObjectWrapper>& args) 00148 { 00149 cout << "basic_function called with arg[0]=" 00150 << args[0].as<int>() << endl; 00151 return PythonObjectWrapper(42); 00152 } 00153 00154 00155 struct X 00156 { 00157 X(int value) : i(value) { } 00158 00159 int i; 00160 PythonObjectWrapper f(const TVec<PythonObjectWrapper>& args); 00161 PythonObjectWrapper g(const TVec<PythonObjectWrapper>& args) const; 00162 }; 00163 00164 PythonObjectWrapper X::f(const TVec<PythonObjectWrapper>& args) 00165 { 00166 cout << "X::f() called with i=" << i << " and arg[0]=" 00167 << args[0].as<int>() << endl; 00168 return PythonObjectWrapper(1337); 00169 } 00170 00171 PythonObjectWrapper X::g(const TVec<PythonObjectWrapper>& args) const 00172 { 00173 cout << "X::g() called with i=" << i << " and arg[0]=" 00174 << args[0].as<int>() << endl; 00175 return PythonObjectWrapper(1337*2); 00176 } 00177 00179 // perform // 00181 void InjectionTest::perform() 00182 { 00183 PP<PythonCodeSnippet> python = new PythonCodeSnippet(python_code); 00184 python->build(); 00185 00186 X x(13370); 00187 00188 python->inject("injected_c_function", InjectionTest_basic_function); 00189 python->inject("injected_c_function2", InjectionTest_basic_function); 00190 python->inject("injected_c_method1", &x, &X::f); 00191 python->inject("injected_c_method2", &x, &X::g); 00192 00193 python->invoke("trampoline_function_call", 64); 00194 python->invoke("trampoline_method_call", 128); 00195 } 00196 00197 } // end of namespace PLearn 00198 00199 00200 /* 00201 Local Variables: 00202 mode:c++ 00203 c-basic-offset:4 00204 c-file-style:"stroustrup" 00205 c-file-offsets:((innamespace . 0)(inline-open . 0)) 00206 indent-tabs-mode:nil 00207 fill-column:79 00208 End: 00209 */ 00210 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :