SWIG: Perl & C++: string conversions

View: New views
2 Messages — Rating Filter:   Alert me  

SWIG: Perl & C++: string conversions

by Andy Karseras :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,

I am calling a C++ method (from perl) which returns a populated map of strings.

The method returns with the map correctly populated although I am
unable to assign the strings to a perl type ( if I print the returned
type, I get _p_std__string=SCALAR(0x87759c4) ).

I am using std_string.i and have defined a typemap in the interface
file as detailed in example in
http://www.swig.org/Doc1.3/Library.html, section 8.4.1.

If I create the struct and try to set it's value to one of the strings
in the map, I get:-

                              Type error in argument 2 of
my_struct_foo_set. Expected a string


I don't have a lot of experience with C++ and (obviously) a newbie to
SWIG so I'm guessing it's just something dumb.

Any ideas ?

Thanks.


Andy


Interface File
------------------

%module ConfigurationClient

%include "std_string.i"
%include "std_vector.i"
%include "std_map.i"

%template(IntVector) std::vector<int>;
%template(StringMap) std::map<std::string, std::string>;
%template(StringVector) std::vector<std::string>;

%apply const std::string& {std::string* foo};

struct my_struct
{
  std::string foo;
};


Perl Code
-------------

....
# Create map to be passed in
my $parameterNameValuesIn = ConfigurationClientc::new_StringMap();

# Pass in params
$parameterNameValuesIn =
ConfigurationClientc::ConfigurationClient_getParameterValues($self,
$parameterNameList, $parameterNameValuesIn);

# Assign the string
my $aString = ConfigurationClientc::new_my_struct();
ConfigurationClientc::my_struct_foo_set($aString,
ConfigurationClientc::StringMap_get($parameterNameValuesIn, "aKey"));
....


C++ Code (From the ConfigurationClien.h file)
--------------

...
typedef struct my_struct
{
  std::string foo;
};
...

------------------------------------------------------------------------------
Crystal Reports - New Free Runtime and 30 Day Trial
Check out the new simplified licensing option that enables unlimited
royalty-free distribution of the report engine for externally facing
server and web deployment.
http://p.sf.net/sfu/businessobjects
_______________________________________________
Swig-user mailing list
Swig-user@...
https://lists.sourceforge.net/lists/listinfo/swig-user

Re: SWIG: Perl & C++: string conversions

by wsfulton :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Andy Karseras wrote:

> Hi,
>
> I am calling a C++ method (from perl) which returns a populated map of
> strings.
>
> The method returns with the map correctly populated although I am
> unable to assign the strings to a perl type ( if I print the returned
> type, I get _p_std__string=SCALAR(0x87759c4) ).
>
> I am using std_string.i and have defined a typemap in the interface
> file as detailed in example in
> http://www.swig.org/Doc1.3/Library.html, section 8.4.1.
>
> If I create the struct and try to set it's value to one of the strings
> in the map, I get:-
>
>                               Type error in argument 2 of
> my_struct_foo_set. Expected a string
>
>
> I don't have a lot of experience with C++ and (obviously) a newbie to
> SWIG so I'm guessing it's just something dumb.
>
> Any ideas ?
>
> Thanks.
>
>
> Andy
>
>
> Interface File
> ------------------
>
> %module ConfigurationClient
>
> %include "std_string.i"
> %include "std_vector.i"
> %include "std_map.i"
>
> %template(IntVector) std::vector<int>;
> %template(StringMap) std::map<std::string, std::string>;
> %template(StringVector) std::vector<std::string>;
>
> %apply const std::string& {std::string* foo};
>
> struct my_struct
> {
>   std::string foo;
> };
>
>
> Perl Code
> -------------
>
> ....
> # Create map to be passed in
> my $parameterNameValuesIn = ConfigurationClientc::new_StringMap();
>
> # Pass in params
> $parameterNameValuesIn =
> ConfigurationClientc::ConfigurationClient_getParameterValues($self,
> $parameterNameList, $parameterNameValuesIn);
>
> # Assign the string
> my $aString = ConfigurationClientc::new_my_struct();
> ConfigurationClientc::my_struct_foo_set($aString,
> ConfigurationClientc::StringMap_get($parameterNameValuesIn, "aKey"));
> ....
>
>
> C++ Code (From the ConfigurationClien.h file)
> --------------
>
> ...
> typedef struct my_struct
> {
>   std::string foo;
> };
> ...
>
>

I'm not sure what you are doing wrong. I tried your code and get
sensible output. I had modify your code as you didn't provide any info
on what getParameterValues is. Here is my version which works great, it
prints out:

s: aValue



use ConfigurationClient;

my $parameterNameValuesIn = ConfigurationClientc::new_StringMap();
ConfigurationClientc::StringMap_set($parameterNameValuesIn, "aKey",
"aValue");

# Pass in params
#    parameterNameValuesIn =
ConfigurationClient_getParameterValues(self, parameterNameList,
parameterNameValuesIn);

# Assign the string
my $aString = ConfigurationClientc::new_my_struct();
ConfigurationClientc::my_struct_foo_set($aString,
ConfigurationClientc::StringMap_get($parameterNameValuesIn, "aKey"));
my $s = ConfigurationClientc::my_struct_foo_get($aString);
print "s: ", $s, "\n";

William

------------------------------------------------------------------------------
_______________________________________________
Swig-user mailing list
Swig-user@...
https://lists.sourceforge.net/lists/listinfo/swig-user