Skip to content

Commit b869e81

Browse files
committed
use unique_ptr
Signed-off-by: Rosen Penev <[email protected]>
1 parent fd1b148 commit b869e81

File tree

1 file changed

+15
-27
lines changed

1 file changed

+15
-27
lines changed

include/exiv2/value.hpp

Lines changed: 15 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1144,8 +1144,6 @@ class ValueType : public Value {
11441144
explicit ValueType(const T& val, TypeId typeId = getType<T>());
11451145
//! Copy constructor
11461146
ValueType(const ValueType<T>& rhs);
1147-
//! Virtual destructor.
1148-
~ValueType() override;
11491147
//@}
11501148

11511149
//! @name Manipulators
@@ -1275,7 +1273,7 @@ class ValueType : public Value {
12751273

12761274
// DATA
12771275
//! Pointer to the buffer, nullptr if none has been allocated
1278-
byte* pDataArea_{nullptr};
1276+
std::unique_ptr<byte[]> pDataArea_;
12791277
//! The current size of the buffer
12801278
size_t sizeDataArea_{0};
12811279
}; // class ValueType
@@ -1448,37 +1446,28 @@ ValueType<T>::ValueType(const T& val, TypeId typeId) : Value(typeId) {
14481446
}
14491447

14501448
template <typename T>
1451-
ValueType<T>::ValueType(const ValueType<T>& rhs) :
1452-
Value(rhs.typeId()),
1453-
value_(rhs.value_)
1454-
1455-
{
1449+
ValueType<T>::ValueType(const ValueType<T>& rhs) : Value(rhs.typeId()), value_(rhs.value_) {
14561450
if (rhs.sizeDataArea_ > 0) {
1457-
pDataArea_ = new byte[rhs.sizeDataArea_];
1458-
std::memcpy(pDataArea_, rhs.pDataArea_, rhs.sizeDataArea_);
1451+
pDataArea_ = std::unique_ptr<byte[]>(new byte[rhs.sizeDataArea_]);
1452+
std::copy_n(rhs.pDataArea_.get(), rhs.sizeDataArea_, pDataArea_.get());
14591453
sizeDataArea_ = rhs.sizeDataArea_;
14601454
}
14611455
}
14621456

1463-
template <typename T>
1464-
ValueType<T>::~ValueType() {
1465-
delete[] pDataArea_;
1466-
}
1467-
14681457
template <typename T>
14691458
ValueType<T>& ValueType<T>::operator=(const ValueType<T>& rhs) {
14701459
if (this == &rhs)
14711460
return *this;
14721461
value_ = rhs.value_;
14731462

1474-
byte* tmp = nullptr;
14751463
if (rhs.sizeDataArea_ > 0) {
1476-
tmp = new byte[rhs.sizeDataArea_];
1477-
std::memcpy(tmp, rhs.pDataArea_, rhs.sizeDataArea_);
1464+
pDataArea_ = std::unique_ptr<byte[]>(new byte[rhs.sizeDataArea_]);
1465+
std::copy_n(rhs.pDataArea_.get(), rhs.sizeDataArea_, pDataArea_.get());
1466+
sizeDataArea_ = rhs.sizeDataArea_;
1467+
} else {
1468+
pDataArea_ = nullptr;
1469+
sizeDataArea_ = 0;
14781470
}
1479-
delete[] pDataArea_;
1480-
pDataArea_ = tmp;
1481-
sizeDataArea_ = rhs.sizeDataArea_;
14821471

14831472
return *this;
14841473
}
@@ -1662,18 +1651,17 @@ size_t ValueType<T>::sizeDataArea() const {
16621651

16631652
template <typename T>
16641653
DataBuf ValueType<T>::dataArea() const {
1665-
return {pDataArea_, sizeDataArea_};
1654+
return {pDataArea_.get(), sizeDataArea_};
16661655
}
16671656

16681657
template <typename T>
16691658
int ValueType<T>::setDataArea(const byte* buf, size_t len) {
1670-
byte* tmp = nullptr;
1659+
std::unique_ptr<byte[]> tmp;
16711660
if (len > 0) {
1672-
tmp = new byte[len];
1673-
std::memcpy(tmp, buf, len);
1661+
tmp = std::unique_ptr<byte[]>(new byte[len]);
1662+
std::copy_n(buf, len, tmp.get());
16741663
}
1675-
delete[] pDataArea_;
1676-
pDataArea_ = tmp;
1664+
pDataArea_ = std::move(tmp);
16771665
sizeDataArea_ = len;
16781666
return 0;
16791667
}

0 commit comments

Comments
 (0)