使用boost::python vector_sui包装std::vector

2024-04-25 22:09:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我在使用Python绑定(使用Booo::Python)表示一个C++库,它表示存储在一个文件中的数据。我的大多数半技术用户将使用Python与之交互,因此我需要使它尽可能像Python一样。但是,我也会使用C++程序员使用API,所以我不想在C++方面妥协来适应Python绑定。

图书馆的很大一部分将由容器构成。为了让python用户更直观,我希望他们的行为类似于python列表,即:

# an example compound class
class Foo:
    def __init__( self, _val ):
        self.val = _val

# add it to a list
foo = Foo(0.0)
vect = []
vect.append(foo)

# change the value of the *original* instance
foo.val = 666.0
# which also changes the instance inside the container
print vect[0].val # outputs 666.0

测试设置

#include <boost/python.hpp>
#include <boost/python/suite/indexing/vector_indexing_suite.hpp>
#include <boost/python/register_ptr_to_python.hpp>
#include <boost/shared_ptr.hpp>

struct Foo {
    double val;

    Foo(double a) : val(a) {}
    bool operator == (const Foo& f) const { return val == f.val; }
};

/* insert the test module wrapping code here */

int main() {
    Py_Initialize();
    inittest();

    boost::python::object globals = boost::python::import("__main__").attr("__dict__");

    boost::python::exec(
        "import test\n"

        "foo = test.Foo(0.0)\n"         // make a new Foo instance
        "vect = test.FooVector()\n"     // make a new vector of Foos
        "vect.append(foo)\n"            // add the instance to the vector

        "foo.val = 666.0\n"             // assign a new value to the instance
                                        //   which should change the value in vector

        "print 'Foo =', foo.val\n"      // and print the results
        "print 'vector[0] =', vect[0].val\n",

        globals, globals
    );

    return 0;
}

方法shared_ptr

使用SysDypTR,我可以得到与上面相同的行为,但也意味着我必须用共享指针来表示C++中的所有数据,这从很多角度来看都不好。

BOOST_PYTHON_MODULE( test ) {
    // wrap Foo
    boost::python::class_< Foo, boost::shared_ptr<Foo> >("Foo", boost::python::init<double>())
        .def_readwrite("val", &Foo::val);

    // wrap vector of shared_ptr Foos
    boost::python::class_< std::vector < boost::shared_ptr<Foo> > >("FooVector")
        .def(boost::python::vector_indexing_suite<std::vector< boost::shared_ptr<Foo> >, true >());
}

在我的测试设置中,这将产生与纯Python相同的输出:

Foo = 666.0
vector[0] = 666.0

方法vector<Foo>

使用向量直接给出C++上一个漂亮的干净设置。但是,结果的行为与纯Python不同。

BOOST_PYTHON_MODULE( test ) {
    // wrap Foo
    boost::python::class_< Foo >("Foo", boost::python::init<double>())
        .def_readwrite("val", &Foo::val);

    // wrap vector of Foos
    boost::python::class_< std::vector < Foo > >("FooVector")
        .def(boost::python::vector_indexing_suite<std::vector< Foo > >());
}

这会产生:

Foo = 666.0
vector[0] = 0.0

这是“错误的”-更改原始实例不会更改容器中的值。

我希望我不要太想要

有趣的是,无论我使用哪种封装,这段代码都可以工作:

footwo = vect[0]
footwo.val = 555.0
print vect[0].val

这意味着boost::python能够处理“假共享所有权”(通过其by_proxy返回机制)。在插入新元素时,是否有任何方法可以达到相同的效果?

然而,如果答案是否定的,我很想听听其他的建议——Python工具包中是否有一个例子实现了类似的集合封装,但它的行为不像Python列表?

非常感谢您阅读本文:)


Tags: thetoinstancetestfoodefvalclass
2条回答

不幸的是,答案是不,你不能随心所欲。在python中,一切都是指针,列表是指针的容器。共享指针的C++向量工作,因为底层数据结构或多或少等同于Python列表。您所要求的是让分配的内存的C++向量像指针的指针,这是不可能完成的。

让我们看看Python列表中发生了什么,用C++等效伪代码:

foo = Foo(0.0)     # Foo* foo = new Foo(0.0)
vect = []          # std::vector<Foo*> vect
vect.append(foo)   # vect.push_back(foo)

此时,foovect[0]都指向同一个分配的内存,因此改变*foo会改变*vect[0]

现在使用vector<Foo>版本:

foo = Foo(0.0)      # Foo* foo = new Foo(0.0)
vect = FooVector()  # std::vector<Foo> vect
vect.append(foo)    # vect.push_back(*foo)

这里,vect[0]有自己分配的内存,是*foo的副本。从根本上说,不能让vect[0]与*foo是同一内存。

另外,在使用std::vector<Foo>时,请注意footwo的生存期管理:

footwo = vect[0]    # Foo* footwo = &vect[0]

后续追加可能需要移动为向量分配的存储,并可能使footwo无效(&vect[0]可能会更改)。

由于语言之间的语义差异,当涉及到集合时,通常很难将一个可重用的解决方案应用于所有场景。最大的问题是Python集合直接支持引用,C++集合需要间接的级别,例如具有^ {CD1}}元素类型。如果没有这种间接性,C++集合将不能支持与Python集合相同的功能。例如,考虑引用同一对象的两个索引:

s = Spam()
spams = []
spams.append(s)
spams.append(s)

没有指针类型的元素类型,C++集合不能有两个索引引用同一个对象。然而,根据使用和需求,可能有选项允许Python用户使用Python接口,同时仍然保持C++的一个实现。

  • 最为python的解决方案是使用自定义转换器,它将Python可重复对象转换为C++集合。有关实现的详细信息,请参见this答案。如果出现以下情况,请考虑此选项:
    • 这些收藏品的元素复制起来很便宜。
    • C++函数只在rVales类型(即^ {< CD2> }或^ {CD3}})上操作。此限制阻止C++对Python集合或其元素进行更改。
  • 增强^{}功能,尽可能多地重用功能,例如用于安全处理索引删除和重新分配基础集合的代理:
    • 使用自定义的^{}公开模型,该模型充当智能指针并委托给从vector_indexing_suite返回的实例或元素代理对象。
    • Monkey修补将元素插入到集合中的集合方法,以便将自定义HeldType设置为委托给元素代理。

向Boost.Python公开类时,HeldType是嵌入到Boost.Python对象中的对象类型。访问wrapped types对象时,Boost.Python为HeldType调用^{}。下面的object_holder类提供了将句柄返回给它拥有的实例或元素代理的能力:

/// @brief smart pointer type that will delegate to a python
///        object if one is set.
template <typename T>
class object_holder
{
public:

  typedef T element_type;

  object_holder(element_type* ptr)
    : ptr_(ptr),
      object_()
  {}

  element_type* get() const
  {
    if (!object_.is_none())
    {
      return boost::python::extract<element_type*>(object_)();
    }
    return ptr_ ? ptr_.get() : NULL;
  }

  void reset(boost::python::object object)
  {
    // Verify the object holds the expected element.
    boost::python::extract<element_type*> extractor(object_);
    if (!extractor.check()) return;

    object_ = object;
    ptr_.reset();
  }

private:
  boost::shared_ptr<element_type> ptr_;
  boost::python::object object_;
};

/// @brief Helper function used to extract the pointed to object from
///        an object_holder.  Boost.Python will use this through ADL.
template <typename T>
T* get_pointer(const object_holder<T>& holder)
{
  return holder.get();
}

在支持间接寻址的情况下,剩下的唯一事情是修补集合以设置object_holder。支持这一点的一种干净且可重用的方法是使用^{}。这是一个通用接口,允许class_对象以非侵入方式扩展。例如,vector_indexing_suite使用此功能。

monkey下面的custom_vector_indexing_suite类修补append()方法以委托给原始方法,然后使用新设置元素的代理调用object_holder.reset()。这将导致object_holder引用集合中包含的元素。

/// @brief Indexing suite that will resets the element's HeldType to
///        that of the proxy during element insertion.
template <typename Container,
          typename HeldType>
class custom_vector_indexing_suite
  : public boost::python::def_visitor<
      custom_vector_indexing_suite<Container, HeldType>>
{
private:

  friend class boost::python::def_visitor_access;

  template <typename ClassT>
  void visit(ClassT& cls) const
  {
    // Define vector indexing support.
    cls.def(boost::python::vector_indexing_suite<Container>());

    // Monkey patch element setters with custom functions that
    // delegate to the original implementation then obtain a 
    // handle to the proxy.
    cls
      .def("append", make_append_wrapper(cls.attr("append")))
      // repeat for __setitem__ (slice and non-slice) and extend
      ;
  }

  /// @brief Returned a patched 'append' function.
  static boost::python::object make_append_wrapper(
    boost::python::object original_fn)
  {
    namespace python = boost::python;
    return python::make_function([original_fn](
          python::object self,
          HeldType& value)
        {
          // Copy into the collection.
          original_fn(self, value.get());
          // Reset handle to delegate to a proxy for the newly copied element.
          value.reset(self[-1]);
        },
      // Call policies.
      python::default_call_policies(),
      // Describe the signature.
      boost::mpl::vector<
        void,           // return
        python::object, // self (collection)
        HeldType>()     // value
      );
  }
};

包装需要在运行时进行,并且不能通过def()在类上直接定义自定义函子对象,因此必须使用^{}函数。对于函子,它同时需要表示签名的CallPoliciesMPL front-extensible sequence


下面是一个完整的示例,demonstrates使用object_holder委托给代理,使用custom_vector_indexing_suite修补集合。

#include <boost/python.hpp>
#include <boost/python/suite/indexing/vector_indexing_suite.hpp>

/// @brief Mockup type.
struct spam
{
  int val;

  spam(int val) : val(val) {}
  bool operator==(const spam& rhs) { return val == rhs.val; }
};

/// @brief Mockup function that operations on a collection of spam instances.
void modify_spams(std::vector<spam>& spams)
{
  for (auto& spam : spams)
    spam.val *= 2;
}

/// @brief smart pointer type that will delegate to a python
///        object if one is set.
template <typename T>
class object_holder
{
public:

  typedef T element_type;

  object_holder(element_type* ptr)
    : ptr_(ptr),
      object_()
  {}

  element_type* get() const
  {
    if (!object_.is_none())
    {
      return boost::python::extract<element_type*>(object_)();
    }
    return ptr_ ? ptr_.get() : NULL;
  }

  void reset(boost::python::object object)
  {
    // Verify the object holds the expected element.
    boost::python::extract<element_type*> extractor(object_);
    if (!extractor.check()) return;

    object_ = object;
    ptr_.reset();
  }

private:
  boost::shared_ptr<element_type> ptr_;
  boost::python::object object_;
};

/// @brief Helper function used to extract the pointed to object from
///        an object_holder.  Boost.Python will use this through ADL.
template <typename T>
T* get_pointer(const object_holder<T>& holder)
{
  return holder.get();
}

/// @brief Indexing suite that will resets the element's HeldType to
///        that of the proxy during element insertion.
template <typename Container,
          typename HeldType>
class custom_vector_indexing_suite
  : public boost::python::def_visitor<
      custom_vector_indexing_suite<Container, HeldType>>
{
private:

  friend class boost::python::def_visitor_access;

  template <typename ClassT>
  void visit(ClassT& cls) const
  {
    // Define vector indexing support.
    cls.def(boost::python::vector_indexing_suite<Container>());

    // Monkey patch element setters with custom functions that
    // delegate to the original implementation then obtain a 
    // handle to the proxy.
    cls
      .def("append", make_append_wrapper(cls.attr("append")))
      // repeat for __setitem__ (slice and non-slice) and extend
      ;
  }

  /// @brief Returned a patched 'append' function.
  static boost::python::object make_append_wrapper(
    boost::python::object original_fn)
  {
    namespace python = boost::python;
    return python::make_function([original_fn](
          python::object self,
          HeldType& value)
        {
          // Copy into the collection.
          original_fn(self, value.get());
          // Reset handle to delegate to a proxy for the newly copied element.
          value.reset(self[-1]);
        },
      // Call policies.
      python::default_call_policies(),
      // Describe the signature.
      boost::mpl::vector<
        void,           // return
        python::object, // self (collection)
        HeldType>()     // value
      );
  }

  // .. make_setitem_wrapper
  // .. make_extend_wrapper
};

BOOST_PYTHON_MODULE(example)
{
  namespace python = boost::python;

  // Expose spam.  Use a custom holder to allow for transparent delegation
  // to different instances.
  python::class_<spam, object_holder<spam>>("Spam", python::init<int>())
    .def_readwrite("val", &spam::val)
    ;

  // Expose a vector of spam.
  python::class_<std::vector<spam>>("SpamVector")
    .def(custom_vector_indexing_suite<
      std::vector<spam>, object_holder<spam>>())
    ;

  python::def("modify_spams", &modify_spams);
}

交互使用:

>>> import example
>>> spam = example.Spam(5)
>>> spams = example.SpamVector()
>>> spams.append(spam)
>>> assert(spams[0].val == 5)
>>> spam.val = 21
>>> assert(spams[0].val == 21)
>>> example.modify_spams(spams)
>>> assert(spam.val == 42)
>>> spams.append(spam)
>>> spam.val = 100
>>> assert(spams[1].val == 100)
>>> assert(spams[0].val == 42) # The container does not provide indirection.

由于仍然使用^ {CD4>},只使用Python对象的API修改基础C++容器。例如,在容器上调用push_back可能会导致底层内存的重新分配,并导致现有Boost.Python代理出现问题。另一方面,可以安全地修改元素本身,比如通过上面的modify_spams()函数来完成。

相关问题 更多 >

    热门问题