mardi 28 novembre 2023

Forward Declaration (PIMPL) for External Libraries also used in Method Declarations?

I have looked at a significant number of posts regarding forward declarations/PIMPL, but haven't quite managed to get it to work with the external libraries I'm using. I want to create a shared library where the client does not need to include the external library headers.

This is what I currently have.

################
## myClass.h ##
################

#include <opencv2/opencv.hpp> ------> I want this header in the implementation file instead.

class A;
class B;
class C;

class __declspec(dllexport) myClass
{
public:
    myClass();
    ~myClass();
    cv::Mat myFunction(cv::Mat &img);
private:
    A *_a;
    B *_b;
    C *_c;
};

################
## myClass.cpp ##
################

#include "myClass.h"
#include "a.h"
#include "b.h"
#include "c.h"

myClass::myClass()
{
    _a = new A;
    _b = new B;
    _c = new C;
}

myClass::~myClass()
{
    delete _a;
    delete _b;
    delete _c;
}

cv::Mat myClass::myFunction(cv::Mat &img){ ... }

################
## a.h ##
################

class A
{
public:
    A();
    ... -----> A's methods
};

################
## b.h ##
################

class B{ ... };

################

################
## c.h ##
################

class C{ ... };

################

Here's what I have tried

################
## myClass.h ##
################

namespace cv{
    class Mat; ------> Forward declaration of cv::Mat
}

class A;
class B;
class C;

class __declspec(dllexport) myClass
{
public:
    myClass();
    ~myClass();
    cv::Mat myFunction(cv::Mat &img); ------> Get an error since I'm declaring an incomplete type cv::Mat.
private:
    A *_a;
    B *_b;
    C *_c;
    std::unique_ptr<cv::Mat> _m; ---------> PIMPL for class inside namespace ?
};

################
## myClass.cpp ##
################

#include "myClass.h"
#include "a.h"
#include "b.h"
#include "c.h"
#include <opencv2/opencv.hpp>

myClass::myClass() ------> I'm supposed to do something here ? Inherit _m ?
{
    _a = new A;
    _b = new B;
    _c = new C;
}

myClass::~myClass() -------> Supposed to clear _m (seen = default been used as well) ?
{
    delete _a;
    delete _b;
    delete _c;
}

cv::Mat myClass::myFunction(cv::Mat &img){ ... }

...

Any assistance or guidance would be appreciated. Thanks

Aucun commentaire:

Enregistrer un commentaire