-
Notifications
You must be signed in to change notification settings - Fork 616
Fix assignment operator issues in ByteArray #51
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
d381ecc
54504c8
a019231
4f7dad2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,130 +3,102 @@ | |
|
|
||
| #include "inc/Core/CommonDataStructure.h" | ||
|
|
||
| using namespace SPTAG; | ||
| namespace SPTAG | ||
| { | ||
|
|
||
| const ByteArray ByteArray::c_empty; | ||
|
|
||
| ByteArray::ByteArray() | ||
| : m_data(nullptr), | ||
| m_length(0) | ||
| ByteArray::ByteArray() noexcept | ||
| : m_data(nullptr) | ||
| , m_length(0) | ||
| { | ||
| } | ||
|
|
||
|
|
||
| ByteArray::ByteArray(ByteArray&& p_right) | ||
| : m_data(p_right.m_data), | ||
| m_length(p_right.m_length), | ||
| m_dataHolder(std::move(p_right.m_dataHolder)) | ||
| ByteArray::ByteArray(ByteArray&& p_right) noexcept | ||
| : m_data(p_right.m_data) | ||
| , m_length(p_right.m_length) | ||
| , m_dataHolder(std::move(p_right.m_dataHolder)) | ||
| { | ||
| p_right.m_data = nullptr; | ||
| p_right.m_length = 0; | ||
| } | ||
|
|
||
|
|
||
| ByteArray::ByteArray(std::uint8_t* p_array, std::size_t p_length, bool p_transferOnwership) | ||
| : m_data(p_array), | ||
| m_length(p_length) | ||
| : m_data(p_array) | ||
| , m_length(p_length) | ||
| { | ||
| if (p_transferOnwership) | ||
| { | ||
| m_dataHolder.reset(m_data, std::default_delete<std::uint8_t[]>()); | ||
| } | ||
| } | ||
|
|
||
|
|
||
| ByteArray::ByteArray(std::uint8_t* p_array, std::size_t p_length, std::shared_ptr<std::uint8_t> p_dataHolder) | ||
| : m_data(p_array), | ||
| m_length(p_length), | ||
| m_dataHolder(std::move(p_dataHolder)) | ||
| ByteArray::ByteArray(std::uint8_t* p_array, std::size_t p_length, std::shared_ptr<std::uint8_t> p_dataHolder) noexcept | ||
| : m_data(p_array) | ||
| , m_length(p_length) | ||
| , m_dataHolder(std::move(p_dataHolder)) | ||
| { | ||
| } | ||
|
|
||
|
|
||
| ByteArray::ByteArray(const ByteArray& p_right) | ||
| ByteArray::ByteArray(const ByteArray& p_right) noexcept | ||
| : m_data(p_right.m_data), | ||
| m_length(p_right.m_length), | ||
| m_dataHolder(p_right.m_dataHolder) | ||
| { | ||
| } | ||
|
|
||
|
|
||
| ByteArray& | ||
| ByteArray::operator= (const ByteArray& p_right) | ||
| ByteArray::operator=(const ByteArray& p_right) noexcept | ||
| { | ||
| m_data = p_right.m_data; | ||
| m_length = p_right.m_length; | ||
| m_dataHolder = p_right.m_dataHolder; | ||
| if (this != std::addressof(p_right)) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think shared_ptr has already handled such case? Not sure if this is necessary.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I haven't checked yet but it is just a recommended standard check in both copy constructor and move constructors
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I take some investigation and think this check is not necessary for std::shared_ptr and most other stl containers (it seems a popular implementation for shared_ptr assign function is to create a temp var and then swap it with
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have reverted back the checks |
||
| { | ||
| m_data = p_right.m_data; | ||
| m_length = p_right.m_length; | ||
| m_dataHolder = p_right.m_dataHolder; | ||
| } | ||
|
|
||
| return *this; | ||
| } | ||
|
|
||
|
|
||
| ByteArray& | ||
| ByteArray::operator= (ByteArray&& p_right) | ||
| ByteArray::operator=(ByteArray&& p_right) noexcept | ||
| { | ||
| m_data = p_right.m_data; | ||
| m_length = p_right.m_length; | ||
| m_dataHolder = std::move(p_right.m_dataHolder); | ||
| if (this != std::addressof(p_right)) | ||
| { | ||
| m_data = p_right.m_data; | ||
| m_length = p_right.m_length; | ||
| m_dataHolder = std::move(p_right.m_dataHolder); | ||
| } | ||
|
|
||
| return *this; | ||
| } | ||
|
|
||
|
|
||
| ByteArray::~ByteArray() | ||
| { | ||
| } | ||
|
|
||
|
|
||
| ByteArray | ||
| ByteArray::Alloc(std::size_t p_length) | ||
| { | ||
| ByteArray byteArray; | ||
| if (0 == p_length) | ||
| { | ||
| return byteArray; | ||
| return ByteArray(); | ||
| } | ||
| else { | ||
| auto array = new std::uint8_t[p_length]; | ||
| return ByteArray(array, p_length, true); | ||
| } | ||
|
|
||
| byteArray.m_dataHolder.reset(new std::uint8_t[p_length], | ||
| std::default_delete<std::uint8_t[]>()); | ||
|
|
||
| byteArray.m_length = p_length; | ||
| byteArray.m_data = byteArray.m_dataHolder.get(); | ||
| return byteArray; | ||
| } | ||
|
|
||
|
|
||
| std::uint8_t* | ||
| ByteArray::Data() const | ||
| { | ||
| return m_data; | ||
| } | ||
|
|
||
|
|
||
| std::size_t | ||
| ByteArray::Length() const | ||
| { | ||
| return m_length; | ||
| } | ||
|
|
||
|
|
||
| void | ||
| ByteArray::SetData(std::uint8_t* p_array, std::size_t p_length) | ||
| ByteArray::SetData(std::uint8_t* p_array, std::size_t p_length) noexcept | ||
| { | ||
| m_data = p_array; | ||
| m_length = p_length; | ||
| } | ||
|
|
||
|
|
||
| std::shared_ptr<std::uint8_t> | ||
| ByteArray::DataHolder() const | ||
| { | ||
| return m_dataHolder; | ||
| m_length = p_length; | ||
| m_dataHolder.reset(); | ||
| } | ||
|
|
||
|
|
||
| void | ||
| ByteArray::Clear() | ||
| ByteArray::Clear() noexcept | ||
| { | ||
| m_data = nullptr; | ||
| m_dataHolder.reset(); | ||
| m_length = 0; | ||
| } | ||
| } | ||
| } | ||
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.
please keep original formatting.
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.
I have reverted the changes done in member initializer lists