Reputation: 1341
I have a C++ function
Version getVersion(){ return Version("1.2.3.5");}
where Version is a class, holding "Version information", e.g.
class Version
{
public:
Version(const string& version = gEmptyString);
bool parse(const string& input);
int getMajor() const;
int getMinor() const;
int getPatch() const;
int getBuild() const;
string asString(const string& format = gEmptyString) const;
private:
int mMajor;
int mMinor;
int mPatch;
int mBuild;
};
When wrapping this code using Swig, a Python user get a Version object back when calling the getVersion() function.
I would want to change the behavior of the getVersion() function when called from Python. Instead of returning a Version object, I would want to return a string, representing the Version value.
I tried the following:
%rename(getVersionHidden) getVersion;
%inline %{
std::string getVersion()
{
Version v = getVersionHidden();
return v.asString();
}
%}
but that does not compile:
Error ... Call to undefined function 'getVersionHidden' in function
getVersion()
Error E2285 P:\builds\dsl\wrappers\python\dslPYTHON_wrap.cxx 4434: Could not
find a match for 'Version::Version(const Version&)' in function getVersion()
Error E2015 P:\builds\dsl\wrappers\python\dslPYTHON_wrap.cxx 16893: Ambiguity
between 'dsl::getVersion() at P:/libs/dsl/Common\dslCommon.h:8' and
'getVersion() at P:\builds\dsl\wrappers\python\dslPYTHON_wrap.cxx:4432' in
function _wrap_getVersionHidden(_object *,_object *)
Perhaps usage of a typemap is the way to go. I'm new to Swig so not sure..?
Upvotes: 1
Views: 299
Reputation: 1670
%rename only renames the function for the target language -- that is, %rename("getVersionHidden") getVersion;
will create a Python function (getVersionHidden) which forwards the getVersion() defined in C/C++.
Instead, you should create a new function and then rename that to override the getVersion that would otherwise get generated automatically:
%rename("getVersion") _getVersion_Swig;
%inline %{
std::string _getVersion_Swig()
{
Version v = getVersion();
return v.asString();
}
%}
Upvotes: 1