Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- std::auto_ptr<cv_ptr> matrix(cv_ptr(cvCreateMat(320, 240, CV_32FC3)));
- cvPow(matrix.get()->get()); // one get for the auto_ptr, one for the cv_ptr
- 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->matrix_.get()->get(); }
- // all the other operators/methods in auto_ptr would be the same, you get the idea
- private:
- auto_ptr<cv_ptr> matrix_; // cv_ptr deletes CvMat properly
- }
- class Mat
- {
- CvMat* impl;
- public:
- Mat(/* Constructor Arguments */)
- {
- impl = cvCreateMat(/* BLAH */);
- }
- ~Mat()
- {
- cvReleaseMat(impl);
- }
- operator CvMat*()
- { // Cast opertator. Convert your C++ wrapper object into C object
- // when you use it with all those C functions that come with the
- // library.
- return impl;
- }
- };
- void Plop(CvMat* x)
- { // Some C function dealing with CvMat
- }
- int main()
- { // Don't need to dynamically allocate
- Mat m; // Just create on the stack.
- Plop(m); // Call Plop directly
- std::auto_ptr<Mat> mP(new Mat);
- Plop(*mP);
- }
- struct CvMatDeleter
- {
- void operator( CvMat* p ) { cvReleaseMat( p ) ; }
- };
- void test()
- {
- std::tr1::shared_ptr< CvMat > pMat( cvCreateMat(320, 240, CV_32FC3), CvMatDeleter() );
- // . . .
- }
- void f() {
- try {
- CvMat* mat = cvCreateMat(320, 240, CV_32FC3));
- // ...
- } catch(...) {
- cvReleaseMat(mat);
- throw;
- }
- cvReleaseMat(mat);
- }
- namespace cv {
- class Mat {
- public:
- enum Type { /* ... */ };
- Mat(int w, int h, Type type) {
- impl = cvCreateMat(w, h, intFromType(type));
- }
- ~Mat() {
- cvReleaseMat(impl);
- }
- void pow() { // wrap all operations
- cvPow(impl);
- }
- private:
- CvMat* impl;
- };
- }
Add Comment
Please, Sign In to add comment