Main Page | Class Hierarchy | Alphabetical List | Class List | Directories | File List

Order.h

Go to the documentation of this file.
00001 /*
00002 Copyright 2003 - 2005 Elliott Kleinrock, Dan Neely, Kurt W. Over, Damon Domjan
00003 
00004 This file is part of FreeStars, a free clone of the Stars! game.
00005 
00006 FreeStars is free software; you can redistribute it and/or modify
00007 it under the terms of the GNU General Public License as published by
00008 the Free Software Foundation; either version 2 of the License, or
00009 (at your option) any later version.
00010 
00011 FreeStars is distributed in the hope that it will be useful,
00012 but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 GNU General Public License for more details.
00015 
00016 You should have received a copy of the GNU General Public License
00017 along with FreeStars; if not, write to the Free Software
00018 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019 
00020 The full GPL Copyright notice should be in the file COPYING.txt
00021 
00022 Contact:
00023 Email Elliott at 9jm0tjj02@sneakemail.com
00024 */
00025 
00030 #if !defined(Order_h)
00031 #define Order_h
00032 
00033 class Order {
00034 public:
00035         virtual ~Order();
00036         bool Undo();
00037         bool Redo();
00038         bool IsUndone() const   { return mUndone; }
00039         virtual TiXmlNode * WriteNode(TiXmlNode * node) const = 0;
00040         virtual bool DoUndo() = 0;
00041 
00042 protected:
00043         Order() : mUndone(false) {}
00044         bool mUndone;
00045 };
00046 
00047 template <class Type> class TypedOrder : public Order {
00048 public:
00049         typedef const string (*StringFunc)(Type);
00050         // Order with conversion to string for writing to xml file
00051         TypedOrder(Type * value, const char * name, StringFunc func, const char * additionalLabel = NULL, const char * additionalString = NULL)
00052                 : Order(), m_pValue(value), mWFunc(NULL), mSFunc(func), mPrior(*value), mName(name), mAddLabel(additionalLabel ? additionalLabel : ""), mAddStr(additionalString ? additionalString : "") {}
00053 
00054         // Order with generic function when writing to xml file
00055         typedef TiXmlElement * (*WriteFunc)(TiXmlNode *, const char *, Type);
00056         TypedOrder(Type * value, WriteFunc func, const char * name, const char * additionalLabel = NULL, const char * additionalString = NULL)
00057                 : Order(), m_pValue(value), mWFunc(func), mSFunc(NULL), mPrior(*value), mName(name), mAddLabel(additionalLabel ? additionalLabel : ""), mAddStr(additionalString ? additionalString : "") {}
00058 
00059         virtual bool DoUndo()   { Type t = mPrior; mPrior = *m_pValue; *m_pValue = t; return true; }
00060         virtual TiXmlNode * WriteNode(TiXmlNode * node) const {
00061                 if (!mUndone) {
00062                         if (!mAddLabel.empty()) {
00063                                 string s = "Set";
00064                                 s += mName;
00065                                 TiXmlElement * child = new TiXmlElement(s.c_str());
00066                                 AddString(child, mAddLabel.c_str(), mAddStr.c_str());
00067                                 if (mWFunc != NULL)
00068                                         mWFunc(child, mName, *m_pValue);
00069                                 else
00070                                         AddString(child, mName, mSFunc(*m_pValue).c_str());
00071                                 node->LinkEndChild(child);
00072                                 return child;
00073                         } else {
00074                                 if (mWFunc != NULL)
00075                                         return mWFunc(node, mName, *m_pValue);
00076                                 else
00077                                         return AddString(node, mName, mSFunc(*m_pValue).c_str());
00078                         }
00079                 } else
00080                         return NULL;
00081         }
00082 
00083 protected:
00084         Type * m_pValue;
00085         Type mPrior;
00086         WriteFunc mWFunc;
00087         StringFunc mSFunc;
00088         const char * mName;
00089         string mAddLabel;
00090         string mAddStr;
00091 };
00092 
00093 template <class Type> class TypedListOrder : public Order {
00094 public:
00095         TypedListOrder(deque<Type *> * value, const char * name)
00096                 : Order(), m_pValue(value), mPrior(*value), mName(name) {}
00097         virtual ~TypedListOrder() { for (int i = 0; i < mPrior.size(); ++i) delete mPrior[i]; }
00098 
00099         virtual bool DoUndo()   { deque<Type *> t = mPrior; mPrior = *m_pValue; *m_pValue = t; return true; }
00100         virtual TiXmlNode * CallWriteNode(const Type * obj, TiXmlNode * node) const     { return obj->WriteNode(node); }
00101         const Type * GetItemNumber(int i) const { return m_pValue->at(i); }
00102         virtual TiXmlNode * WriteNode(TiXmlNode * node) const {
00103                 if (!mUndone) {
00104                         if (mName == NULL) {
00105                                 for (int i = 0; i < m_pValue->size(); ++i)
00106                                         CallWriteNode(m_pValue->at(i), node);
00107 
00108                                 return node;
00109                         } else {
00110                                 TiXmlElement * txe = new TiXmlElement(mName);
00111                                 for (int i = 0; i < m_pValue->size(); ++i)
00112                                         CallWriteNode(m_pValue->at(i), txe);
00113 
00114                                 node->LinkEndChild(txe);
00115                                 return txe;
00116                         }
00117                 }
00118                 return NULL;
00119         }
00120 
00121 private:
00122         deque<Type *> * m_pValue;
00123         deque<Type *> mPrior;
00124         const char * mName;
00125 };
00126 
00127 class WaypointOrder : public TypedListOrder<WayOrder> {
00128 public:
00129         WaypointOrder(long f, deque<WayOrder *> * ords)
00130                 : TypedListOrder<WayOrder>(ords, "Waypoints"), mFleet(f) {}
00131 
00132         virtual TiXmlNode * WriteNode(TiXmlNode * node) const;
00133 
00134 private:
00135         long mFleet;
00136 };
00137 
00138 class BattlePlanOrder : public Order {
00139 public:
00140         BattlePlanOrder(BattlePlan * bp, int number, Player * p)
00141                 : Order(), mBP(bp), mNum(number), mPlayer(p) {}
00142         virtual ~BattlePlanOrder();
00143 
00144         virtual bool DoUndo();
00145         virtual TiXmlNode * WriteNode(TiXmlNode * node) const;
00146 
00147         BattlePlan * GetBattlePlan(BattlePlan * old)
00148                 { BattlePlan * t = mBP; mBP = old; return t; }
00149         int GetNumber() { return mNum; }
00150 
00151 private:
00152         BattlePlan * mBP;
00153         int mNum;
00154         Player * mPlayer;
00155 };
00156 
00157 class RelationsOrder : public Order {
00158 public:
00159         RelationsOrder(deque<long> * rel)
00160                 : Order(), m_pValue(rel), mPrior(*rel) {}
00161 
00162         virtual bool DoUndo()   { deque<long> t = mPrior; mPrior = *m_pValue; *m_pValue = t; return true; }
00163         virtual TiXmlNode * WriteNode(TiXmlNode * node) const;
00164 
00165 private:
00166         deque<long> mPrior;
00167         deque<long> * m_pValue;
00168 };
00169 
00170 class ProductionOrder : public TypedListOrder<ProdOrder> {
00171 public:
00172         ProductionOrder(const char * name, deque<ProdOrder *> * ords)
00173                 : TypedListOrder<ProdOrder>(ords, "ProductionQueue"), mQName(name) {}
00174 
00175         virtual TiXmlNode * WriteNode(TiXmlNode * node) const;
00176 
00177 private:
00178         const char * mQName;
00179 };
00180 
00181 class MultipleOrder : public Order {
00182 public:
00183         MultipleOrder() : Order() {}
00184         virtual ~MultipleOrder();
00185         virtual TiXmlNode * WriteNode(TiXmlNode * node) const;
00186         virtual bool DoUndo();
00187 
00188         void AddOrder(Order * o);
00189 
00190 private:
00191         deque<Order *> mOrds;
00192 };
00193 
00194 class TransportOrder : public Order {
00195 public:
00196         TransportOrder(Player * p, CargoHolder * owned, CargoHolder * other, long pop, long fuel, deque<long> & cargo)
00197                 : Order(), mPlayer(p), mOwned(owned), mOther(other), mPop(pop), mFuel(fuel), mCargo(cargo) {}
00198 
00199         virtual bool DoUndo();
00200         virtual TiXmlNode * WriteNode(TiXmlNode * node) const;
00201 
00202 private:
00203         Player * mPlayer;
00204         CargoHolder * mOwned;
00205         CargoHolder * mOther;
00206         long mPop;
00207         long mFuel;
00208         deque<long> mCargo;
00209 };
00210 
00211 #endif // !defined(Order_h)

Generated on Mon Aug 8 21:33:43 2005 for Freestars by  doxygen 1.4.2-20050421