diff options
author | bubnikv <bubnikv@gmail.com> | 2017-10-17 21:00:15 +0300 |
---|---|---|
committer | bubnikv <bubnikv@gmail.com> | 2017-10-17 21:00:15 +0300 |
commit | d9d6d996e96a223a33580a19299312eadc3e5e53 (patch) | |
tree | 5a8ed6cd9aa95e92e8bc4dfb74ec764f27eafac7 /xs/src/slic3r | |
parent | af51220f3467d128075ee2fdc8bf5d091f4ea192 (diff) |
Utility functions to pass wxWidgets pointers from Perl to C++ code.
C++ var_dir / set_var_dir() interface to access the UI resources
from the C++ code.
Diffstat (limited to 'xs/src/slic3r')
-rw-r--r-- | xs/src/slic3r/GUI/wxPerlIface.cpp | 97 |
1 files changed, 97 insertions, 0 deletions
diff --git a/xs/src/slic3r/GUI/wxPerlIface.cpp b/xs/src/slic3r/GUI/wxPerlIface.cpp new file mode 100644 index 000000000..216ca4b3b --- /dev/null +++ b/xs/src/slic3r/GUI/wxPerlIface.cpp @@ -0,0 +1,97 @@ +// Derived from the following: + +///////////////////////////////////////////////////////////////////////////// +// Name: cpp/helpers.cpp +// Purpose: implementation for helpers.h +// Author: Mattia Barbon +// Modified by: +// Created: 29/10/2000 +// RCS-ID: $Id: helpers.cpp 3397 2012-09-30 02:26:07Z mdootson $ +// Copyright: (c) 2000-2011 Mattia Barbon +// Licence: This program is free software; you can redistribute it and/or +// modify it under the same terms as Perl itself +///////////////////////////////////////////////////////////////////////////// + +#ifdef __cplusplus +extern "C" { +#endif +#include "EXTERN.h" +#include "perl.h" +#include "XSUB.h" +#include "ppport.h" +#undef do_open +#undef do_close +#ifdef __cplusplus +} +#endif + +//#include <xsinit.h> + +// ---------------------------------------------------------------------------- +// Utility functions for working with MAGIC +// ---------------------------------------------------------------------------- + +struct my_magic +{ + my_magic() : object( NULL ), deleteable( true ) { } + + void* object; + bool deleteable; +}; + +//STATIC MGVTBL my_vtbl = { 0, 0, 0, 0, 0, 0, 0, 0 }; + +my_magic* wxPli_get_magic( pTHX_ SV* rv ) +{ + // check for reference + if( !SvROK( rv ) ) + return NULL; + SV* ref = SvRV( rv ); + + // if it isn't a SvPVMG, then it can't have MAGIC + // so it is deleteable + if( !ref || SvTYPE( ref ) < SVt_PVMG ) + return NULL; + + // search for '~' / PERL_MAGIC_ext magic, and check the value +// MAGIC* magic = mg_findext( ref, PERL_MAGIC_ext, &my_vtbl ); + MAGIC* magic = mg_find( ref, '~' ); + if( !magic ) + return NULL; + + return (my_magic*)magic->mg_ptr; +} + +// gets 'this' pointer from a blessed scalar/hash reference +void* wxPli_sv_2_object( pTHX_ SV* scalar, const char* classname ) +{ + // is it correct to use undef as 'NULL'? + if( !SvOK( scalar ) ) + { + return NULL; + } + + if( !SvROK( scalar ) ) + croak( "variable is not an object: it must have type %s", classname ); + + if( !classname || sv_derived_from( scalar, (char*) classname ) ) + { + SV* ref = SvRV( scalar ); + + my_magic* mg = wxPli_get_magic( aTHX_ scalar ); + + // rationale: if this is an hash-ish object, it always + // has both mg and mg->object; if however this is a + // scalar-ish object that has been marked/unmarked deletable + // it has mg, but not mg->object + if( !mg || !mg->object ) + return INT2PTR( void*, SvOK( ref ) ? SvIV( ref ) : 0 ); + + return mg->object; + } + else + { + croak( "variable is not of type %s", classname ); + return NULL; // dummy, for compiler + } +} |