CGAL 6.1 - STL Extensions for CGAL
|
#include <CGAL/Object.h>
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 Object
provides an abstraction. An object obj
of the class 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 Object
is NOT a common base class for the elementary classes. Therefore, there is no automatic conversion from these classes to Object
. Rather this is done with the global function make_object()
. This encapsulation mechanism requires the use of assign
or object_cast
to use the functionality of the encapsulated class.
This class is similar in spirit to std::any
.
Example
In the following example, the object class is used as return value for the intersection computation, as there are possibly different return values.
A more efficient way to access the object is to use object_cast
, which allows to skip a default construction and assignment:
The intersection routine itself looks roughly as follows:
Related Functions | |
(Note that these are not member functions.) | |
template<class T > | |
Object | make_object (const T &t) |
Creates an object that contains t . | |
template<class T > | |
bool | assign (T &c, const Object &o) |
assigns o to c if o was constructed from an object of type T . | |
template<class T > | |
const T * | object_cast (const Object *o) |
Returns a pointer to the object of type T stored by o , if any, otherwise returns NULL . | |
template<class T > | |
T | object_cast (const Object &o) |
Returns a copy of the object of type T stored by o , if any, otherwise throws an exception of type Bad_object_cast . | |
Creation | |
Objects of type | |
Object () | |
introduces an empty object. | |
Object (const Object &o) | |
Copy constructor. | |
Object (std::variant< T... >) | |
Implicit converting constructor for compatibility with std::variant . | |
Object (std::optional< std::variant< T... > >) | |
Implicit converting constructor for compatibility with std::optional and std::variant . | |
Operations | |
Object & | operator= (const Object &o) |
Assignment. | |
bool | empty () |
returns true, if obj does not contain an object of type T . | |
template<class T > | |
bool | is () |
returns true, iff obj contains an object of type T . | |
const std::type_info & | type () const |
returns the type information of the contained type, or typeid(void) if empty. | |
|
related |
assigns o
to c
if o
was constructed from an object of type T
.
Returns true
, if the assignment was possible. For efficiency reasons, we recommend using object_cast
instead.