Modified Stack class to use std::array instead of a raw array pointer.

This allows to remove assert statements (and instead use exceptions),
as well as fix a bug issued by Coverity.

Fix final bug reported by Coverity in ComboDialog class, where array
contents weren't being initialized.  This isn't really a bug at all,
since the lambda takes care of initialization, but Coverity doesn't
support lambdas yet.


git-svn-id: svn://svn.code.sf.net/p/stella/code/trunk@3238 8b62c5a3-ac7e-4cc8-8f21-d9a121418aba
This commit is contained in:
stephena 2015-12-11 15:24:29 +00:00
parent fd8eb1aa1d
commit a7f4b76b3b
4 changed files with 33 additions and 52 deletions

View File

@ -15,57 +15,41 @@
// this file, and for a DISCLAIMER OF ALL WARRANTIES.
//
// $Id$
//
// Based on code from ScummVM - Scumm Interpreter
// Copyright (C) 2002-2004 The ScummVM project
//============================================================================
#ifndef STACK_HXX
#define STACK_HXX
#include <cassert>
#include <array>
/**
* Simple fixed size stack class.
*/
namespace Common {
template <class T, int MAX_SIZE = 50>
template <class T, uInt32 CAPACITY = 50>
class FixedStack
{
protected:
T _stack[MAX_SIZE];
int _size;
private:
array<T, CAPACITY> _stack;
uInt32 _size;
public:
FixedStack<T, MAX_SIZE>() : _size(0) { }
FixedStack<T, CAPACITY>() : _size(0) { }
bool empty() const { return _size <= 0; }
bool full() const { return _size >= MAX_SIZE; }
void clear() { _size = 0; }
void push(const T& x)
{
assert(_size < MAX_SIZE);
_stack[_size++] = x;
}
T top() const
{
if(_size > 0)
return _stack[_size - 1];
else
return 0;
}
T pop()
{
assert(_size > 0);
return std::move(_stack[--_size]);
}
int size() const { return _size; }
T operator [](int i) const
{
assert(0 <= i && i < MAX_SIZE);
return _stack[i];
}
bool full() const { return _size >= CAPACITY; }
T top() const { return _stack[_size - 1]; }
void push(const T& x) { _stack[_size++] = x; }
T pop() { return std::move(_stack[--_size]); }
uInt32 size() const { return _size; }
T* begin() { return _stack.begin(); }
T* end() { return _stack.begin() + _size; }
const T* cbegin() const { return _stack.begin(); }
const T* cend() const { return _stack.begin() + _size; }
private:
// Following constructors and assignment operators not supported

View File

@ -71,14 +71,14 @@ ComboDialog::ComboDialog(GuiObject* boss, const GUI::Font& font,
};
xpos = 10;
ADD_EVENT_POPUP(0, "Event 1: ");
ADD_EVENT_POPUP(1, "Event 2: ");
ADD_EVENT_POPUP(2, "Event 3: ");
ADD_EVENT_POPUP(3, "Event 4: ");
ADD_EVENT_POPUP(4, "Event 5: ");
ADD_EVENT_POPUP(5, "Event 6: ");
ADD_EVENT_POPUP(6, "Event 7: ");
ADD_EVENT_POPUP(7, "Event 8: ");
myEvents[0] = nullptr; ADD_EVENT_POPUP(0, "Event 1: ");
myEvents[1] = nullptr; ADD_EVENT_POPUP(1, "Event 2: ");
myEvents[2] = nullptr; ADD_EVENT_POPUP(2, "Event 3: ");
myEvents[3] = nullptr; ADD_EVENT_POPUP(3, "Event 4: ");
myEvents[4] = nullptr; ADD_EVENT_POPUP(4, "Event 5: ");
myEvents[5] = nullptr; ADD_EVENT_POPUP(5, "Event 6: ");
myEvents[6] = nullptr; ADD_EVENT_POPUP(6, "Event 7: ");
myEvents[7] = nullptr; ADD_EVENT_POPUP(7, "Event 8: ");
// Add Defaults, OK and Cancel buttons
ButtonWidget* b;
@ -88,9 +88,6 @@ ComboDialog::ComboDialog(GuiObject* boss, const GUI::Font& font,
addOKCancelBGroup(wid, font);
addToFocusList(wid);
// NOTE: Coverity doesn't yet support lambdas, so it complains that 'myEvents'
// isn't initialized (when it obviously is with ADD_EVENT_POPUP)
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

View File

@ -302,10 +302,10 @@ void Dialog::drawDialog()
// Extra surfaces must be rendered afterwards, so they are drawn on top
if(s.render())
{
for(int i = 0; i < mySurfaceStack.size(); ++i)
for(auto& surface: mySurfaceStack)
{
mySurfaceStack[i]->setDirty();
mySurfaceStack[i]->render();
surface->setDirty();
surface->render();
}
}
}

View File

@ -103,11 +103,11 @@ void DialogContainer::draw(bool full)
// Draw all the dialogs on the stack when we want a full refresh
if(full)
{
for(int i = 0; i < myDialogStack.size(); i++)
for(auto& dialog: myDialogStack)
{
myDialogStack[i]->center();
myDialogStack[i]->setDirty();
myDialogStack[i]->drawDialog();
dialog->center();
dialog->setDirty();
dialog->drawDialog();
}
}
else if(!myDialogStack.empty())