src/tools/values/vCollection.h

Go to the documentation of this file.
00001 /*
00002 
00003 *************************************************************************
00004 
00005 ArmageTron -- Just another Tron Lightcycle Game in 3D.
00006 Copyright (C) 2000  Manuel Moos (manuel@moosnet.de)
00007 
00008 **************************************************************************
00009 
00010 This program is free software; you can redistribute it and/or
00011 modify it under the terms of the GNU General Public License
00012 as published by the Free Software Foundation; either version 2
00013 of the License, or (at your option) any later version.
00014 
00015 This program is distributed in the hope that it will be useful,
00016 but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 GNU General Public License for more details.
00019 
00020 You should have received a copy of the GNU General Public License
00021 along with this program; if not, write to the Free Software
00022 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00023 
00024 ***************************************************************************
00025 
00026 */
00027 
00030 
00031 #ifndef ARMAGETRON_vCollection_h
00032 #define ARMAGETRON_vCollection_h
00033 
00034 #include <set>
00035 
00036 #include "values/vCore.h"
00037 
00038 namespace vValue {
00039 namespace MiscWTF {
00040 
00041 class FooPtrOps;
00042 
00043 typedef std::set<BasePtr, FooPtrOps> myCol;
00044 
00045 struct FooPtrOps
00046 {
00047     bool operator()( const BasePtr & a, const BasePtr & b )
00048     {
00049         return (*a < *b);
00050     }
00051     /*
00052      * Usefull for debug purpose
00053      */
00054     /*
00055       void operator()( const BasePtr & a )
00056       { std::cout << a->GetInt() << "\n"; }
00057     */
00058 };
00059 
00060 }
00061 using namespace MiscWTF;
00062 namespace Expr {
00063 namespace Collection {
00064 
00065 class BaseExt: public Base {
00066 public:
00067     BaseExt() :Base() {};
00068     BaseExt(Base const &other): Base(other) {};
00069     BaseExt(BaseExt const &other): Base(other) {};
00070     virtual ~BaseExt() { };
00071 
00072     virtual Base *copy(void) const {return new BaseExt();};
00073 
00076     virtual myCol GetCol(void) const ;
00077     operator myCol() const { return GetCol(); }
00078 };
00079 
00080 class ColNode : public BaseExt {
00081 protected:
00082     myCol m_col;
00083 
00084 public:
00085     template <typename InputIterator>
00086     ColNode(InputIterator begin, InputIterator end): BaseExt(), m_col(begin, end) { }
00087 
00089 
00090     ColNode(BaseExt const &other): BaseExt(other), m_col() { };
00091     ColNode(ColNode const &other): BaseExt(other), m_col(other.m_col) { };
00092     virtual ~ColNode() { };
00093 
00094     Base *copy(void) const { return new ColNode(*this); };
00095     virtual Variant GetValue(void) const; 
00096     virtual int GetInt(void) const;
00097     virtual float GetFloat(void) const;
00098     myCol GetCol(void) const;
00099 
00100     bool operator==(Base const &other) const;
00101     bool operator!=(Base const &other) const;
00102     bool operator>=(Base const &other) const;
00103     bool operator<=(Base const &other) const;
00104     bool operator> (Base const &other) const;
00105     bool operator< (Base const &other) const;
00106 };
00107 
00108 
00109 /*
00110   ColUnary holds one BaseExt object.
00111   Any query toward the underlying BaseExt object are done only as collection.
00112   
00113   For each operation, ColUnary and its childs will query the BaseExt object for 
00114   its collection of vValue, possibly do a manipulation on the collection and
00115   return the associated result.
00116   
00117   It is to be noted that GetInt and GetFloat will return the value of the 
00118   first element of the resulting collection, if not otherly overridden.
00119   
00120   It is to be noted that there are no guaranties that 2 consecutive queries 
00121   will provide the same results. Any subsequent query should will be considered 
00122   as a new one, possibly affecting the state of the data. It is recommended to 
00123   store the data returned for as long as its current state is relevant.
00124 */
00125 class ColUnary : public BaseExt {
00126 protected:
00127     BasePtr m_child;
00128 public:
00129     ColUnary(BasePtr child) : BaseExt(), m_child(child) { };
00130 
00131     ColUnary(const ColUnary &other): BaseExt(), m_child(other.m_child->copy()) { };
00132     virtual ~ColUnary() { };
00133 
00134     Base *copy(void) const { return new ColUnary(*this); };
00135 
00136     virtual Variant GetValue(void) const; 
00137     virtual int GetInt(void) const;
00138     virtual float GetFloat(void) const;
00139     myCol GetCol(void) const;
00140 
00141     bool operator==(Base const &other) const;
00142     bool operator!=(Base const &other) const;
00143     bool operator>=(Base const &other) const;
00144     bool operator<=(Base const &other) const;
00145     bool operator> (Base const &other) const;
00146     bool operator< (Base const &other) const;
00147 protected:
00148     virtual myCol _operation(void) const {
00149         //      return m_child->GetCol();
00150         Base *base = m_child->copy();
00151         BaseExt const *baseExt = dynamic_cast<BaseExt*>(base);
00152         return baseExt->GetCol();
00153     };
00154 };
00155 
00156 
00157 /*
00158   After querying the child BaseExt object contained for its collection, 
00159   PickOneCol will select a random element and return only this one
00160 */
00161 class ColPickOne : public ColUnary {
00162 public:
00163     ColPickOne(BasePtr child = BasePtr( new Base() ) ) : ColUnary(child) { };
00164 
00165     ColPickOne(const ColPickOne &other): ColUnary(other) { };
00166     virtual ~ColPickOne() { };
00167 
00168     Base *copy(void) const { return new ColPickOne(*this); };
00169 
00170 protected:
00171     virtual myCol _operation(void) const ;
00172 };
00173 
00174 
00175 /*
00176    Set
00177    Intersection: Elements that are both on A and B
00178    Difference  : Elements that are in A and not B
00179    Union       :
00180    Symetric Difference: Elements that are either in A or B, but not in both
00181 */
00182 
00183 /*
00184   The ColBinary and its children class operates on 2 vValue. The exact manipulation is determined by the child class
00185   
00186   It is to be noted that there are no guaranties that 2 consecutive queries 
00187   will provide the same results. Any subsequent query should will be considered 
00188   as a new one, possibly affecting the state of the data. It is recommended to 
00189   store the data returned for as long as its current state is relevant.
00190 */
00191 class ColBinary : public BaseExt {
00192 protected:
00193     BasePtr r_child;
00194     BasePtr l_child;
00195 
00196 public:
00197     ColBinary(BaseExt *r_value = new BaseExt,
00198               BaseExt *l_value = new BaseExt ) :
00199             r_child(r_value),
00200             l_child(l_value)
00201     { };
00202     ColBinary(BasePtr &r_value,
00203               BasePtr &l_value):
00204             r_child(r_value),
00205             l_child(l_value)
00206     { };
00207     ColBinary(const ColBinary &other) :
00208             BaseExt(other),
00209             r_child(other.r_child->copy()),
00210             l_child(other.l_child->copy())
00211     { };
00212     virtual ~ColBinary() { };
00213 
00214     Base *copy(void) const { return new ColBinary(*this); };
00215 
00216     virtual Variant GetValue(void) const; 
00217     virtual int GetInt(void) const;
00218     virtual float GetFloat(void) const;
00219     myCol GetCol(void) const;
00220 
00221     bool operator==(Base const &other) const;
00222     bool operator!=(Base const &other) const;
00223     bool operator>=(Base const &other) const;
00224     bool operator<=(Base const &other) const;
00225     bool operator> (Base const &other) const;
00226     bool operator< (Base const &other) const;
00227 protected:
00228     virtual myCol _operation(void) const;
00229 };
00230 
00231 
00232 
00233 class ColUnion : public ColBinary {
00234 public:
00235     ColUnion(BaseExt *r_value = new BaseExt,
00236              BaseExt *l_value = new BaseExt ) :
00237             ColBinary(r_value, l_value)
00238     { };
00239     ColUnion(BasePtr &r_value,
00240              BasePtr &l_value):
00241     ColBinary(r_value, l_value) { };
00242     ColUnion(const ColBinary &other) :
00243     ColBinary(other) { };
00244     virtual ~ColUnion() { };
00245 
00246     Base *copy(void) const { return new ColUnion(*this); };
00247 
00248 protected:
00249     myCol _operation(void) const;
00250 };
00251 
00252 
00253 class ColIntersection : public ColBinary {
00254 
00255 public:
00256     ColIntersection(BaseExt *r_value = new BaseExt,
00257                     BaseExt *l_value = new BaseExt ) :
00258             ColBinary(r_value, l_value)
00259     { };
00260     ColIntersection(BasePtr &r_value,
00261                     BasePtr &l_value):
00262     ColBinary(r_value, l_value) { };
00263     ColIntersection(const ColBinary &other) :
00264     ColBinary(other) { };
00265     virtual ~ColIntersection() { };
00266 
00267     Base *copy(void) const { return new ColIntersection(*this); };
00268 
00269 protected:
00270     myCol _operation(void) const;
00271 };
00272 
00273 
00274 
00275 class ColDifference : public ColBinary {
00276 public:
00277     ColDifference(BaseExt *r_value = new BaseExt,
00278                   BaseExt *l_value = new BaseExt ) :
00279             ColBinary(r_value, l_value)
00280     { };
00281     ColDifference(BasePtr &r_value,
00282                   BasePtr &l_value):
00283     ColBinary(r_value, l_value) { };
00284     ColDifference(const ColBinary &other) :
00285     ColBinary(other) { };
00286     virtual ~ColDifference() { };
00287 
00288     Base *copy(void) const { return new ColDifference(*this); };
00289 
00290 protected:
00291     myCol _operation(void) const;
00292 };
00293 
00294 }
00295 }
00296 }
00297 
00298 #endif

Generated on Sat Mar 15 22:56:03 2008 for Armagetron Advanced by  doxygen 1.5.4