jeudi 24 mai 2018

move semantics and cv::Mat

Opencv's documentation on cv::Mat seems to indicate that there are no move constructors at the moment, so something like cv::Mat A=std::move(some_other_cv_mat) doesn't make much sense. My current (and naïve) solution to this problem is to derive a class from cv::Mat, for which I implement a move constructor, and then to specialize std::move for this object, as follows:

namespace cv
{
    //matrix object derived from cv::Mat
    class Mvbl_Mat final: public Mat 
    {
        public:
          //constructors
          Mvbl_Mat(){};
          Mvbl_Mat(int rows, int cols, int type, const Scalar   &s):Mat(rows, cols, type, s){}
         //destructor
         ~Mvbl_Mat(){};

          //move constructor
          Mvbl_Mat(Mvbl_Mat && other) noexcept
          {   
              this->data=other.data;
              other.data=nullptr;
           } 
          //move assignment operator
          Mvbl_Mat & operator=(Mvbl_Mat && other)
          {   
              this->data=other.data;
              other.data=nullptr;
              return *this; 
          }   
     };

}

namespace std 
{
    //specialization of std::move for cv::Mvbl_Mat
    template<>
    constexpr typename std::remove_reference<cv::Mvbl_Mat>::type&&
    move(cv::Mvbl_Mat&& __t) 
    {   
        return static_cast<typename std::remove_reference<cv::Mvbl_Mat>::type&&>(__t); 
    }   
}

While this works for the limited problems that I face at the moment, there are obviously many limitations and the solution is far from ideal. So, what is the best way to emulate move semantics for cv::Mat?

Aucun commentaire:

Enregistrer un commentaire