Avoid naked new operator in sample apps

This commit is contained in:
Luis Díaz Más 2022-02-20 10:10:46 +01:00
parent b3517a6ab0
commit 2505e52345
4 changed files with 25 additions and 21 deletions

View File

@ -57,7 +57,7 @@ try {
std::cout << "Added a few tags the quick way.\n";
// Create a ASCII string value (note the use of create)
Exiv2::Value::UniquePtr v = Exiv2::Value::create(Exiv2::asciiString);
auto v = Exiv2::Value::create(Exiv2::asciiString);
// Set the value to a string
v->read("1999:12:31 23:59:59");
// Add the value together with its key to the Exif data container
@ -66,16 +66,16 @@ try {
std::cout << "Added key \"" << key << "\", value \"" << *v << "\"\n";
// Now create a more interesting value (without using the create method)
Exiv2::URationalValue::UniquePtr rv(new Exiv2::URationalValue);
Exiv2::URationalValue rv;
// Set two rational components from a string
rv->read("1/2 1/3");
rv.read("1/2 1/3");
// Add more elements through the extended interface of rational value
rv->value_.emplace_back(2, 3);
rv->value_.emplace_back(3, 4);
rv.value_.emplace_back(2, 3);
rv.value_.emplace_back(3, 4);
// Add the key and value pair to the Exif data
key = Exiv2::ExifKey("Exif.Image.PrimaryChromaticities");
exifData.add(key, rv.get());
std::cout << "Added key \"" << key << "\", value \"" << *rv << "\"\n";
exifData.add(key, &rv);
std::cout << "Added key \"" << key << "\", value \"" << rv << "\"\n";
// *************************************************************************
// Modify Exif data
@ -92,18 +92,21 @@ try {
// Alternatively, we can use findKey()
key = Exiv2::ExifKey("Exif.Image.PrimaryChromaticities");
auto pos = exifData.findKey(key);
if (pos == exifData.end()) throw Exiv2::Error(Exiv2::kerErrorMessage, "Key not found");
if (pos == exifData.end())
throw Exiv2::Error(Exiv2::kerErrorMessage, "Key not found");
// Get a pointer to a copy of the value
v = pos->getValue();
// Downcast the Value pointer to its actual type
auto prv = dynamic_cast<Exiv2::URationalValue*>(v.release());
if (prv == nullptr)
throw Exiv2::Error(Exiv2::kerErrorMessage, "Downcast failed");
rv = Exiv2::URationalValue::UniquePtr(prv);
rv = Exiv2::URationalValue(*prv);
// Modify the value directly through the interface of URationalValue
rv->value_.at(2) = {88, 77};
rv.value_.at(2) = {88, 77};
// Copy the modified value back to the metadatum
pos->setValue(rv.get());
pos->setValue(&rv);
std::cout << "Modified key \"" << key
<< "\", new value \"" << pos->value() << "\"\n";

View File

@ -65,8 +65,10 @@ int main(int argc, char* const argv[])
if ( argc >= 5 ) {
int blocksize = argc==6 ? atoi(ba) : 10000;
// ensure blocksize is sane
if (blocksize>1024*1024) blocksize=10000;
Exiv2::byte* bytes = blocksize > 0 ? new Exiv2::byte[blocksize] : nullptr;
if (blocksize>1024*1024)
blocksize=10000;
std::vector<Exiv2::byte> bytes (blocksize);
// copy fileIn from a remote location.
BasicIo::UniquePtr io = Exiv2::ImageFactory::createIo(fr);
@ -78,11 +80,11 @@ int main(int argc, char* const argv[])
Error(Exiv2::kerFileOpenFailed, output.path() , "w+b", strError());
}
size_t l = 0;
if ( bytes ) {
if ( !bytes.empty() ) {
int r ;
while ( (r=io->read(bytes,blocksize)) > 0 ) {
while ( (r=io->read(bytes.data(),blocksize)) > 0 ) {
l += r;
output.write(bytes,r) ;
output.write(bytes.data(),r) ;
}
} else {
// read/write byte-wise (#1029)
@ -90,7 +92,6 @@ int main(int argc, char* const argv[])
output.putb(io->getb()) ;
}
}
delete[] bytes;
output.close();
}

View File

@ -49,9 +49,9 @@ try {
}
// Use MemIo to increase test coverage.
Exiv2::BasicIo::UniquePtr fileIo(new Exiv2::FileIo(params.read_));
Exiv2::BasicIo::UniquePtr memIo(new Exiv2::MemIo);
memIo->transfer(*fileIo);
Exiv2::FileIo fileIo(params.read_);
auto memIo = std::make_unique<Exiv2::MemIo>();
memIo->transfer(fileIo);
Exiv2::Image::UniquePtr readImg = Exiv2::ImageFactory::open(std::move(memIo));
assert(readImg.get() != 0);

View File

@ -91,7 +91,7 @@ void mini1(const char* path)
void mini9(const char* path)
{
TiffImage tiffImage(BasicIo::UniquePtr(new FileIo(path)), false);
TiffImage tiffImage(std::make_unique<FileIo>(path), false);
tiffImage.readMetadata();
std::cout << "MIME type: " << tiffImage.mimeType() << "\n";