Use the classes from AutoDeleter.h to improve BLayout::AddView() and to simplify the ViewRemover struct created for BLayout::AddItem().

This commit is contained in:
Alex Wilson
2011-10-28 14:16:13 -06:00
parent ea907ab587
commit 30b07d019d
+13 -25
View File
@@ -13,12 +13,15 @@
#include <LayoutContext.h> #include <LayoutContext.h>
#include <Message.h> #include <Message.h>
#include <AutoDeleter.h>
#include <View.h> #include <View.h>
#include <ViewPrivate.h> #include <ViewPrivate.h>
#include "ViewLayoutItem.h" #include "ViewLayoutItem.h"
using BPrivate::AutoDeleter;
using std::nothrow; using std::nothrow;
using std::swap; using std::swap;
@@ -43,27 +46,9 @@ namespace {
struct ViewRemover { struct ViewRemover {
ViewRemover(BView* view) : fView(view) {}; inline void operator()(BView* view) {
BView::Private(view).RemoveSelf();
~ViewRemover()
{
if (fView)
BView::Private(fView).RemoveSelf();
} }
void SetTo(BView* view)
{
if (fView)
BView::Private(fView).RemoveSelf();
fView = view;
}
void Detach()
{
fView = NULL;
}
BView* fView;
}; };
} }
@@ -146,14 +131,17 @@ BLayoutItem*
BLayout::AddView(int32 index, BView* child) BLayout::AddView(int32 index, BView* child)
{ {
BLayoutItem* item = child->GetLayout(); BLayoutItem* item = child->GetLayout();
if (!item) ObjectDeleter<BLayoutItem> itemDeleter(NULL);
if (!item) {
item = new(nothrow) BViewLayoutItem(child); item = new(nothrow) BViewLayoutItem(child);
itemDeleter.SetTo(item);
}
if (item && AddItem(index, item)) if (item && AddItem(index, item)) {
itemDeleter.Detach();
return item; return item;
}
if (!child->GetLayout())
delete item;
return NULL; return NULL;
} }
@@ -174,7 +162,7 @@ BLayout::AddItem(int32 index, BLayoutItem* item)
// if the item refers to a BView, we make sure it is added to the parent // if the item refers to a BView, we make sure it is added to the parent
// view // view
BView* view = item->View(); BView* view = item->View();
ViewRemover remover(view); AutoDeleter<BView, ViewRemover> remover(view);
if (view && view->fParent != fTarget && !fTarget->_AddChild(view, NULL)) { if (view && view->fParent != fTarget && !fTarget->_AddChild(view, NULL)) {
remover.Detach(); // view wasn't added remover.Detach(); // view wasn't added
return false; return false;