Navigation: Up, Table of Contents, Bibliography, Index, Title Page

Generic Object (CGAL_Object)

#include <CGAL/basic.h>

Definition

Some functions can return different types of objects. A typical C++ solution to this problem is to derive all possible return types from a common base class, to return a pointer to this class and to perform a dynamic cast on this pointer. The class CGAL_Object provides an abstraction. An object obj of the class CGAL_Object can represent an arbitrary class. The only operations it provides is to make copies and assignments, so that you can put them in lists or arrays. Note that CGAL_Object s NOT a common base class for the elementary classes. Therefore, there is no automatic conversion from these classes to CGAL_Object. Rather this is done with the global function CGAL_make_object(). This encapsulation mechanism requires the use of CGAL_assign to use the functionality of the encapsulated class.

Creation

CGAL_Object object;
introduces an uninitialized variable.

CGAL_Object object ( o);
Copy constructor.

Objects of type CGAL_Object are normally created via the global function

template <class T>
CGAL_Object CGAL_make_object ( T t)
Creates an object that contains t.

Operations

CGAL_Object & object = o Assignment.

Assignment of an object of type CGAL_Object to an object of type T is done using

template <class T>
bool CGAL_assign ( T& c, o)
assigns o to c if o was constructed from an object of type T. Returns true, if the assignment was possible.

There is also a member function to check whether an object of type CGAL_Object contains an object.

bool object.is_empty () returns true, if object does not contain an object.

Example

In the following example, the object class is used as return value for the intersection computation, as there are possibly different return values.

{
    CGAL_Point_2< CGAL_Cartesian<double> > point;
    CGAL_Segment_2< CGAL_Cartesian<double> > segment,  segment_1, segment_2;

    cin >> segment_1 >> segment_2;

   CGAL_Object obj = CGAL_intersection(segment_1, segment_2);

    if (CGAL_assign(point, obj)) {
        /* do something with point */
    } else if ((CGAL_assign(segment, obj)) {
        /* do something with segment*/
    }
    /*  there was no intersection */
}

The intersection routine itself looks roughly as follows:


template < class R >
CGAL_Object  CGAL_intersection(CGAL_Segment_2<R> s1, CGAL_Segment_2<R> s2)
{
    if (/* intersection in a point */ ) {
       CGAL_Point_2<R> p = ... ;
       return CGAL_make_object(p);
    } else if (/* intersection in a segment */ ) {
       CGAL_Segment_2<R> s = ... ;
       return CGAL_make_object(s);
    }
    return CGAL_Object();
}


Return to chapter: Kernel Utilities
Navigation: Up, Table of Contents, Bibliography, Index, Title Page
The CGAL Project. Wed, January 20, 1999.