pyplusplus.code_repository.ctypes_integration

Package pyplusplus :: Package code_repository :: Module ctypes_integration |

# Source Code for Module pyplusplus.code_repository.ctypes_integration

      1  # Copyright 2004-2008 Roman Yakovenko.
      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
      6  """
      7  This file contains C++ code needed to export one dimensional static arrays.
      8  """
      9
     10
     11  namespace = "pyplusplus::convenience"
     12
     13  file_name = "__ctypes_integration.pypp.hpp"
     14
     15  code = \
     16  """// Copyright 2004-2008 Roman Yakovenko.
     17  // Distributed under the Boost Software License, Version 1.0. (See
     18  // accompanying file LICENSE_1_0.txt or copy at
     19  // http://www.boost.org/LICENSE_1_0.txt)
     20
     21  #ifndef __ctypes_integration_pyplusplus_hpp__
     22  #define __ctypes_integration_pyplusplus_hpp__
     23
     24  #include "boost/python.hpp"
     25  #include "boost/utility/addressof.hpp"
     26  #include "boost/mpl/vector.hpp"
     27  #include "boost/function.hpp"
     28  #include "boost/cstdint.hpp"
     29  #include "boost/type.hpp"
     30  #include "boost/bind.hpp"
     31
     32
     33  namespace pyplusplus{ namespace convenience{
     34
     35  template< typename TType, typename TMemVarType >
     36  boost::uint32_t
     37  addressof( const TType* inst_ptr, const TMemVarType TType::* offset){
     38      if( !inst_ptr ){
     39          throw std::runtime_error( "unable to dereference null pointer" );
     40      }
     41      const TType& inst = *inst_ptr;
     42      return boost::uint32_t( boost::addressof( inst.*offset ) );
     43  }
     44
     45  template< typename TType >
     46  boost::uint32_t
     47  addressof_inst( const TType* inst_ptr){
     48      if( !inst_ptr ){
     49          throw std::runtime_error( "unable to dereference null pointer" );
     50      }
     51
     52      return boost::uint32_t( inst_ptr );
     53  }
     54
     55  template< typename TType, typename TMemVarType >
     56  boost::python::object
     57  make_addressof_getter( const TMemVarType TType::* offset ){
     58      namespace bpl = boost::python;
     59      namespace pyppc = pyplusplus::convenience;
     60      return bpl::make_function( boost::bind( &pyppc::addressof< TType, TMemVarType >, _1, offset )
     61                                 , bpl::default_call_policies()
     62                                 , boost::mpl::vector< boost::uint32_t, const TType* >() );
     63  }
     64
     65  template< typename TType >
     66  boost::python::object
     67  make_addressof_inst_getter(){
     68      namespace bpl = boost::python;
     69      namespace pyppc = pyplusplus::convenience;
     70      return bpl::make_function( boost::bind( &pyppc::addressof_inst< TType >, _1 )
     71                                 , bpl::default_call_policies()
     72                                 , boost::mpl::vector< boost::uint32_t, const TType* >() );
     73  }
     74
     75  class register_addressof_static_var : public boost::python::def_visitor<register_addressof_static_var>
     76  {
     77      friend class boost::python::def_visitor_access;
     78
     79  public:
     80
     81      template< typename TVarType >
     82      register_addressof_static_var( const char* name, const TVarType& var )
     83      : m_name( name )
     84        , m_address( addressof_inst( boost::addressof( var ) ) )
     85      {}
     86
     87      template <class classT>
     88      void visit(classT& c) const{
     89          boost::python::scope cls_scope( c );
     90          cls_scope.attr(m_name) = m_address;
     91      }
     92
     93  private:
     94      boost::uint32_t m_address;
     95      const char* m_name;
     96  };
     97
     98
     99  class register_sizeof : public boost::python::def_visitor<register_sizeof>
    100  {
    101      friend class boost::python::def_visitor_access;
    102
    103  public:
    104
    105      template< typename TType >
    106      register_sizeof( boost::type< TType > )
    107      : m_sizeof( sizeof( TType ) )
    108      {}
    109
    110      template <class classT>
    111      void visit(classT& c) const{
    112          boost::python::scope cls_scope( c );
    113          cls_scope.attr("sizeof") = m_sizeof;
    114      }
    115
    116  private:
    117      size_t m_sizeof;
    118  };
    119
    120
    121  } /*pyplusplus*/ } /*convenience*/
    122
    123  namespace pyplus_conv = pyplusplus::convenience;
    124
    125  #endif//__ctypes_integration_pyplusplus_hpp__
    126
    127  """
    128