Skip to content

Commit 6bbe609

Browse files
committed
crwimage_int: remove C pointers
Easier to use references. Signed-off-by: Rosen Penev <rosenp@gmail.com>
1 parent a5899b6 commit 6bbe609

File tree

2 files changed

+17
-35
lines changed

2 files changed

+17
-35
lines changed

src/crwimage_int.cpp

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -134,22 +134,16 @@ const CrwSubDir CrwMap::crwSubDir_[] = {
134134
CiffComponent::CiffComponent(uint16_t tag, uint16_t dir) : dir_(dir), tag_(tag) {
135135
}
136136

137-
CiffDirectory::~CiffDirectory() {
138-
for (auto&& component : components_) {
139-
delete component;
140-
}
141-
}
142-
143-
CiffComponent* CiffComponent::add(UniquePtr component) {
137+
const CiffComponent::UniquePtr& CiffComponent::add(UniquePtr component) {
144138
return doAdd(std::move(component));
145139
}
146140

147-
CiffComponent* CiffEntry::doAdd(UniquePtr /*component*/) {
141+
const CiffComponent::UniquePtr& CiffEntry::doAdd(UniquePtr /*component*/) {
148142
throw Error(ErrorCode::kerFunctionNotSupported, "CiffEntry::add");
149143
} // CiffEntry::doAdd
150144

151-
CiffComponent* CiffDirectory::doAdd(UniquePtr component) {
152-
components_.push_back(component.release());
145+
const CiffComponent::UniquePtr& CiffDirectory::doAdd(UniquePtr component) {
146+
components_.push_back(std::move(component));
153147
return components_.back();
154148
} // CiffDirectory::doAdd
155149

@@ -517,20 +511,20 @@ void CiffHeader::add(uint16_t crwTagId, uint16_t crwDir, DataBuf&& buf) {
517511
if (!pRootDir_) {
518512
pRootDir_ = std::make_unique<CiffDirectory>();
519513
}
520-
if (auto child = pRootDir_->add(crwDirs, crwTagId)) {
514+
if (const auto& child = pRootDir_->add(crwDirs, crwTagId)) {
521515
child->setValue(std::move(buf));
522516
}
523517
} // CiffHeader::add
524518

525-
CiffComponent* CiffComponent::add(CrwDirs& crwDirs, uint16_t crwTagId) {
519+
const CiffComponent::UniquePtr& CiffComponent::add(CrwDirs& crwDirs, uint16_t crwTagId) {
526520
return doAdd(crwDirs, crwTagId);
527521
} // CiffComponent::add
528522

529-
CiffComponent* CiffComponent::doAdd(CrwDirs& /*crwDirs*/, uint16_t /*crwTagId*/) {
530-
return nullptr;
523+
const CiffComponent::UniquePtr& CiffComponent::doAdd(CrwDirs& /*crwDirs*/, uint16_t /*crwTagId*/) {
524+
throw Error(ErrorCode::kerFunctionNotSupported, "CiffEntry::doAdd");
531525
} // CiffComponent::doAdd
532526

533-
CiffComponent* CiffDirectory::doAdd(CrwDirs& crwDirs, uint16_t crwTagId) {
527+
const CiffComponent::UniquePtr& CiffDirectory::doAdd(CrwDirs& crwDirs, uint16_t crwTagId) {
534528
/*
535529
add()
536530
if stack not empty
@@ -602,7 +596,6 @@ void CiffDirectory::doRemove(CrwDirs& crwDirs, uint16_t crwTagId) {
602596
// Find the tag
603597
auto it = std::find_if(components_.begin(), components_.end(), [=](const auto& c) { return c->tag() == crwTagId; });
604598
if (it != components_.end()) {
605-
delete *it;
606599
components_.erase(it);
607600
}
608601
}

src/crwimage_int.hpp

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -54,16 +54,14 @@ class CiffComponent {
5454
//! CiffComponent auto_ptr type
5555
using UniquePtr = std::unique_ptr<CiffComponent>;
5656
//! Container type to hold all metadata
57-
using Components = std::vector<CiffComponent*>;
57+
using Components = std::vector<UniquePtr>;
5858

5959
//! @name Creators
6060
//@{
6161
//! Default constructor
6262
CiffComponent() = default;
6363
//! Constructor taking a tag and directory
6464
CiffComponent(uint16_t tag, uint16_t dir);
65-
CiffComponent(const CiffComponent&) = delete;
66-
CiffComponent& operator=(const CiffComponent&) = delete;
6765
//! Virtual destructor.
6866
virtual ~CiffComponent() = default;
6967
//@}
@@ -73,7 +71,7 @@ class CiffComponent {
7371
// Default assignment operator is fine
7472

7573
//! Add a component to the composition
76-
CiffComponent* add(UniquePtr component);
74+
const UniquePtr& add(UniquePtr component);
7775
/*!
7876
@brief Add \em crwTagId to the parse tree, if it doesn't exist
7977
yet. \em crwDirs contains the path of subdirectories, starting
@@ -87,7 +85,7 @@ class CiffComponent {
8785
8886
@return A pointer to the newly added component.
8987
*/
90-
CiffComponent* add(CrwDirs& crwDirs, uint16_t crwTagId);
88+
const UniquePtr& add(CrwDirs& crwDirs, uint16_t crwTagId);
9189
/*!
9290
@brief Remove \em crwTagId from the parse tree, if it exists yet. \em
9391
crwDirs contains the path of subdirectories, starting with the
@@ -231,9 +229,9 @@ class CiffComponent {
231229
//! @name Manipulators
232230
//@{
233231
//! Implements add()
234-
virtual CiffComponent* doAdd(UniquePtr component) = 0;
232+
virtual const UniquePtr& doAdd(UniquePtr component) = 0;
235233
//! Implements add(). The default implementation does nothing.
236-
virtual CiffComponent* doAdd(CrwDirs& crwDirs, uint16_t crwTagId);
234+
virtual const UniquePtr& doAdd(CrwDirs& crwDirs, uint16_t crwTagId);
237235
//! Implements remove(). The default implementation does nothing.
238236
virtual void doRemove(CrwDirs& crwDirs, uint16_t crwTagId);
239237
//! Implements read(). The default implementation reads a directory entry.
@@ -291,7 +289,7 @@ class CiffEntry : public CiffComponent {
291289
//@{
292290
using CiffComponent::doAdd;
293291
// See base class comment
294-
CiffComponent* doAdd(UniquePtr component) override;
292+
const UniquePtr& doAdd(UniquePtr component) override;
295293
/*!
296294
@brief Implements write(). Writes only the value data of the entry,
297295
using writeValueData().
@@ -312,15 +310,6 @@ class CiffDirectory : public CiffComponent {
312310
using CiffComponent::CiffComponent;
313311

314312
public:
315-
//! @name Creators
316-
//@{
317-
//! Virtual destructor
318-
~CiffDirectory() override;
319-
//@}
320-
321-
CiffDirectory(const CiffDirectory&) = delete;
322-
CiffDirectory& operator=(const CiffDirectory&) = delete;
323-
324313
//! @name Manipulators
325314
//@{
326315
// Default assignment operator is fine
@@ -339,9 +328,9 @@ class CiffDirectory : public CiffComponent {
339328
//! @name Manipulators
340329
//@{
341330
// See base class comment
342-
CiffComponent* doAdd(UniquePtr component) override;
331+
const UniquePtr& doAdd(UniquePtr component) override;
343332
// See base class comment
344-
CiffComponent* doAdd(CrwDirs& crwDirs, uint16_t crwTagId) override;
333+
const UniquePtr& doAdd(CrwDirs& crwDirs, uint16_t crwTagId) override;
345334
// See base class comment
346335
void doRemove(CrwDirs& crwDirs, uint16_t crwTagId) override;
347336
/*!

0 commit comments

Comments
 (0)