a_map_indexing_suite.cpp

来自「Boost provides free peer-reviewed portab」· C++ 代码 · 共 85 行

CPP
85
字号
// Copyright Joel de Guzman 2004. Distributed under the Boost// Software License, Version 1.0. (See accompanying// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)#include <boost/python/suite/indexing/map_indexing_suite.hpp>#include <boost/python/module.hpp>#include <boost/python/def.hpp>#include <boost/python/implicit.hpp>using namespace boost::python;struct A{  int value;  A() : value(0){};  A(int v) : value(v) {};};bool operator==(const A& v1, const A& v2){  return (v1.value == v2.value);}struct B{  A a;};// Converter from A to python intstruct AToPython {  static PyObject* convert(const A& s)  {    return boost::python::incref(boost::python::object((int)s.value).ptr());  }};// Conversion from python int to Astruct AFromPython {  AFromPython()  {    boost::python::converter::registry::push_back(        &convertible,        &construct,        boost::python::type_id< A >());  }  static void* convertible(PyObject* obj_ptr)  {    if (!PyInt_Check(obj_ptr)) return 0;    return obj_ptr;  }  static void construct(      PyObject* obj_ptr,      boost::python::converter::rvalue_from_python_stage1_data* data)  {    void* storage = (        (boost::python::converter::rvalue_from_python_storage< A >*)        data)-> storage.bytes;    new (storage) A((int)PyInt_AsLong(obj_ptr));    data->convertible = storage;  }};void a_map_indexing_suite(){  to_python_converter< A , AToPython >();  AFromPython();  class_< std::map<int, A> >("AMap")    .def(map_indexing_suite<std::map<int, A>, true >())    ;  class_< B >("B")    .add_property("a", make_getter(&B::a, return_value_policy<return_by_value>()),        make_setter(&B::a, return_value_policy<return_by_value>()))    ;}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?