-
Notifications
You must be signed in to change notification settings - Fork 127
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add const ADatatype::getRaw(), Buffer::getData(); add copy+move Buffer::setData() #331
Conversation
…r::setData() - fixes luxonis#330
@szabi-luxonis would you please enable the CI to run? I want to see those results :-) |
Running. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO, this const
s should be overloads - eg:
std::vector<std::uint8_t>& getData();
const std::vector<std::uint8_t>& getData() const;
Same with getRaw
.
Otherwise, one might have a const message but still being able to modify the data when calling getData
.
Humm...🤔
The code so far in this PR enables code like... auto preview = previewQueue->get<dai::ImgFrame>();
auto theArray = preview->getData();
theArray[0] = 111;
auto theRaw = preview->getRaw();
theRaw->data.front() = 222;
// works because ImgFrame has a shared_ptr<RawBuffer>. RawBuffer is non-const.
const dai::ImgFrame theConstFrame{};
auto theNonConstArray = theConstFrame.getData();
theNonConstArray[1] = 111;
auto theNonConstRaw = theConstFrame.getRaw();
theNonConstRaw->data.front() = 222; Is there value in special-casing Or broading the work to add the new semantic throughout ImgFrame and all its apis/bases doesn't make sense to me with zerocopy refactoring all this code. 🤷♀️ Comments? Ideas? |
ewww, I could probably |
Agree the points above - some of the current API already breaks this behavior (nodes and getting properties, for example) Otherwise I'm not fully decided yet on whether Lets leave this as is for the current PR and we'll address const on messages when refactoring later 👍 |
Got it. 👍 "transitive" that's a good way to describe opencv supportdepthai optional opencv support functions get/setFrame() use a mix of Also, the OpenCV api does not support the idea of a const uchar* const buf = zerocopy.constdataptr();
cv::Mat myMat(640, 480, CV_8U, buf); // valid; constructor param is void*
*myMat.data = 12; // .data returns a non-const `uchar*`; this is undefined behavior or a crash
myMat.data[1] = 23; // this is undefined behavior or a crash
myMat.at(2) = 34; // returns non-const reference; this is undefined behavior or a crash
const cv::Mat myConstMat(640, 480, CV_8U, buf);
*myMat.data = 12; // .data returns a non-const `uchar*`; this is undefined behavior or a crash
myMat.data[1] = 23; // this is undefined behavior or a crash
myMat.at(2) = 34; // won't compile; returns const reference which can not be an lvalue prevent silent copies of bufferI wrote the code needed to implement your transitive suggestion above. getData() and getRaw() both worked as desired. I lost about half an hour because of my own subtle bug. I used void myfuncconst(const dai::ImgFrame& frame) {
auto theConstRaw = frame.getRaw();
auto s0 = theConstRaw->data.size();
// below correctly fails to compile, because front() returns `const uchar&`
//theConstRaw->data.front() = 56;
// dev mistake `auto` vs `auto&`
// `auto` creates a vector<uchar> var and COPIES all the vector data from the const ref return
// works, COPIES all vector data
auto theFalseConstData = frame.getData();
auto s1 = theFalseConstData.size();
// and changes my COPY and not the original frame data
theFalseConstData.front() = 33;
theFalseConstData[1] = 12;
// `auto&` creates a const vector<uchar>& var and assigns the ref from the const ref return
auto& theConstDataRef = frame.getData();
auto s2 = theConstDataRef.size();
// correctly fails, because front() returns `const uchar&`
theConstDataRef.front() = 33;
// correctly fails, because operator[]() returns `const uchar&`
theConstDataRef[1] = 12;
} |
Adds
const
to two member methods, enables operation on const and constref objectsAlso add/clarify the copy and move Buffer::setData() functions to ensure there is no fallback to internal copy+move when a single move should be used instead.
All 65 tests + examples pass. And I have been using these changes since ~7 December which had considerable testing over the last month.
Fixes #330