Please note that I am not a Python/PHP expert but I need to solve C++ to PHP case on overloaded constructors.
I have C++ code like:
enum Enum_Foo {
Foo_A.
Foo_B,
Foo_C
};
enum Enum_Bar {
Bar_A.
Bar_B,
Bar_C
};
class Creator
{
public:
Creator();
Creator(Enum_Foo);
Creator(Enum_Bar);
};
SWIG complains on overloaded methods and it is ok. Recomendation is to use %rename
for the second method (constructor) and I do that as:
Python.i
:
%rename (CreatorFromBar) Creator::Creator(Enum_Bar);
The Python result is a function (reworked a bit from original):
def CreatorFromBar(in_enum_bar):
val = _SomeNamespacePython.new_CreatorFromBar(in_enum_bar)
return val
which could be used in final Python code as:
bar = Enum_Bar.Bar_A
creator = CreatorFromBar(bar)
But PHP is a different case:
PHP.i
:
%rename (CreateFromBar) Creator::Creator(Enum_Bar);
The PHP result is a class method (reworked a bit from original):
class Creator {
...
function CreateFromBar($in_enum_bar_type) {
...
return new Creator(new_CreateFromBar($in_enum_bar_type));
}
...
}
which could be used in final PHP code only as:
$enum_bar = Enum_Bar::Bar_A;
$dummy = new Creator();
$correct = $dummy->CreatorFromBar($enum_bar);
If the SWIG result was with static
qualifier - it would be called as:
$enum_bar = Enum_Bar::Bar_A;
$correct = Creator::CreatorFromBar($enum_bar);
Questions:
- Is there any way to force
static
in front of PHP function SWIG result? - Is there any other way to avoid
dummy
instance?
Please note that I am not a Python/PHP expert but I need to solve C++ to PHP case on overloaded constructors.
I have C++ code like:
enum Enum_Foo {
Foo_A.
Foo_B,
Foo_C
};
enum Enum_Bar {
Bar_A.
Bar_B,
Bar_C
};
class Creator
{
public:
Creator();
Creator(Enum_Foo);
Creator(Enum_Bar);
};
SWIG complains on overloaded methods and it is ok. Recomendation is to use %rename
for the second method (constructor) and I do that as:
Python.i
:
%rename (CreatorFromBar) Creator::Creator(Enum_Bar);
The Python result is a function (reworked a bit from original):
def CreatorFromBar(in_enum_bar):
val = _SomeNamespacePython.new_CreatorFromBar(in_enum_bar)
return val
which could be used in final Python code as:
bar = Enum_Bar.Bar_A
creator = CreatorFromBar(bar)
But PHP is a different case:
PHP.i
:
%rename (CreateFromBar) Creator::Creator(Enum_Bar);
The PHP result is a class method (reworked a bit from original):
class Creator {
...
function CreateFromBar($in_enum_bar_type) {
...
return new Creator(new_CreateFromBar($in_enum_bar_type));
}
...
}
which could be used in final PHP code only as:
$enum_bar = Enum_Bar::Bar_A;
$dummy = new Creator();
$correct = $dummy->CreatorFromBar($enum_bar);
If the SWIG result was with static
qualifier - it would be called as:
$enum_bar = Enum_Bar::Bar_A;
$correct = Creator::CreatorFromBar($enum_bar);
Questions:
- Is there any way to force
static
in front of PHP function SWIG result? - Is there any other way to avoid
dummy
instance?
1 Answer
Reset to default 0I found a way by creating a global function at the end of PHP.i
:
First set overloaded constructor to be ignored (not necessary but prevents warning):
%ignore SomeNamespace::Creator(Enum_Bar);
and then define a global function:
%inline %{
namespace SomeNamespace {
Creator* CreatorFromBar(Enum_Bar in_enum_bar) {
return new Creator(in_enum_bar);
}
}
%}
And the call is as:
$enum_bar = Enum_Bar::Bar_A; // May come via argument of a function
$correct = CreatorFromBar($enum_bar);
PS: It isn't answer to the question but solves my problem. It works for PHP and Ruby. I will apply for Python too - in order to have consistent code.
$dummy
by just writing it inline:$correct = (new Creator())->CreatorFromBar($enum_bar);
But I think the "how can I make itstatic
?" part, to avoid the extra constructor call completely, is more important, so won't leave this as an answer. – IMSoP Commented Mar 10 at 16:57