On 2009-06-23, Miklos Vajna <
vmiklos@...> wrote:
> On Tue, Jun 23, 2009 at 10:01:34AM -0400, Andres Gonzalez <
andres@...> wrote:
>> Does SWIG support this? How do I format my data in the C/C++
>> domain so that I can get associative arrays in the PHP domain?
>
> I haven't tried it myself, but looking at the contents of the Lib/php
> directory, I would try just returning an
> std::map<int,std::vector<std::string>>.
Another approach is to write a SWIG typemap which builds the PHP associative
array using the PHP C API. I don't have an example for associative arrays to
hand, but here is one which returns a PHP array for a C function which
returns a pair of C++ iterators:
%typemap(out) std::pair<Xapian::TermIterator, Xapian::TermIterator> {
if (array_init($result) == FAILURE) {
SWIG_PHP_Error(E_ERROR, "array_init failed");
}
for (Xapian::TermIterator i = $1.first; i != $1.second; ++i) {
/* We have to cast away const here because the PHP API is rather
* poorly thought out - really there should be two API methods
* one of which takes a const char * and copies the string and
* the other which takes char * and takes ownership of the string.
*
* Passing 1 as the last parameter of add_next_index_stringl() tells
* PHP to copy the string pointed to by p, so it won't be modified.
*/
string term = *i;
char *p = const_cast<char*>(term.data());
add_next_index_stringl($result, p, term.length(), 1);
}
}
Cheers,
Olly
------------------------------------------------------------------------------
_______________________________________________
Swig-user mailing list
Swig-user@...
https://lists.sourceforge.net/lists/listinfo/swig-user