View Full Version : Classes and Methods
Charles Pegge
19-03-2009, 14:21
The following examples need the latest Oxygen compile download, (relaxing some requirements for empty brackets)
'CLASSES AND METHODS
'
uses "oxygen","file"
dim p0,p1 as long, src as string
dim dd(100) as double
src = "
class stat
method sum() as double
method average() as double
/
da(100) as double
dn as long
end class
'--------------
methods of stat
'==============
method sum() as double
dim i
for i = 1 to this.dn
method += this.da(i)
next
end method
method average() as double
method = this.sum / this.dn
end method
end methods
'====
'TEST
'====
dim s as stat
s.da=>2,4,6,8,10,12,14,16,18
s.dn=9
print `Sum: ` str(s.sum) ` Average: ` str s.average
"
o2_basic src
'msgbox 0, o2_view "o2h "+src
if len(o2_error) then
msgbox 0, o2_error : stop
end if
o2_exec
Charles Pegge
19-03-2009, 14:36
Classes are a specialised form of TYPE which references a table of functions (or methods as they are known),
as well as data elements.
Each method for the class is defined in the methods block, which, under normal circumstances provides a cloak of invisibilty to the rest of the program. The methods can only be used by an object which belongs to the class.
'CLASSES AND METHODS
'
uses "oxygen","file"
dim p0,p1 as long, src as string
dim dd(100) as double
src = "
class string_stack
method push(s as string)
method pop() as string
/
buf as string
enb as long
le as long
end class
methods of string_stack
method push(s as string)
dim as long lb,ls,le
lb=len this.buf
ls=len s
'
'EXTEND STRINH BUFFER IF NEEDED
'
le=this.enb+ls+100
if le>lb then
this.buf+=space ls+1000
end if
'
'STORE STRING
'
mid(this.buf,this.enb+1)=s
'
'STORE STRING LENGTH
'
this.enb+=ls
dim as long p, a at p
p=*this.buf + this.enb
a=ls
this.enb+=4
end method
method pop() as string
'
'GET STRING LENGTH AND STRING INDEX
'
this.enb-=4
dim as long p, le at p
p=*this.buf + this.enb
this.enb-=le
'
'EXTRACT STRING
'
method=mid this.buf,this.enb+1,le
end method
end methods
'====
'TEST
'====
dim s as string_stack
s.push `abc` : s.push `defg` : s.push `hi`
print s.pop + `-` s.pop + `-` s.pop
terminate
"
o2_basic src
'msgbox 0, o2_view "o2h "+src
if len(o2_error) then
msgbox 0, o2_error : stop
end if
o2_exec
jcfuller
19-03-2009, 17:32
Charles,
Has this been implemented in the download from yesterday?
I get nesting errors on both class examples
James
jcfuller
19-03-2009, 18:26
Never mind I found the update from today and all is well.
James
Charles Pegge
19-03-2009, 18:43
Hi James,
I'll be putting quite a few changes through over the next few weeks, as the examples expose new issues. Fortunately its a very small download.
Charles
Charles Pegge
20-03-2009, 17:41
This is a stub program, with an Opengl flavour, showing how a surface might be created from a list of points, each of which contain location,normal,color and texture mapping vectors which themselves contain various scalars: x y z r g b a.
The overall surface has stub methods for appending points, rendering and clearing its points list.
The surface object, which may contain any number of points, also has its own set of vectors for material: diffuse, specula and shininess, describing the way in which the surface interacts with light.
Quite a lot to squeeze into a few lines :)
'CLASSES AND MULTIPLE INHERITANCE
uses "oxygen","file"
dim src as string
src = "
class color_rgba
r as byte
g as byte
b as byte
a as byte
=
rgba as long
end class
class vector_2d
x as single
y as single
end class
class vector_3d
x as single
y as single
z as single
end class
#view
class point
vector_3d,
color_rgba,
normal as vector_3d
texmap as vector_2d
end class
class surface
method append(byval pt as long)
method clear()
method render()
/
list as string
po as long
le as long
mat_diffuse as color_rgba
mat_specular as color_rgba
mat_shininess as single
end class
#endv
methods of surface
method append(byval pt as long)
dim p,q as long
'
'CHECK BUFFER SPACE
'
p=this.po + sizeof point
if (p>=this.le) then
this.list+=space 2000
p=*this.list-4
this.le=*p
end if
'
'COPY IN
'
q=*this.list + this.po
copyn q, pt, sizeof point
this.po+=sizeof point
end method
method clear()
this.po=0
this.le=0
this.list=``
end method
method render()
'...
end method
end methods
'====
'TEST
'====
dim pt as point
dim sf as surface
sf.append &pt
sf.render
sf.clear
print `ok`
'
terminate
"
o2_basic src
msgbox 0, o2_len+$cr+o2_prep "o2h "+src
if len(o2_error) then msgbox 0, o2_error : stop
o2_exec
largo_winch
01-12-2011, 18:07
hello charles, your examples in post #1 and post #2 don't run here (string stack). problems are word: "this.dn" (#1) and word "s.pop" (#2)
' Empty GUI script created on 12-01-2011 17:04:09 by (ThinAIR)
'CLASSES AND METHODS
'
Uses "oxygen","file"
Dim p0,p1 As Long, src As String
Dim dd(100) As Double
src = "
CLASS string_stack
METHOD Push(s As String)
METHOD Pop() As String
/
buf As String
enb As Long
le As Long
End CLASS
methods of string_stack
METHOD Push(s As String)
Dim As Long lb,ls,le
lb=Len THIS.buf
ls=Len s
'
'EXTEND STRINH BUFFER IF NEEDED
'
le=THIS.enb+ls+100
If le>lb Then
THIS.buf+=space ls+1000
End If
'
'STORE STRING
'
mid(THIS.buf,THIS.enb+1)=s
'
'STORE STRING LENGTH
'
THIS.enb+=ls
Dim As Long p, a At p
p=*THIS.buf + THIS.enb
a=ls
THIS.enb+=4
End METHOD
METHOD Pop() As String
'
'GET STRING LENGTH AND STRING INDEX
'
THIS.enb-=4
Dim As Long p, le At p
p=*THIS.buf + THIS.enb
THIS.enb-=le
'
'EXTRACT STRING
'
METHOD=mid THIS.buf,THIS.enb+1,le
End METHOD
End methods
'====
'TEST
'====
Dim s As string_stack
s.Push `abc` : s.Push `defg` : s.Push `HI`
Print s.Pop + `-` s.Pop + `-` s.Pop
terminate
"
O2_BASIC src
'msgbox 0, o2_view "o2h "+src
If Len(O2_ERROR) Then
MsgBox 0, O2_ERROR : Stop
End If
O2_EXEC
bye, largo
Charles Pegge
01-12-2011, 19:42
Hi Largo,
This is an ancient example. Suffering from multiple version syndrome, I feel like Dr Who :)
I have recoded this idea from scratch. I hope it works on your current version.
uses "oxygen"
dim as string src
src="
'================
class StringStack
'================
string buf
sys pb
sys lb
method push(string s)
'====================
sys ls=len s
sys pn=pb+ls+4
'
'ELASTIC BUFFER
'
if pn>lb then
buf+=nuls 4000
lb=len buf
end if
'
'APPEND STRING AND ITS LENGTH
'
method push(string s) '====================
sys ls=len s
sys pn=pb+ls+4
'
'ELASTIC BUFFER
'
if pn>lb then
buf+=nuls 4000
lb=len buf
end if
'
'APPEND STRING AND ITS LENGTH
'
mid (buf,pb+1)=s 'APPEND STRING
pb+=ls
sys p=*buf + pb
*p=ls 'PATCH IN LENGTH AS LONG BINARY
pb+=4
end method
method pop() as string
'=====================
if pb<4 then return ""
'
pb-=4 'POINT TO ITEM LENGTH ENCODING
sys i=*buf+pb 'LENGTH OF ITEM
sys ls=*i
pb-=ls 'POINT TO START OF STRING
'
return mid buf,pb+1,ls
end method
end class
StringStack s
s.push "Hello"
s.push "World"
print s.pop +" "+s.pop
"
o2_asmo src
if len(o2_error) then
print o2_error
stop
end if
o2_exec
Charles
largo_winch
05-12-2011, 15:56
hello charles again, your new example still doesn't work as expected. my thinbasic_Oxygen.dll named from 9. octobre 2011. I've packed all in zip file. error appeared same as in my last post. after my idea the script stops because of an error here. no "hello world" messagebox was displayed. bye, largo