diff options
| author | Ben Boeckel <MathStuf@gmail.com> | 2008-03-31 01:17:59 +0000 |
|---|---|---|
| committer | Ben Boeckel <MathStuf@gmail.com> | 2008-03-31 01:17:59 +0000 |
| commit | 9a65bc6bb7c8da1dfa5b101579e52845c75848ef (patch) | |
| tree | 258900f882a6998ac6fa525bd247e302028a8911 /general | |
| parent | 8e1ec2aec50999bae30625303f2c96e5b3b7f318 (diff) | |
[FIX] Member variables now use m_ prefix
[FIX] Lots of minor fixes
git-svn-id: https://pokegen.svn.sourceforge.net/svnroot/pokegen/trunk@95 6ecfd1a5-f3ed-3746-8530-beee90d26b22
Diffstat (limited to 'general')
| -rw-r--r-- | general/Audio.cpp | 61 | ||||
| -rw-r--r-- | general/Audio.h | 81 | ||||
| -rw-r--r-- | general/BugCatcher.cpp | 40 | ||||
| -rw-r--r-- | general/BugCatcher.h | 57 | ||||
| -rw-r--r-- | general/Exception.h | 86 | ||||
| -rw-r--r-- | general/Flag.cpp | 38 | ||||
| -rw-r--r-- | general/Flag.h | 77 | ||||
| -rw-r--r-- | general/Frac.cpp | 85 | ||||
| -rw-r--r-- | general/Frac.h | 94 | ||||
| -rw-r--r-- | general/Hat.h | 100 | ||||
| -rw-r--r-- | general/ImageCache.cpp | 49 | ||||
| -rw-r--r-- | general/ImageCache.h | 46 | ||||
| -rw-r--r-- | general/Ini.cpp | 139 | ||||
| -rw-r--r-- | general/Ini.h | 72 | ||||
| -rw-r--r-- | general/Matrix.h | 225 | ||||
| -rw-r--r-- | general/Point.h | 92 | ||||
| -rw-r--r-- | general/TODO | 0 | ||||
| -rw-r--r-- | general/general.pro | 2 |
18 files changed, 664 insertions, 680 deletions
diff --git a/general/Audio.cpp b/general/Audio.cpp index 034de04d..46f2853b 100644 --- a/general/Audio.cpp +++ b/general/Audio.cpp @@ -1,25 +1,24 @@ -///////////////////////////////////////////////////////////////////////////// -// Name: general/Audio.cpp -// Purpose: Cache for sound effects -// Author: Ben Boeckel -// Modified by: Ben Boeckel -// Created: Fri Jan 18 21:34:16 2008 -// Copyright: ©2007-2008 Ben Boeckel and Nerdy Productions -// Licence: -// 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 3 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, see <http://www.gnu.org/licenses/>. -///////////////////////////////////////////////////////////////////////////// +/* + * Copyright 2008 Ben Boeckel <MathStuf@gmail.com> + * + * 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 3 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, see <http://www.gnu.org/licenses/>. + */ +// Qt includes +#include <QMutableListIterator> + +// Header include #include "Audio.h" void Audio::playSFX(const QString& url) throw(OpenException) @@ -29,28 +28,28 @@ void Audio::playSFX(const QString& url) throw(OpenException) if (sfx->state() == Phonon::ErrorState) throw(OpenException("Audio", url)); sfx->play(); - curPlay.append(sfx); + m_curPlay.append(sfx); } void Audio::playMusic(const QString& url) throw(OpenException) { - if (!started) + if (!m_started) start(); - musicUrl = url; - if (music.state() == Phonon::PlayingState) - music.seek(music.totalTime() - 1000); + m_musicUrl = url; + if (m_music.state() == Phonon::PlayingState) + m_music.seek(m_music.totalTime() - 1000); else { - music.setCurrentSource(url); - if (music.state() == Phonon::ErrorState) + m_music.setCurrentSource(url); + if (m_music.state() == Phonon::ErrorState) throw(OpenException("Audio", url)); - music.play(); + m_music.play(); } } void Audio::prune() { - for (QMutableListIterator<Phonon::MediaObject*> i(curPlay); i.hasNext(); i.next()) + for (QMutableListIterator<Phonon::MediaObject*> i(m_curPlay); i.hasNext(); i.next()) { if (i.value()->state() == Phonon::StoppedState) { @@ -62,7 +61,7 @@ void Audio::prune() void Audio::clear() { - for (QMutableListIterator<Phonon::MediaObject*> i(curPlay); i.hasNext(); i.next()) + for (QMutableListIterator<Phonon::MediaObject*> i(m_curPlay); i.hasNext(); i.next()) { if (i.value()->state() == Phonon::PlayingState) i.value()->stop(); diff --git a/general/Audio.h b/general/Audio.h index 9c841af0..aef1cb63 100644 --- a/general/Audio.h +++ b/general/Audio.h @@ -1,35 +1,34 @@ -///////////////////////////////////////////////////////////////////////////// -// Name: general/Audio.h -// Purpose: Sound effect handling -// Author: Ben Boeckel -// Modified by: Ben Boeckel -// Created: Fri Jan 18 15:29:22 2008 -// Copyright: ©2007-2008 Ben Boeckel and Nerdy Productions -// Licence: -// 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 3 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, see <http://www.gnu.org/licenses/>. -///////////////////////////////////////////////////////////////////////////// +/* + * Copyright 2008 Ben Boeckel <MathStuf@gmail.com> + * + * 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 3 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, see <http://www.gnu.org/licenses/>. + */ #ifndef __AUDIO__ #define __AUDIO__ +// KDE includes #include <phonon/audiooutput.h> -//#include <Phonon/AudioOutput> #include <phonon/mediaobject.h> -//#include <Phonon/MediaObject> +// QT includes +// #include <Phonon/AudioOutput> +// #include <Phonon/MediaObject> +#include <QList> #include <QObject> +// General includes #include "Exception.h" class Audio : public QObject @@ -37,12 +36,12 @@ class Audio : public QObject Q_OBJECT public: - Audio() : - output(Phonon::MusicCategory), - started(false) + inline Audio() : + m_output(Phonon::MusicCategory), + m_started(false) { } - ~Audio() + inline ~Audio() { clear(); } @@ -53,31 +52,31 @@ class Audio : public QObject void prune(); void clear(); - double getVolume() + inline double volume() { - return output.volume(); + return m_output.volume(); } - void setVolume(const double v) + inline void setVolume(const double volume) { - output.setVolume(v); + m_output.setVolume(volume); } private: - void start() + inline void start() { - if (!started) - connect(&music, SIGNAL(aboutToFinish()), this, SLOT(loopMusic())); + if (!m_started) + connect(&m_music, SIGNAL(aboutToFinish()), this, SLOT(loopMusic())); } private slots: - void loopMusic() + inline void loopMusic() { - music.enqueue(musicUrl); + m_music.enqueue(m_musicUrl); } private: - QList<Phonon::MediaObject*> curPlay; - Phonon::MediaObject music; - QString musicUrl; - Phonon::AudioOutput output; - bool started; + QList<Phonon::MediaObject*> m_curPlay; + Phonon::MediaObject m_music; + QString m_musicUrl; + Phonon::AudioOutput m_output; + bool m_started; }; #endif diff --git a/general/BugCatcher.cpp b/general/BugCatcher.cpp index eff96816..e83dc87c 100644 --- a/general/BugCatcher.cpp +++ b/general/BugCatcher.cpp @@ -1,25 +1,21 @@ -///////////////////////////////////////////////////////////////////////////// -// Name: general/BugCatcher.cpp -// Purpose: Exceptions get sent here -// Author: Ben Boeckel -// Modified by: Ben Boeckel -// Created: Tue Jan 22 14:47:50 2008 -// Copyright: ©2007-2008 Ben Boeckel and Nerdy Productions -// Licence: -// 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 3 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, see <http://www.gnu.org/licenses/>. -///////////////////////////////////////////////////////////////////////////// +/* + * Copyright 2008 Ben Boeckel <MathStuf@gmail.com> + * + * 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 3 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, see <http://www.gnu.org/licenses/>. + */ +// Header include #include "BugCatcher.h" -KAboutData* BugCatcher::about = NULL; +KAboutData* BugCatcher::m_aboutData = NULL; diff --git a/general/BugCatcher.h b/general/BugCatcher.h index bfae6290..8d1bc1e0 100644 --- a/general/BugCatcher.h +++ b/general/BugCatcher.h @@ -1,55 +1,52 @@ -///////////////////////////////////////////////////////////////////////////// -// Name: general/BugCatcher.h -// Purpose: Exceptions get sent here -// Author: Ben Boeckel -// Modified by: Ben Boeckel -// Created: Mon Jan 21 15:57:40 2008 -// Copyright: ©2007-2008 Ben Boeckel and Nerdy Productions -// Licence: -// 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 3 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, see <http://www.gnu.org/licenses/>. -///////////////////////////////////////////////////////////////////////////// +/* + * Copyright 2008 Ben Boeckel <MathStuf@gmail.com> + * + * 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 3 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, see <http://www.gnu.org/licenses/>. + */ #ifndef __BUGCATCHER__ #define __BUGCATCHER__ -#include <kaboutdata.h> +// KDE includes +class KAboutData; #include <kbugreport.h> #include <kmessagebox.h> +// General includes #include "Exception.h" class BugCatcher { public: - static void setAbout(KAboutData* data) + static void setAbout(KAboutData* aboutData) { - about = data; + m_aboutData = aboutData; } static void deleteAbout() { - about = NULL; + m_aboutData = NULL; } - static void report(const Exception& e) + static void report(const Exception& exception) { - if (!about) + if (!m_aboutData) return; - KBugReport bug(NULL, true, about); - if (KMessageBox::questionYesNo(&bug, "Missed an error catching spot. Please email me (MathStuf@gmail.com) with the error and \"PokeModr - error\" as the subject.", e.getMsg()) == KMessageBox::Yes) + KBugReport bug(NULL, true, m_aboutData); + if (KMessageBox::questionYesNo(&bug, "Missed an error catching spot. Please email me (MathStuf@gmail.com) with the error and \"PokeModr - error\" as the subject.", exception.getMsg()) == KMessageBox::Yes) bug.exec(); } private: - static KAboutData* about; + static KAboutData* m_aboutData; }; #endif diff --git a/general/Exception.h b/general/Exception.h index ea8b314a..c5eacd4e 100644 --- a/general/Exception.h +++ b/general/Exception.h @@ -1,53 +1,49 @@ -///////////////////////////////////////////////////////////////////////////// -// Name: general/Exception.h -// Purpose: Define exceptions used throughout PokéGen -// Author: Ben Boeckel -// Modified by: Ben Boeckel -// Created: Wed Jan 16 13:13:13 2008 -// Copyright: ©2007-2008 Ben Boeckel and Nerdy Productions -// Licence: -// 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 3 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, see <http://www.gnu.org/licenses/>. -///////////////////////////////////////////////////////////////////////////// +/* + * Copyright 2007-2008 Ben Boeckel <MathStuf@gmail.com> + * + * 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 3 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, see <http://www.gnu.org/licenses/>. + */ #ifndef __EXCEPTIONS__ #define __EXCEPTIONS__ +// Qt includes #include <QString> class Exception { public: - Exception(const QString& c, const QString e) : - cls(c), - error(e) + Exception(const QString& className, const QString error) : + m_className(className), + m_error(error) { } QString getMsg() const { - return QString("%1: %2").arg(cls).arg(error); + return QString("%1: %2").arg(m_className).arg(m_error); } private: - const QString cls; - const QString error; + const QString m_className; + const QString m_error; }; class UnusedException : public Exception { public: - UnusedException(const QString& c, const QString& var) : - Exception(c, QString("setting unused %1").arg(var)) + UnusedException(const QString& className, const QString& variable) : + Exception(className, QString("setting unused %1").arg(variable)) { } }; @@ -55,8 +51,8 @@ class UnusedException : public Exception class BoundsException : public Exception { public: - BoundsException(const QString& c, const QString& var) : - Exception(c, QString("%1 out-of-bounds").arg(var)) + BoundsException(const QString& className, const QString& variable) : + Exception(className, QString("%1 out-of-bounds").arg(variable)) { } }; @@ -64,8 +60,8 @@ class BoundsException : public Exception class IndexException : public BoundsException { public: - IndexException(const QString& c) : - BoundsException(c, "index") + IndexException(const QString& className) : + BoundsException(className, "index") { } }; @@ -73,8 +69,8 @@ class IndexException : public BoundsException class ReplaceException : public Exception { public: - ReplaceException(const QString& c, const QString& f) : - Exception(c, QString("unable to replace %1").arg(f)) + ReplaceException(const QString& className, const QString& fileName) : + Exception(className, QString("unable to replace %1").arg(fileName)) { } }; @@ -82,8 +78,8 @@ class ReplaceException : public Exception class SaveException : public Exception { public: - SaveException(const QString& c, const QString& f) : - Exception(c, QString("unable to save %1").arg(f)) + SaveException(const QString& className, const QString& fileName) : + Exception(className, QString("unable to save %1").arg(fileName)) { } }; @@ -91,8 +87,8 @@ class SaveException : public Exception class OpenException : public Exception { public: - OpenException(const QString& c, const QString& f) : - Exception(c, QString("unable to open %1").arg(f)) + OpenException(const QString& className, const QString& fileName) : + Exception(className, QString("unable to open %1").arg(fileName)) { } }; @@ -100,8 +96,8 @@ class OpenException : public Exception class DirException : public Exception { public: - DirException(const QString& c, const QString& p) : - Exception(c, QString("unable to create path %1").arg(p)) + DirException(const QString& className, const QString& path) : + Exception(className, QString("unable to create path %1").arg(path)) { } }; @@ -109,8 +105,8 @@ class DirException : public Exception class InvalidException : public Exception { public: - InvalidException(const QString& c, const QString& f) : - Exception(c, QString("%1 is invalid").arg(f)) + InvalidException(const QString& className, const QString& fileName) : + Exception(className, QString("%1 is invalid").arg(fileName)) { } }; @@ -118,8 +114,8 @@ class InvalidException : public Exception class RemoveException : public Exception { public: - RemoveException(const QString& c, const QString& f) : - Exception(c, QString("unable to remove %1").arg(f)) + RemoveException(const QString& className, const QString& fileName) : + Exception(className, QString("unable to remove %1").arg(fileName)) { } }; diff --git a/general/Flag.cpp b/general/Flag.cpp index f2002f5a..cc8918b4 100644 --- a/general/Flag.cpp +++ b/general/Flag.cpp @@ -1,25 +1,21 @@ -///////////////////////////////////////////////////////////////////////////// -// Name: general/Flag.cpp -// Purpose: A class that handles flags -// Author: Ben Boeckel -// Modified by: Ben Boeckel -// Created: Sun Mar 18 15:25:16 2007 -// Copyright: ©2007-2008 Ben Boeckel and Nerdy Productions -// Licence: -// 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 3 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, see <http://www.gnu.org/licenses/>. -///////////////////////////////////////////////////////////////////////////// +/* + * Copyright 2007-2008 Ben Boeckel <MathStuf@gmail.com> + * + * 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 3 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, see <http://www.gnu.org/licenses/>. + */ +// Header include #include "Flag.h" const QStringList Flag::ValueStr = QStringList() << "Off" << "On" << "Ignore"; diff --git a/general/Flag.h b/general/Flag.h index 1b7d5857..ecf567f3 100644 --- a/general/Flag.h +++ b/general/Flag.h @@ -1,30 +1,29 @@ -///////////////////////////////////////////////////////////////////////////// -// Name: general/Flag.h -// Purpose: A class that handles flags -// Author: Ben Boeckel -// Modified by: Ben Boeckel -// Created: Tue Mar 20 19:22 2007 -// Copyright: ©2007-2008 Ben Boeckel and Nerdy Productions -// Licence: -// 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 3 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, see <http://www.gnu.org/licenses/>. -///////////////////////////////////////////////////////////////////////////// +/* + * Copyright 2007-2008 Ben Boeckel <MathStuf@gmail.com> + * + * 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 3 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, see <http://www.gnu.org/licenses/>. + */ #ifndef __FLAG__ #define __FLAG__ +// Qt includes #include <QStringList> +// General includes +#include "Exception.h" + class Flag { public: @@ -37,39 +36,39 @@ class Flag }; static const QStringList ValueStr; - Flag(const int f = 0, const int s = 0) : - flag(f) + inline Flag(const int flag = 0, const int status = 0) : + m_flag(flag) { - setStatus(s); + setStatus(status); } - void set(const int f, const int s) + inline void set(const int flag, const int status) { - setFlag(f); - setStatus(s); + setFlag(flag); + setStatus(status); } - void setFlag(const int f) + inline void setFlag(const int flag) { - flag = f; + m_flag = flag; } - void setStatus(const int s) + void setStatus(const int status) throw(BoundsException) { - if (End <= s) - throw("Flag: status out-of-bounds"); - status = s; + if (End <= status) + throw(BoundsException("Flag", "status")); + m_status = status; } - int getFlag() const + inline int flag() const { - return flag; + return m_flag; } - int getStatus() const + inline int status() const { - return status; + return m_status; } private: - int flag; - int status; + int m_flag; + int m_status; }; #endif diff --git a/general/Frac.cpp b/general/Frac.cpp index 317fdc53..36d0f591 100644 --- a/general/Frac.cpp +++ b/general/Frac.cpp @@ -1,60 +1,53 @@ -///////////////////////////////////////////////////////////////////////////// -// Name: general/Frac.cpp -// Purpose: A class to make handling fractions easier -// Author: Ben Boeckel -// Modified by: Ben Boeckel -// Created: Sun Mar 18 15:25:16 2007 -// Copyright: ©2007-2008 Ben Boeckel and Nerdy Productions -// Licence: -// 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 3 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, see <http://www.gnu.org/licenses/>. -///////////////////////////////////////////////////////////////////////////// +/* + * Copyright 2007-2008 Ben Boeckel <MathStuf@gmail.com> + * + * 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 3 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, see <http://www.gnu.org/licenses/>. + */ +// Header include #include "Frac.h" -void Frac::set(const int n, const int d) throw(Exception) +void Frac::set(const int numerator, const int denominator) throw(Exception) { - if (((type == Improper) || ((n <= d) && (type == Proper)) || ((d <= n) && (type == Over1))) && (0 < d)) + if (((m_type == Improper) || ((numerator <= denominator) && (m_type == Proper)) || ((denominator <= numerator) && (m_type == Over1))) && (0 < numerator)) { - num = n; - denom = d; + m_numerator = numerator; + m_denominator = denominator; } else throw(Exception("Frac", "values conflict with type")); } -void Frac::set(const int n, const int d, const int t) throw(Exception) +void Frac::set(const int numerator, const int demoninator, const int type) throw(Exception) { - setType(t); - set(n, d); + setType(type); + set(numerator, demoninator); } -void Frac::setNum(const int n) throw(Exception) +void Frac::setType(const int type) throw(BoundsException) { - set(n, denom); -} - -void Frac::setDenom(const int d) throw(Exception) -{ - set(num, d); -} - -void Frac::setType(const int t) throw(BoundsException) -{ - if (t < End) + if (type < End) { - type = t; - set(1, 1); + m_type = type; + try + { + set(m_numerator, m_denominator); + } + catch (Exception& e) + { + set(1, 1); + } } else throw(BoundsException("Frac", "type")); @@ -62,10 +55,10 @@ void Frac::setType(const int t) throw(BoundsException) void Frac::reduce() { - int i = num; - int j = denom; + int i = m_numerator; + int j = m_denominator; while (i - j) (i > j) ? (i -= j) : (j -= i); - num /= i; - denom /= i; + m_numerator /= i; + m_denominator /= i; } diff --git a/general/Frac.h b/general/Frac.h index adf39b06..6a2a8f70 100644 --- a/general/Frac.h +++ b/general/Frac.h @@ -1,31 +1,29 @@ -///////////////////////////////////////////////////////////////////////////// -// Name: general/Frac.h -// Purpose: A class to make handling fractions easier -// Author: Ben Boeckel -// Modified by: Ben Boeckel -// Created: Sun Mar 18 15:25:16 2007 -// Copyright: ©2007-2008 Ben Boeckel and Nerdy Productions -// Licence: -// 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 3 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, see <http://www.gnu.org/licenses/>. -///////////////////////////////////////////////////////////////////////////// +/* + * Copyright 2007-2008 Ben Boeckel <MathStuf@gmail.com> + * + * 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 3 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, see <http://www.gnu.org/licenses/>. + */ #ifndef __FRAC__ #define __FRAC__ +// Qt includes +#include <QtGlobal> #include <QFile> #include <QString> +// General includes #include "Exception.h" class Frac @@ -34,49 +32,59 @@ class Frac enum { Proper = 0, - Improper = 1, - Over1 = 2, + Over1 = 1, + Improper = 2, End = 3 }; - Frac(const int t = Proper) + inline Frac(const int type = Proper) { - setType(t); + set(1, 1, type); } - Frac(const int n, const int d, const int t = Proper) throw(Exception) + inline Frac(const int numerator, const int denominator, const int type = Proper) throw(Exception) { - set(n, d, t); + set(numerator, denominator, type); } - void set(const int n, const int d) throw(Exception); - void set(const int n, const int d, const int t) throw(Exception); - void setNum(const int n) throw(Exception); - void setDenom(const int d) throw(Exception); - void setType(const int t) throw(BoundsException); + void set(const int numerator, const int denominator) throw(Exception); + void set(const int numerator, const int denominator, const int type) throw(Exception); + inline void setNumerator(const int numerator) throw(Exception) + { + set(numerator, m_denominator); + } + inline void setDenominator(const int denominator) throw(Exception) + { + set(m_numerator, denominator); + } + void setType(const int type) throw(BoundsException); - int getNum() const + inline int numerator() const { - return num; + return m_numerator; } - int getDenom() const + inline int denominator() const { - return denom; + return m_denominator; } - int getType() const + inline int type() const { - return type; + return m_type; } void reduce(); - operator double() const + inline operator double() const + { + return (double(m_numerator) / m_denominator); + } + inline Frac operator*(const Frac& rhs) const { - return (double(num) / denom); + return Frac(m_numerator * rhs.m_numerator, m_denominator * rhs.m_denominator, qMax(m_type, rhs.m_type)); } private: - int num; - int denom; - int type; + int m_numerator; + int m_denominator; + int m_type; }; #endif diff --git a/general/Hat.h b/general/Hat.h index 904f0cb9..ebb19e40 100644 --- a/general/Hat.h +++ b/general/Hat.h @@ -1,83 +1,93 @@ -///////////////////////////////////////////////////////////////////////////// -// Name: general/Hat.h -// Purpose: Class for easy random containers -// Author: Ben Boeckel -// Modified by: Ben Boeckel -// Created: Tue May 29 2007 08:23:57 -// Copyright: ©2007-2008 Ben Boeckel and Nerdy Productions -// Licence: -// 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 3 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, see <http://www.gnu.org/licenses/>. -///////////////////////////////////////////////////////////////////////////// +/* + * Copyright 2007-2008 Ben Boeckel <MathStuf@gmail.com> + * + * 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 3 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, see <http://www.gnu.org/licenses/>. + */ #ifndef __HAT__ #define __HAT__ +// Qt includes +#include <QtGlobal> #include <QDateTime> #include <QMap> -#include <QMutableMapIterator> -#include <QtGlobal> + +// General includes +#include "Exception.h" template<class T> class Hat { public: Hat() : - totalWeight(1) + m_count(0) { qsrand(QDateTime().toTime_t()); } T pick() const { - int choice = qrand() % totalWeight; - for (QMutableMapIterator<T, unsigned> i(objects); i.hasNext(); i.next()) + int choice = qrand() % m_count; + foreach (T key, m_objects.keys()) { - choice -= i.value(); + choice -= m_objects[key]; if (choice < 0) - return i.key(); + break; } + return key; } T take() { T chosen = pick(); - if (!(--objects[chosen])) - objects.erase(chosen); - --totalWeight; + if (!(--m_objects[chosen])) + m_objects.erase(chosen); + --m_count; return chosen; } - - void setCount(const T& key, const unsigned wt) + void setCount(const T& key, const unsigned weight) { - if (objects.find(key) != objects.end()) - totalWeight -= objects[key]; - objects[key] = wt; - totalWeight += wt; + if (m_objects.contains(key)) + m_count -= m_objects[key]; + m_objects[key] = weight; + m_count += weight; } - - unsigned getCount(const T& key) const + void add(const T& key, const unsigned weight) + { + m_objects[key] += weight; + m_count += weight; + } + unsigned count() const + { + return m_count; + } + unsigned count(const T& key) const + { + if (m_objects.contains(key)) + return m_objects[key]; + return 0; + } + double chance(const T& key) const { - if (objects.find(key) != objects.end()) - return objects[key]; - throw("Hat: key does not exist"); + return (double(count(key)) / m_count); } unsigned operator[](const T& key) const { - return getCount(key); + return count(key); } private: - QMap<T, unsigned> objects; - unsigned totalCount; + QMap<T, unsigned> m_objects; + unsigned m_count; }; #endif diff --git a/general/ImageCache.cpp b/general/ImageCache.cpp index a5ce28f3..22f09a03 100644 --- a/general/ImageCache.cpp +++ b/general/ImageCache.cpp @@ -1,36 +1,33 @@ -///////////////////////////////////////////////////////////////////////////// -// Name: general/ImageCache.h -// Purpose: Cache for quick image loading -// Author: Ben Boeckel -// Modified by: Ben Boeckel -// Created: Thu Jan 17 2008 13:42:44 -// Copyright: ©2007-2008 Ben Boeckel and Nerdy Productions -// Licence: -// 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 3 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, see <http://www.gnu.org/licenses/>. -///////////////////////////////////////////////////////////////////////////// +/* + * Copyright 2008 Ben Boeckel <MathStuf@gmail.com> + * + * 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 3 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, see <http://www.gnu.org/licenses/>. + */ +// Qt includes #include <QFile> #include <QString> +// Header include #include "ImageCache.h" -KPixmapCache ImageCache::cache("pokegen"); +KPixmapCache ImageCache::m_cache("pokegen"); -QPixmap ImageCache::open(const QString& fname) throw(OpenException) +QPixmap ImageCache::open(const QString& fileName) throw(OpenException) { - if (!QFile::exists(fname)) - throw(OpenException("ImageCache", fname)); - QPixmap pm(cache.loadFromFile(fname)); + if (!QFile::exists(fileName)) + throw(OpenException("ImageCache", fileName)); + QPixmap pm(m_cache.loadFromFile(fileName)); return pm; } diff --git a/general/ImageCache.h b/general/ImageCache.h index e693dc1d..3f8b29c0 100644 --- a/general/ImageCache.h +++ b/general/ImageCache.h @@ -1,44 +1,42 @@ -///////////////////////////////////////////////////////////////////////////// -// Name: general/ImageCache.h -// Purpose: Cache for quick image loading -// Author: Ben Boeckel -// Modified by: Ben Boeckel -// Created: Thu Jan 17 2008 12:35:22 -// Copyright: ©2007-2008 Ben Boeckel and Nerdy Productions -// Licence: -// 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 3 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, see <http://www.gnu.org/licenses/>. -///////////////////////////////////////////////////////////////////////////// +/* + * Copyright 2008 Ben Boeckel <MathStuf@gmail.com> + * + * 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 3 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, see <http://www.gnu.org/licenses/>. + */ #ifndef __IMAGECACHE__ #define __IMAGECACHE__ +// KDE includes #include <kpixmapcache.h> +// Qt includes #include <QPixmap> +// General includes #include "Exception.h" class ImageCache { public: - static QPixmap open(const QString& fname) throw(OpenException); + static QPixmap open(const QString& fileName) throw(OpenException); static void clear() { - cache.discard(); + m_cache.discard(); } private: - static KPixmapCache cache; + static KPixmapCache m_cache; }; #endif diff --git a/general/Ini.cpp b/general/Ini.cpp index 269a832d..859dc59b 100644 --- a/general/Ini.cpp +++ b/general/Ini.cpp @@ -1,45 +1,42 @@ -///////////////////////////////////////////////////////////////////////////// -// Name: general/Ini.cpp -// Purpose: Define sections for data files -// Author: Ben Boeckel -// Modified by: Ben Boeckel -// Created: Fri May 4 23:27:37 2007 -// Copyright: ©2007-2008 Ben Boeckel and Nerdy Productions -// Licence: -// 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 3 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, see <http://www.gnu.org/licenses/>. -///////////////////////////////////////////////////////////////////////////// - +/* + * Copyright 2007-2008 Ben Boeckel <MathStuf@gmail.com> + * + * 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 3 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, see <http://www.gnu.org/licenses/>. + */ + +// Qt includes #include <QDir> #include <QStringList> #include <QTextStream> +// Header include #include "Ini.h" Ini::Ini() { } -Ini::Ini(const QString& fname) throw(Exception) +Ini::Ini(const QString& fileName) throw(Exception) { - load(fname); + load(fileName); } -void Ini::load(const QString& fname) throw(Exception) +void Ini::load(const QString& fileName) throw(Exception) { - QFile fin(fname); + QFile fin(fileName); if (!fin.exists()) - throw(OpenException("Ini", fname)); + throw(OpenException("Ini", fileName)); load(fin); fin.close(); } @@ -51,30 +48,30 @@ void Ini::load(QFile& fin) throw(InvalidException) QString field; QString value; int pos; - fields.clear(); + m_fields.clear(); while (!file.atEnd()) { pos = line.indexOf('='); - if (pos == -1) + if (pos == INT_MAX) throw(InvalidException("Ini", fin.fileName())); field = line.mid(0, pos - 1); value = line.mid(pos + 1, line.length() - pos); if (field.isEmpty()) throw(InvalidException("Ini", fin.fileName())); - fields[field] = value; + m_fields[field] = value; line = file.readLine(); } } -void Ini::save(const QString& fname) const throw(Exception) +void Ini::save(const QString& fileName) const throw(Exception) { - QStringList path = fname.split(QDir::separator(), QString::SkipEmptyParts); + QStringList path = fileName.split(QDir::separator(), QString::SkipEmptyParts); path.removeLast(); if (!QDir().mkpath(path.join("/"))) throw(DirException("Ini", path.join("/"))); - QFile fout(fname); + QFile fout(fileName); if (!fout.open(QIODevice::WriteOnly)) - throw(OpenException("Ini", fname)); + throw(OpenException("Ini", fileName)); save(fout); fout.close(); } @@ -82,96 +79,96 @@ void Ini::save(const QString& fname) const throw(Exception) void Ini::save(QFile& file) const { QTextStream fout(&file); - for (QMapIterator<QString, QString> i(fields); i.hasNext(); i.next()) - fout << i.key() << '=' << i.value() << '\n'; + foreach (QString field, m_fields) + fout << field << '=' << m_fields[field] << '\n'; fout << '\n'; } -void Ini::addField(const QString& n, const bool v) +void Ini::addField(const QString& field, const bool value) { - fields[n] = v ? "true" : "false"; + m_fields[field] = value ? "true" : "false"; } -void Ini::addField(const QString& n, const unsigned char v) +void Ini::addField(const QString& field, const unsigned char value) { - fields[n] = QString::number(v); + m_fields[field] = QString::number(value); } -void Ini::addField(const QString& n, const int v) +void Ini::addField(const QString& field, const int value) { - fields[n] = QString::number(v); + m_fields[field] = QString::number(value); } -void Ini::addField(const QString& n, const double v) +void Ini::addField(const QString& field, const double value) { - fields[n] = QString::number(v); + m_fields[field] = QString::number(value); } -void Ini::addField(const QString& n, const QString& v) +void Ini::addField(const QString& field, const QString& value) { - fields[n] = v; + m_fields[field] = value; } -void Ini::getValue(const QString& field, bool& val, const bool def) +void Ini::getValue(const QString& field, bool& value, const bool defaultValue) { - if (!fields.contains(field)) + if (!m_fields.contains(field)) { - val = def; + value = defaultValue; return; } - val = (fields[field] == "true") ? true : ((fields[field] == "false") ? false : def); + value = (m_fields[field] == "true") ? true : ((m_fields[field] == "false") ? false : defaultValue); } -void Ini::getValue(const QString& field, unsigned char& val, const unsigned char def) +void Ini::getValue(const QString& field, unsigned char& value, const unsigned char defaultValue) { - if (!fields.contains(field)) + if (!m_fields.contains(field)) { - val = def; + value = defaultValue; return; } bool ok; unsigned temp; - temp = fields[field].toUInt(&ok); - val = (ok && (temp <= UCHAR_MAX)) ? temp : def; + temp = m_fields[field].toUInt(&ok); + value = (ok && (temp <= UCHAR_MAX)) ? temp : defaultValue; } -void Ini::getValue(const QString& field, int& val, const int def) +void Ini::getValue(const QString& field, int& value, const int defaultValue) { - if (!fields.contains(field)) + if (!m_fields.contains(field)) { - val = def; + value = defaultValue; return; } bool ok; - val = fields[field].toInt(&ok); + value = m_fields[field].toInt(&ok); if (!ok) - val = def; + value = defaultValue; } -void Ini::getValue(const QString& field, double& val, const double def) +void Ini::getValue(const QString& field, double& value, const double defaultValue) { - if (!fields.contains(field)) + if (!m_fields.contains(field)) { - val = def; + value = defaultValue; return; } bool ok; - val = fields[field].toDouble(&ok); + value = m_fields[field].toDouble(&ok); if (!ok) - val = def; + value = defaultValue; } -void Ini::getValue(const QString& field, QString& val, const QString& def) +void Ini::getValue(const QString& field, QString& value, const QString& defaultValue) { - if (!fields.contains(field)) + if (!m_fields.contains(field)) { - val = def; + value = defaultValue; return; } - val = fields[field]; + value = m_fields[field]; } -QStringList Ini::getFields() const +QStringList Ini::fields() const { - return fields.keys(); + return m_fields.keys(); } diff --git a/general/Ini.h b/general/Ini.h index 5a695aff..741c5dfd 100644 --- a/general/Ini.h +++ b/general/Ini.h @@ -1,59 +1,55 @@ -///////////////////////////////////////////////////////////////////////////// -// Name: general/Ini.h -// Purpose: Define sections for data files -// Author: Ben Boeckel -// Modified by: Ben Boeckel -// Created: Fri May 4 23:27:37 2007 -// Copyright: ©2007-2008 Ben Boeckel and Nerdy Productions -// Licence: -// 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 3 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, see <http://www.gnu.org/licenses/>. -///////////////////////////////////////////////////////////////////////////// +/* + * Copyright 2007-2008 Ben Boeckel <MathStuf@gmail.com> + * + * 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 3 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, see <http://www.gnu.org/licenses/>. + */ #ifndef __INI__ #define __INI__ +// Qt includes #include <QFile> #include <QMap> #include <QString> #include <QStringList> +// General includes #include "Exception.h" class Ini { public: Ini(); - Ini(const QString& fname) throw(Exception); + Ini(const QString& fileName) throw(Exception); - void load(const QString& fname) throw(Exception); - void save(const QString& fname) const throw(Exception); + void load(const QString& fileName) throw(Exception); + void save(const QString& fileName) const throw(Exception); - void addField(const QString& n, const bool v); - void addField(const QString& n, const unsigned char v); - void addField(const QString& n, const int v); - void addField(const QString& n, const double v); - void addField(const QString& n, const QString& v); + void addField(const QString& field, const bool value); + void addField(const QString& field, const unsigned char value); + void addField(const QString& field, const int value); + void addField(const QString& field, const double value); + void addField(const QString& field, const QString& value); - QString getName() const; - void getValue(const QString& field, bool& val, const bool def = true); - void getValue(const QString& field, unsigned char& val, const unsigned char def = UCHAR_MAX); - void getValue(const QString& field, int& val, const int def = -1); - void getValue(const QString& field, double& val, const double def = 0); - void getValue(const QString& field, QString& val, const QString& def = ""); + void getValue(const QString& field, bool& value, const bool defaultValue = true); + void getValue(const QString& field, unsigned char& value, const unsigned char defaultValue = 0); + void getValue(const QString& field, int& value, const int defaultValue = INT_MAX); + void getValue(const QString& field, double& value, const double defaultValue = 0); + void getValue(const QString& field, QString& value, const QString& defaultValue = ""); - QStringList getFields() const; - protected: + QStringList fields() const; + private: enum LinePos { Field = 0, @@ -63,7 +59,7 @@ class Ini void load(QFile& fin) throw(InvalidException); void save(QFile& fout) const; - QMap<QString, QString> fields; + QMap<QString, QString> m_fields; }; #endif diff --git a/general/Matrix.h b/general/Matrix.h index f1613c09..60398d26 100644 --- a/general/Matrix.h +++ b/general/Matrix.h @@ -1,193 +1,180 @@ -///////////////////////////////////////////////////////////////////////////// -// Name: general/Matrix.h -// Purpose: A 2D vector class -// Author: Ben Boeckel -// Modified by: Ben Boeckel -// Created: Sun Apr 8 12:51:20 2007 -// Copyright: ©2007-2008 Ben Boeckel and Nerdy Productions -// Licence: -// 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 3 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, see <http://www.gnu.org/licenses/>. -///////////////////////////////////////////////////////////////////////////// +/* + * Copyright 2007-2008 Ben Boeckel <MathStuf@gmail.com> + * + * 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 3 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, see <http://www.gnu.org/licenses/>. + */ #ifndef __MATRIX__ #define __MATRIX__ +// Qt includes +#include <QtGlobal> #include <QFile> -#include <QMutableVectorIterator> #include <QVector> -#include <QVectorIterator> +// General includes #include "Exception.h" template<class T> class Matrix { public: Matrix() : - width(0), - height(0) + m_width(0), + m_height(0) { } - Matrix(const int w, const int h, const T& d = T()) : - width(w), - height(h), - matrix(h, QVector<T>(w, d)) + Matrix(const int height, const int width, const T& value = T()) { - if (!width ^ !height) - { - if (width) - addRow(d); - if (height) - addCol(d); - } + resize(height, width, value); } - Matrix(const Matrix<T>& rhs) : - width(rhs.getWidth()), - height(rhs.getHeight()), - matrix(height, QVector<T>(width, T())) + Matrix(const Matrix<T>& rhs) { - for (int i = 0; i < height; ++i) - { - for (int j = 0; j < width; ++j) - matrix[i][j] = rhs(i, j); - } + *this = rhs; } - void addRow(const T& d = T()) + void addRow(const T& value = T()) { - width += !width; - matrix.append(QVector<T>(width, d)); - ++height; + m_width += !m_width; + m_matrix.append(QVector<T>(m_width, value)); + ++m_height; } - void addCol(const T& d = T()) + void addColumn(const T& value = T()) { - if (!height) + if (!m_height) { - matrix.append(QVector<T>()); - ++height; + m_matrix.append(QVector<T>()); + ++m_height; } - for (QMutableVectorIterator< QVector<T> > i(matrix); i.hasNext(); ) - i.next().append(d); - ++width; + foreach (QVector<T> row, m_matrix) + row.append(value); + ++m_width; } - void insertRow(const int pos, const T& d = T()) throw(BoundsException) + void insertRow(const int row, const T& value = T()) throw(BoundsException) { - if (height < pos) + if (m_height < row) throw(BoundsException("Matrix", "dimension")); - width += !width; - matrix.insert(pos, QVector<T>(width, d)); - ++height; + m_width += !m_width; + m_matrix.insert(row, QVector<T>(m_width, value)); + ++m_height; } - void insertCol(const int pos, const T& d = T()) throw(BoundsException) + void insertColumn(const int column, const T& value = T()) throw(BoundsException) { - if (width < pos) + if (m_width < column) throw(BoundsException("Matrix", "dimension")); - if (!height) + if (!m_height) { - matrix.append(QVector<T>()); - ++height; + m_matrix.append(QVector<T>()); + ++m_height; } - for (QMutableVectorIterator< QVector<T> > i(matrix); i.hasNext(); ) - i.next().insert(pos, d); - ++width; + foreach (QVector<T> row, m_matrix) + row.insert(column, value); + ++m_width; } - void deleteRow(const int pos) throw(BoundsException) + void deleteRow(const int row) throw(BoundsException) { - if (height <= pos) + if (m_height <= row) throw(BoundsException("Matrix", "dimension")); - matrix.remove(pos); - if (!(--height)) - width = 0; + m_matrix.remove(row); + if (!(--m_height)) + m_width = 0; } - void deleteCol(const int pos) throw(BoundsException) + void deleteColumn(const int column) throw(BoundsException) { - if (width <= pos) + if (m_width <= column) throw(BoundsException("Matrix", "dimension")); - for (QMutableVectorIterator< QVector<T> > i(matrix); i.hasNext(); ) - i.next().remove(pos); - if (!(--width)) - height = 0; + foreach (QVector<T> row, m_matrix) + row.remove(column); + if (!(--m_width)) + m_height = 0; } void clear() { - matrix.clear(); + m_width = 0; + m_height = 0; + m_matrix.clear(); } - void set(const int row, const int col, const T& s) throw(BoundsException) + void set(const int row, const int column, const T& value) throw(BoundsException) { - if ((height <= row) || (width <= col)) + if ((m_height <= row) || (m_width <= column)) throw(BoundsException("Matrix", "dimension")); - (*this)(row, col) = s; + (m_matrix[row])(column) = value; } - void resize(const int h, const int w, const T& d = T()) + void resize(const int height, const int width, const T& value = T()) { clear(); - width = w; - height = h; - matrix = QVector< QVector<T> >(h, QVector<T>(w, d)); + m_width = width; + m_height = height; + if (!m_width ^ !m_height) + { + if (m_width) + ++m_height; + if (m_height) + ++m_width; + } + m_matrix = QVector< QVector<T> >(m_height, QVector<T>(m_width, value)); } - const T& at(const int row, const int col) const throw(BoundsException) + const T& at(const int row, const int column) const throw(BoundsException) { - if ((height <= row) || (width <= col)) + if ((m_height <= row) || (m_width <= column)) throw(BoundsException("Matrix", "dimension")); - return matrix.at(row).at(col); + return m_matrix.at(row).at(column); } - const QVector<T> getRow(const int row) const throw(BoundsException) + const QVector<T> row(const int row) const throw(BoundsException) { if (height <= row) throw(BoundsException("Matrix", "dimension")); - return matrix.at(row); + return m_matrix.at(row); } - const QVector<T> getCol(const int col) const throw(BoundsException) + const QVector<T> column(const int column) const throw(BoundsException) { - if (width <= col) + if (m_width <= column) throw(BoundsException("Matrix", "dimension")); QVector<T> c; - for (QVectorIterator< QVector<T> > i(matrix); i.hasNext(); ) - c.append(i.next().at(col)); + foreach (QVector<T> row, m_matrix) + c.append(row.at(column)); return c; } - int getHeight() const + int height() const { - return height; + return m_height; } - int getWidth() const + int width() const { - return width; + return m_width; } - T& operator()(const int row, const int col) throw(BoundsException) + T& operator()(const int row, const int column) throw(BoundsException) { - if ((height <= row) || (width <= col)) + if ((m_height <= row) || (m_width <= column)) throw(BoundsException("Matrix", "dimension")); - return (matrix[row])[col]; + return (m_matrix[row])[column]; } - const T& operator()(const int row, const int col) const + const T& operator()(const int row, const int column) const { - return at(row, col); + return at(row, column); } Matrix<T> operator=(const Matrix<T>& rhs) { if (this == &rhs) return *this; - height = rhs.getHeight(); - width = rhs.getWidth(); - resize(height, width); - for (int i = 0; i < height; ++i) + resize(rhs.m_height, rhs.m_width); + for (int i = 0; i < m_height; ++i) { - for (int j = 0; j < width; ++j) - matrix[i][j] = rhs(i, j); + for (int j = 0; j < m_width; ++j) + m_matrix[i][j] = rhs(i, j); } return *this; } @@ -195,13 +182,13 @@ template<class T> class Matrix { if (this == &rhs) return true; - if ((height != rhs.getHeight()) || (width == rhs.getWidth())) + if ((m_height != rhs.m_height) || (m_width == rhs.m_width)) return false; - for (int i = 0; i < height; ++i) + for (int i = 0; i < m_height; ++i) { - for (int j = 0; j < width; ++j) + for (int j = 0; j < m_width; ++j) { - if (matrix[i][j] != rhs(i, j)) + if (m_matrix[i][j] != rhs(i, j)) return false; } } @@ -212,9 +199,9 @@ template<class T> class Matrix return !(*this == rhs); } protected: - int width; - int height; - QVector< QVector<T> > matrix; + int m_width; + int m_height; + QVector< QVector<T> > m_matrix; }; #endif diff --git a/general/Point.h b/general/Point.h index 209c3f6e..0525f85b 100644 --- a/general/Point.h +++ b/general/Point.h @@ -1,64 +1,78 @@ -///////////////////////////////////////////////////////////////////////////// -// Name: general/Point.h -// Purpose: Define a coordinate point -// Author: Ben Boeckel -// Modified by: Ben Boeckel -// Created: Sun Apr 8 12:53:15 2007 -// Copyright: ©2007-2008 Ben Boeckel and Nerdy Productions -// Licence: -// 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 3 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, see <http://www.gnu.org/licenses/>. -///////////////////////////////////////////////////////////////////////////// +/* + * Copyright 2007-2008 Ben Boeckel <MathStuf@gmail.com> + * + * 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 3 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, see <http://www.gnu.org/licenses/>. + */ #ifndef __POINT__ #define __POINT__ -#include <QFile> - class Point { public: - Point(const int _x = 0, const int _y = 0) : - x(_x), - y(_y) + inline Point(const int x = 0, const int y = 0) : + m_x(x), + m_y(y) + { + } + inline Point(const Point& rhs) { + *this = rhs; } - void set(const int _x, const int _y) + inline void set(const int x, const int y) + { + m_x = x; + m_y = y; + } + inline void setX(const int x) { - x = _x; - y = _y; + m_x = x; } - void setX(const int _x) + inline void setY(const int y) { - x = _x; + m_y = y; } - void setY(const int _y) + + inline int x() const + { + return m_x; + } + inline int y() const { - y = _y; + return m_y; } - int getX() const + Point& operator=(const Point& rhs) + { + if (this == &rhs) + return *this; + m_x = rhs.m_x; + m_y = rhs.m_y; + return *this; + } + inline bool operator==(const Point& rhs) const { - return x; + return ((m_x == rhs.m_x) && (m_y == rhs.m_y)); } - int getY() const + inline bool operator!=(const Point& rhs) const { - return y; + return !(*this == rhs); } private: - int x; - int y; + int m_x; + int m_y; }; #endif diff --git a/general/TODO b/general/TODO new file mode 100644 index 00000000..e69de29b --- /dev/null +++ b/general/TODO diff --git a/general/general.pro b/general/general.pro index 307f78e1..1b774ecf 100644 --- a/general/general.pro +++ b/general/general.pro @@ -67,3 +67,5 @@ HEADERS += Audio.h \ INSTALLS += target target.path = /usr/lib`kde4-config --libsuffix` +DISTFILES += TODO + |
