* Replaced some "new" with new(std::nothrow) where appropriate in our base

classes (BView, BWindow, BAlert, BButton - BTextView should be part of this,
  too, to make BAlerts work).
* However, it's not that simple, because there is often no way to return an
  error. Most of that code obviously assumes to be able to throw exceptions
  (it's just not communicated to the caller). Maybe we should just start
  documenting exceptions for R1 (and properly use exceptions later on).
* Automatic white space cleanup.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@28646 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Axel Dörfler
2008-11-14 10:23:38 +00:00
parent d662196ed0
commit eaccfb9dd0
4 changed files with 161 additions and 132 deletions
+15 -9
View File
@@ -1,5 +1,5 @@
/* /*
* Copyright 2001-2006, Haiku. * Copyright 2001-2008, Haiku.
* Distributed under the terms of the MIT License. * Distributed under the terms of the MIT License.
* *
* Authors: * Authors:
@@ -182,7 +182,7 @@ BAlert::BAlert(BMessage* data)
if (data->FindInt32("_but_width", &temp) == B_OK) if (data->FindInt32("_but_width", &temp) == B_OK)
fButtonWidth = (button_width)temp; fButtonWidth = (button_width)temp;
AddCommonFilter(new _BAlertFilter_(this)); AddCommonFilter(new(std::nothrow) _BAlertFilter_(this));
} }
@@ -192,7 +192,7 @@ BAlert::Instantiate(BMessage* data)
if (!validate_instantiation(data, "BAlert")) if (!validate_instantiation(data, "BAlert"))
return NULL; return NULL;
return new BAlert(data); return new(std::nothrow) BAlert(data);
} }
@@ -455,7 +455,10 @@ BAlert::_InitObject(const char* text, const char* button0, const char* button1,
fButtonWidth = buttonWidth; fButtonWidth = buttonWidth;
// Set up the "_master_" view // Set up the "_master_" view
TAlertView* view = new TAlertView(Bounds()); TAlertView* view = new(std::nothrow) TAlertView(Bounds());
if (view == NULL)
return;
AddChild(view); AddChild(view);
view->SetBitmap(_InitIcon()); view->SetBitmap(_InitIcon());
@@ -541,9 +544,12 @@ BAlert::_InitObject(const char* text, const char* button0, const char* button1,
textViewRect.left = (kWindowIconOffset textViewRect.left = (kWindowIconOffset
+ kIconStripeWidth) * iconLayoutScale - 2; + kIconStripeWidth) * iconLayoutScale - 2;
fTextView = new BTextView(textViewRect, "_tv_", fTextView = new(std::nothrow) BTextView(textViewRect, "_tv_",
textViewRect.OffsetByCopy(B_ORIGIN), textViewRect.OffsetByCopy(B_ORIGIN),
B_FOLLOW_LEFT | B_FOLLOW_TOP, B_WILL_DRAW); B_FOLLOW_LEFT | B_FOLLOW_TOP, B_WILL_DRAW);
if (fTextView == NULL)
return;
fTextView->SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR)); fTextView->SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
rgb_color textColor = ui_color(B_PANEL_TEXT_COLOR); rgb_color textColor = ui_color(B_PANEL_TEXT_COLOR);
fTextView->SetFontAndColor(be_plain_font, B_FONT_ALL, &textColor); fTextView->SetFontAndColor(be_plain_font, B_FONT_ALL, &textColor);
@@ -562,7 +568,7 @@ BAlert::_InitObject(const char* text, const char* button0, const char* button1,
textViewRect.bottom += textHeight; textViewRect.bottom += textHeight;
fTextView->SetTextRect(textViewRect); fTextView->SetTextRect(textViewRect);
AddCommonFilter(new _BAlertFilter_(this)); AddCommonFilter(new(std::nothrow) _BAlertFilter_(this));
MoveTo(AlertPosition(Frame().Width(), Frame().Height())); MoveTo(AlertPosition(Frame().Width(), Frame().Height()));
} }
@@ -627,7 +633,7 @@ BAlert::_InitIcon()
int32 iconSize = 32 * icon_layout_scale(); int32 iconSize = 32 * icon_layout_scale();
// Allocate the icon bitmap // Allocate the icon bitmap
icon = new (std::nothrow) BBitmap(BRect(0, 0, iconSize - 1, iconSize - 1), icon = new(std::nothrow) BBitmap(BRect(0, 0, iconSize - 1, iconSize - 1),
0, B_RGBA32); 0, B_RGBA32);
if (icon == NULL || icon->InitCheck() < B_OK) { if (icon == NULL || icon->InitCheck() < B_OK) {
FTRACE((stderr, "BAlert::_InitIcon() - No memory for bitmap\n")); FTRACE((stderr, "BAlert::_InitIcon() - No memory for bitmap\n"));
@@ -693,7 +699,7 @@ BAlert::_CreateButton(int32 which, const char* label)
char name[32]; char name[32];
snprintf(name, sizeof(name), "_b%ld_", which); snprintf(name, sizeof(name), "_b%ld_", which);
BButton* button = new (std::nothrow) BButton(rect, name, label, message, BButton* button = new(std::nothrow) BButton(rect, name, label, message,
B_FOLLOW_RIGHT | B_FOLLOW_BOTTOM); B_FOLLOW_RIGHT | B_FOLLOW_BOTTOM);
if (button == NULL) if (button == NULL)
return NULL; return NULL;
@@ -742,7 +748,7 @@ TAlertView::Instantiate(BMessage* archive)
if (!validate_instantiation(archive, "TAlertView")) if (!validate_instantiation(archive, "TAlertView"))
return NULL; return NULL;
return new TAlertView(archive); return new(std::nothrow) TAlertView(archive);
} }
+11 -9
View File
@@ -1,5 +1,5 @@
/* /*
* Copyright 2001-2005, Haiku. * Copyright 2001-2008, Haiku.
* Distributed under the terms of the MIT License. * Distributed under the terms of the MIT License.
* *
* Authors: * Authors:
@@ -13,6 +13,8 @@
#include <Button.h> #include <Button.h>
#include <new>
#include <Font.h> #include <Font.h>
#include <LayoutUtils.h> #include <LayoutUtils.h>
#include <String.h> #include <String.h>
@@ -21,8 +23,8 @@
#include <binary_compatibility/Interface.h> #include <binary_compatibility/Interface.h>
BButton::BButton(BRect frame, const char *name, const char *label, BMessage *message, BButton::BButton(BRect frame, const char* name, const char* label,
uint32 resizingMode, uint32 flags) BMessage* message, uint32 resizingMode, uint32 flags)
: BControl(frame, name, label, message, resizingMode, : BControl(frame, name, label, message, resizingMode,
flags | B_WILL_DRAW | B_FULL_UPDATE_ON_RESIZE), flags | B_WILL_DRAW | B_FULL_UPDATE_ON_RESIZE),
fPreferredSize(-1, -1), fPreferredSize(-1, -1),
@@ -37,7 +39,7 @@ BButton::BButton(BRect frame, const char *name, const char *label, BMessage *mes
} }
BButton::BButton(const char* name, const char* label, BMessage *message, BButton::BButton(const char* name, const char* label, BMessage* message,
uint32 flags) uint32 flags)
: BControl(name, label, message, : BControl(name, label, message,
flags | B_WILL_DRAW | B_FULL_UPDATE_ON_RESIZE), flags | B_WILL_DRAW | B_FULL_UPDATE_ON_RESIZE),
@@ -47,7 +49,7 @@ BButton::BButton(const char* name, const char* label, BMessage *message,
} }
BButton::BButton(const char* label, BMessage *message) BButton::BButton(const char* label, BMessage* message)
: BControl(NULL, label, message, : BControl(NULL, label, message,
B_WILL_DRAW | B_NAVIGABLE | B_FULL_UPDATE_ON_RESIZE), B_WILL_DRAW | B_NAVIGABLE | B_FULL_UPDATE_ON_RESIZE),
fPreferredSize(-1, -1), fPreferredSize(-1, -1),
@@ -61,7 +63,7 @@ BButton::~BButton()
} }
BButton::BButton(BMessage *archive) BButton::BButton(BMessage* archive)
: BControl(archive), : BControl(archive),
fPreferredSize(-1, -1) fPreferredSize(-1, -1)
{ {
@@ -72,11 +74,11 @@ BButton::BButton(BMessage *archive)
} }
BArchivable * BArchivable*
BButton::Instantiate(BMessage *archive) BButton::Instantiate(BMessage* archive)
{ {
if (validate_instantiation(archive, "BButton")) if (validate_instantiation(archive, "BButton"))
return new BButton(archive); return new(std::nothrow) BButton(archive);
return NULL; return NULL;
} }
+24 -14
View File
@@ -482,7 +482,7 @@ BView::Instantiate(BMessage *data)
if (!validate_instantiation(data , "BView")) if (!validate_instantiation(data , "BView"))
return NULL; return NULL;
return new BView(data); return new(std::nothrow) BView(data);
} }
@@ -1265,11 +1265,14 @@ BView::DragMessage(BMessage *message, BRect dragRect, BHandler *replyTo)
return; return;
} }
// TODO: that's not really what should happen - the app_server should take the chance // TODO: that's not really what should happen - the app_server should take
// *NOT* to need to drag a whole bitmap around but just a frame. // the chance *NOT* to need to drag a whole bitmap around but just a frame.
// create a drag bitmap for the rect // create a drag bitmap for the rect
BBitmap *bitmap = new BBitmap(dragRect, B_RGBA32); BBitmap *bitmap = new(std::nothrow) BBitmap(dragRect, B_RGBA32);
if (bitmap == NULL)
return;
uint32 *bits = (uint32*)bitmap->Bits(); uint32 *bits = (uint32*)bitmap->Bits();
uint32 bytesPerRow = bitmap->BytesPerRow(); uint32 bytesPerRow = bitmap->BytesPerRow();
uint32 width = dragRect.IntegerWidth() + 1; uint32 width = dragRect.IntegerWidth() + 1;
@@ -1316,7 +1319,7 @@ BView::DragMessage(BMessage *message, BBitmap *image,
if (image == NULL) { if (image == NULL) {
// TODO: workaround for drags without a bitmap - should not be necessary if // TODO: workaround for drags without a bitmap - should not be necessary if
// we move the rectangle dragging into the app_server // we move the rectangle dragging into the app_server
image = new (nothrow) BBitmap(BRect(0, 0, 0, 0), B_RGBA32); image = new(std::nothrow) BBitmap(BRect(0, 0, 0, 0), B_RGBA32);
if (image == NULL) if (image == NULL)
return; return;
} }
@@ -1348,8 +1351,8 @@ BView::DragMessage(BMessage *message, BBitmap *image,
// TODO: create area and flatten message into that area! // TODO: create area and flatten message into that area!
// send area info over port, not the actual message! // send area info over port, not the actual message!
int32 bufferSize = privateMessage.NativeFlattenedSize(); int32 bufferSize = privateMessage.NativeFlattenedSize();
char* buffer = new (nothrow) char[bufferSize]; char* buffer = new(std::nothrow) char[bufferSize];
if (buffer) { if (buffer != NULL) {
privateMessage.NativeFlatten(buffer, bufferSize); privateMessage.NativeFlatten(buffer, bufferSize);
fOwner->fLink->StartMessage(AS_VIEW_DRAG_IMAGE); fOwner->fLink->StartMessage(AS_VIEW_DRAG_IMAGE);
@@ -3373,12 +3376,14 @@ BView::BeginLineArray(int32 count)
// not fatal, but it helps during // not fatal, but it helps during
// development of your app and is in // development of your app and is in
// line with R5... // line with R5...
delete [] fCommArray->array; delete[] fCommArray->array;
delete fCommArray; delete fCommArray;
} }
// TODO: since this method cannot return failure, and further AddLine()
// calls with a NULL fCommArray would drop into the debugger anyway,
// we allow the possible std::bad_alloc exceptions here...
fCommArray = new _array_data_; fCommArray = new _array_data_;
fCommArray->maxCount = count; fCommArray->maxCount = count;
fCommArray->count = 0; fCommArray->count = 0;
fCommArray->array = new _array_hdr_[count]; fCommArray->array = new _array_hdr_[count];
@@ -4631,7 +4636,7 @@ BView::_InitData(BRect frame, const char *name, uint32 resizingMode,
// BView constructor. This does not cause problems under BeOS as it just // BView constructor. This does not cause problems under BeOS as it just
// ors the two fields to one 32bit flag. // ors the two fields to one 32bit flag.
// For now we do the same but print the above warning message. // For now we do the same but print the above warning message.
// ToDo: this should be removed at some point and the original // TODO: this should be removed at some point and the original
// version restored: // version restored:
// fFlags = (resizingMode & _RESIZE_MASK_) | (flags & ~_RESIZE_MASK_); // fFlags = (resizingMode & _RESIZE_MASK_) | (flags & ~_RESIZE_MASK_);
fFlags = resizingMode | flags; fFlags = resizingMode | flags;
@@ -4662,6 +4667,8 @@ BView::_InitData(BRect frame, const char *name, uint32 resizingMode,
fIsPrinting = false; fIsPrinting = false;
fAttached = false; fAttached = false;
// TODO: Since we cannot communicate failure, we don't use std::nothrow here
// TODO: Maybe we could auto-delete those views on AddChild() instead?
fState = new BPrivate::ViewState; fState = new BPrivate::ViewState;
fBounds = frame.OffsetToCopy(B_ORIGIN); fBounds = frame.OffsetToCopy(B_ORIGIN);
@@ -4744,19 +4751,22 @@ BView::_ClipToPicture(BPicture *picture, BPoint where,
bounds.right = bounds.left + ((bounds.IntegerWidth() + 1) / 32 + 1) * 32 - 1; bounds.right = bounds.left + ((bounds.IntegerWidth() + 1) / 32 + 1) * 32 - 1;
// TODO: I used a RGBA32 bitmap because drawing on a GRAY8 doesn't work. // TODO: I used a RGBA32 bitmap because drawing on a GRAY8 doesn't work.
BBitmap *bitmap = new BBitmap(bounds, B_RGBA32, true); BBitmap *bitmap = new(std::nothrow) BBitmap(bounds, B_RGBA32, true);
if (bitmap && bitmap->InitCheck() == B_OK && bitmap->Lock()) { if (bitmap != NULL && bitmap->InitCheck() == B_OK && bitmap->Lock()) {
BView *view = new BView(bounds, "drawing view", B_FOLLOW_NONE, 0); BView *view = new(std::nothrow) BView(bounds, "drawing view",
B_FOLLOW_NONE, 0);
if (view != NULL) {
bitmap->AddChild(view); bitmap->AddChild(view);
view->DrawPicture(picture, where); view->DrawPicture(picture, where);
view->Sync(); view->Sync();
}
bitmap->Unlock(); bitmap->Unlock();
} }
BRegion region; BRegion region;
int32 width = bounds.IntegerWidth() + 1; int32 width = bounds.IntegerWidth() + 1;
int32 height = bounds.IntegerHeight() + 1; int32 height = bounds.IntegerHeight() + 1;
if (bitmap->LockBits() == B_OK) { if (bitmap != NULL && bitmap->LockBits() == B_OK) {
uint32 bit = 0; uint32 bit = 0;
uint32 *bits = (uint32 *)bitmap->Bits(); uint32 *bits = (uint32 *)bitmap->Bits();
clipping_rect rect; clipping_rect rect;
+18 -7
View File
@@ -424,7 +424,7 @@ BWindow::Instantiate(BMessage *data)
if (!validate_instantiation(data , "BWindow")) if (!validate_instantiation(data , "BWindow"))
return NULL; return NULL;
return new BWindow(data); return new(std::nothrow) BWindow(data);
} }
@@ -1225,7 +1225,7 @@ FrameMoved(origin);
status_t error = fLink->Read<int32>(&token); status_t error = fLink->Read<int32>(&token);
if (error < B_OK || token == B_NULL_TOKEN) if (error < B_OK || token == B_NULL_TOKEN)
break; break;
ViewUpdateInfo* info = new (std::nothrow) ViewUpdateInfo; ViewUpdateInfo* info = new(std::nothrow) ViewUpdateInfo;
if (info == NULL || !infos.AddItem(info)) { if (info == NULL || !infos.AddItem(info)) {
delete info; delete info;
break; break;
@@ -1596,7 +1596,7 @@ BWindow::SetPulseRate(bigtime_t rate)
if (rate > 0) { if (rate > 0) {
if (fPulseRunner == NULL) { if (fPulseRunner == NULL) {
BMessage message(B_PULSE); BMessage message(B_PULSE);
fPulseRunner = new BMessageRunner(BMessenger(this), fPulseRunner = new(std::nothrow) BMessageRunner(BMessenger(this),
&message, rate); &message, rate);
} else { } else {
fPulseRunner->SetInterval(rate); fPulseRunner->SetInterval(rate);
@@ -1619,7 +1619,9 @@ BWindow::PulseRate() const
void void
BWindow::AddShortcut(uint32 key, uint32 modifiers, BMenuItem *item) BWindow::AddShortcut(uint32 key, uint32 modifiers, BMenuItem *item)
{ {
Shortcut* shortcut = new Shortcut(key, modifiers, item); Shortcut* shortcut = new(std::nothrow) Shortcut(key, modifiers, item);
if (shortcut == NULL)
return;
// removes the shortcut if it already exists! // removes the shortcut if it already exists!
RemoveShortcut(key, modifiers); RemoveShortcut(key, modifiers);
@@ -1636,12 +1638,16 @@ BWindow::AddShortcut(uint32 key, uint32 modifiers, BMessage *message)
void void
BWindow::AddShortcut(uint32 key, uint32 modifiers, BMessage *message, BHandler *target) BWindow::AddShortcut(uint32 key, uint32 modifiers, BMessage* message,
BHandler* target)
{ {
if (message == NULL) if (message == NULL)
return; return;
Shortcut* shortcut = new Shortcut(key, modifiers, message, target); Shortcut* shortcut = new(std::nothrow) Shortcut(key, modifiers, message,
target);
if (shortcut == NULL)
return;
// removes the shortcut if it already exists! // removes the shortcut if it already exists!
RemoveShortcut(key, modifiers); RemoveShortcut(key, modifiers);
@@ -2569,8 +2575,12 @@ BWindow::_InitData(BRect frame, const char* title, window_look look,
STRACE(("BWindow::InitData(): contacting app_server...\n")); STRACE(("BWindow::InitData(): contacting app_server...\n"));
// let app_server know that a window has been created. // let app_server know that a window has been created.
fLink = new BPrivate::PortLink( fLink = new(std::nothrow) BPrivate::PortLink(
BApplication::Private::ServerLink()->SenderPort(), receivePort); BApplication::Private::ServerLink()->SenderPort(), receivePort);
if (fLink == NULL) {
// Zombie!
return;
}
{ {
BPrivate::AppServerLink lockLink; BPrivate::AppServerLink lockLink;
@@ -2873,6 +2883,7 @@ BWindow::_CreateTopView()
STRACE(("_CreateTopView(): enter\n")); STRACE(("_CreateTopView(): enter\n"));
BRect frame = fFrame.OffsetToCopy(B_ORIGIN); BRect frame = fFrame.OffsetToCopy(B_ORIGIN);
// TODO: what to do here about std::nothrow?
fTopView = new BView(frame, "fTopView", fTopView = new BView(frame, "fTopView",
B_FOLLOW_ALL, B_WILL_DRAW); B_FOLLOW_ALL, B_WILL_DRAW);
fTopView->fTopLevelView = true; fTopView->fTopLevelView = true;