IgorOOP /Tutorial1

Top
Up
Contents
RecentChanges
Attachment
Backlinks
Print
Page
Edit
User:38.103.63.18
(anonymous)
Login
3560 hits since Mon Jun 27 16:58:16 2005
''I'll use a pretty dumb example which has no practical use other than illustrating how to use Igor OOP system. ''

Contents

  1. Contents
  2. Preparation
  3. Defining a Class
  4. Defining GUI
  5. Making an object
  6. Making GUI panel
  7. Defining methods
  8. How to inherit a class
  9. Functions in this example
  10. More Details

Preparation

Load following files,
  • OOP
  • OOP_gui
  • OOP_util
and compile them. (You'll need to activate Get State XOP which is in More Extensions Folder in Igor Pro Folder)

(use #pragma rtGlobals=1 //modern global access method)

Defining a Class

Let's say you want to define a person class with data member,
  1. name (string)
  2. date of birth (string, YYYY/MM/DD)

First thing to do is to define an init() function

Init function should have the form classname_init(). Underscore shouldn't apper in classname.

String members are defined as string globals. You can define default values in init() function. Similarly variable members are defined as global variables. You can also define waves in init function.

At the end of init function, you should call GUI initializatin function. (In this case, person_guiParam())

Defining GUI

Next you want to define GUI. You do it by defining function person_guiParam(). There you make a text wave named "guiparam_edit" and fill it with parameters describing what element should apper on the GUI panel. Let's say you want to have name and dateofbirth member editable and also want buttons that represent method of this object. (Say, printage and sayhello.) Then, guiParam() function should look like this:

Making an object

At this point, we can make an instance of the person class. An instance of a class is a datafolder, and it is specified by a path to the datafolder.

Type in the following to the command window:

oop_new("root:jack", "person") 

oop_new(path,classname) is a function to create an instance. For the first argument, you specify the location of the object (path). For the second argument, you specify the class name.

If you open the Data Browser, you'll see a datafolder named "jack" under the "root" folder.

Making GUI panel

Type in the following command:
oop_call("root:jack", "edit") 

Then you should get a panel that looks like this:

You can edit name and dateofbirth field in the panel. Now check the datafolder "root:jack" with the Data Browser. The values in the panel and the values of the global string variable (name, dateofbirth) in the datafolder are synchronized.

Now try clicking the buttons in the panel. It just gives you error message, since we haven't defined the methods.

Defining methods

Method of a class is a function of the form classname_methodname(). For example method sayhello of the person class is a function: person_sayhello(). Following is a sample definition of printage and sayhello method of the person class.

To access data member from within a method function, just use NVAR, SVAR, wave, or wave /t. (You can assume that the current datafolder is set to that of the instance.)

Now try pushing the buttons in the panel. The functions we defined above will be called.

How to inherit a class

OOP is not OOP without inheritance. Let's make a descendant of the person class. You can inherit a class by calling obj_inherit(classname) in the init() function. You can inherit from multiple classes.

Let's define a german class inherited from person class. And let's override the sayhello method:

You can type in the following and try clicking sayhello button.

oop_new("root:albert", "german") 
oop_call("root:albert", "edit") 

The overridden method will be called.


To call sayhello method programmatically, you use:
oop_call("root:albert", "sayhello") 
Then you get:
  Hallo, mein Name ist Jack. Wie geht es Ihnen?   

If you call:

oop_call("root:jack", "sayhello") 
You get:
 
  Hello, my name is Jack . How are you doing?   

That is, oop_call (objpath, methodname) implements virtual method call.

There are other variants of oop_call functions depending on return type(number or string) and arguments number and types:

 
oop_call_s(obj, method, sarg1) 
oop_call_v(obj, method, varg1) 
/s oop_s_call(obj, method) 
oop_call_ss(obj, method, sarg1, sarg2) 
/s oop_s_call_svv(obj, method, sarg1, varg2, varg3) 
etc. 

Functions in this example

More Details

Back To IgorOOP