Prepare 1.0 alpha3 release.
[tagua/yd.git] / src / piecegrid.h
blob8ffe0c59a18ad2931969dbdb16cebd8611e2d1f2
1 /*
2 Copyright (c) 2006 Paolo Capriotti <p.capriotti@gmail.com>
3 (c) 2006 Maurizio Monge <maurizio.monge@kdemail.net>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9 */
11 #ifndef PIECEGRID_H
12 #define PIECEGRID_H
14 #include "grid.h"
16 /**
17 * A specialization of Grid useful for containing pointers.
18 * It is used only to contain pointers to Piece and derived classes.
20 template <typename T>
21 class PointerGrid : public Grid<T*> {
22 public:
23 using Grid<T*>::board; // why???
24 PointerGrid(int sizeX, int sizeY);
25 PointerGrid(const PointerGrid<T>& other);
26 template <typename T1>
27 PointerGrid(const PointerGrid<T1>& other);
29 bool operator==(const PointerGrid<T>& other) const;
31 Point find(const T& x) const {
32 for (Point i = this->first(); i <= this->last(); i = this->next(i)) {
33 if ((*this)[i] && *(*this)[i] == x)
34 return i;
36 return Point::invalid();
39 virtual ~PointerGrid();
43 //BEGIN Implementation
45 template <typename T>
46 PointerGrid<T>::PointerGrid(int sizeX, int sizeY) : Grid<T*>(sizeX, sizeY, 0) {}
48 template <typename T>
49 PointerGrid<T>::PointerGrid(const PointerGrid<T>& other)
50 : Grid<T*>(other) {
51 for (uint i = 0; i < board.size(); ++i) {
52 T* p = other.board[i];
53 if (p)
54 board[i] = new T(*p);
55 else
56 board[i] = 0;
60 template <typename T>
61 template <typename T1>
62 PointerGrid<T>::PointerGrid(const PointerGrid<T1>& other)
63 : Grid<T*>(other.getSize().x, other.getSize().y) {
64 for (uint i = 0; i < board.size(); ++i) {
65 T1* p = other.board[i];
66 if (p)
67 board[i] = new T(*p);
68 else
69 board[i] = 0;
73 template <typename T>
74 PointerGrid<T>::~PointerGrid() {
75 for (uint i = 0; i < board.size(); ++i)
76 delete board[i];
79 template <typename T>
80 bool PointerGrid<T>::operator==(const PointerGrid<T>& other) const {
81 for (uint i = 0; i < board.size(); ++i) {
82 if (!board[i]) {
83 if (other.board[i]) return false;
85 else
86 if (! board[i]->equals(other.board[i])) return false;
88 return true;
92 //END Implementation
94 #endif // PIECEGRID_H