'boost::python'에 해당되는 글 1건

  1. 부스트 파이썬: 베이스 클래스 2007/08/07

부스트 파이썬: 베이스 클래스

Posted at 2007/08/07 17:34// Posted in python/pyutils
// stdafx.h : 자주 사용하지만 자주 변경되지는 않는
// 표준 시스템 포함 파일 및 프로젝트 관련 포함 파일이
// 들어 있는 포함 파일입니다.
//

#pragma once

#ifndef _WIN32_WINNT        // Windows XP 이상에서만 기능을 사용할 수 있습니다.                  
#define _WIN32_WINNT 0x0501 // 다른 버전의 Windows에 맞도록 적합한 값으로 변경해 주십시오.
#endif

#include <stdio.h>
#include <tchar.h>

#include <string>

#include <boost/python/module.hpp>
#include <boost/python/class.hpp>
#include <boost/python/pure_virtual.hpp>
#include <boost/python/return_value_policy.hpp>
#include <boost/python/manage_new_object.hpp>
#include <boost/python/def.hpp>

// boost_python.cpp : 콘솔 응용 프로그램에 대한 진입점을 정의합니다.
//

#include "stdafx.h"

class base
{
public:
    base(const char* name) : m_name(name)
    {}

    virtual ~base()
    {
        puts("del base");
    }

    void func()
    {
    }
    virtual void pure_func() = 0;

    std::string m_name;
};

class derived : public base
{
public:
    derived(const char* name) : base(name)
    {
    }
    virtual ~derived()
    {
        puts("del derived");
    }

    void pure_func()
    {
        puts(m_name.c_str());
    }
};

base* Make(const char* name)
{
    return new derived(name);
}

using namespace boost::python;

BOOST_PYTHON_MODULE(test)
{
    class_<base, boost::noncopyable>("base", no_init)
        .def("pure_func", pure_virtual(&base::pure_func))
    ;
    class_<derived, bases<base> >("derived", init<const char*>())
        .def("pure_func", &derived::pure_func)
    ;

    def("Make", Make, return_value_policy<manage_new_object>());
}


int _tmain(int argc, _TCHAR* argv[])
{
    Py_Initialize();
    inittest();
    PyRun_SimpleString("import test;obj = test.Make('abc');obj.pure_func()");
    Py_Finalize();
    return 0;
}
이올린에 북마크하기(0) 이올린에 추천하기(0)
2007/08/07 17:34 2007/08/07 17:34