Modeling Services > Relations > List-relations > Declaring Simple List-relations
 
Declaring Simple List-relations
The class Person below declares a relation data member used_cars of type IlsUsesList and a relation data member owned_cars of type IlsOwnsList:
class Person:
public IlsObject
{
public:
   IlsUsesList<Person,Car> used_cars;
   IlsOwnsList<Person,Car> owned_cars;
   Person(CarP car1, CarP car2);
};
Constraints that apply to the arguments of IlsOwns and IlsUses also apply to IlsOwnsList and IlsUsesList. For more information, refer to sections “Defining Ownership Relations”. and “Defining Use Relations”..
Building List-relations
List-relations do not have a default constructor. To build a relation, it is generally sufficient to pass a reference to the origin-object (*this) to the constructor. The other arguments, described hereafter, are set to default values and are thus optional.
Let us consider the constructor of the relations owned_cars and used_cars:
Person::Person (CarP car1, CarP car2):
used_cars(*this),
owned_cars(*this, IlsDefaultRelationId, ILS_INACTIVE, 2, 10,
          ((*new IlsOwnsList<Person,Car>::Initial())<<car1<<car2))
The list-relation owned_cars is initialized by providing a reference to *this.
The second argument is the relation identifier. In this example, we have used the identifier default value, IlsDefaultRelationId. This identifier is used to invert relations. For details, refer to sections “Using Relation Identifiers with an Inverted Relation”. and “Using Relation Identifiers with an Inverted List-Relation”..
The third argument is the enumerated type IlsActivity. This type has two values, ILS_ACTIVE and ILS_INACTIVE, which specify whether the relation is active or not. If the relation is set as ILS_ACTIVE, the recomputation of derived data members whose evaluation function depends on the relation will be set off if the relation is modified. The default value is the one set by a call to the static function IlsModel::SetActive. If this function has never been invoked, the relation is set as inactive.
You can associate a cardinality with a list-relation. A cardinality is a pair of values representing the minimum and the maximum numbers of items in a list. The minimum value can be null. The maximum value can be an unsigned integer or can be indeterminate.
The fourth argument passed to the constructor is the minimal cardinality of the list—2 in the example. This argument is optional. Its default value is 0.
The fifth argument passed to the constructor is the maximal cardinality of the list—10 in the example. This argument is optional. Its default value is ILS_UNLIMITED_MAXCARD.
The sixth argument is a list of smart pointers to the objects initially placed in the list. You have to specify this argument to initialize a list with a non-null minimum cardinality. This argument is a newly created list that will automatically be destroyed by the constructor of Person. You should be sure to provide a list allocated by new and you should not exploit that list in the code of the constructor nor later on. Note that the code, above, does not contain a user-defined delete to de-allocate the new specified in the constructor. The number of objects in the list must be between the minimum and the maximum cardinalities set for the list. In our example, since the minimal cardinality of the list is non-null, when the constructor of Person is defined, the constructor of owned_cars must be provided with two objects of type Car at least, that is, car1 and car2. Any constructor of Person that does not provide these two objects will be rejected at runtime. If the number of objects supplied is less than the minimum cardinality, the exception IlsMincardViolated is thrown.
This rule ensures that when a list of objects with a non-null minimal cardinality is created, that minimum will be respected. If the number specified is greater than the maximum cardinality, the exception of type IlsMaxcardViolated is thrown. In the light of what we said in “Exception Handling”., you should not try to catch such exceptions.
For details on the nested type IlsOwnsList::Initial, see the Reference Manual.
The last argument specifies whether cardinalities should be checked. It works only during the construction of the list. By default, its value is IlsTrue.
The list used_cars is initialized by providing a single reference to *this. The other arguments are missing. This means that the minimal and the maximal cardinalities of the list are set to 0 and ILS_UNLIMITED_MAXCARD.
Thus, you can add as many smart pointers to objects as you want to the list. No smart pointer is passed to the constructor. Any list with a number of items between 0 and infinity could have been provided.

Version 5.8
Copyright © 2014, Rogue Wave Software, Inc. All Rights Reserved.