/* File: VECTOR.QRPGLESRC RPG Vector ala Java */ /* Copyright (C) 2009 Dieter Bender */ /* */ /* This program is free software; you can redistribute it and/or modify */ /* it under the terms of the GNU General Public License as published by */ /* the Free Software Foundation. */ /* */ /* This program is distributed in the hope that it will be useful, */ /* but WITHOUT ANY WARRANTY; without even the implied warranty of */ /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */ /* GNU General Public License for more details. */ /* */ /* You should have received a copy of the GNU General Public License */ /* along with this program; if not, write to the Free Software */ /* Foundation, Inc., 59 Temple Place, */ /* Suite 330, Boston, MA 02111-1307 USA */ /* You might find a version at http://www.gnu.org */ h nomain D*B CRTRPGMOD VECTOR D*B+ DBGVIEW(*SOURCE) D*B CRTSRVPGM VECTOR D*B+ EXPORT(*ALL) D*B+ ACTGRP(*CALLER) D*B+ BNDDIR(QC2lE) /*--- Prototypes Export ----------------------------------*/ /COPY QRPGLEH,VECTOR /*--- Prototypes Import ----------------------------------*/ /COPY QRPGLEH,CEE4RAGE /COPY QRPGLEH,HEAP /COPY QRPGLEH,MEMCPY /COPY QRPGLEH,QMHSNDPM /*--- local Prototypes ----------------------------------*/ d setInstance pr d instance 10i 0 d allocBloc pr d throw pr d message 512 value d exit pr d actMark 10u 0 options(*nopass) d reason 10u 0 options(*nopass) d result 10u 0 options(*nopass) d user 10u 0 options(*nopass) d init pr /*--- Constants ----------------------------------*/ d TRUE c *ON d FALSE c *OFF d MAXINST c 767 /*--- Types ----------------------------------*/ /*--- statefull Variables ----------------------------------*/ d initFlag s n inz(FALSE) d instanceCount s 5i 0 inz(0) d instanceP s * dim(MAXINST) d thisp s * d this ds based(thisp) d align d qualified d objectPP * d heap 10i 0 d size 10i 0 d used 10i 0 d current 10i 0 d allocated n d ObjectPP s * D ObjectP s * DIM(32767) D BASED(ObjectPP) /*-------------------------------------------------------------*/ /*--- SQL Work Variables ----------------------------------*/ /*-------------------------------------------------------------*/ p add b export d add pi d handle 10i 0 d element 65535a VALUE d VARYING d position 10i 0 OPTIONS(*NOPASS) d value d i s 10i 0 d elemLeng s 10i 0 /free if not initFlag; init(); endif; setInstance(handle); if this.used = this.size; allocBloc(); endif; this.used = this.used + 1; if %parms = 3; for i = this.used downto position; objectP(i) = objectP(i - 1); endfor; this.current = position; else; this.current = this.used; endif; elemLeng = %len(element) + 2; allocHeap(this.heap:elemLeng :objectP(this.current):*OMIT); memcpy( ObjectP(this.current) : %addr(element) : elemLeng ) ; /end-free p add e /*-------------------------------------------------------------*/ p get b export d get pi 65535a varying d handle 10i 0 d position 10i 0 OPTIONS(*NOPASS) d value d elemLeng s 5u 0 d result s 65535a varying /free if not initFlag; init(); endif; setInstance(handle); if %parms = 2; if position > this.used or position < 1; throw('position exceeds size of Vector'); endif; this.current = position; else; this.current = this.current + 1; endif; if this.current > this.used; this.current = this.used; result = *BLANK; return result; endif; memcpy( %addr(elemLeng) : ObjectP(this.current) : 2 ) ; memcpy( %addr(result) : ObjectP(this.current) : elemLeng + 2 ) ; return result; /end-free p get e /*-------------------------------------------------------------*/ p set b export d set pi 65535a d handle 10i 0 d element 65535a VALUE d VARYING d position 10i 0 value d result s 65535a /free if not initFlag; init(); endif; setInstance(handle); result = get(handle : position); this.current = position; if %size(element) <> %size(result); reallocHeap(objectP(this.current) :%size(element) + 2:*OMIT); endif; memcpy( ObjectP(this.current) : %addr(element) : %size(element) + 2 ) ; return result; /end-free p set e /*-------------------------------------------------------------*/ p remove b export d remove pi 65535a d handle 10i 0 d position 10i 0 OPTIONS(*NOPASS) d value d i s 10i 0 d result s 65535a /free if not initFlag; init(); endif; setInstance(handle); if position > this.used or position < 1; throw('position exceeds size of Vector'); endif; result = get(handle : position); freeStorage(ObjectP(position):*OMIT); for i = position to this.used - 1; objectP(i) = objectP(i + 1); endfor; objectP(this.used) = *NULL; this.used = this.used - 1; this.current = position; return result; /end-free p remove e /*-------------------------------------------------------------*/ p isEmpty b export d isEmpty pi n d handle 10i 0 /free if not initFlag; init(); endif; setInstance(handle); if this.used = 0; return TRUE; else; return FALSE; endif; /end-free p isEmpty e /*-------------------------------------------------------------*/ p clear b export d clear pi d handle 10i 0 d i s 10i 0 /free if not initFlag; init(); endif; setInstance(handle); if this.allocated = FALSE; return; endif; for i = 1 to this.used ; freeStorage(ObjectP(i):*OMIT) ; endfor ; freeStorage(this.ObjectPP:*OMIT); this.size = 0; this.used = 0 ; this.current = 0 ; this.allocated = FALSE ; return; /end-free p clear e /*-------------------------------------------------------------*/ p size b export d size pi 10i 0 d handle 10i 0 /free if not initFlag; init(); endif; setInstance(handle); return this.used; /end-free p size e /*-------------------------------------------------------------*/ p VECTOR_getHandle... p b export d pi 10i 0 d heap s 10i 0 d result s 10i 0 /free if not initFlag; init(); endif; result = %lookup( *NULL : instanceP : 1 : instanceCount + 1 ); if result < 1; throw('number of maximal instances of Vector reached'); endif; createHeap(heap:*OMIT:*OMIT:*OMIT:*OMIT); allocHeap(heap:%len(this) :instanceP(result):*OMIT); if result > instanceCount; instanceCount = result; endif; thisP = instanceP(result); clear this; this.heap = heap; return result; /end-free p e /*-------------------------------------------------------------*/ p VECTOR_finalize... p b export d pi d handle 10i 0 /free if not initFlag; init(); endif; setInstance(handle); discardHeap(this.heap); instanceP(handle) = *NULL; /end-free p e /*-------------------------------------------------------------*/ P allocBloc B D allocBloc PI d i s 10i 0 d start s 10i 0 /free start = this.size + 1; this.size = this.size + 100 ; if this.allocated ; reallocHeap( this.ObjectPP : this.size * %size(ObjectP) : *OMIT ); objectPP = this.objectPP; for i = start to this.size; clear objectP(i); endfor; else ; allocHeap(this.heap:this.size * %size(ObjectP) :this.ObjectPP:*OMIT); objectPP = this.objectPP; this.allocated = TRUE ; endif ; /end-free P allocBloc E /*-------------------------------------------------------------------*/ p setInstance b d setInstance pi d handle 10i 0 /free if instanceP(handle) = *NULL; throw('there is no Vector with handle ' + %char(handle)); endif; thisp = instanceP(handle); objectPP = this.objectPP; return; /end-free p setInstance e /*-------------------------------------------------------------*/ p throw b d throw pi d message 512 value /*-------------------------------------------------------------*/ /free sendSysMsg( 'CPF9898' : 'QCPFMSG ' + '*LIBL ' : message : 512 : '*ESCAPE ' : '*PGMBDY ' : 1 : QMHSNDPM_MSGKEY : QMHSNDPM_MSGERR ); return; /end-free p throw e /*-------------------------------------------------------------*/ P init b d init pi * called once at begin /*-------------------------------------------------------------*/ /free CEE4RAGE(%paddr(exit) : *OMIT); // TODO your init code initFlag = TRUE; /end-free P init e /*-------------------------------------------------------------*/ P exit b d exit pi * called at very end by runtime d actMark 10u 0 options(*nopass) d reason 10u 0 options(*nopass) d result 10u 0 options(*nopass) d user 10u 0 options(*nopass) /*-------------------------------------------------------------*/ /free // TODO your exit code return; /end-free P exit e