Skip to content

Commit a5899b6

Browse files
committed
pass pointer instead of returning void
A bit simpler to directly return from the function. Reduce some indentation as a result. Signed-off-by: Rosen Penev <rosenp@gmail.com>
1 parent b917b34 commit a5899b6

File tree

2 files changed

+20
-28
lines changed

2 files changed

+20
-28
lines changed

src/crwimage_int.cpp

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -140,16 +140,17 @@ CiffDirectory::~CiffDirectory() {
140140
}
141141
}
142142

143-
void CiffComponent::add(UniquePtr component) {
144-
doAdd(std::move(component));
143+
CiffComponent* CiffComponent::add(UniquePtr component) {
144+
return doAdd(std::move(component));
145145
}
146146

147-
void CiffEntry::doAdd(UniquePtr /*component*/) {
147+
CiffComponent* CiffEntry::doAdd(UniquePtr /*component*/) {
148148
throw Error(ErrorCode::kerFunctionNotSupported, "CiffEntry::add");
149149
} // CiffEntry::doAdd
150150

151-
void CiffDirectory::doAdd(UniquePtr component) {
151+
CiffComponent* CiffDirectory::doAdd(UniquePtr component) {
152152
components_.push_back(component.release());
153+
return components_.back();
153154
} // CiffDirectory::doAdd
154155

155156
void CiffHeader::read(const byte* pData, size_t size) {
@@ -542,39 +543,30 @@ CiffComponent* CiffDirectory::doAdd(CrwDirs& crwDirs, uint16_t crwTagId) {
542543
if not found, create it
543544
set value
544545
*/
545-
CiffComponent* cc = nullptr;
546546
if (!crwDirs.empty()) {
547547
auto dir = crwDirs.top();
548548
crwDirs.pop();
549549
// Find the directory
550550
for (const auto& c : components_)
551551
if (c->tag() == dir.dir) {
552-
cc = c;
553-
break;
552+
// Recursive call to next lower level directory
553+
return c->add(crwDirs, crwTagId);
554554
}
555-
if (!cc) {
556-
// Directory doesn't exist yet, add it
557-
auto m = std::make_unique<CiffDirectory>(dir.dir, dir.parent);
558-
add(std::move(m));
559-
cc = components_.back();
560-
}
561-
// Recursive call to next lower level directory
562-
return cc->add(crwDirs, crwTagId);
555+
556+
// Directory doesn't exist yet, add it
557+
auto m = std::make_unique<CiffDirectory>(dir.dir, dir.parent);
558+
return add(std::move(m))->add(crwDirs, crwTagId);
563559
}
564560

565561
// Find the tag
566562
for (const auto& c : components_)
567563
if (c->tagId() == crwTagId) {
568-
cc = c;
569-
break;
564+
return c;
570565
}
571-
if (!cc) {
572-
// Tag doesn't exist yet, add it
573-
auto m = std::make_unique<CiffEntry>(crwTagId, tag());
574-
add(std::move(m));
575-
cc = components_.back();
576-
}
577-
return cc;
566+
567+
// Tag doesn't exist yet, add it
568+
auto m = std::make_unique<CiffEntry>(crwTagId, tag());
569+
return add(std::move(m));
578570
} // CiffDirectory::doAdd
579571

580572
void CiffHeader::remove(uint16_t crwTagId, uint16_t crwDir) const {

src/crwimage_int.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ class CiffComponent {
7373
// Default assignment operator is fine
7474

7575
//! Add a component to the composition
76-
void add(UniquePtr component);
76+
CiffComponent* add(UniquePtr component);
7777
/*!
7878
@brief Add \em crwTagId to the parse tree, if it doesn't exist
7979
yet. \em crwDirs contains the path of subdirectories, starting
@@ -231,7 +231,7 @@ class CiffComponent {
231231
//! @name Manipulators
232232
//@{
233233
//! Implements add()
234-
virtual void doAdd(UniquePtr component) = 0;
234+
virtual CiffComponent* doAdd(UniquePtr component) = 0;
235235
//! Implements add(). The default implementation does nothing.
236236
virtual CiffComponent* doAdd(CrwDirs& crwDirs, uint16_t crwTagId);
237237
//! Implements remove(). The default implementation does nothing.
@@ -291,7 +291,7 @@ class CiffEntry : public CiffComponent {
291291
//@{
292292
using CiffComponent::doAdd;
293293
// See base class comment
294-
void doAdd(UniquePtr component) override;
294+
CiffComponent* doAdd(UniquePtr component) override;
295295
/*!
296296
@brief Implements write(). Writes only the value data of the entry,
297297
using writeValueData().
@@ -339,7 +339,7 @@ class CiffDirectory : public CiffComponent {
339339
//! @name Manipulators
340340
//@{
341341
// See base class comment
342-
void doAdd(UniquePtr component) override;
342+
CiffComponent* doAdd(UniquePtr component) override;
343343
// See base class comment
344344
CiffComponent* doAdd(CrwDirs& crwDirs, uint16_t crwTagId) override;
345345
// See base class comment

0 commit comments

Comments
 (0)