<html>
<head>
<style>
.hmmessage P
{
margin:0px;
padding:0px
}
body.hmmessage
{
font-size: 10pt;
font-family:Verdana
}
</style>
</head>
<body class='hmmessage'>
Hello,<br><br>I am currently working on a way to convert boost::any to/from Python/C++.<br><br>So far the idea is that for any C++ type T exported to Python via Boost.Python,<br>you also register a function that converts an instance of boost::any storing a<br>value of type T to Python as well.<br><br>In other words, if you had a function f and exported it to Python as follows:<br><br>// C++ code:<br>boost::any f() {<br> int x = 5;<br> boost::any a = x;<br> return a;<br>}<br><br>Then in Python it is possible to do this:<br><br>x = f()<br>print x<br><br>And it will print "5", the result of f() automatically gets converted into a Python<br>integer instead of an instance of boost::any storing an integer.<br><br>This part I got working, thankfully, my problem is doing the flip side, passing the value<br>5 from Python to a C++ function that takes a boost::any as a parameter and having<br>that boost::any store an int.<br><br>The problem with the solution I have now is that when I pass an arbitrary Python value<br>to a function taking a boost::any, the boost::any holds an instance of type PyObject*.<br><br>My AnyFromPython converter looks like this:<br><br>struct AnyFromPython {<br> AnyFromPython() {<br> converter::registry::push_back(&convertible, &construct, type_id<any>());<br> }<br><br> static void* convertible(PyObject* value) {<br> return value;<br> }<br><br> static void construct(PyObject* value,<br> converter::rvalue_from_python_stage1_data* data) {<br> if(value == 0) {<br> throw_error_already_set();<br> }<br> void* storage = ((converter::rvalue_from_python_storage<any>*)<br> data)->storage.bytes;<br> new (storage) any(value);<br> data->convertible = storage;<br> }<br>};<br><br><br>The question is... is it possible to convert the parameter "value" into what it<br>would have otherwise been converted to in C++, and then take that result and<br>wrap it with a boost::any, as opposed to what it's doing now, which is just<br>wrapping a raw PyObject*.<br><br>If I could do this, then boost::any would work seamlessly with Python, and users<br>of my library would never need to know that boost::any is being used behind the<br>scenes in the C++ world.<br><br>Any help would be appreciated,<br>Kamal<br><br /><hr />Attention all humans. We are your photos. <a href='http://go.microsoft.com/?linkid=9666044' target='_new'>Free us.</a></body>
</html>