<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>&nbsp; int x = 5;<br>&nbsp; boost::any a = x;<br>&nbsp; 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>&nbsp; AnyFromPython() {<br>&nbsp;&nbsp;&nbsp; converter::registry::push_back(&amp;convertible, &amp;construct, type_id&lt;any&gt;());<br>&nbsp; }<br><br>&nbsp; static void* convertible(PyObject* value) {<br>&nbsp;&nbsp;&nbsp; return value;<br>&nbsp; }<br><br>&nbsp; static void construct(PyObject* value,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; converter::rvalue_from_python_stage1_data* data) {<br>&nbsp;&nbsp;&nbsp; if(value == 0) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; throw_error_already_set();<br>&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp; void* storage = ((converter::rvalue_from_python_storage&lt;any&gt;*)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; data)-&gt;storage.bytes;<br>&nbsp;&nbsp;&nbsp; new (storage) any(value);<br>&nbsp;&nbsp;&nbsp; data-&gt;convertible = storage;<br>&nbsp; }<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>