How to do forward declarations with handles?

Hello.

Maybe I'm missing something but I can't do forward declarations of handle objcts.

Why doesn't this work?

class Handle(AIS_Shape);

nor

class Handle_AIS_Shape);

What am I missing here?

Thanks

Laszlo Kudela's picture

This might be a very old topic, but I am also interested in the answer.

Here is the situation:
I have a class "SomeCurve" that stores a Handle_Geom_Curve as a protected member myGeom_Curve. There is a class "SomeNURBS" that inherits from "SomeCurve". The constructor of "SomeNURBS" gets control points, degress, etc. and creates a Handle_Geom_BSplineCurve and stores it in myGeom_Curve.

If I forward declare

class Handle_Geom_Curve;

in the header file of "SomeCurve", the compiler returns an error. Of course it does so, because it needs "Geom_Curve.hxx" in the includes.

But I would like to avoid including OpenCASCADE headers in my header files as, as my code will be used later on as an external library, and the users of the library do not necesserarily need the headers of OCC, only the libs.

I already tried to overcome this issue by putting the Handle_Geom_Curve in a PIMPL that holds the Handle_Geom_Curve. This would work really nice, if I would not inherit anything from "SomeCurve". But as "SomeNURBS" also needs access to the PIMPL, I need to declare the PIMPL in a separate header file, which then also "Geom_Curve.hxx" included.

Any suggestions?

Laszlo Kudela's picture

In case, anyone is interested, here is a solution:

SomeCurve.hpp:

class Handle_Geom_Curve;

class SomeCurve:

public:
...

private:
class OpenCascadeHandleWrapper;
typedef std::shared_ptr OpenCascadeHandleWrapperPtr;
OpenCascadeHandleWrapperPtr myOpenCascadeHandleWrapper;

protected:
void setOpenCascadeHandle(Handle_Geom_Curve);

};

SomeCurve.cpp:

#include "../inc/SomeCurve.hpp"

struct SomeCurve::OpenCascadeHandleWrapper
{
Handle_Geom_Curve myGeom_Curve;
};

SomeCurve::SomeCurve(): myOpenCascadeHandleWrapper(OpenCascadeHandleWrapperPtr(new OpenCascadeHandleWrapper)
{
// CONSTRUCTOR
}

...

void SomeCurve::setOpenCascadeHandle(Handle_Geom_Curve theCurve)
{
myOpenCascadeHandleWrapper->myOpenCascadeCurve = theCurve;
}

Thus, any class that inherits from SomeCurve can set the Geom_Curve member of the superclass. The header file of someCurve only has a forward declaration of Handle_Geom_Curve, and the actual header file is only included where the handle wrapper is implemented (SomeCurve.cpp).

I hope this helps to everyone who had similar questions.

László

Laszlo Kudela's picture

Sorry, I made two mistakes:
- The "setOpenCascadeHandle" function is actually:
void SomeCurve::setOpenCascadeHandle(Handle_Geom_Curve theCurve)
{
myOpenCascadeHandleWrapper->myGeom_Curve = theCurve;
}

-The class definition of SomeCurve has a typo:
class SomeCurve
{
...
};