Tutorial: Building an Rogue Wave Server Application > Designing the Server Object Model > Defining Relations > Creating the Classes
 
Creating the Classes
Classes Network and Domain, and their Relations
1. Add the classes Network and Domain at the beginning of your network.h file, like this:
class Network: public IlsEntity
{
public:
   IlsOwnsList<Network,Domain> domains;
   Network();
};
 
class Domain: public IlsObject
{
public:
   IlsOwnsList<Domain,Line> lines;
   IlsOwnsList<Domain,Node> nodes;
   Domain();
};
The class Network derives from the class IlsEntity, whereas the class Domain derives from IlsObject. The classes IlsObject and IlsEntity are the base classes of any class whose data members are instances of Rogue Wave Server class templates.
The instances of the IlsOwnsList class template you have just declared are data members of the classes Network and Domains. These instances declare the relations described earlier —that is, a network that owns one or more domains and a domain that owns one or more lines and nodes.
Network() and Domain() are the constructors of the Network and Domain classes.
Class Line and Its Relations
2. Add the class Line right after the class Node in the network.h file, like this:
class Line: public IlsObject
{
public:
   IlsUses<Line,Node> input,output;
   Line(NodeP i, NodeP o);
};
Like the class Domain, the class Line derives from IlsObject. As this class is the origin of a use relation, it must directly or transitively derive from the class IlsObject or IlsEntity. Moreover, derivation must be public. The instances of the IlsUses class template are data members of the class Line. They declare the following relation: a line uses an input and an output node. Line(NodeP i, NodeP o) is the constructor for the class.
Your file network.h should now contain the following code:
#include <iostream.h>
#include <ilserver/model.h>
 
class Node;
typedef IlsSmartPointer<Node> NodeP;
class Network;
typedef IlsSmartPointer<Network> NetworkP;
class Domain;
typedef IlsSmartPointer<Domain> DomainP;
class Line;
typedef IlsSmartPointer<Line> LineP;
 
class Network: public IlsEntity
{
public:
   IlsOwnsList<Network,Domain> domains;
   Network(IlsIdentifier);
};
 
class Domain: public IlsObject
{
public:
   IlsOwnsList<Domain,Line> lines;
   IlsOwnsList<Domain,Node> nodes;
   Domain();
};
 
class Node: public IlsObject
{
};
 
class Line: public IlsObject
{
public:
   Line(NodeP i,NodeP o);
   IlsUses<Line,Node> input, output;
};
In the class Node, you have replaced the line public IlsRefCounted with public IlsObject, because IlsObject derives from IlsRefCounted. A class that can be both used and owned must directly or transitively derive from the class IlsObject. Also, derivation must be public. A class that can only be used must derive from the class IlsEntity. you have also added smart pointers to the classes Network, Domain, and Line. For details on how to define smart pointers, see “Defining Smart Pointers”..
Note: IlsObject and IlsEntity both derive from IlsRefCounted.
Defining the Constructors
3. Now define the constructors for the classes Network, Domain and Line in the file network.cpp.
#include <network.h>
 
Network::Network(IlsIdentifier id):
   IlsEntity(id),
   domains(*this)
{
}
 
Domain::Domain():
   nodes(*this),
   lines(*this)
{
}
 
Line::Line(NodeP i, NodeP o):
   input(*this, i),
   output(*this, o)
{
}
Server data members are instances of Rogue Wave® Server class templates. The initialization list of the constructors of classes that contain these instances takes a reference to this. *this provides a data member with a reference to its corresponding object.
The statement IlsEntity(id), which is specified in the initialization list of the constructor of the class Network, initializes the IlsEntity base class with the name of the network passed to the constructor. The statement domains(*this) builds an empty relation domains for a network.
The statements nodes(*this) and lines(*this), which are specified in the initialization list of the constructor of the class Domain, build the relations nodes and lines for a domain.
The statements input(*this,i) and output(*this,o), which are specified in the initialization list for the constructor of the class Line, build the extremities of a line (that is, the input and the output nodes).
Note: When you define a relation of type IlsOwnsList, you can restrict how many elements are in the list by setting the minimum and maximum cardinalities of the list in the constructor of the origin class. (For example, a domain can contain 0 to n nodes.)
Testing the Relations
4. To test the relations you have just defined, type the following code in a main.cpp file and compile.
#include <network.h>
 
int
main()
{
   NetworkP n=new Network("mynetwork"); // builds a network
   DomainP d=new Domain(); // builds a domain
   n->domains<<d; // inserts domain in the network
   NodeP n1=new Node(); // creates n1
   NodeP n2=new Node(); // creates n2
   NodeP n3=new Node(); // creates n3
   NodeP n4=new Node(); // creates n4
   d->nodes<<n1<<n2<<n3<<n4; // inserts nodes in the domain
   LineP l1=new Line(n1,n2); // creates l1
   LineP l2=new Line(n2,n3); // creates l2
   LineP l3=new Line(n2,n4); // creates l3
   d->lines<<l1<<l2<<l3; // inserts lines in the domain
   return 0;
}
Note: The operator << adds an element to a list. Conversely, the operator >> extracts an element from a list.
Summary
In this section, you have learned how to define relations between objects. Using these relations, you have built a network containing one domain, which in turn contains 4 nodes in which line1 links node1 to node2, line2 links node2 to node3, and line3 links node2 to node4.
The following figure shows a diagram of the relations you have just defined in your model.
Figure 6.1    Relation Diagram of the Network Object Model

Version 5.7
Copyright © 2013, Rogue Wave Software, Inc. All Rights Reserved.