AutoHotkey Community

It is currently May 26th, 2012, 6:46 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 3 posts ] 
Author Message
PostPosted: June 19th, 2009, 12:13 am 
Offline

Joined: July 16th, 2008, 10:40 pm
Posts: 4
Hello all,

I had problems to understand the class library found here in the forum Class Wish List so I thougt that I invest some time to make another one:

The library Jimulation.ahk is the only file needed to use it.

Jimulation.ahk

A demo that include a class for cars and the demonstration of it
and a browser-class that can be used to open a url either in the default browser or in firefox
can be found here: Jimulation_demo.ahk

The library is (hopefully) good documented but here I show some aspects of it twice:

it gives in jimulation.ahk only one main function that is used to create classes:

Code:
class(cmd,class,objname,name,value)


Quote:
cmd can be new/set/get or destroy
objname is the object name that is to be used
name is the variable name that is to be set or to be get
value is the value that is wished to be saved in the variable name


It exist helper functions - you can say shortcuts for the commands that are understood by the class function:

Code:
class_new()


Quote:
Function: make a new object instance

the return value is a object instance name that is used by the other class-function commands


Code:
class_set(obj,name,value)


Quote:
Function: set the value from the object instance variable

obj is the return value from a class_new call
name is the variable name of this object instance
value is the value that is saved to this variable


Code:
class_get(obj,name)


Quote:
Function: get the value from the object instance variable

obj is the return value from a class_new call
name is the wished variable of this object instance


Code:
class_destroy(obj)


Quote:
Function: delete a object instance

obj is the return value from a class_new call

it is very easy to make a new class:


Code:
#include jimulation.ahk
dog(name) {
class:=class_new()
class_set(class,"name",name)
}
dog_bark(obj) {
msgbox, % class_get(obj,"name") ": wuff"
}

lassy:=dog("Lassy")
superdog:=dog("Superdog")
dog_bark(superdog)
dog_bark(lassy)


Hope this is interesting for someone


Report this post
Top
 Profile  
Reply with quote  
PostPosted: June 21st, 2009, 1:25 pm 
Offline

Joined: July 16th, 2008, 10:40 pm
Posts: 4
Following code is a base for a one-function class:

Code:
classname(this="",cmd="",param="") {
static
static counter:=0
local splitparam
local class:="classname"
if (this="new") {
param:=cmd
cmd:="new"
counter:=counter+1
this:=counter
out:=this
}
stringsplit, splitparam, param , `,
if (islabel(class "_" cmd)) {
gosub %class%_%cmd%
return out
}
if (islabel(class "_error")) {
gosub %class%_error
return
}
return

subroutines
}


  • %this%_{name} is the variable name from this object instance
  • out is the variable to give a method a return value
  • class is the name of the class used for calling the method wanted

and for the new-method is a return value forbidden, because the value from this is in the return variable before new is called

Each subroutine is a method for the named.

So if you have a function after this model named car and it has for example following subroutines:

  • car_new param: name,speed
  • car_break
  • car_name

when a demo programm can use this code:
Code:
carone:=car("new","carone,0")
car(carone,"break")
msgbox, car(carone,"name")


a demo can downloaded from this url: oop.ahk

This class-style is similar to the one used by python in new-style classes :
Code:
class car(object):
         def __init__(self,param):
         def break(self)


in python the self parameter (in my autohotkey script is the var named this) is implicit given to the class-method so you must only write following code to make a car:

Code:
mycar=car(param)
mycar.break()


the mycar.break() is in reality following code: car(mycar).break()
And with my template you write in Autohotkey: car(mycar,"break")

I post this because I want some views from others about oop in autohotkey.

Cu all


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 21st, 2009, 2:18 pm 
Thanks for your effort. Much appreciated. Lets hope this is of use for some geeks out there! Stay tuned. 8)


Report this post
Top
  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: Apollo, fusion1920, Google [Bot], infogulch, Ragnar, Retro Gamer and 9 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group