Re: [C++-sig] Call Policy for Singleton

2009-02-13 Thread John Reid

ZaeX wrote:
 And in my opinion, you'd better be careful if you expect that the c++ 
code and python code are using the same singleton instance.
Because there're two Singleton::_instance if you export the Singleton 
class to Python. one at c++ side, the other at python side.

I don't think this is true. What makes you think this?

John.

___
Cplusplus-sig mailing list
[email protected]
http://mail.python.org/mailman/listinfo/cplusplus-sig


[C++-sig] Returning PyObject and return_internal_reference<>

2009-02-13 Thread Václav Haisman
Hi,
I am trying to return a new Python buffer that is a view to internals of
my Message object. My understanding is that in such case, I should use
return_internal_reference<> to make the return value hold reference to
the Message class because of this bit of PyBuffer_FromMemory() docs:
"The caller is responsible for ensuring that the memory buffer, passed
in as ptr, is not deallocated while the returned buffer object exists."

The problem is that whatever approach I could think of results into
uncompilable code. (See the attached file.) I am using Boost 1.37.0.

Is there any other way that I can do this?

--
VH
#define BOOST_PYTHON_STATIC_MODULE
#include 
#include 

namespace py = boost::python;


struct Message
{
char const * data () const { return buf; }
size_t size () const { return 10; }
char buf[10];
};


// error C2027: use of undefined type
// 
'boost::python::detail::reference_existing_object_requires_a_pointer_or_reference_return_type
py::object
get_buffer_impl_1 (boost::shared_ptr const & msg)
{
py::handle<> h (
::PyBuffer_FromMemory (
const_cast(msg->data ()),
msg->size ()));
py::object o (h);
return o;
}


// pointer_holder.hpp(176) : error C2535:
// 
'boost::python::objects::pointer_holder::pointer_holder(Pointer)'
 :
// member function already defined or declared
::PyObject *
get_buffer_impl_2 (boost::shared_ptr const & msg)
{
return ::PyBuffer_FromMemory (
const_cast(msg->data ()),
msg->size ());
}


void
dec_ref (::PyObject * o)
{
Py_XDECREF (o);
}


// Same error as the 1st attempt:
// error C2027: use of undefined type
// 
'boost::python::detail::reference_existing_object_requires_a_pointer_or_reference_return_type'
boost::shared_ptr< ::PyObject>
get_buffer_impl_3 (boost::shared_ptr const & msg)
{
return boost::shared_ptr< ::PyObject> (
::PyBuffer_FromMemory (
const_cast(msg->data ()),
msg->size ()),
dec_ref);
}


BOOST_PYTHON_MODULE(M)
{
py::class_
("Message", py::no_init)
.def ("get_buffer_1", &get_buffer_impl_1,
py::return_internal_reference<1> ())
.def ("get_buffer_2", &get_buffer_impl_2,
py::return_internal_reference<1> ())
.def ("get_buffer_3", &get_buffer_impl_3,
py::return_internal_reference<1> ())
;
}


smime.p7s
Description: S/MIME Cryptographic Signature
___
Cplusplus-sig mailing list
[email protected]
http://mail.python.org/mailman/listinfo/cplusplus-sig

Re: [C++-sig] Call Policy for Singleton

2009-02-13 Thread ZaeX
When the Singleton class is linked, the DLL will get the
Singleton::_instance as an unique static variable.
Well, In my last attempt, it appeared to be like that.

On Fri, Feb 13, 2009 at 5:33 PM, John Reid wrote:

> ZaeX wrote:
>
>>  And in my opinion, you'd better be careful if you expect that the c++
>> code and python code are using the same singleton instance.
>> Because there're two Singleton::_instance if you export the Singleton
>> class to Python. one at c++ side, the other at python side.
>>
> I don't think this is true. What makes you think this?
>
> John.
>
>
> ___
> Cplusplus-sig mailing list
> [email protected]
> http://mail.python.org/mailman/listinfo/cplusplus-sig
>



-- 
Time is mana, we must hurry
___
Cplusplus-sig mailing list
[email protected]
http://mail.python.org/mailman/listinfo/cplusplus-sig