In Math::GMPz, the overloading of '++' is:
void overload_inc(pTHX_ SV * p, SV * a, SV * b) {
mpz_add_ui(*(INT2PTR(mpz_t *, SvIVX(SvRV(p)))), *(INT2PTR(mpz_t *
+, SvIVX(SvRV(p)))), 1);
}
which is simply doing p = p + 1, with "p" being a Math::GMPz object.
Then, in the bottom section of the XS file I have:
void
overload_inc (p, a, b)
SV * p
SV * a
SV * b
CODE:
overload_inc(aTHX_ p, a, b);
XSRETURN_EMPTY; /* return empty stack */
Works fine.
For the overloading of '+=', it gets a bit drawn out because we might be adding on something other than an IV.
But if we were just dealing with adding on an unsigned long int we would actually have:
SV * overload_add_eq(pTHX_ SV * p, SV * a, SV * b) {
mpz_add_ui(*(INT2PTR(mpz_t *, SvIVX(SvRV(p)))), *(INT2PTR(mpz_t *
+, SvIVX(SvRV(p)))), SvUV(a));
SvREFCNT_inc(p);
return p;
}
which is just doing p = p + a, and returning p (after increasing the refcount).
The corresponding code in the bottom section of the XS file would be:
SV *
overload_add_eq (p, a, b)
SV * p
SV * a
SV * b
CODE:
RETVAL = overload_add_eq (aTHX_ p, a, b);
OUTPUT: RETVAL
Now, it's not hard to work out what that overload_add_eq() would look like if I were to apply the same approach as used by overload_inc():
void overload_add_eq(pTHX_ SV * p, SV * a, SV * b) {
mpz_add_ui(*(INT2PTR(mpz_t *, SvIVX(SvRV(p)))), *(INT2PTR(mpz_t *
+, SvIVX(SvRV(p)))), SvUV(a));
}
and
void
overload_add_eq (p, second, third)
SV * p
SV * a
SV * b
CODE:
overload_add_eq(aTHX_ p, a, b);
XSRETURN_EMPTY; /* return empty stack */
But I've never managed to get that to work.
It has, however, been a while since I've tried - so I'll have another crack at it tomorrow (as it's now getting late over here) and see if I can get lucky.
(I can't actually remember just what the problem is.)
BTW, I've fiddled about with that code that I've posted, so it might contain one or more typos.
Cheers,
Rob
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.