Changeset 2015

Show
Ignore:
Timestamp:
11/26/07 02:16:22 (5 years ago)
Author:
gyuque
Message:

/lang/ruby/RubyESRIShape: getter of polygon parts

Location:
lang/ruby/RubyESRIShape
Files:
5 modified

Legend:

Unmodified
Added
Removed
  • lang/ruby/RubyESRIShape/RubyESRIShape.c

    r2000 r2015  
    44static VALUE rb_cShape   = 0; 
    55static VALUE rb_cPolygon = 0; 
     6static VALUE rb_cPoint = 0; 
    67 
    78VALUE getRubyESRIShapePolygonClass() 
    89{ 
    910        return rb_cPolygon; 
     11} 
     12 
     13VALUE getRubyESRIShapePointClass() 
     14{ 
     15        return rb_cPoint; 
    1016} 
    1117 
     
    2329static VALUE rb_polygon_allocate(VALUE klass); 
    2430static void  rb_polygon_free(void *ptr); 
     31static void  rb_polygon_mark(void *ptr); 
    2532 
    2633// Shape 
     
    3643static void rb_shape_free(void *ptr) 
    3744{ 
    38 puts("del!"); 
    3945        if (ptr) 
    4046        { 
     
    4854static void rb_shape_mark(void *ptr) 
    4955{ 
    50 puts("mk!"); 
    5156        if (ptr) 
    5257        { 
     
    7277{ 
    7378        RShapeData* indata; 
    74         es_error_t rv; 
    7579    Data_Get_Struct(self, RShapeData, indata); 
    7680 
     
    8185{ 
    8286        RShapeData* indata; 
    83         es_error_t rv; 
    8487    Data_Get_Struct(self, RShapeData, indata); 
    8588 
    8689        return INT2NUM( c_Shape_getLength(indata->pThis) ); 
     90} 
     91 
     92static VALUE rb_shape_each(VALUE self) 
     93{ 
     94        RShapeData* indata; 
     95        int len, i; 
     96    Data_Get_Struct(self, RShapeData, indata); 
     97 
     98        len = c_Shape_getLength(indata->pThis); 
     99        for (i = 0;i < len;i++) 
     100        { 
     101                VALUE arr = rb_ary_new2(2); 
     102                rb_ary_push(arr, INT2NUM(i+1) ); 
     103                rb_ary_push(arr, c_Shape_getRecordAt(indata->pThis, i+1) ); 
     104 
     105                rb_yield(arr); 
     106        } 
     107 
     108        return self; 
    87109} 
    88110 
     
    94116        indata->pThis = NULL; 
    95117 
    96         return Data_Wrap_Struct(klass, 0, rb_polygon_free, indata); 
     118        return Data_Wrap_Struct(klass, rb_polygon_mark, rb_polygon_free, indata); 
    97119} 
    98120 
     
    107129} 
    108130 
     131static void rb_polygon_mark(void *ptr) 
     132{ 
     133        if (ptr) 
     134        { 
     135                if (((RPolygonData*)ptr)->pThis) 
     136                        c_Polygon_markRubyGC(((RPolygonData*)ptr)->pThis); 
     137        } 
     138} 
     139 
     140static VALUE rb_polygon_aref(VALUE self, VALUE index) 
     141{ 
     142        RPolygonData* indata; 
     143    Data_Get_Struct(self, RPolygonData, indata); 
     144 
     145        return c_Polygon_getPartAt(indata->pThis, NUM2LONG(index)); 
     146} 
     147 
     148static VALUE rb_polygon_length(VALUE self) 
     149{ 
     150        RPolygonData* indata; 
     151    Data_Get_Struct(self, RPolygonData, indata); 
     152 
     153        return INT2NUM( c_Polygon_getPartsLength(indata->pThis) ); 
     154} 
     155 
     156static VALUE rb_polygon_each(VALUE self) 
     157{ 
     158        RPolygonData* indata; 
     159        int len, i; 
     160    Data_Get_Struct(self, RPolygonData, indata); 
     161 
     162        len = c_Polygon_getPartsLength(indata->pThis); 
     163        for (i = 0;i < len;i++) 
     164        { 
     165                VALUE arr = rb_ary_new2(2); 
     166                rb_ary_push(arr, INT2NUM(i) ); 
     167                rb_ary_push(arr, c_Polygon_getPartAt(indata->pThis, i) ); 
     168 
     169                rb_yield(arr); 
     170        } 
     171 
     172        return self; 
     173} 
    109174 
    110175void Init_RubyESRIShape() 
     
    120185 
    121186        rb_define_private_method(rb_cShape, "initialize", rb_shape_initialize, 1); 
    122         rb_define_method(rb_cShape, "[]", rb_shape_aref, 1); 
     187        rb_define_method(rb_cShape, "[]",     rb_shape_aref  , 1); 
    123188        rb_define_method(rb_cShape, "length", rb_shape_length, 0); 
     189        rb_define_method(rb_cShape, "each",   rb_shape_each  , 0); 
    124190 
    125191        // class 
    126192        rb_cPolygon = rb_define_class_under (rb_mRubyESRIShape, "Polygon", rb_cObject); 
    127193        rb_define_alloc_func(rb_cPolygon, rb_polygon_allocate); 
     194        rb_define_method(rb_cPolygon, "[]"    , rb_polygon_aref  , 1); 
     195        rb_define_method(rb_cPolygon, "length", rb_polygon_length, 0); 
     196        rb_define_method(rb_cPolygon, "each"  , rb_polygon_each  , 0); 
     197 
     198        // class 
     199        rb_cPoint = rb_define_class_under (rb_mRubyESRIShape, "Point", rb_cObject); 
    128200 
    129201} 
  • lang/ruby/RubyESRIShape/Shape.cpp

    r2001 r2015  
    5454        ShpReadContext rctx; 
    5555        rctx.readSize = 0; 
    56  
    57         _tprintf(_T("%s\n"), filename); 
    5856 
    5957        mSHPFHeader.filecode   = freadBig32(fp); 
     
    191189void Shape::markRubyGC() 
    192190{ 
    193         RecordList::iterator it; 
     191        RValueList::iterator it; 
    194192        for (it = mRORecords.begin();it != mRORecords.end();it++) 
    195193                rb_gc_mark(*it); 
     
    255253 
    256254        ESInt32 i; 
     255 
    257256        for (i = 0;i < mNParts;i++) 
    258257        { 
     
    268267                rctx.readSize              += 16; 
    269268                rctx.readRecordContentSize += 16; 
    270  
    271269        } 
    272270 
     
    319317} 
    320318 
     319void ShpPolygon::markRubyGC() 
     320{ 
     321} 
     322 
     323VALUE ShpPolygon::getPartAt(int index) 
     324{ 
     325        if (index < 0 || index >= mNParts) 
     326                return Qnil; 
     327 
     328        int len = (index == (mNParts-1)) ? mNPoints : mPartIndexes[index+1]; 
     329        len -= mPartIndexes[index]; 
     330 
     331        VALUE arr = rb_ary_new2(len); 
     332        for (int i = 0;i < len;i++) 
     333                rb_ary_push(arr, INT2FIX(9 /*dmy*/ )); 
     334 
     335        return arr; 
     336} 
     337 
     338int ShpPolygon::getPartsLength() 
     339{ 
     340        return mNParts; 
     341} 
     342 
     343 
     344 
    321345void* c_Shape_new() 
    322346{return new Shape();} 
     
    339363void c_Polygon_delete(void* pThis) 
    340364{delete (ShpPolygon*)pThis;} 
     365 
     366void c_Polygon_markRubyGC(void* pThis) 
     367{((ShpPolygon*)pThis)->markRubyGC();} 
     368 
     369VALUE c_Polygon_getPartAt(void* pThis, int index) 
     370{return ((ShpPolygon*)pThis)->getPartAt(index);} 
     371 
     372int c_Polygon_getPartsLength(void* pThis) 
     373{return ((ShpPolygon*)pThis)->getPartsLength();} 
  • lang/ruby/RubyESRIShape/Shape.h

    r2000 r2015  
    9696        SHPFileHeader mSHPFHeader; 
    9797 
    98         RecordList mRORecords; 
     98        RValueList mRORecords; 
    9999}; 
    100100 
     
    110110 
    111111void* c_Shape_new(); 
    112 void c_Shape_delete(void* pThis); 
     112void  c_Shape_delete(void* pThis); 
    113113es_error_t c_Shape_readFromFile(void* pThis, const TCHAR* filename); 
    114114VALUE c_Shape_getRecordAt(void* pThis, int index); 
    115 int c_Shape_getLength(void* pThis); 
    116 void c_Shape_markRubyGC(void* pThis); 
     115int   c_Shape_getLength(void* pThis); 
     116void  c_Shape_markRubyGC(void* pThis); 
    117117 
    118 void c_Polygon_delete(void* pThis); 
     118void  c_Polygon_delete(void* pThis); 
     119void  c_Polygon_markRubyGC(void* pThis); 
     120VALUE c_Polygon_getPartsArray(void* pThis); 
     121VALUE c_Polygon_getPartAt(void* pThis, int index); 
     122int   c_Polygon_getPartsLength(void* pThis); 
    119123 
    120124#ifdef __cplusplus 
  • lang/ruby/RubyESRIShape/Shape_int.h

    r2000 r2015  
    55 
    66#include <vector> 
    7 typedef std::vector<VALUE> RecordList; 
     7typedef std::vector<VALUE> RValueList; 
    88 
    99typedef struct _ShpReadContext { 
     
    2929        virtual ~ShpPolygon(); 
    3030        virtual VALUE toRubyObject(); 
     31        VALUE getPartAt(int index); 
     32        int getPartsLength(); 
     33 
     34        void markRubyGC(); 
    3135 
    3236        es_error_t readFromMainFile(FILE* fp, ShpReadContext& rctx); 
     
    4953}; 
    5054 
     55class ShpPoint : public ShpRecordObject 
     56{ 
     57public: 
     58        ShpPoint(); 
     59        virtual ~ShpPoint(); 
     60        virtual VALUE toRubyObject(); 
     61}; 
     62 
    5163#endif 
  • lang/ruby/RubyESRIShape/ruby_min.h

    r2000 r2015  
    11typedef unsigned long VALUE; 
    22#define Qnil   ((VALUE)4) 
    3 extern "C" VALUE rb_class_new_instance(int, VALUE*, VALUE); 
    4 extern "C" void rb_gc_mark (VALUE); 
     3extern "C" { 
     4VALUE rb_class_new_instance(int, VALUE*, VALUE); 
     5void rb_gc_mark (VALUE); 
     6VALUE rb_ary_new2 (long); 
     7VALUE rb_yield (VALUE); 
     8VALUE rb_ary_push (VALUE, VALUE); 
     9} 
     10 
     11#define FIXNUM_FLAG 0x01 
     12#define INT2FIX(i) ((VALUE)(((long)(i))<<1 | FIXNUM_FLAG)) 
    513 
    614struct RBasic {