随笔 - 67  文章 - 79  trackbacks - 0
<2009年1月>
28293031123
45678910
11121314151617
18192021222324
25262728293031
1234567

常用链接

留言簿(1)

随笔档案

文章档案

相册

搜索

  •  

最新评论

阅读排行榜

评论排行榜

这几天单位的同事好多请假不来,上班如同值班一般,但是事情一样没少,甚是繁忙。
父亲去外地开会去了,母亲在医院陪伴住院的外祖母,晚饭都是自己简单对付一下,随便弄些糊口了事。

今年的工作乏善可陈,除非上了几个项目,真不知道总结怎么来写。最大的成就或许是对python的学习和应用,本来想写一些应用的,但是各个都是半成品。Java自己没怎么去看过,或许是因为Java这一年也没什么大的发展吧。C++倒是看了不少书,泛型也有点入门了,凑合着能写点忽悠人的代码了。Linux shell和gnu tools 那块今年有学了不少东西。通过移植几个win32的库,把autotools了解的差不多了。这个月加了内存,升到4G后 装了个64位的Vista玩,感觉就是windows的64位版本远不如linux的。

想想今年帮过不少人做过毕业设计,有本科的,也有研究生。有最铁的兄弟,也有
________________
上面写于12.26 哪几天身体不好 没写完 其实也没多大意思 都已经到明年了 算了吧

找地方贴下 我修改的python_def_function的代码和示例
/******************************************************************************
 * How to wrap boost.function objects with boost.python and use them both from
 * C++ and Python.
 * 
 *   AUTHOR: Alcides Viamontes Esquivel
 *
 *   LICENSE: You're free to use this file for whatever you want. You're 
 *            specially welcome to improve it in any way and give the 
 *            changes back.
 *
 ******************************************************************************
 *
 * Usage example:
 *
 *     For the boost.python C++ module:
 *     
 *     
 *     #include <boost/python.hpp>
 *     
 *     #include "py_boost_function.hpp"
 *     
 *
 *     void module_greeter_f(std::string const& origin)
 *     {
 *           cout << "Hello world, by " << origin << endl;
 *     }
 *
 *
 *     boost::function< void( std::string const& ) > module_greeter( 
 *          module_greeter_f
 *     ) ;
 *     
 *
 *     BOOST_PYTHON_MODULE( foo ) {
 *
 *          using namespace boost.python;
 *          
 *
 *          def_function< void(string const&) >(
 *              "greeter_function_t", 
 *              "A greeting function" 
 *          );
 *
 *          
 *          scope().attr("module_greeter") = module_greeter;
 *     }
 *
 * From python code:
 *
 *     - Invoke:
 *
 *          >>> import foo
 *          >>> foo.module_greeter("world")
 *
 *     - Create instances from python:
 *             
 *          >>> def my_greetings(hi):
 *          >>>  print hi, ", world"
 *          >>> 
 *          >>> grfunc = foo.greeter_function_t.from_callable( my_greetings )
 *
 
*/



#ifndef PY_BOOST_FUNCTION_INCLUDE_GUARD
#define PY_BOOST_FUNCTION_INCLUDE_GUARD 

#include 
<boost/python.hpp>

#include 
<boost/function.hpp>
#include 
<boost/function_types/function_type.hpp>
#include 
<boost/function_types/result_type.hpp>
#include 
<boost/function_types/is_function.hpp>

#include 
<boost/type_traits/function_traits.hpp>

#include 
<boost/preprocessor/repetition/enum_binary_params.hpp>
#include 
<boost/preprocessor/repetition/enum_params.hpp>
#include 
<boost/preprocessor/repetition/repeat.hpp>
#include 
<boost/preprocessor/repetition/repeat_from_to.hpp>


namespace detail{

    template 
<typename RT, int arity>
    
struct pyobject_invoker;

    template 
<typename RT> 
    
struct pyobject_invoker<RT, 0 > 
    { 
        boost::python::
object callable; 
        
        RT 
operator( )( )
        {
            
return boost::python::extract<RT>( callable( ) );
        }
    };

#define MAKE_PYOBJECT_INVOKER(z, n, data) \
    template 
<typename RT> \
    
struct pyobject_invoker<RT, n >{ \
        boost::python::
object callable; \
        template 
< BOOST_PP_ENUM_PARAMS(n, typename Arg) > \
        RT 
operator( )(BOOST_PP_ENUM_BINARY_PARAMS(n, Arg, arg ) )\
        {\
            boost::python::
object o = callable( BOOST_PP_ENUM_PARAMS(n, arg) );\
            
return boost::python::extract<RT>( o );\
        }\
    };\
    template 
<> \
    
struct pyobject_invoker<void, n >{ \
        boost::python::
object callable; \
        template 
< BOOST_PP_ENUM_PARAMS(n, typename Arg) > \
        
void operator( )(BOOST_PP_ENUM_BINARY_PARAMS(n, Arg, arg ) )\
        {\
            boost::python::
object o = callable( BOOST_PP_ENUM_PARAMS(n, arg) );\
        }\
    };
    
BOOST_PP_REPEAT_FROM_TO( 
16, MAKE_PYOBJECT_INVOKER, nothing );

#undef MAKE_PYOBJECT_INVOKER
    
    template 
<typename FT>
    boost::function
< FT > function_frompyobj( boost::python::object o )
    {
        
const int arity = boost::function_traits< FT >::arity;
        typedef 
            typename  boost::function_types::result_type
< FT >::type
            result_type;
        pyobject_invoker
<result_type, arity > inv;
        inv.callable 
= o;
        
return inv;
    }

// namespace detail

template 
<typename FT>
void def_function(const char* func_name, const char* func_doc)
{
    BOOST_STATIC_ASSERT( boost::function_types::is_function
< FT >::value ) ;
    
namespace bp = boost::python;
    typedef boost::function
<FT> function_t;
    bp::class_
< function_t >
    (func_name, func_doc, bp::no_init)
        .def(
"__call__"&function_t::operator() )
        .def(
"from_callable"&detail::function_frompyobj<FT> )
        .staticmethod(
"from_callable")
    ;
// def_function0




#endif // PY_BOOST_FUNCTION


// python_test.cpp : 定义控制台应用程序的入口点。
//

#include 
"stdafx.h"
#include 
<boost/function.hpp>
#include 
<boost/bind.hpp>
#include 
<string>

void error(){
    PyErr_Print();
    boost::python::
object sys(
      boost::python::handle
<>(PyImport_ImportModule("sys"))
    );
    boost::python::
object err = sys.attr("stderr");
    std::
string err_text = boost::python::extract<std::string>(err.attr("getvalue")());
    fprintf(stderr,
"Python Error:\n%s\n",err_text.c_str());

}
void py_init(boost::python::object& main_module,boost::python::object& main_namespace){
    Py_Initialize();
    main_module 
= boost::python::import("__main__");
    main_namespace 
= main_module.attr("__dict__");
    PyRun_SimpleString(
"import cStringIO");
    PyRun_SimpleString(
"import sys");
    PyRun_SimpleString(
"sys.stderr = cStringIO.StringIO()");

}
class Sample{
private:
    
int _value;
public:
    Sample(
int value):_value(value){}
    Sample(
const Sample& rhs):_value(rhs._value){}
    
void setValue(int value){_value=value;}
    
int  getValue(){return _value;}

};
void print_hello(const char* str){
    printf(
"%s\n",str);
}


int _tmain(int argc, _TCHAR* argv[])
{

    Sample sample(
10);
    boost::function
<void(int)> setter = boost::bind(&Sample::setValue,&sample,_1);
    boost::function
<int(void)> getter = boost::bind(&Sample::getValue,&sample);
    
    boost::python::
object main_module,main_namespace;
    py_init(main_module,main_namespace);

    def_function
<int(void)>("getter_t""A functor sample");
    def_function
<void(int)>("setter_t""A functor sample 2");
    
using namespace boost::python;
    
    
    main_namespace[
"hello"]=make_function(print_hello);
    main_namespace[
"get"]=getter;
    main_namespace[
"set"]=setter;
   
    
try
    {
        
object ignored = exec("set(20);print get()", main_namespace,main_namespace);
        printf(
"%d\n",sample.getValue());
    }
    
catch(error_already_set const &)
    {
        error();
    }
    
return 0;
}



posted on 2009-01-24 09:58 zarra 阅读(310) 评论(1)  编辑  收藏

FeedBack:
# re: 写给明年的我[未登录] 2009-01-24 20:21 apple
你都快成毕设专业户了~~
树大招风!!  回复  更多评论
  

只有注册用户登录后才能发表评论。


网站导航: