Convert Documents from std::list<> to a class.

This commit is contained in:
David Capello 2011-03-13 19:41:59 -03:00
parent 8d53d10a77
commit 55c611c08b
5 changed files with 144 additions and 28 deletions

View File

@ -73,6 +73,7 @@ add_library(aseprite-library
check_args.cpp check_args.cpp
console.cpp console.cpp
context.cpp context.cpp
documents.cpp
gfxmode.cpp gfxmode.cpp
gui_xml.cpp gui_xml.cpp
job.cpp job.cpp

View File

@ -18,13 +18,13 @@
#include "config.h" #include "config.h"
#include <algorithm>
#include "console.h" #include "console.h"
#include "context.h" #include "context.h"
#include "commands/command.h" #include "commands/command.h"
#include "raster/sprite.h" #include "raster/sprite.h"
#include <algorithm>
Context::Context(ISettings* settings) Context::Context(ISettings* settings)
{ {
m_currentSprite = NULL; m_currentSprite = NULL;
@ -33,13 +33,6 @@ Context::Context(ISettings* settings)
Context::~Context() Context::~Context()
{ {
for (Documents::iterator
it = m_documents.begin(), end = m_documents.end(); it != end; ++it) {
Sprite* sprite = *it;
delete sprite;
}
m_documents.clear();
delete m_settings; delete m_settings;
} }
@ -51,7 +44,7 @@ const Documents& Context::getDocuments() const
Sprite* Context::getFirstSprite() const Sprite* Context::getFirstSprite() const
{ {
if (!m_documents.empty()) if (!m_documents.empty())
return m_documents.front(); return m_documents.getByIndex(0);
else else
return NULL; return NULL;
} }
@ -74,7 +67,7 @@ void Context::addSprite(Sprite* sprite)
{ {
ASSERT(sprite != NULL); ASSERT(sprite != NULL);
m_documents.push_front(sprite); m_documents.addDocument(sprite);
// Generate onAddSprite event // Generate onAddSprite event
onAddSprite(sprite); onAddSprite(sprite);
@ -84,11 +77,8 @@ void Context::removeSprite(Sprite* sprite)
{ {
ASSERT(sprite != NULL); ASSERT(sprite != NULL);
Documents::iterator it = std::find(m_documents.begin(), m_documents.end(), sprite); // Remove the item from the sprites list.
ASSERT(it != m_documents.end()); m_documents.removeDocument(sprite);
// remove the item from the sprites list
m_documents.erase(it);
// generate on_remove_sprite event // generate on_remove_sprite event
onRemoveSprite(sprite); onRemoveSprite(sprite);
@ -100,16 +90,9 @@ void Context::removeSprite(Sprite* sprite)
void Context::sendSpriteToTop(Sprite* sprite) void Context::sendSpriteToTop(Sprite* sprite)
{ {
ASSERT(sprite); ASSERT(sprite != NULL);
Documents::iterator it = std::find(m_documents.begin(), m_documents.end(), sprite); m_documents.moveDocument(sprite, 0);
ASSERT(it != m_documents.end());
// remove the item from the sprites list
m_documents.erase(it);
// add it again
m_documents.push_front(sprite);
} }
Sprite* Context::getCurrentSprite() const Sprite* Context::getCurrentSprite() const

View File

@ -19,9 +19,9 @@
#ifndef CONTEXT_H_INCLUDED #ifndef CONTEXT_H_INCLUDED
#define CONTEXT_H_INCLUDED #define CONTEXT_H_INCLUDED
#include <list>
#include "base/disable_copying.h" #include "base/disable_copying.h"
#include "base/exception.h" #include "base/exception.h"
#include "documents.h"
#include "settings/settings.h" #include "settings/settings.h"
class Sprite; class Sprite;
@ -29,8 +29,6 @@ class SpriteReader;
class Command; class Command;
class Params; class Params;
typedef std::list<Sprite*> Documents;
class CommandPreconditionException : public base::Exception class CommandPreconditionException : public base::Exception
{ {
public: public:

65
src/documents.cpp Normal file
View File

@ -0,0 +1,65 @@
/* ASE - Allegro Sprite Editor
* Copyright (C) 2001-2011 David Capello
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "config.h"
#include "documents.h"
#include "raster/sprite.h"
#include <algorithm>
Documents::Documents()
{
}
Documents::~Documents()
{
deleteAll();
}
void Documents::addDocument(Sprite* sprite)
{
ASSERT(sprite != NULL);
m_documents.insert(begin(), sprite);
}
void Documents::removeDocument(Sprite* sprite)
{
iterator it = std::find(begin(), end(), sprite);
ASSERT(it != end());
if (it != end())
m_documents.erase(it);
}
void Documents::moveDocument(Sprite* sprite, int index)
{
removeDocument(sprite);
m_documents.insert(begin()+index, sprite);
}
void Documents::deleteAll()
{
for (iterator it = begin(), end = this->end(); it != end; ++it) {
Sprite* sprite = *it;
delete sprite;
}
m_documents.clear();
}

69
src/documents.h Normal file
View File

@ -0,0 +1,69 @@
/* ASE - Allegro Sprite Editor
* Copyright (C) 2001-2011 David Capello
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifndef DOCUMENTS_H_INCLUDED
#define DOCUMENTS_H_INCLUDED
#include "base/disable_copying.h"
#include <vector>
class Sprite;
class Documents
{
public:
typedef std::vector<Sprite*>::iterator iterator;
typedef std::vector<Sprite*>::const_iterator const_iterator;
Documents();
~Documents();
iterator begin() { return m_documents.begin(); }
iterator end() { return m_documents.end(); }
const_iterator begin() const { return m_documents.begin(); }
const_iterator end() const { return m_documents.end(); }
int size() const { return m_documents.size(); }
bool empty() const { return m_documents.empty(); }
// Adds a new document to the list. If the destructor is called the
// document is deleted automatically.
void addDocument(Sprite* sprite);
// Removes a document from the list without deleting it. You must to
// delete the document after removing it.
void removeDocument(Sprite* sprite);
// Moves the document to the given location in the same
// list. E.g. It is used to reorder documents when they are selected
// as active.
void moveDocument(Sprite* sprite, int index);
// Deletes all documents in the list (calling "delete" operation).
void deleteAll();
Sprite* getByIndex(int index) const { return m_documents[index]; }
private:
std::vector<Sprite*> m_documents;
DISABLE_COPYING(Documents)
};
#endif