I'm new to C# and C++/CLI and not quite familiar with them. I searched but couldn't find an existing solution for a pretty simple thing (which I suppose should be common).
I am writing a simple C++/CLI project (a wrapper to C++ native dlls to be used in C#). There are many examples how to write a wrapper for native class:
class CNativeClass {
CNativeClass() {}
void Foo() {}
}
public ref class CNativeClassWrapper {
public:
CNativeClassWrapper() {
_Wrapper = new CNativeClass;
}
~CNativeClassWrapper() {
this->!CNativeClassWrapper();
}
!CNativeClassWrapper() {
delete _Wrapper;
_Wrapper = nullptr;
}
void Foo() {
_Wrapper->Foo();
}
private:
CNativeClass* _Wrapper;
}
This is simlified example code. There may be more native classes, there may be late initialization... A lot minor but necessary code.
Is there any wrapper for variable of native class which hides all these details? Like smart pointer templates in pure C++.
Moreover, if a native class is actually a smart pointer (90% of my cases, we use boost::shared_ptr<>) the code becomes a mess:
- check can be
if ( _Wrapper )orif ( *_Wrapper )or evenif ( _Wrapper && *_Wrapper ), - access to methods
(*_Wrapper)->Foo();, - how to reset? both
(*_Wrapper).reset();and_Wrapper->reset();are correct but it is too confusing.
Does anybody know a solution for this?