Skip to content

Commit

Permalink
pass pointer instead of returning void
Browse files Browse the repository at this point in the history
A bit simpler to directly return from the function.

Reduce some indentation as a result.

Signed-off-by: Rosen Penev <[email protected]>
  • Loading branch information
neheb committed Feb 7, 2025
1 parent b917b34 commit a5899b6
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 28 deletions.
40 changes: 16 additions & 24 deletions src/crwimage_int.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,16 +140,17 @@ CiffDirectory::~CiffDirectory() {
}
}

void CiffComponent::add(UniquePtr component) {
doAdd(std::move(component));
CiffComponent* CiffComponent::add(UniquePtr component) {
return doAdd(std::move(component));
}

void CiffEntry::doAdd(UniquePtr /*component*/) {
CiffComponent* CiffEntry::doAdd(UniquePtr /*component*/) {
throw Error(ErrorCode::kerFunctionNotSupported, "CiffEntry::add");
} // CiffEntry::doAdd

void CiffDirectory::doAdd(UniquePtr component) {
CiffComponent* CiffDirectory::doAdd(UniquePtr component) {
components_.push_back(component.release());
return components_.back();
} // CiffDirectory::doAdd

void CiffHeader::read(const byte* pData, size_t size) {
Expand Down Expand Up @@ -542,39 +543,30 @@ CiffComponent* CiffDirectory::doAdd(CrwDirs& crwDirs, uint16_t crwTagId) {
if not found, create it
set value
*/
CiffComponent* cc = nullptr;
if (!crwDirs.empty()) {
auto dir = crwDirs.top();
crwDirs.pop();
// Find the directory
for (const auto& c : components_)
if (c->tag() == dir.dir) {
cc = c;
break;
// Recursive call to next lower level directory
return c->add(crwDirs, crwTagId);
}
if (!cc) {
// Directory doesn't exist yet, add it
auto m = std::make_unique<CiffDirectory>(dir.dir, dir.parent);
add(std::move(m));
cc = components_.back();
}
// Recursive call to next lower level directory
return cc->add(crwDirs, crwTagId);

// Directory doesn't exist yet, add it
auto m = std::make_unique<CiffDirectory>(dir.dir, dir.parent);
return add(std::move(m))->add(crwDirs, crwTagId);
}

// Find the tag
for (const auto& c : components_)
if (c->tagId() == crwTagId) {
cc = c;
break;
return c;
}
if (!cc) {
// Tag doesn't exist yet, add it
auto m = std::make_unique<CiffEntry>(crwTagId, tag());
add(std::move(m));
cc = components_.back();
}
return cc;

// Tag doesn't exist yet, add it
auto m = std::make_unique<CiffEntry>(crwTagId, tag());
return add(std::move(m));
} // CiffDirectory::doAdd

void CiffHeader::remove(uint16_t crwTagId, uint16_t crwDir) const {
Expand Down
8 changes: 4 additions & 4 deletions src/crwimage_int.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class CiffComponent {
// Default assignment operator is fine

//! Add a component to the composition
void add(UniquePtr component);
CiffComponent* add(UniquePtr component);
/*!
@brief Add \em crwTagId to the parse tree, if it doesn't exist
yet. \em crwDirs contains the path of subdirectories, starting
Expand Down Expand Up @@ -231,7 +231,7 @@ class CiffComponent {
//! @name Manipulators
//@{
//! Implements add()
virtual void doAdd(UniquePtr component) = 0;
virtual CiffComponent* doAdd(UniquePtr component) = 0;
//! Implements add(). The default implementation does nothing.
virtual CiffComponent* doAdd(CrwDirs& crwDirs, uint16_t crwTagId);
//! Implements remove(). The default implementation does nothing.
Expand Down Expand Up @@ -291,7 +291,7 @@ class CiffEntry : public CiffComponent {
//@{
using CiffComponent::doAdd;
// See base class comment
void doAdd(UniquePtr component) override;
CiffComponent* doAdd(UniquePtr component) override;
/*!
@brief Implements write(). Writes only the value data of the entry,
using writeValueData().
Expand Down Expand Up @@ -339,7 +339,7 @@ class CiffDirectory : public CiffComponent {
//! @name Manipulators
//@{
// See base class comment
void doAdd(UniquePtr component) override;
CiffComponent* doAdd(UniquePtr component) override;
// See base class comment
CiffComponent* doAdd(CrwDirs& crwDirs, uint16_t crwTagId) override;
// See base class comment
Expand Down

0 comments on commit a5899b6

Please sign in to comment.