add C++ operator aliases

git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@7924 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
Marcelo Matus 2005-12-04 05:08:50 +00:00
commit db923a855e
2 changed files with 73 additions and 45 deletions

View file

@ -4,6 +4,7 @@
#ifdef __cplusplus #ifdef __cplusplus
%define %pybinoperator(pyname,oper) %define %pybinoperator(pyname,oper)
%rename(pyname) oper; %rename(pyname) oper;
%pythonmaybecall oper; %pythonmaybecall oper;
@ -30,31 +31,6 @@
%pybinoperator(__eq__, *::operator==); %pybinoperator(__eq__, *::operator==);
%pybinoperator(__ne__, *::operator!=); %pybinoperator(__ne__, *::operator!=);
%define %pybinoperation(oper)
%pythonmaybecall __ ## oper ## __;
%pythonmaybecall __r ## oper ## __;
%enddef
%pybinoperation(add);
%pybinoperation(pos);
%pybinoperation(pos);
%pybinoperation(sub);
%pybinoperation(neg);
%pybinoperation(neg);
%pybinoperation(mul);
%pybinoperation(div);
%pybinoperation(mod);
%pybinoperation(lshift);
%pybinoperation(rshift);
%pybinoperation(and);
%pybinoperation(or);
%pybinoperation(xor);
%pybinoperation(lt);
%pybinoperation(le);
%pybinoperation(gt);
%pybinoperation(ge);
%pybinoperation(eq);
%pybinoperation(ne);
/* Special cases */ /* Special cases */
@ -63,19 +39,14 @@
/* Ignored operators */ /* Ignored operators */
%ignorewarn("361:operator! ignored") operator!; %ignorewarn("361:operator! ignored") operator!;
%ignorewarn("361:operator not ignored") operator not;
%ignorewarn("381:operator&& ignored") operator&&; %ignorewarn("381:operator&& ignored") operator&&;
%ignorewarn("381:operator and ignored") operator and;
%ignorewarn("382:operator|| ignored") operator||; %ignorewarn("382:operator|| ignored") operator||;
%ignorewarn("382:operator or ignored") operator or;
%ignorewarn("362:operator= ignored") *::operator=; %ignorewarn("362:operator= ignored") *::operator=;
%ignorewarn("383:operator++ ignored") *::operator++; %ignorewarn("383:operator++ ignored") *::operator++;
%ignorewarn("384:operator-- ignored") *::operator--; %ignorewarn("384:operator-- ignored") *::operator--;
%ignorewarn("386:operator->* ignored") *::operator->*; %ignorewarn("386:operator->* ignored") *::operator->*;
%ignorewarn("389:operator[] ignored (consider using %extend)") *::operator[]; %ignorewarn("389:operator[] ignored (consider using %extend)") *::operator[];
/* /*
Inplace operator declarations. Inplace operator declarations.
@ -115,21 +86,70 @@
*/ */
%define %swig_inplace_oper(Oper, PyOper) %define %pyinplaceoper(PyOper, Oper)
%feature("self:disown") *::Oper; %feature("self:disown") Oper;
%feature("new") *::Oper; %feature("new") Oper;
%rename(__##PyOper##__) *::Oper; %rename(PyOper) Oper;
%enddef %enddef
%swig_inplace_oper(operator +=, iadd); %pyinplaceoper(__iadd__ , *::operator +=);
%swig_inplace_oper(operator -=, isub); %pyinplaceoper(__isub__ , *::operator -=);
%swig_inplace_oper(operator *=, imul); %pyinplaceoper(__imul__ , *::operator *=);
%swig_inplace_oper(operator /=, idiv); %pyinplaceoper(__idiv__ , *::operator /=);
%swig_inplace_oper(operator %=, imod); %pyinplaceoper(__imod__ , *::operator %=);
%swig_inplace_oper(operator &=, iand); %pyinplaceoper(__iand__ , *::operator &=);
%swig_inplace_oper(operator |=, ior); %pyinplaceoper(__ior__ , *::operator |=);
%swig_inplace_oper(operator ^=, ixor); %pyinplaceoper(__ixor__ , *::operator ^=);
%swig_inplace_oper(operator <<=, ilshift); %pyinplaceoper(__ilshift__, *::operator <<=);
%swig_inplace_oper(operator >>=, irshift); %pyinplaceoper(__irshift__, *::operator >>=);
/* C++ operator aliases */
%ignorewarn("381:operator and ignored") operator and; // `and' `&&'
%ignorewarn("361:operator not ignored") operator not; // `not' `!'
%ignorewarn("382:operator or ignored") operator or; // `or' `||'
%pyinplaceoper(__iand__, *::operator and_eq); // `and_eq' `&='
%pybinoperator(__and__, *::operator bitand) // `bitand' `&'
%pybinoperator(__or__, *::operator bitor); // `bitor' `|'
%rename(__invert__) *::operator compl; // `compl' `~'
%pybinoperator(__ne__, *::operator not_eq); // `not_eq' `!='
%pyinplaceoper(__ior__, *::operator or_eq); // `or_eq' `|='
%pybinoperator(__xor__, *::operator xor); // `xor' `^'
%pyinplaceoper(__ixor__, *::operator xor_eq); // `xor_eq' `^='
/* Finally, in python we need to mark the binary operations to fail as
'maybecall' methods */
%define %pybinopermaybecall(oper)
%pythonmaybecall __ ## oper ## __;
%pythonmaybecall __r ## oper ## __;
%enddef
%pybinopermaybecall(add);
%pybinopermaybecall(pos);
%pybinopermaybecall(pos);
%pybinopermaybecall(sub);
%pybinopermaybecall(neg);
%pybinopermaybecall(neg);
%pybinopermaybecall(mul);
%pybinopermaybecall(div);
%pybinopermaybecall(mod);
%pybinopermaybecall(lshift);
%pybinopermaybecall(rshift);
%pybinopermaybecall(and);
%pybinopermaybecall(or);
%pybinopermaybecall(xor);
%pybinopermaybecall(lt);
%pybinopermaybecall(le);
%pybinopermaybecall(gt);
%pybinopermaybecall(ge);
%pybinopermaybecall(eq);
%pybinopermaybecall(ne);
#endif #endif

View file

@ -1240,8 +1240,16 @@ int yylex(void) {
|| (strcmp(t,"new[]") == 0) || (strcmp(t,"new[]") == 0)
|| (strcmp(t,"delete[]") == 0) || (strcmp(t,"delete[]") == 0)
|| (strcmp(t,"and") == 0) || (strcmp(t,"and") == 0)
|| (strcmp(t,"or") == 0) || (strcmp(t,"and_eq") == 0)
|| (strcmp(t,"bitand") == 0)
|| (strcmp(t,"bitor") == 0)
|| (strcmp(t,"compl") == 0)
|| (strcmp(t,"not") == 0) || (strcmp(t,"not") == 0)
|| (strcmp(t,"not_eq") == 0)
|| (strcmp(t,"or") == 0)
|| (strcmp(t,"or_eq") == 0)
|| (strcmp(t,"xor") == 0)
|| (strcmp(t,"xor_eq") == 0)
)) { )) {
/* retract(strlen(t));*/ /* retract(strlen(t));*/
retract(count); retract(count);