os/ossrv/ossrv_pub/boost_apis/boost/python/converter/implicit.hpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
     1 // Copyright David Abrahams 2002.
     2 // Distributed under the Boost Software License, Version 1.0. (See
     3 // accompanying file LICENSE_1_0.txt or copy at
     4 // http://www.boost.org/LICENSE_1_0.txt)
     5 #ifndef IMPLICIT_DWA2002326_HPP
     6 # define IMPLICIT_DWA2002326_HPP
     7 
     8 # include <boost/python/converter/rvalue_from_python_data.hpp>
     9 # include <boost/python/converter/registrations.hpp>
    10 # include <boost/python/converter/registered.hpp>
    11 
    12 # include <boost/python/extract.hpp>
    13 
    14 namespace boost { namespace python { namespace converter { 
    15 
    16 template <class Source, class Target>
    17 struct implicit
    18 {
    19     static void* convertible(PyObject* obj)
    20     {
    21         // Find a converter which can produce a Source instance from
    22         // obj. The user has told us that Source can be converted to
    23         // Target, and instantiating construct() below, ensures that
    24         // at compile-time.
    25         return implicit_rvalue_convertible_from_python(obj, registered<Source>::converters)
    26             ? obj : 0;
    27     }
    28       
    29     static void construct(PyObject* obj, rvalue_from_python_stage1_data* data)
    30     {
    31         void* storage = ((rvalue_from_python_storage<Target>*)data)->storage.bytes;
    32 
    33         arg_from_python<Source> get_source(obj);
    34         bool convertible = get_source.convertible();
    35         BOOST_ASSERT(convertible);
    36         
    37         new (storage) Target(get_source());
    38         
    39         // record successful construction
    40         data->convertible = storage;
    41     }
    42 };
    43 
    44 }}} // namespace boost::python::converter
    45 
    46 #endif // IMPLICIT_DWA2002326_HPP