c++ - Smart pointers with a library written in C -


I am using C ++ with the OpenCV library, which is a library image processing, however for this question Not relevant. Currently I have a design decision

Near OpenCV, having a C Library, its data structure (such as CVMM) has been declared as Stract. To make them, you use functions like cvCreateMat, and to release them, you use functions such as cvReleaseMat, being a C ++ programmer, I created a special cv_scoped class Which will automatically call cvReleaseMat when it will exit the scope (such as boost :: scoped_ptr ).

Now what I am feeling is that I want to be able to use it in the auto_ptr and shared_ptr cases too. I do not want to mention the waste of time, writing code for my own cv_auto_ptr and cv_shared_ptr classes would be a bad idea, so I am looking for solutions , And I've come up with three possibilities.

Earlier , I could use the cv_scoped class I already created, I would replace it with the name of cv_ptr and then such smart pointers Use: std :: auto_ptr & lt; Cv_ptr & gt; The annoying thing about this is that, I have always been confused twice:

  std :: auto_ptr  Mill ()); // for an auto_ptr, for a cv_ptr  

I know that it seems that I can declare a built-in conversion, but I can not really do it - Mostly OpenCV functions will not be parameter -fixed * - then no underlying conversion will be called. I really want a way to do this, where I will not have to do double deviation.

Second , I can override the operator by any means I do not want to override the global operator delete because I only want to apply it on CVMAT (and some other) types. However, I can not change the library, so I can not add the operator delete to the Cvmat struct, so I do not know how it will work.

Third , I can rewrite my own auto_ptr , scoped_ptr , and shared_ptr They are not big squares, so it will not be very difficult, but I think this is a bad design. If I was doing this, then I would probably do something with these lines:

  class cv_auto_ptr {public: cv_auto_ptr (); ~ Cv_auto_ptr (); // Each method will be just a proxy for smart pointer CvMat * get () {this-> matrix_.get () -> gt; Get (); } // All other operators / ways will be in auto_ptr, you will get this idea private: auto_ptr & lt; Cv_ptr & gt; Matrix_; // cv_ptr correctly removes cvmut}  

What will you do in my situation? Please help me figure it out.

Auto_ptR is actually designed for RAI on a C ++ class / destroyer Here are their experiments going forward, for whom they can not possibly be used (but can).

Still would you like to be able to use your C ++ object as if it is the usual stack variable allocated without dynamically all the time?

Make a cover with the standard solution constructor / district of your problem.
But by using only an internal cast operator to be used by the C function, the C object is converted into a C object when the C function is passed

by adding an internal cast operator Type the wrapper class.

Class Matt {CvMat * impl; Public: Mat (/ * Constructor Arguments * /) {impl = cvCreateMat (/ * BLAH * /); } ~ Mat () {cvReleaseMat (impl); } Operator CVMAT * () {// Cast operator convert your C ++ wrapper object to C object / when you use it with all C functions that come with // library. Return implication; }}; Void Plop (CvMat * x) {// Working with some CVMat function} int main () {// Dynamically allocation is not required; // Just put the rack on the stack (M); // Call Plop directly std :: auto_ptr & lt; Mat & gt; Mp (new mat); Clap (* Madhya Pradesh); }

Comments