Classes

Classes can be declared the following way:

  class name
  {
    method1(...) {}
    ...
  };

Inside the class, method functions are declared without the 'static' keyword. The method with the name of the class is the class constructor. For example:

  class myclass
  {
     myclass(x,y)
     {
       print("myclass constructor has been called with two arguments: ", x, y);
       this.x = x;
       this.y = x;
     }
     ~myclass()
     {
       print("destructor has been called: ", this);
     }
  };

Inside the class methods, the 'this' variable can be used to refer to the current object.

Only one constructor per class is allowed.

Class instances are created like this:

  o = myclass(1, 2);

And object attributes (or fields) are accessed like this:

  print(o.x);
  o.y = o.x + 1;

A new attribute is created upon assigning to it:

  o.new_attr = 1;

Accessing an unexisting attribute generates an exception, which can be caught.

The following special method names exist:

  __setattr__(attr, value)
    This method is called when an object attribute is assigned a new value.
    Instead of assigning a value, this method can do something else.
    To modify a class attribute from this method, please use the setattr()
    global function.
    Compare with: Python _setattr_ method

  __getattr__(attr)
    This method is called when an object attribute is accessed for reading.
    Instead of simply returning a value, this method can do something else,
    for example to calculate the attribute on the fly. To retrieve a
    attribute from this function, use the getattr() global function.
    Compare with: Python _getattr_ method

Simple class inheritance is support. Derived classed are declared like this:

  class derived : base
  {
    ...
  };

Here we declare the 'derived' class that is derived from the 'base' class. For derived classes, the base class constructor can be called explicitly:

  class derived : base
  {
    derived() : base(args...)
    {
    }
  };

If the base class constructor is not called explicitly, IDA will call it implicitly, without any arguments.

It is possible to call base class methods using full names:

    base::func(this, args...);

The 'this' argument must be passed explicitly in this case.

When there are no more references to an object, it is automatically destroyed. We use a simple reference count algorithm to track the object use. Circularly dependent objects are not detected: they are never destroyed.

The following built-in object classes exist:

  - object: the default base class for all new classes. When the base class
    is not specified, the new class will inherit from 'object'
    This class has no fields and no special constructor. It has the following
    methods:
  • void object.retrieve(typeinfo, ea, flags)

    - exception: class used to report exceptions. It has the following attributes:
    
      file - source file name where the exception occurred
      func - current function name
      line - line number
      pc   - program counter value (internal to idc)
    
      Runtime errors are reported as exceptions too. They are two more fields:
    
      qerrno      - runtime error code
      description - printable error description
    
      This class has no special constructor and has no methods.
    
    - typeinfo: class used to represent type info. It has the following attributes:
    
      type        - binary encoded type string
      fields      - field names for the type (e.g. structure member names)
      name        - (optional) variable/function name

Human readable form of the typeinfo can be obtained by calling the print() method. The type size can be calculated using the size() method.

Last updated