[ Web Proxy ]
URL:
Viewing: https://raw.githubusercontent.com/shelltdf/python/travis/example/std_pair.cpp [Back]  [Original]

// Copyright Ralf W. Grosse-Kunstleve 2002-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 
#include 
#include 
#include 

namespace { // Avoid cluttering the global namespace.

  // Converts a std::pair instance to a Python tuple.
  template 
  struct std_pair_to_tuple
  {
    static PyObject* convert(std::pair const& p)
    {
      return boost::python::incref(
        boost::python::make_tuple(p.first, p.second).ptr());
    }
    static PyTypeObject const *get_pytype () {return &PyTuple_Type; }
  };

  // Helper for convenience.
  template 
  struct std_pair_to_python_converter
  {
    std_pair_to_python_converter()
    {
      boost::python::to_python_converter<
        std::pair,
        std_pair_to_tuple,
        true //std_pair_to_tuple has get_pytype
        >();
    }
  };

  // Example function returning a std::pair.
  std::pair
  foo() { return std::pair(3, 5); }

} // namespace anonymous

BOOST_PYTHON_MODULE(std_pair_ext)
{
  using namespace boost::python;
  std_pair_to_python_converter();
  def("foo", foo);
}

Web Proxy Viewer  |  New URL  |  Original Page