Home · Examples 


Object and Value Types

In C++, we have a distinction between object and value types. A value type is typically added to the stack and sent by const reference in C++. An object type is typically allocated on the heap, and passed between functions using pointers.

In java, all objects behave like C++ object types. The Java primitives (e.g., int, char, and long) behave like C++ value types. All objects are passed using Java references (which are similar to pointers in C++) and primitives are passed by value.

When a class is mapped in the type system, the generator needs to know if it should be treated as an object or a value type. Object types will be sent by Java reference to methods and value types will be copied, and the copy will then be sent (also by Java reference) to methods.

Note that when a class has been declared as an object type, the generator will not accept functions in which it is passed by const reference in C++.

In this example, we look at a type system containing both object and value types. We also show how to deal with inheritance and polymorphic datatypes.

Disk Collection Example

We have a small library that can keep track of a disk collection, containing CD, DVD, and BlueRay disks. We have the DiskCollection class, which knows how to store and retrieve disks from a disk storage. Data about a single disk is stored in the EntryData class. We have a hierarchy of polymorphic disk classes with Disk as the base class. Disk has three subclasses representing CDs, DVDs, and BlueRays.

The important thing to remember when creating class hierarchies is that Java supports runtime type inference, which, for instance, gives life to the instanceof operator. For this to work, we need to equip Qt Jambi with a way to identify the type of objects at runtime.

This is how the type system looks for the disk classes:

<object-type name="Disk" polymorphic-base="yes" />
<object-type name="CD" polymorphic-id-expression="%1->type() == Disk::CDType" />
<object-type name="DVD"  polymorphic-id-expression="%1->type() == Disk::DVDType" />
<object-type name="BlueRay" polymorphic-id-expression="%1->type() == Disk::BlueRayType" />
The polymorphic-id-expression is a C++ expression evaluating to a bool value, which is true if the object of wich type is determined is an instance of the given
object-type; the generator simply inserts this into the generated C++ code when it needs to do runtime type inference. Note that %1 is the object of which type is to be determined.

The typesystem will now issue a warning because it assumes that Disk needs it's own polymorphic-id-expression. However, since Disk is the base class, this check is not necessary. We can silence the generator by giving it a suppress-warning.

<suppress-warning text="*class 'Disk' inherits from polymorphic class 'Disk'*" />
We also want to stop the generator complaints about the unmapped Disk::Type enum, which we only use for the type inference.
<suppress-warning text="*enum 'Disk::Type' does not have a type entry or is not an enum*" />
Note that for all QObject subclasses, type inference is handeled automatically.

We also have one value-type in our type system, the EntryData class. For a value type, you must implement a default constructor, and it can not have virtual functions or be the base of other classes.


Copyright © 2009 Nokia Corporation and/or its subsidiary(-ies) Trademarks
Qt Jambi 4.5.2_01