c++ move semantics for cv::Mat

This commit is contained in:
Alexander Alekhin 2015-08-15 16:46:05 +03:00
parent 5a0af53683
commit 1afc9eb730
2 changed files with 68 additions and 0 deletions

View File

@ -1854,6 +1854,11 @@ public:
/** @overload */
template<typename _Tp, typename Functor> void forEach(const Functor& operation) const;
#ifdef CV_CXX_MOVE_SEMANTICS
Mat(Mat&& m);
Mat& operator = (Mat&& m);
#endif
enum { MAGIC_VAL = 0x42FF0000, AUTO_STEP = 0, CONTINUOUS_FLAG = CV_MAT_CONT_FLAG, SUBMATRIX_FLAG = CV_SUBMAT_FLAG };
enum { MAGIC_MASK = 0xFFFF0000, TYPE_MASK = 0x00000FFF, DEPTH_MASK = 7 };

View File

@ -1107,6 +1107,69 @@ void Mat::push_back(const MatExpr& expr)
push_back(static_cast<Mat>(expr));
}
#ifdef CV_CXX_MOVE_SEMANTICS
inline
Mat::Mat(Mat&& m)
: flags(m.flags), dims(m.dims), rows(m.rows), cols(m.cols), data(m.data),
datastart(m.datastart), dataend(m.dataend), datalimit(m.datalimit), allocator(m.allocator),
u(m.u), size(&rows)
{
if (m.dims <= 2) // move new step/size info
{
step[0] = m.step[0];
step[1] = m.step[1];
}
else
{
CV_DbgAssert(m.step.p != m.step.buf);
step.p = m.step.p;
size.p = m.size.p;
m.step.p = m.step.buf;
m.size.p = &m.rows;
}
m.flags = MAGIC_VAL; m.dims = m.rows = m.cols = 0;
m.data = NULL; m.datastart = NULL; m.dataend = NULL; m.datalimit = NULL;
m.allocator = NULL;
m.u = NULL;
}
inline
Mat& Mat::operator = (Mat&& m)
{
release();
flags = m.flags; dims = m.dims; rows = m.rows; cols = m.cols; data = m.data;
datastart = m.datastart; dataend = m.dataend; datalimit = m.datalimit; allocator = m.allocator;
u = m.u;
if (step.p != step.buf) // release self step/size
{
fastFree(step.p);
step.p = step.buf;
size.p = &rows;
}
if (m.dims <= 2) // move new step/size info
{
step[0] = m.step[0];
step[1] = m.step[1];
}
else
{
CV_DbgAssert(m.step.p != m.step.buf);
step.p = m.step.p;
size.p = m.size.p;
m.step.p = m.step.buf;
m.size.p = &m.rows;
}
m.flags = MAGIC_VAL; m.dims = m.rows = m.cols = 0;
m.data = NULL; m.datastart = NULL; m.dataend = NULL; m.datalimit = NULL;
m.allocator = NULL;
m.u = NULL;
return *this;
}
#endif
///////////////////////////// MatSize ////////////////////////////
inline