You know what's better than adding move semantics and initializer

lists to the Array class?  Completely deleting all that code and
using a std::vector directly :)


git-svn-id: svn://svn.code.sf.net/p/stella/code/trunk@3054 8b62c5a3-ac7e-4cc8-8f21-d9a121418aba
This commit is contained in:
stephena 2014-11-07 23:28:40 +00:00
parent 9cf9b41989
commit 740eeed579
27 changed files with 62 additions and 259 deletions

View File

@ -59,7 +59,7 @@ Cheat* CheatManager::add(const string& name, const string& code,
{
if(myCheatList[i]->name() == name || myCheatList[i]->code() == code)
{
myCheatList.remove_at(i);
myCheatList.removeAt(i);
break;
}
}
@ -68,7 +68,7 @@ Cheat* CheatManager::add(const string& name, const string& code,
if(idx == -1)
myCheatList.push_back(cheat);
else
myCheatList.insert_at(idx, cheat);
myCheatList.insertAt(idx, cheat);
// And enable/disable it (the cheat knows how to enable or disable itself)
if(enable)
@ -91,7 +91,7 @@ void CheatManager::remove(int idx)
addPerFrame(c, false);
// Then remove it from the cheatlist entirely
myCheatList.remove_at(idx);
myCheatList.removeAt(idx);
c->disable();
delete c;
}
@ -122,7 +122,7 @@ void CheatManager::addPerFrame(Cheat* cheat, bool enable)
else
{
if(found)
myPerFrameList.remove_at(i);
myPerFrameList.removeAt(i);
}
}

View File

@ -15,9 +15,6 @@
// 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 ARRAY_HXX
@ -30,208 +27,30 @@
namespace Common {
template <class T>
class Array
class Array : public vector<T>
{
protected:
uInt32 _capacity;
uInt32 _size;
T* _data;
public:
typedef T* iterator;
typedef const T* const_iterator;
public:
// Standard c'tor
Array<T>() : _capacity(0), _size(0), _data(nullptr) { }
// Copy c'tor
Array<T>(const Array<T>& array)
: _capacity(array._size+128), _size(array._size), _data(nullptr)
void append(const Array<T>& array)
{
_data = new T[_capacity];
for(uInt32 i = 0; i < _size; i++)
_data[i] = array._data[i];
}
// Copy assignment
Array<T>& operator =(const Array<T>& array)
{
delete [] _data;
_size = array._size;
_capacity = _size + 128;
_data = new T[_capacity];
for(uInt32 i = 0; i < _size; i++)
_data[i] = array._data[i];
return *this;
this->insert(this->end(), array.begin(), array.end());
}
// Move c'tor
Array<T>(Array<T>&& array)
: _capacity(array._capacity), _size(array._size), _data(array._data)
void insertAt(uInt32 idx, const T& element)
{
array._size = 0;
array._capacity = 0;
array._data = nullptr;
}
// Move assignment
Array<T>& operator =(Array<T>&& array)
{
if(this != &array)
{
delete[] _data;
_capacity = array._size;
_size = array._size;
_data = array._data;
array._capacity = 0;
array._size = 0;
array._data = nullptr;
}
return *this;
this->insert(this->cbegin()+idx, element);
}
// Initializer list c'tor
Array<T>(std::initializer_list<T> il) : _capacity(0), _size(0), _data(nullptr)
void removeAt(uInt32 idx)
{
ensureCapacity(il.size());
for(int e: il)
_data[_size++] = e;
}
// D'tor
~Array<T>()
{
if(_data)
{
delete[] _data;
_data = nullptr;
}
}
void reserve(uInt32 capacity)
{
if(capacity > _capacity)
ensureCapacity(capacity - 128);
}
void push_back(const T& element)
{
ensureCapacity(_size + 1);
_data[_size++] = element;
}
void push_back(const Array<T>& array)
{
ensureCapacity(_size + array._size);
for(uInt32 i = 0; i < array._size; i++)
_data[_size++] = array._data[i];
}
void insert_at(uInt32 idx, const T& element)
{
assert(idx >= 0 && idx <= _size);
ensureCapacity(_size + 1);
// The following loop is not efficient if you can just memcpy things around.
// e.g. if you have a list of ints. But for real objects (String...), memcpy
// usually isn't correct (specifically, for any class which has a non-default
// copy behaviour. E.g. the String class uses a refCounter which has to be
// updated whenever a String is copied.
for(uInt32 i = _size; i > idx; i--)
_data[i] = _data[i-1];
_data[idx] = element;
_size++;
}
T remove_at(uInt32 idx)
{
assert(idx >= 0 && idx < _size);
T tmp = _data[idx];
for(uInt32 i = idx; i < _size - 1; i++)
_data[i] = _data[i+1];
_size--;
return tmp;
}
T& operator [](uInt32 idx)
{
assert(idx >= 0 && idx < _size);
return _data[idx];
}
const T& operator [](uInt32 idx) const
{
assert(idx >= 0 && idx < _size);
return _data[idx];
}
uInt32 size() const { return _size; }
uInt32 capacity() const { return _capacity; }
void clear(bool fullerase = true)
{
if(fullerase)
{
if(_data)
{
delete [] _data;
_data = nullptr;
}
_capacity = 0;
}
_size = 0;
}
bool isEmpty() const
{
return (_size == 0);
}
iterator begin()
{
return _data;
}
iterator end()
{
return _data + _size;
}
const_iterator begin() const
{
return _data;
}
const_iterator end() const
{
return _data + _size;
}
protected:
void ensureCapacity(uInt32 new_len)
{
if (new_len <= _capacity)
return;
T* old_data = _data;
_capacity = new_len + 128;
_data = new T[_capacity];
if(old_data)
{
// Copy old data
for(uInt32 i = 0; i < _size; i++)
_data[i] = old_data[i];
delete[] old_data;
}
this->erase(this->cbegin()+idx);
}
};
} // Namespace Common
typedef Common::Array<Int32> IntArray;
typedef Common::Array<bool> BoolArray;
typedef Common::Array<uInt8> ByteArray;
// Common array types
class IntArray : public Common::Array<Int32> { };
class BoolArray : public Common::Array<bool> { };
class ByteArray : public Common::Array<uInt8> { };
#endif

View File

@ -159,7 +159,7 @@ void PNGLibrary::saveImage(const string& filename, const FBSurface& surface,
// Do we want the entire surface or just a section?
png_uint_32 width = rect.width(), height = rect.height();
if(rect.isEmpty())
if(rect.empty())
{
width = surface.width();
height = surface.height();

View File

@ -21,23 +21,10 @@
#define STRING_LIST_HXX
#include "Array.hxx"
#include "bspf.hxx"
class StringList : public Common::Array<string>
{
public:
void push_back(const char* str)
{
ensureCapacity(_size + 1);
_data[_size++] = str;
}
void push_back(const string& str)
{
ensureCapacity(_size + 1);
_data[_size++] = str;
}
static string removePattern(const string& str, const string& pattern)
{
// This can probably be made more efficient ...

View File

@ -76,22 +76,19 @@ class Variant
};
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
static const Variant EmptyVariant("");
static const Variant EmptyVariant = Variant();
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
class VariantList : public Common::Array<pair<string,Variant>>
{
public:
VariantList() { }
void push_back(const Variant& name, const Variant& tag = EmptyVariant)
{
ensureCapacity(_size + 1);
_data[_size++] = make_pair(name.toString(), tag);
emplace_back(name.toString(), tag);
}
};
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
static const VariantList EmptyVarList;
static const VariantList EmptyVarList = VariantList();
#endif

View File

@ -302,7 +302,7 @@ bool CartDebug::disassemble(bool force)
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
bool CartDebug::fillDisassemblyList(BankInfo& info, uInt16 search)
{
myDisassembly.list.clear(false);
myDisassembly.list.clear();
myDisassembly.fieldwidth = 14 + myLabelLength;
DiStella distella(*this, myDisassembly.list, info, DiStella::settings,
myDisLabels, myDisDirectives, myReserved);
@ -1010,7 +1010,7 @@ string CartDebug::saveDisassembly()
{
BankInfo& info = myBankInfo[bank];
// Disassemble bank
disasm.list.clear(false); // don't fully de-allocate space
disasm.list.clear();
DiStella distella(*this, disasm.list, info, settings,
myDisLabels, myDisDirectives, myReserved);

View File

@ -684,7 +684,7 @@ bool Debugger::RewindManager::addState()
myStateList[myTop] = new Serializer();
Serializer& s = *(myStateList[myTop]);
if(s.isValid())
if(s.valid())
{
s.reset();
if(myOSystem.state().saveState(s) && myOSystem.console().tia().saveDisplay(s))
@ -723,7 +723,7 @@ bool Debugger::RewindManager::rewindState()
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
bool Debugger::RewindManager::isEmpty()
bool Debugger::RewindManager::empty()
{
return mySize == 0;
}

View File

@ -328,7 +328,7 @@ class Debugger : public DialogContainer
public:
bool addState();
bool rewindState();
bool isEmpty();
bool empty();
void clear();
private:

View File

@ -886,7 +886,7 @@ void DebuggerParser::executeDelwatch()
int which = args[0] - 1;
if(which >= 0 && which < (int)watches.size())
{
watches.remove_at(which);
watches.removeAt(which);
commandResult << "removed watch";
}
else

View File

@ -28,6 +28,7 @@ struct Command;
#include "bspf.hxx"
#include "Array.hxx"
#include "StringList.hxx"
#include "FrameBuffer.hxx"
#include "Settings.hxx"

View File

@ -450,7 +450,7 @@ void CartridgeCTY::loadTune(uInt8 index)
void CartridgeCTY::loadScore(uInt8 index)
{
Serializer serializer(myEEPROMFile, true);
if(serializer.isValid())
if(serializer.valid())
{
uInt8 scoreRAM[256];
try
@ -470,7 +470,7 @@ void CartridgeCTY::loadScore(uInt8 index)
void CartridgeCTY::saveScore(uInt8 index)
{
Serializer serializer(myEEPROMFile);
if(serializer.isValid())
if(serializer.valid())
{
// Load score RAM
uInt8 scoreRAM[256];
@ -504,7 +504,7 @@ void CartridgeCTY::saveScore(uInt8 index)
void CartridgeCTY::wipeAllScores()
{
Serializer serializer(myEEPROMFile);
if(serializer.isValid())
if(serializer.valid())
{
// Erase score RAM
uInt8 scoreRAM[256];

View File

@ -371,7 +371,7 @@ uInt8 CartridgeFA2::ramReadWrite()
// We go ahead and do the access now, and only return when a sufficient
// amount of time has passed
Serializer serializer(myFlashFile);
if(serializer.isValid())
if(serializer.valid())
{
if(myRAM[255] == 1) // read
{
@ -423,7 +423,7 @@ uInt8 CartridgeFA2::ramReadWrite()
void CartridgeFA2::flash(uInt8 operation)
{
Serializer serializer(myFlashFile);
if(serializer.isValid())
if(serializer.valid())
{
if(operation == 0) // erase
{

View File

@ -27,12 +27,12 @@ class OSystem;
class DialogContainer;
class EventMappingWidget;
class MouseControl;
class StringList;
class VariantList;
#include "Array.hxx"
#include "Event.hxx"
#include "StellaKeys.hxx"
#include "StringList.hxx"
#include "bspf.hxx"
enum MouseButton {

View File

@ -325,7 +325,7 @@ int EventHandler::JoystickHandler::add(StellaJoystick* stick)
}
stick->type = StellaJoystick::JT_REGULAR;
}
mySticks.insert_at(stick->ID, stick);
mySticks.insertAt(stick->ID, stick);
// Map the stelladaptors we've found according to the specified ports
if(specialAdaptor)

View File

@ -39,7 +39,7 @@ void FBSurface::readPixels(uInt8* buffer, uInt32 pitch, const GUI::Rect& rect) c
{
uInt8* src = (uInt8*) myPixels + rect.y() * myPitch + rect.x();
if(rect.isEmpty())
if(rect.empty())
memcpy(buffer, src, width() * height() * 4);
else
{

View File

@ -76,7 +76,7 @@ bool FrameBuffer::initialize()
// Check the 'maxres' setting, which is an undocumented developer feature
// that specifies the desktop size (not normally set)
const GUI::Size& s = myOSystem.settings().getSize("maxres");
if(s.isValid())
if(s.valid())
{
query_w = s.w;
query_h = s.h;
@ -943,9 +943,9 @@ void FrameBuffer::VideoModeList::clear()
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
bool FrameBuffer::VideoModeList::isEmpty() const
bool FrameBuffer::VideoModeList::empty() const
{
return myModeList.isEmpty();
return myModeList.empty();
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

View File

@ -32,7 +32,6 @@ namespace GUI {
#include "EventHandler.hxx"
#include "Rect.hxx"
#include "StringList.hxx"
#include "Variant.hxx"
#include "FBSurface.hxx"
#include "TIASurface.hxx"
@ -484,7 +483,7 @@ class FrameBuffer
void add(const VideoMode& mode);
void clear();
bool isEmpty() const;
bool empty() const;
uInt32 size() const;
void previous();

View File

@ -444,8 +444,8 @@ void M6502::delCondBreak(uInt32 brk)
if(brk < myBreakConds.size())
{
delete myBreakConds[brk];
myBreakConds.remove_at(brk);
myBreakCondNames.remove_at(brk);
myBreakConds.removeAt(brk);
myBreakCondNames.removeAt(brk);
}
}

View File

@ -99,7 +99,7 @@ Serializer::~Serializer()
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
bool Serializer::isValid() const
bool Serializer::valid() const
{
return myStream != NULL;
}

View File

@ -50,7 +50,7 @@ class Serializer
If a file is opened readonly, we can never write to it.
The isValid() method must immediately be called to verify the stream
The valid() method must immediately be called to verify the stream
was correctly initialized.
*/
Serializer(const string& filename, bool readonly = false);
@ -66,7 +66,7 @@ class Serializer
Answers whether the serializer is currently initialized for reading
and writing.
*/
bool isValid() const;
bool valid() const;
/**
Resets the read/write location to the beginning of the stream.

View File

@ -173,7 +173,7 @@ void StateManager::loadState(int slot)
// Make sure the file can be opened in read-only mode
Serializer in(buf.str(), true);
if(!in.isValid())
if(!in.valid())
{
buf.str("");
buf << "Can't open/load from state file " << slot;
@ -217,7 +217,7 @@ void StateManager::saveState(int slot)
// Make sure the file can be opened for writing
Serializer out(buf.str());
if(!out.isValid())
if(!out.valid())
{
buf.str("");
buf << "Can't open/save to state file " << slot;
@ -267,7 +267,7 @@ bool StateManager::loadState(Serializer& in)
if(myOSystem.hasConsole())
{
// Make sure the file can be opened for reading
if(in.isValid())
if(in.valid())
{
// First test if we have a valid header and cart type
// If so, do a complete state load using the Console
@ -287,7 +287,7 @@ bool StateManager::saveState(Serializer& out)
if(myOSystem.hasConsole())
{
// Make sure the file can be opened for writing
if(out.isValid())
if(out.valid())
{
// Add header so that if the state format changes in the future,
// we'll know right away, without having to parse the rest of the file

View File

@ -114,7 +114,7 @@ void ComboDialog::loadConfig()
StringList events;
instance().eventHandler().getComboListForEvent(myComboEvent, events);
int size = BSPF_min(events.size(), 8u);
int size = BSPF_min((int)events.size(), 8);
for(int i = 0; i < size; ++i)
myEvents[i]->setSelected("", events[i]);

View File

@ -150,7 +150,7 @@ void Dialog::addToFocusList(WidgetArray& list)
for(uInt32 i = 0; i < list.size(); ++i)
list[i]->setFlags(WIDGET_RETAIN_FOCUS);
_myFocus.list.push_back(list);
_myFocus.list.append(list);
_focusList = _myFocus.list;
if(list.size() > 0)
@ -176,14 +176,14 @@ void Dialog::addToFocusList(WidgetArray& list, TabWidget* w, int tabId)
// Now insert in the correct place in that focus list
uInt32 id = tabId;
if(id < focus.size())
focus[id].list.push_back(list);
focus[id].list.append(list);
else
{
// Make sure the array is large enough
while(focus.size() <= id)
focus.push_back(Focus());
focus[id].list.push_back(list);
focus[id].list.append(list);
}
if(list.size() > 0)
@ -245,13 +245,13 @@ void Dialog::buildCurrentFocusList(int tabID)
_myTabList[id].appendFocusList(_focusList);
// Add remaining items from main focus list
_focusList.push_back(_myFocus.list);
_focusList.append(_myFocus.list);
// Add button group at end of current focus list
// We do it this way for TabWidget, so that buttons are scanned
// *after* the widgets in the current tab
if(_buttonGroup.size() > 0)
_focusList.push_back(_buttonGroup);
_focusList.append(_buttonGroup);
// Finally, the moment we've all been waiting for :)
// Set the actual focus widget
@ -727,7 +727,7 @@ void Dialog::TabFocus::appendFocusList(WidgetArray& list)
int active = widget->getActiveTab();
if(active >= 0 && active < (int)focus.size())
list.push_back(focus[active].list);
list.append(focus[active].list);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

View File

@ -254,7 +254,7 @@ void LauncherDialog::loadConfig()
// Assume that if the list is empty, this is the first time that loadConfig()
// has been called (and we should reload the list)
if(myList->getList().isEmpty())
if(myList->getList().empty())
{
myPrevDirButton->setEnabled(false);
myCurrentNode = FilesystemNode(romdir == "" ? "~" : romdir);

View File

@ -88,7 +88,7 @@ void ListWidget::setSelected(int item)
void ListWidget::setSelected(const string& item)
{
int selected = -1;
if(!_list.isEmpty())
if(!_list.empty())
{
if(item == "")
selected = 0;

View File

@ -76,7 +76,7 @@ void MessageBox::addText(const GUI::Font& font, const StringList& text)
for(uInt32 i = 0; i < text.size(); ++i)
str_w = BSPF_max((int)text[i].length(), str_w);
_w = BSPF_min(str_w * fontWidth + 20, _w);
_h = BSPF_min(((text.size() + 2) * lineHeight + 20), (uInt32)_h);
_h = BSPF_min((uInt32)((text.size() + 2) * lineHeight + 20), (uInt32)_h);
xpos = 10; ypos = 10;
for(uInt32 i = 0; i < text.size(); ++i)

View File

@ -74,7 +74,7 @@ struct Size
if(c != 'x')
w = h = 0;
}
bool isValid() const { return w > 0 && h > 0; }
bool valid() const { return w > 0 && h > 0; }
Size& operator=(const Size& s) { w = s.w; h = s.h; return *this; };
bool operator==(const Size& s) const { return w == s.w && h == s.h; };
@ -118,7 +118,7 @@ struct Rect
Rect(const Point& p, uInt32 w, uInt32 h) : top(p.y), left(p.x), bottom(h), right(w) {}
Rect(uInt32 x1, uInt32 y1, uInt32 x2, uInt32 y2) : top(y1), left(x1), bottom(y2), right(x2)
{
assert(isValid());
assert(valid());
}
uInt32 x() const { return left; }
@ -138,14 +138,14 @@ struct Rect
left = x1;
bottom = y2;
right = x2;
assert(isValid());
assert(valid());
}
bool isValid() const {
bool valid() const {
return (left <= right && top <= bottom);
}
bool isEmpty() const {
bool empty() const {
return top == 0 && left == 0 && bottom == 0 && right == 0;
}