Note that there are some explanatory texts on larger screens.

plurals
  1. POSmart pointers with a library written in C
    primarykey
    data
    text
    <p>I'm using C++ with the OpenCV library, which is a library image-processing although that's not relevant for this question. Currently I have a design decision to make. </p> <p>OpenCV, being a C library, has its data structures (such as CvMat) declared as structs. To create them, you use functions like cvCreateMat, and to release them, you use functions like cvReleaseMat. Being a C++ programmer, I created a special <code>cv_scoped</code> class which would automatically call cvReleaseMat when it went out of scope (like <code>boost::scoped_ptr</code>). </p> <p>What I'm realising now is that I wish I could use <code>auto_ptr</code> and <code>shared_ptr</code> in cases as well. I just feel that writing code for my own <code>cv_auto_ptr</code> and <code>cv_shared_ptr</code> classes would be a bad idea, not to mention a waste of time. So I've been looking for solutions, and I've come up with three possibilities.</p> <p><strong>First</strong>, I could use the cv_scoped class I've already made. I'd rename it to <code>cv_ptr</code> and then use smart pointers like so: <code>std::auto_ptr&lt;cv_ptr&gt;</code>. The annoying thing about this though is, I'd always have to dereference twice:</p> <pre><code>std::auto_ptr&lt;cv_ptr&gt; matrix(cv_ptr(cvCreateMat(320, 240, CV_32FC3))); cvPow(matrix.get()-&gt;get()); // one get for the auto_ptr, one for the cv_ptr </code></pre> <p>I know it looks like I could declare an implicit conversion, but I couldn't actually - most of OpenCV's functions have the parameter void* - so no implicit conversion would be called. I would really like a way of doing this where I didn't have to do the double dereference.</p> <p><strong>Second</strong>, I could somehow override <code>operator delete</code>. I don't want to override the global operator delete because I'd only want this to apply to CvMat (and a few other) types. However, I can't change the library, so I can't add <code>operator delete</code> to the CvMat struct. So I don't know how this would work.</p> <p><strong>Third</strong>, I could just rewrite my own <code>auto_ptr</code>, <code>scoped_ptr</code>, and <code>shared_ptr</code>. They're not large classes so it wouldn't be too difficult, but I just feel like this is bad design. If I were to do this, I would probably do something along these lines:</p> <pre><code>class cv_auto_ptr { public: cv_auto_ptr(); ~cv_auto_ptr(); // each method would just be a proxy for the smart pointer CvMat* get() { return this-&gt;matrix_.get()-&gt;get(); } // all the other operators/methods in auto_ptr would be the same, you get the idea private: auto_ptr&lt;cv_ptr&gt; matrix_; // cv_ptr deletes CvMat properly } </code></pre> <p>What would you do in my situation? Please help me figure this one out.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload