📄 typemaps_ruby.i
字号:
/* ------------- Ruby Hash <-> char ** ---------------------- * Used to convert a native dictionary/hash type into name value pairs. *//* Hash -> char** */%typemap(in) char **dict{ /* %typemap(in) char **dict */ $1 = NULL; /* is the provided object an array or a hash? */ if ( TYPE($input) == T_ARRAY) { /* get the size of the array */ int size = RARRAY($input)->len; for (int i = 0; i < size; i++) { /* get the ruby object */ VALUE value = rb_ary_entry($input, i); /* Convert the value to a string via ruby duck typing * (i.e., the object might not actually be a string) */ char *pszItem = StringValuePtr(value); $1 = CSLAddString( $1, pszItem ); } } else if ( TYPE($input) == T_HASH) { /* This is a hash - get the size by calling via the ruby method */ int size = NUM2INT(rb_funcall($input, rb_intern("size"), 0, NULL)); if ( size > 0 ) { /* Get the keys by caling via ruby */ VALUE keys_arr = rb_funcall($input, rb_intern("keys"), 0, NULL); for( int i=0; i<size; i++ ) { /* Get the key and value as ruby objects */ VALUE key = rb_ary_entry(keys_arr, i); VALUE value = rb_hash_aref($input, key); /* Convert the key and value to strings via ruby duck typing * (i.e., the objects might not actually be strings) */ char *nm = StringValuePtr(key); char *val = StringValuePtr(value); /* Add the value */ $1 = CSLAddNameValue( $1, nm, val ); } } } else { rb_raise(rb_eTypeError, "Argument must be dictionary or sequence of strings"); }}/* char** --> Hash */%typemap(out) char **dict{ /* %typemap(out) char **dict */ /* Get a pointer to the c array */ char **stringarray = $1; /* Create a new hash table, this will be returned to Ruby. */ $result = rb_hash_new(); if ( stringarray != NULL ) { while (*stringarray != NULL ) { /* Get the key and value */ char const *valptr; char *keyptr; valptr = CPLParseNameValue( *stringarray, &keyptr ); if ( valptr != 0 ) { /* Convert the key and value to Ruby strings */ VALUE nm = rb_str_new2( keyptr ); VALUE val = rb_str_new2( valptr ); /* Save the key, value pair to the hash table. */ rb_hash_aset($result, nm, val); CPLFree( keyptr ); } stringarray++; } }}/* * Typemap char **<- dict. This typemap actually supports lists as well, * Then each entry in the list must be a string and have the form: * "name=value" so gdal can handle it. */%typemap(typecheck,precedence=SWIG_TYPECHECK_POINTER) (char **dict){ /* %typemap(typecheck,precedence=SWIG_TYPECHECK_POINTER) (char **dict) */ $1 = ((TYPE($input) == T_HASH) || (TYPE($input) == T_ARRAY)) ? 1 : 0;}%typemap(freearg) char **dict{ /* %typemap(freearg) char **dict */ CSLDestroy( $1 );}/* ------------- Ruby Array <-> array of char* ------------*//* Typemap maps char** arguments from Ruby Array */%typemap(in) char **options{ /* %typemap(in) char **options */ /* Check if is a list */ Check_Type($input, T_ARRAY); int size = RARRAY($input)->len; for (int i = 0; i < size; i++) { VALUE item = rb_ary_entry($input, i); char *pszItem = StringValuePtr(item); $1 = CSLAddString( $1, pszItem ); }}%typemap(out) char **options{ /* %typemap(out) char **options */ char **stringarray = $1; if ( stringarray == NULL ) { $result = Qnil; } else { int len = CSLCount( stringarray ); $result = rb_ary_new2( len ); for ( int i = 0; i < len; ++i, ++stringarray ) { VALUE nm = rb_str_new2( *stringarray ); rb_ary_push($result, nm); } }}%typemap(freearg) char **options{ /* %typemap(freearg) char **options */ CSLDestroy( $1 );}/* * Typemaps map mutable char ** arguments from Ruby Strings. Does not * return the modified argument */%typemap(in) char ** ( char *val=0 ){ /* %typemap(in) char ** ( char *val=0 ) */ val = StringValuePtr($input); $1 = &val;}%apply char** {char **ignorechange};/* ------------- Ruby String <- char ** no lengths ------------------*/%typemap(in,numinputs=0) (char **argout) ( char *argout=0 ){ /* %typemap(in,numinputs=0) (char **argout) ( char *argout=0 ) */ $1 = &argout;}%typemap(argout,fragment="output_helper") char **argout{ /* %typemap(argout) (char **argout) */ VALUE outArg; if ( $1 ) { outArg = rb_str_new2( *$1 ); } else { outArg = Qnil; } $result = SWIG_AppendOutput($result, outArg);}%typemap(freearg) (char **argout){ /* %typemap(freearg) (char **argout) */ if ( *$1 ) CPLFree( *$1 );}/* ------------- POD Typemaps ----------------------*//* * Typemap for an optional POD argument. * Declare function to take POD *. If the parameter * is NULL then the function needs to define a default * value. *//*%define OPTIONAL_POD(type,argstring)%typemap(in) (type *optional_##type) ( type val ){*/ /* %typemap(in) (type *optional_##type) *//* if ( $input == Qnil ) { $1 = 0; } else if ( PyArg_Parse( $input, #argstring ,&val ) ) { $1 = ($1_type) &val; } else { rb_raise(rb_eRuntimeError, "Invalid Parameter"); }}*//*%typemap(typecheck,precedence=0) (type *optional_##type){*/ /* %typemap(typecheck,precedence=0) (type *optionalInt) */ /* $1 = (($input==Py_None) || my_PyCheck_##type($input)) ? 1 : 0;}%enddef*///OPTIONAL_POD(int,i);/* -------- const char * <- Any object ------------ *//* Formats the object using str and returns the string representation */%typemap(in) (tostring argin) (VALUE rubyString){ /* %typemap( in) (tostring argin) */ $1 = StringValuePtr($input);}%typemap(typecheck,precedence=SWIG_TYPECHECK_POINTER) (tostring argin){ /* %typemap(typecheck,precedence=SWIG_TYPECHECK_POINTER) (tostring argin) */ $1 = 1;}/* ------------- Ruby Exception <- CPLErr ----------------------*/%typemap(out) CPLErr{ /* %typemap(out) CPLErr */ $result = ($1_type)LONG2NUM($1);}/* ----------- Ruby Arrays <------> XML Trees Helper Methods --------------- */%typemap(in) (CPLXMLNode* xmlnode ){ /* %typemap(in) (CPLXMLNode* xmlnode ) */ $1 = RubyArrayToXMLTree($input); if ( !$1 ) { rb_raise(rb_eRuntimeError, "Could not convert Ruby Array to XML tree."); }}%typemap(freearg) (CPLXMLNode *xmlnode){ /* %typemap(freearg) (CPLXMLNode *xmlnode) */ if ( $1 ) { CPLDestroyXMLNode( $1 ); }}%typemap(out,fragment="XMLTreeToPyList") (CPLXMLNode*){ /* %typemap(out) (CPLXMLNode*) */ $result = XMLTreeToRubyArray($1);}%typemap(ret) (CPLXMLNode*){ /* %typemap(ret) (CPLXMLNode*) */ if ( $1 ) { CPLDestroyXMLNode( $1 ); }}%apply char* {tostring argin}%apply int* {int* optional_int};%typemap(in) GDALDataType, CPLErr, GDALPaletteInterp, GDALAccess, GDALResampleAlg, GDALColorInterp, OGRwkbGeometryType, OGRFieldType, OGRJustification, OGRwkbByteOrder{ /* %typemap(in) CPLErr */ $1 = ($1_type) NUM2INT($input);}%typemap(out) SWIGTYPE* ParentReference { /* %typemap(out) SWIGTYPE* ParentReference */ /* There parent C++ object (self) owns the returned C++ object (result). If the parent goes out of scope it will free the child, invalidating the scripting language object that represents the child. To prevent that create a reference from the child to the parent, thereby telling the garabage collector not to GC the parent.*/ $result = SWIG_NewPointerObj((void *) $1, $1_descriptor,$owner); rb_iv_set($result, "swig_parent_reference", self);}/*%typemap(freearg) SWIGTYPE* ParentReference { /* %typemap(freearg) SWIGTYPE* ParentReference */ /* Subtract 2, 1 for self and 1 since argv is 0-based */ //rb_iv_set(argv[$argnum-2], "swig_parent_reference", self);//}*//* ----------- GByte --------------- *//* Tread byte arrays as char arrays */%typemap(in,numinputs=1,fragment="SWIG_AsCharPtrAndSize") (int nBytes, const GByte *pabyData) (int res, GByte *buf = 0, size_t size = 0, int alloc = 0) { /*%typemap(in,numinputs=1,fragment="SWIG_AsCharPtrAndSize") (int nBytes, const GByte *pabyData) */ res = SWIG_AsCharPtrAndSize($input, (char**)&buf, &size, &alloc); if (!SWIG_IsOK(res)) { %argument_fail(res, "(GByte*, int)", $symname, $argnum); } $1 = ($1_ltype) size - 1; $2 = ($2_ltype) buf; }%typemap(freearg) (int nBytes, const GByte *pabyData) { /* %typemap(freearg) (int nBytes, const GByte *pabyData) */ CPLFree(result);}%typemap(in,numinputs=1,fragment="SWIG_AsCharPtrAndSize") (const char *pszHex, int *pnBytes) (int res, char *buf = 0, int size = 0, int alloc = 0) { /*% typemap(in,numinputs=1,fragment="SWIG_AsCharPtrAndSize") (const char *pszHex, int *pnBytes) */ $2 = &size; res = SWIG_AsCharPtr($input, &buf, &alloc); if (!SWIG_IsOK(res)) { %argument_fail(res,"$type",$symname, $argnum); } $1 = buf;} %typemap(argout) (const char *pszHex, int *pnBytes) { /* %typemap(argout) (const char *pszHex, int *pnBytes) */ $result = SWIG_FromCharPtrAndSize((char*)result, (size_t)*$2); CPLFree(result);}%typemap(out) GByte* { /* %typemap(out) GByte* */ /* Stops insertion of default type map. */}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -