Skip to content

Commit

Permalink
[cleanup] Use std::unique_ptr for TabbedArea
Browse files Browse the repository at this point in the history
  • Loading branch information
Jarod42 committed Oct 15, 2024
1 parent c758bd4 commit e50b3bf
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 29 deletions.
15 changes: 8 additions & 7 deletions include/guisan/widgets/tabbedarea.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,17 @@
#ifndef GCN_TABBEDAREA_HPP
#define GCN_TABBEDAREA_HPP

#include <map>
#include <string>
#include <vector>

#include "guisan/actionlistener.hpp"
#include "guisan/keylistener.hpp"
#include "guisan/mouselistener.hpp"
#include "guisan/platform.hpp"
#include "guisan/widget.hpp"

#include <map>
#include <memory>
#include <string>
#include <vector>

namespace gcn
{
class Container;
Expand Down Expand Up @@ -272,19 +273,19 @@ namespace gcn
/**
* Holds the container for the tabs.
*/
Container* mTabContainer = nullptr;
std::unique_ptr<Container> mTabContainer;

/**
* Holds the container for the widgets.
*/
Container* mWidgetContainer = nullptr;
std::unique_ptr<Container> mWidgetContainer;

/**
* Holds a vector of tabs to delete in the destructor.
* A tab that is to be deleted is a tab that has been
* internally created by the tabbed area.
*/
std::vector<Tab*> mTabsToDelete;
std::vector<std::unique_ptr<Tab>> mTabsToDelete;

/**
* A map between a tab and a widget to display when the
Expand Down
36 changes: 14 additions & 22 deletions src/widgets/tabbedarea.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@

namespace gcn
{
TabbedArea::TabbedArea() : mTabContainer(new Container()), mWidgetContainer(new Container())
TabbedArea::TabbedArea() :
mTabContainer(std::make_unique<Container>()),
mWidgetContainer(std::make_unique<Container>())
{
setFrameSize(1);
setFocusable(true);
Expand All @@ -82,29 +84,21 @@ namespace gcn

mTabContainer->setOpaque(false);

add(mTabContainer);
add(mWidgetContainer);
add(mTabContainer.get());
add(mWidgetContainer.get());
}

TabbedArea::~TabbedArea()
{
remove(mTabContainer);
remove(mWidgetContainer);

delete mTabContainer;
delete mWidgetContainer;

for (unsigned int i = 0; i < mTabsToDelete.size(); i++)
{
delete mTabsToDelete[i];
}
remove(mTabContainer.get());
remove(mWidgetContainer.get());
}

void TabbedArea::addTab(const std::string& caption, Widget* widget)
{
Tab* tab = new Tab();
mTabsToDelete.emplace_back(std::make_unique<Tab>());
auto tab = mTabsToDelete.back().get();
tab->setCaption(caption);
mTabsToDelete.push_back(tab);

addTab(tab, widget);
}
Expand Down Expand Up @@ -170,14 +164,12 @@ namespace gcn
}
}

for (auto iter2 = mTabsToDelete.begin(); iter2 != mTabsToDelete.end(); ++iter2)
auto it2 = std::find_if(mTabsToDelete.begin(), mTabsToDelete.end(), [&](const auto& p) {
return p.get() == tab;
});
if (it2 != mTabsToDelete.end())
{
if (*iter2 == tab)
{
mTabsToDelete.erase(iter2);
delete tab;
break;
}
mTabsToDelete.erase(it2);
}

if (tabIndexToBeSelected == -1)
Expand Down

0 comments on commit e50b3bf

Please sign in to comment.