Skip to content

Commit 5172704

Browse files
committed
maybe Blob is better than unique_ptr
Signed-off-by: Rosen Penev <[email protected]>
1 parent 0e90364 commit 5172704

File tree

2 files changed

+18
-31
lines changed

2 files changed

+18
-31
lines changed

include/exiv2/value.hpp

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1273,9 +1273,7 @@ class ValueType : public Value {
12731273

12741274
// DATA
12751275
//! Pointer to the buffer, nullptr if none has been allocated
1276-
std::unique_ptr<byte[]> pDataArea_;
1277-
//! The current size of the buffer
1278-
size_t sizeDataArea_{0};
1276+
Blob pDataArea_;
12791277
}; // class ValueType
12801278

12811279
//! Unsigned short value type
@@ -1447,11 +1445,8 @@ ValueType<T>::ValueType(const T& val, TypeId typeId) : Value(typeId) {
14471445

14481446
template <typename T>
14491447
ValueType<T>::ValueType(const ValueType<T>& rhs) : Value(rhs.typeId()), value_(rhs.value_) {
1450-
if (rhs.sizeDataArea_ > 0) {
1451-
pDataArea_ = std::unique_ptr<byte[]>(new byte[rhs.sizeDataArea_]);
1452-
std::copy_n(rhs.pDataArea_.get(), rhs.sizeDataArea_, pDataArea_.get());
1453-
sizeDataArea_ = rhs.sizeDataArea_;
1454-
}
1448+
if (!rhs.pDataArea_.empty())
1449+
pDataArea_ = rhs.pDataArea_;
14551450
}
14561451

14571452
template <typename T>
@@ -1460,14 +1455,10 @@ ValueType<T>& ValueType<T>::operator=(const ValueType<T>& rhs) {
14601455
return *this;
14611456
value_ = rhs.value_;
14621457

1463-
if (rhs.sizeDataArea_ > 0) {
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;
1470-
}
1458+
if (!rhs.pDataArea_.empty())
1459+
pDataArea_ = rhs.pDataArea_;
1460+
else
1461+
pDataArea_.clear();
14711462

14721463
return *this;
14731464
}
@@ -1646,23 +1637,20 @@ inline Rational ValueType<double>::toRational(size_t n) const {
16461637

16471638
template <typename T>
16481639
size_t ValueType<T>::sizeDataArea() const {
1649-
return sizeDataArea_;
1640+
return pDataArea_.size();
16501641
}
16511642

16521643
template <typename T>
16531644
DataBuf ValueType<T>::dataArea() const {
1654-
return {pDataArea_.get(), sizeDataArea_};
1645+
return {pDataArea_.data(), pDataArea_.size()};
16551646
}
16561647

16571648
template <typename T>
16581649
int ValueType<T>::setDataArea(const byte* buf, size_t len) {
1659-
std::unique_ptr<byte[]> tmp;
1660-
if (len > 0) {
1661-
tmp = std::unique_ptr<byte[]>(new byte[len]);
1662-
std::copy_n(buf, len, tmp.get());
1663-
}
1664-
pDataArea_ = std::move(tmp);
1665-
sizeDataArea_ = len;
1650+
if (len > 0)
1651+
pDataArea_ = Blob(buf, buf + len);
1652+
else
1653+
pDataArea_.clear();
16661654
return 0;
16671655
}
16681656
} // namespace Exiv2

src/basicio.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -631,9 +631,8 @@ class BlockMap {
631631
//! @param num The size of data
632632
void populate(const byte* source, size_t num) {
633633
size_ = num;
634-
data_ = std::make_unique<byte[]>(size_);
634+
data_ = Blob(source, source + num);
635635
type_ = bMemory;
636-
std::memcpy(data_.get(), source, size_);
637636
}
638637

639638
/*!
@@ -655,8 +654,8 @@ class BlockMap {
655654
return type_ == bKnown;
656655
}
657656

658-
[[nodiscard]] byte* getData() const {
659-
return data_.get();
657+
[[nodiscard]] auto getData() const {
658+
return data_.data();
660659
}
661660

662661
[[nodiscard]] size_t getSize() const {
@@ -665,8 +664,8 @@ class BlockMap {
665664

666665
private:
667666
blockType_e type_{bNone};
668-
std::unique_ptr<byte[]> data_;
669-
size_t size_{0};
667+
Blob data_;
668+
size_t size_{};
670669
};
671670

672671
void MemIo::Impl::reserve(size_t wcount) {

0 commit comments

Comments
 (0)