Let's say I have a class MainApp using methods of a dynamic library via an interface FrontEnd
FrontEnd uses internally instances of a class Data (contained in a class BackEnd)
This class Data only contains a member and its accessor, no public method to manipulate the member
Ok now MainApp has no knowledge of Data and can't access it but it needs indirectly to manipulate it
My goal is to create some kind of a reference keeping a pointer to Data with 2 behaviours
- forbidding access to Data when in MainApp
- Allowing access to Data when in FrontEnd
I listed some solutions like PIMPL idiom, Bridge or Adaptor pattern but the problem is I don't want to use the Reference like an interface but just like an holder
How can I handle it?
Here some code to illustrate:
Data.hpp
struct Data {
Data(int i):member(i){}
int getMember();
private :
int member;
};
BackEnd.hpp
#include "Data.hpp"
struct BackEnd {
BackEnd(){}
Data* createData(int i) {
Data* d = new Data(i);
mDatas.push_back(d);
return d;
}
void doSomething(Data* d, int param) {
int r = param+d->getMember();
/*do other things with d*/
}
private:
vector<Data*> mDatas;
};
Reference.hpp
#include //???
struct Reference {
Reference(Data* d):mData(d){}
private:
//no access to data->member
Data* mData;
};
FrontEnd.hpp
#include "Data.hpp"
#include "Reference.hpp"
#include "BackEnd.hpp"
struct FrontEnd {
Reference* createData(int i) {
Data* d = mBackEnd->createData(i);
//conversion Data to Reference
Reference ref = new Reference(d);
return ref;
}
void doSomething(Reference* ref) {
//In the Front-End, I want an access to Ref->mData
Data* d = ref->getData();//Allows access to Ref->mData
int result = mBackEnd->doSomething(d);
}
private:
BackEnd* mBackEnd;
};
MainApp.hpp
//I don't want to reference Data.hpp
#include "Reference.hpp"
#include "FrontEnd.hpp"
struct MainApp {
Reference* createRef(){ mRef = mFrontEnd->createData(8);}
void doSomething(){ mFrontEnd->doSomething(mRef); }
private:
FrontEnd* mFrontEnd;
Reference* mRef;
//I want to keep a reference to Data without using Data directly
//Forbids access to mRef->mData
};