Hi Monks, I am trying to use Boost::variant in XS. However I was not able to pass variant variable back and forth between Perl and C++. Would you please help? Any suggestion is greatly appreciated. My code is pasted below
typedef boost::variant<macro,module> ref_var; macro* nDB::create_macro(char* myName) { macro* tmp_macro = new macro; tmp_macro->set_name(myName); if (_BREF->insert(std::make_pair(myName,*tmp_macro)).second) { printf("Insert macro %s OK!\n",myName); } else { printf("Could not insert macro %s into database!\n",myName); } return (tmp_macro); } ref_var* nDB::lookup_ref(char* myName) { boost::unordered_map<char*,ref_var,myhash,cmp_str>::iterator _ITER; if ((_ITER = _BREF->find(myName)) == _BREF->end()) { printf("ERROR:: Could not find macro %s in database \n",myName) +; exit(0); } else { _ITER = _BREF->find(myName); printf("Ref %s is found\n",myName); } ref_var* ref = new ref_var; *ref = (_ITER->second); macro *mac = boost::get<macro>(ref); printf("Macro class is %s\n",mac->get_class()); return(ref); }
Below is my typemap
TYPEMAP ref_var* REF_VARIANT OUTPUT REF_VARIANT char* type = get_ref_type(*$var); if (std::strcmp(type,"macro")) { printf ("HEY"); macro* mac = new macro; mac = boost::get<macro>(&$var); printf("Macro class is %s",mac->get_class()); sv_setref_pv($arg,"macro",mac); } else { module* mod = new module; mod = boost::get<module>(&$var); sv_setref_pv($arg,"module",mod); }
And here is what I got from Perl
TEST MACRO Insert macro macro1 OK! Insert macro macro2 OK! Insert macro macro3 OK! Insert port PA OK! Insert port P2 OK! Insert port P3 OK! Ref macro2 is found Macro class is COVER *** glibc detected *** free(): invalid pointer: 0x0000000000910f90 *** Abort
Would you please tell me what did I do wrong? I have spent the whole week on this trying many solutions I could think of but couldn't succeed. Thank you very much for your help!

In reply to XSUB and Boost::variant by bipham

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.