Карта сайта Kansoftware
НОВОСТИУСЛУГИРЕШЕНИЯКОНТАКТЫ
Разработка программного обеспечения

How can I automatically convert my custom string type to and from a Python string?

Boost , Boost.Python , Chapter 4. Frequently Asked Questions (FAQs)

Boost C++ Libraries

...one of the most highly regarded and expertly designed C++ library projects in the world. Herb Sutter and Andrei Alexandrescu, C++ Coding Standards

PrevUpHomeNext

Ральф В. Гросс-Кунстлве приводит следующие примечания:

Ниже приведен небольшой автономный модуль расширения, который показывает, как это сделать. Вот соответствующий тривиальный тест:

import custom_string
assert custom_string.hello() == "Hello world."
assert custom_string.size("california") == 10

Если вы посмотрите код, вы найдете:

  • Удобный<to_python>конвертер (легкий):<custom_string_to_python_str>
  • Пользовательский конвертер lvalue (нужен больше кода):<custom_string_from_python_str>

Пользовательские преобразователи зарегистрированы в Global Boost. Реестр Python находится в верхней части функции инициализации модуля. После того, как управление потоком пройдет через регистрационный код, автоматические преобразования из и в строки Python будут работать в любом модуле, импортируемом в том же процессе.

#include <boost/python/module.hpp>
#include <boost/python/def.hpp>
#include <boost/python/to_python_converter.hpp>
namespace sandbox { namespace {
class custom_string
{
  public:
    custom_string() {}
    custom_string(std::string const &value) : value_(value) {}
    std::string const &value() const { return value_; }
  private:
    std::string value_;
};
struct custom_string_to_python_str
{
  static PyObject* convert(custom_string const &s)
  {
    return boost::python::incref(boost::python::object(s.value()).ptr());
  }
};
struct custom_string_from_python_str
{
  custom_string_from_python_str()
  {
    boost::python::converter::registry::push_back(
      &convertible,
      &construct,
      boost::python::type_id<custom_string>());
  }
  static void* convertible(PyObject* obj_ptr)
  {
    if (!PyString_Check(obj_ptr)) return 0;
    return obj_ptr;
  }
  static void construct(
    PyObject* obj_ptr,
    boost::python::converter::rvalue_from_python_stage1_data* data)
  {
    const char* value = PyString_AsString(obj_ptr);
    if (value == 0) boost::python::throw_error_already_set();
    void* storage = (
      (boost::python::converter::rvalue_from_python_storage<custom_string>*)
        data)->storage.bytes;
    new (storage) custom_string(value);
    data->convertible = storage;
  }
};
custom_string hello() { return custom_string("Hello world."); }
std::size_t size(custom_string const &s) { return s.value().size(); }
void init_module()
{
  using namespace boost::python;
  boost::python::to_python_converter<
    custom_string,
    custom_string_to_python_str>();
  custom_string_from_python_str();
  def("hello", hello);
  def("size", size);
}
}} // namespace sandbox::<anonymous>
BOOST_PYTHON_MODULE(custom_string)
{
  sandbox::init_module();
}

PrevUpHomeNext

Статья How can I automatically convert my custom string type to and from a Python string? раздела Boost.Python Chapter 4. Frequently Asked Questions (FAQs) может быть полезна для разработчиков на c++ и boost.




Материалы статей собраны из открытых источников, владелец сайта не претендует на авторство. Там где авторство установить не удалось, материал подаётся без имени автора. В случае если Вы считаете, что Ваши права нарушены, пожалуйста, свяжитесь с владельцем сайта.



:: Главная :: Chapter 4. Frequently Asked Questions (FAQs) ::


реклама


©KANSoftWare (разработка программного обеспечения, создание программ, создание интерактивных сайтов), 2007
Top.Mail.Ru

Время компиляции файла: 2024-08-30 11:47:00
2025-05-19 20:53:34/0.0087659358978271/1