Replace naked new operators

main
Luis Díaz Más 3 years ago
parent 2505e52345
commit aec36f86d7

@ -916,7 +916,7 @@ namespace Exiv2 {
// Pimpl idiom // Pimpl idiom
class Impl; class Impl;
//! Pointer to implementation //! Pointer to implementation
Impl* p_ {nullptr}; std::unique_ptr<Impl> p_;
}; // class RemoteIo }; // class RemoteIo
/*! /*!

@ -219,7 +219,7 @@ namespace Exiv2 {
} // FileIo::Impl::stat } // FileIo::Impl::stat
FileIo::FileIo(const std::string& path) FileIo::FileIo(const std::string& path)
: p_(new Impl(path)) : p_(std::make_unique<Impl>(path))
{ {
} }
@ -548,9 +548,11 @@ namespace Exiv2 {
int FileIo::close() int FileIo::close()
{ {
int rc = 0; int rc = 0;
if (munmap() != 0) rc = 2; if (munmap() != 0)
rc = 2;
if (p_->fp_ != nullptr) { if (p_->fp_ != nullptr) {
if (std::fclose(p_->fp_) != 0) rc |= 1; if (std::fclose(p_->fp_) != 0)
rc |= 1;
p_->fp_ = nullptr; p_->fp_ = nullptr;
} }
return rc; return rc;
@ -738,12 +740,12 @@ namespace Exiv2 {
} }
MemIo::MemIo() MemIo::MemIo()
: p_(new Impl()) : p_(std::make_unique<Impl>())
{ {
} }
MemIo::MemIo(const byte* data, long size) MemIo::MemIo(const byte* data, long size)
: p_(new Impl(data, size)) : p_(std::make_unique<Impl>(data, size))
{ {
} }
@ -1172,7 +1174,6 @@ namespace Exiv2 {
{ {
if (p_) { if (p_) {
close(); close();
delete p_;
} }
} }
@ -1588,11 +1589,10 @@ namespace Exiv2 {
// encode base64 // encode base64
size_t encodeLength = ((size + 2) / 3) * 4 + 1; size_t encodeLength = ((size + 2) / 3) * 4 + 1;
auto encodeData = new char[encodeLength]; std::vector<char> encodeData (encodeLength);
base64encode(data, size, encodeData, encodeLength); base64encode(data, size, encodeData.data(), encodeLength);
// url encode // url encode
const std::string urlencodeData = urlencode(encodeData); const std::string urlencodeData = urlencode(encodeData.data());
delete[] encodeData;
std::stringstream ss; std::stringstream ss;
ss << "path=" << hostInfo_.Path << "&" ss << "path=" << hostInfo_.Path << "&"
@ -1613,9 +1613,10 @@ namespace Exiv2 {
throw Error(kerFileOpenFailed, "http",Exiv2::Internal::stringFormat("%d",serverCode), hostInfo_.Path); throw Error(kerFileOpenFailed, "http",Exiv2::Internal::stringFormat("%d",serverCode), hostInfo_.Path);
} }
} }
HttpIo::HttpIo(const std::string& url, size_t blockSize) HttpIo::HttpIo(const std::string& url, size_t blockSize)
{ {
p_ = new HttpImpl(url, blockSize); p_ = std::make_unique<HttpImpl>(url, blockSize);
} }
#ifdef EXV_USE_CURL #ifdef EXV_USE_CURL
@ -1776,11 +1777,10 @@ namespace Exiv2 {
// encode base64 // encode base64
size_t encodeLength = ((size + 2) / 3) * 4 + 1; size_t encodeLength = ((size + 2) / 3) * 4 + 1;
auto encodeData = new char[encodeLength]; std::vector<char> encodeData (encodeLength);
base64encode(data, size, encodeData, encodeLength); base64encode(data, size, encodeData.data(), encodeLength);
// url encode // url encode
const std::string urlencodeData = urlencode(encodeData); const std::string urlencodeData = urlencode(encodeData.data());
delete[] encodeData;
std::stringstream ss; std::stringstream ss;
ss << "path=" << hostInfo.Path << "&" ss << "path=" << hostInfo.Path << "&"
<< "from=" << from << "&" << "from=" << from << "&"
@ -1824,7 +1824,7 @@ namespace Exiv2 {
CurlIo::CurlIo(const std::string& url, size_t blockSize) CurlIo::CurlIo(const std::string& url, size_t blockSize)
{ {
p_ = new CurlImpl(url, blockSize); p_ = std::make_unique<CurlImpl>(url, blockSize);
} }
#endif #endif

@ -177,7 +177,7 @@ namespace Exiv2 {
ed.erase(std::remove_if(ed.begin(), ed.end(), FindExifdatum(filteredIfd)), ed.end()); ed.erase(std::remove_if(ed.begin(), ed.end(), FindExifdatum(filteredIfd)), ed.end());
} }
std::unique_ptr<TiffHeaderBase> header(new Cr2Header(byteOrder)); Cr2Header header(byteOrder);
OffsetWriter offsetWriter; OffsetWriter offsetWriter;
offsetWriter.setOrigin(OffsetWriter::cr2RawIfdOffset, Cr2Header::offset2addr(), byteOrder); offsetWriter.setOrigin(OffsetWriter::cr2RawIfdOffset, Cr2Header::offset2addr(), byteOrder);
return TiffParserWorker::encode(io, return TiffParserWorker::encode(io,
@ -188,7 +188,7 @@ namespace Exiv2 {
xmpData, xmpData,
Tag::root, Tag::root,
TiffMapping::findEncoder, TiffMapping::findEncoder,
header.get(), &header,
&offsetWriter); &offsetWriter);
} }

@ -169,8 +169,6 @@ namespace Exiv2 {
CiffHeader::~CiffHeader() CiffHeader::~CiffHeader()
{ {
delete pRootDir_;
delete[] pPadding_;
} }
CiffDirectory::~CiffDirectory() CiffDirectory::~CiffDirectory()
@ -214,12 +212,12 @@ namespace Exiv2 {
throw Error(kerNotACrwImage); throw Error(kerNotACrwImage);
} }
delete[] pPadding_; pPadding_.clear();
pPadding_ = new byte[offset_ - 14]; pPadding_.resize(offset_ - 14);
padded_ = offset_ - 14; padded_ = offset_ - 14;
std::memcpy(pPadding_, pData + 14, padded_); std::copy_n(pData+14, padded_, pPadding_.begin());
pRootDir_ = new CiffDirectory; pRootDir_ = std::make_unique<CiffDirectory>();
pRootDir_->readDirectory(pData + offset_, size - offset_, byteOrder_); pRootDir_->readDirectory(pData + offset_, size - offset_, byteOrder_);
} // CiffHeader::read } // CiffHeader::read
@ -329,8 +327,9 @@ namespace Exiv2 {
void CiffHeader::decode(Image& image) const void CiffHeader::decode(Image& image) const
{ {
// Nothing to decode from the header itself, just add correct byte order // Nothing to decode from the header itself, just add correct byte order
if (pRootDir_) pRootDir_->decode(image, byteOrder_); if (pRootDir_)
} // CiffHeader::decode pRootDir_->decode(image, byteOrder_);
}
void CiffComponent::decode(Image& image, ByteOrder byteOrder) const void CiffComponent::decode(Image& image, ByteOrder byteOrder) const
{ {
@ -369,9 +368,9 @@ namespace Exiv2 {
append(blob, reinterpret_cast<const byte*>(signature_), 8); append(blob, reinterpret_cast<const byte*>(signature_), 8);
o += 8; o += 8;
// Pad as needed // Pad as needed
if (pPadding_) { if (pPadding_.empty() == false) {
assert(padded_ == offset_ - o); assert(padded_ == offset_ - o);
append(blob, pPadding_, padded_); append(blob, pPadding_.data(), padded_);
} }
else { else {
for (uint32_t i = o; i < offset_; ++i) { for (uint32_t i = o; i < offset_; ++i) {
@ -622,7 +621,7 @@ namespace Exiv2 {
assert(rootDirectory == 0x0000); assert(rootDirectory == 0x0000);
crwDirs.pop(); crwDirs.pop();
if (!pRootDir_) { if (!pRootDir_) {
pRootDir_ = new CiffDirectory; pRootDir_ = std::make_unique<CiffDirectory>();
} }
CiffComponent* child = pRootDir_->add(crwDirs, crwTagId); CiffComponent* child = pRootDir_->add(crwDirs, crwTagId);
if (child) { if (child) {
@ -683,7 +682,7 @@ namespace Exiv2 {
} }
if (cc_ == nullptr) { if (cc_ == nullptr) {
// Tag doesn't exist yet, add it // Tag doesn't exist yet, add it
m_ = UniquePtr(new CiffEntry(crwTagId, tag())); m_ = std::make_unique<CiffEntry>(crwTagId, tag());
cc_ = m_.get(); cc_ = m_.get();
add(std::move(m_)); add(std::move(m_));
} }

@ -493,10 +493,10 @@ namespace Exiv2 {
// DATA // DATA
static const char signature_[]; //!< Canon CRW signature "HEAPCCDR" static const char signature_[]; //!< Canon CRW signature "HEAPCCDR"
CiffDirectory* pRootDir_ = nullptr; //!< Pointer to the root directory std::unique_ptr<CiffDirectory> pRootDir_; //!< Pointer to the root directory
ByteOrder byteOrder_ = littleEndian; //!< Applicable byte order ByteOrder byteOrder_ = littleEndian; //!< Applicable byte order
uint32_t offset_ = 0; //!< Offset to the start of the root dir uint32_t offset_ = 0; //!< Offset to the start of the root dir
byte* pPadding_ = nullptr; //!< Pointer to the (unknown) remainder std::vector<byte> pPadding_; //!< the (unknown) remainder
uint32_t padded_ = 0; //!< Number of padding-bytes uint32_t padded_ = 0; //!< Number of padding-bytes
}; // class CiffHeader }; // class CiffHeader

@ -181,7 +181,7 @@ namespace Exiv2 {
template<typename T> template<typename T>
Exiv2::Exifdatum& setValue(Exiv2::Exifdatum& exifDatum, const T& value) Exiv2::Exifdatum& setValue(Exiv2::Exifdatum& exifDatum, const T& value)
{ {
auto v = std::unique_ptr<Exiv2::ValueType<T> >(new Exiv2::ValueType<T>); auto v = std::make_unique<Exiv2::ValueType<T>>();
v->value_.push_back(value); v->value_.push_back(value);
exifDatum.value_ = std::move(v); exifDatum.value_ = std::move(v);
return exifDatum; return exifDatum;
@ -696,9 +696,9 @@ namespace Exiv2 {
// Encode and check if the result fits into a JPEG Exif APP1 segment // Encode and check if the result fits into a JPEG Exif APP1 segment
MemIo mio1; MemIo mio1;
std::unique_ptr<TiffHeaderBase> header(new TiffHeader(byteOrder, 0x00000008, false)); TiffHeader header(byteOrder, 0x00000008, false);
WriteMethod wm = TiffParserWorker::encode(mio1, pData, size, ed, emptyIptc, emptyXmp, Tag::root, WriteMethod wm = TiffParserWorker::encode(mio1, pData, size, ed, emptyIptc, emptyXmp, Tag::root,
TiffMapping::findEncoder, header.get(), nullptr); TiffMapping::findEncoder, &header, nullptr);
if (mio1.size() <= 65527) { if (mio1.size() <= 65527) {
append(blob, mio1.mmap(), static_cast<uint32_t>(mio1.size())); append(blob, mio1.mmap(), static_cast<uint32_t>(mio1.size()));
return wm; return wm;
@ -796,7 +796,7 @@ namespace Exiv2 {
// Encode the remaining Exif tags again, don't care if it fits this time // Encode the remaining Exif tags again, don't care if it fits this time
MemIo mio2; MemIo mio2;
wm = TiffParserWorker::encode(mio2, pData, size, ed, emptyIptc, emptyXmp, Tag::root, TiffMapping::findEncoder, wm = TiffParserWorker::encode(mio2, pData, size, ed, emptyIptc, emptyXmp, Tag::root, TiffMapping::findEncoder,
header.get(), nullptr); &header, nullptr);
append(blob, mio2.mmap(), static_cast<uint32_t>(mio2.size())); append(blob, mio2.mmap(), static_cast<uint32_t>(mio2.size()));
#ifdef EXIV2_DEBUG_MESSAGES #ifdef EXIV2_DEBUG_MESSAGES
if (wm == wmIntrusive) { if (wm == wmIntrusive) {

@ -848,13 +848,12 @@ namespace Exiv2 {
throw Error(kerDataSourceOpenFailed, io_->path(), strError()); throw Error(kerDataSourceOpenFailed, io_->path(), strError());
} }
IoCloser closer(*io_); IoCloser closer(*io_);
BasicIo::UniquePtr tempIo(new MemIo); auto tempIo = std::make_unique<MemIo>();
assert (tempIo.get() != 0);
doWriteMetadata(*tempIo); // may throw doWriteMetadata(*tempIo); // may throw
io_->close(); io_->close();
io_->transfer(*tempIo); // may throw io_->transfer(*tempIo); // may throw
} // JpegBase::writeMetadata }
void JpegBase::doWriteMetadata(BasicIo& outIo) void JpegBase::doWriteMetadata(BasicIo& outIo)
{ {

@ -183,9 +183,9 @@ namespace Exiv2 {
ed.erase(std::remove_if(ed.begin(), ed.end(), FindExifdatum(filteredIfd)), ed.end()); ed.erase(std::remove_if(ed.begin(), ed.end(), FindExifdatum(filteredIfd)), ed.end());
} }
std::unique_ptr<TiffHeaderBase> header(new OrfHeader(byteOrder)); OrfHeader header(byteOrder);
return TiffParserWorker::encode(io, pData, size, ed, iptcData, xmpData, Tag::root, TiffMapping::findEncoder, return TiffParserWorker::encode(io, pData, size, ed, iptcData, xmpData, Tag::root, TiffMapping::findEncoder,
header.get(), nullptr); &header, nullptr);
} }
// ************************************************************************* // *************************************************************************

@ -4186,19 +4186,20 @@ namespace Exiv2 {
prefix_ = prefix; prefix_ = prefix;
} }
XmpKey::XmpKey(const std::string& key) : p_(new Impl) XmpKey::XmpKey(const std::string& key) : p_(std::make_unique<Impl>())
{ {
p_->decomposeKey(key); p_->decomposeKey(key);
} }
XmpKey::XmpKey(const std::string& prefix, const std::string& property) : p_(new Impl(prefix, property)) XmpKey::XmpKey(const std::string& prefix, const std::string& property)
: p_(std::make_unique<Impl>(prefix, property))
{ {
} }
XmpKey::~XmpKey() = default; XmpKey::~XmpKey() = default;
XmpKey::XmpKey(const XmpKey& rhs) XmpKey::XmpKey(const XmpKey& rhs)
: p_(new Impl(*rhs.p_)) : p_(std::make_unique<Impl>(*rhs.p_))
{ {
} }

@ -295,7 +295,7 @@ namespace Exiv2 {
} }
ExifKey::ExifKey(uint16_t tag, const std::string& groupName) ExifKey::ExifKey(uint16_t tag, const std::string& groupName)
: p_(new Impl) : p_(std::make_unique<Impl>())
{ {
IfdId ifdId = groupId(groupName); IfdId ifdId = groupId(groupName);
// Todo: Test if this condition can be removed // Todo: Test if this condition can be removed
@ -311,7 +311,7 @@ namespace Exiv2 {
} }
ExifKey::ExifKey(const TagInfo& ti) ExifKey::ExifKey(const TagInfo& ti)
: p_(new Impl) : p_(std::make_unique<Impl>())
{ {
auto ifdId = static_cast<IfdId>(ti.ifdId_); auto ifdId = static_cast<IfdId>(ti.ifdId_);
if (!Internal::isExifIfd(ifdId) && !Internal::isMakerIfd(ifdId)) { if (!Internal::isExifIfd(ifdId) && !Internal::isMakerIfd(ifdId)) {
@ -322,13 +322,13 @@ namespace Exiv2 {
} }
ExifKey::ExifKey(const std::string& key) ExifKey::ExifKey(const std::string& key)
: p_(new Impl) : p_(std::make_unique<Impl>())
{ {
p_->decomposeKey(key); p_->decomposeKey(key);
} }
ExifKey::ExifKey(const ExifKey& rhs) ExifKey::ExifKey(const ExifKey& rhs)
: p_(new Impl(*rhs.p_)) : p_(std::make_unique<Impl>(*rhs.p_))
{ {
} }

@ -298,9 +298,9 @@ namespace Exiv2 {
ed.erase(std::remove_if(ed.begin(), ed.end(), FindExifdatum(filteredIfd)), ed.end()); ed.erase(std::remove_if(ed.begin(), ed.end(), FindExifdatum(filteredIfd)), ed.end());
} }
std::unique_ptr<TiffHeaderBase> header(new TiffHeader(byteOrder)); TiffHeader header(byteOrder);
return TiffParserWorker::encode(io, pData, size, ed, iptcData, xmpData, Tag::root, TiffMapping::findEncoder, return TiffParserWorker::encode(io, pData, size, ed, iptcData, xmpData, Tag::root, TiffMapping::findEncoder,
header.get(), nullptr); &header, nullptr);
} // TiffParser::encode } // TiffParser::encode
// ************************************************************************* // *************************************************************************

@ -2057,7 +2057,7 @@ namespace Exiv2 {
// Create standard TIFF header if necessary // Create standard TIFF header if necessary
std::unique_ptr<TiffHeaderBase> ph; std::unique_ptr<TiffHeaderBase> ph;
if (!pHeader) { if (!pHeader) {
ph = std::unique_ptr<TiffHeaderBase>(new TiffHeader); ph = std::make_unique<TiffHeader>();
pHeader = ph.get(); pHeader = ph.get();
} }

Loading…
Cancel
Save