OptionEntry.h

Go to the documentation of this file.
00001 ////////////////////////////////////////////////////////////////////////////////
00002 //    Scorched3D (c) 2000-2009
00003 //
00004 //    This file is part of Scorched3D.
00005 //
00006 //    Scorched3D 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 //    Scorched3D 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 Scorched3D; if not, write to the Free Software
00018 //    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019 ////////////////////////////////////////////////////////////////////////////////
00020 
00021 
00022 #if !defined(__INCLUDE_OptionEntryh_INCLUDE__)
00023 #define __INCLUDE_OptionEntryh_INCLUDE__
00024 
00025 #include <net/NetBuffer.h>
00026 #include <common/Vector.h>
00027 #include <common/FixedVector.h>
00028 #include <common/ARGParser.h>
00029 #include <XML/XMLParser.h>
00030 
00031 class OptionEntry
00032 {
00033 public:
00034         enum StandardData
00035         {
00036                 DataDepricated = 128,
00037                 DataProtected = 256
00038         };
00039         enum EntryType
00040         {
00041                 OptionEntryIntType,
00042                 OptionEntryStringType,
00043                 OptionEntryTextType,
00044                 OptionEntryBoolType,
00045                 OptionEntryFloatType,
00046                 OptionEntryFixedType,
00047                 OptionEntryVectorType,
00048                 OptionEntryFixedVectorType,
00049                 OptionEntryEnumType,
00050                 OptionEntryStringEnumType,
00051                 OptionEntryBoundedIntType
00052         };
00053 
00054         OptionEntry(std::list<OptionEntry *> &group,
00055                                 const char *name,
00056                                 const char *description,
00057                                 unsigned int data);
00058         virtual ~OptionEntry();
00059 
00060         const char *getName() { return name_.c_str(); }
00061         virtual const char *getDescription() { return description_.c_str(); }
00062         virtual unsigned getData() { return data_; }
00063 
00064         virtual EntryType getEntryType() = 0;
00065         virtual const char *getDefaultValueAsString() = 0;
00066         virtual const char *getValueAsString() = 0;
00067         virtual bool isDefaultValue() = 0;
00068         virtual bool isChangedValue() { return changedValue_; }
00069         virtual bool setValueFromString(const std::string &string) = 0;
00070         virtual bool addToArgParser(ARGParser &parser) = 0;
00071         virtual void setNotChanged() { changedValue_ = false; }
00072 
00073         virtual const char *getComsBufferValue() 
00074                 { return getValueAsString(); }
00075         virtual bool setComsBufferValue(const char *string) 
00076                 { return setValueFromString(string); }
00077 
00078 protected:
00079         bool changedValue_;
00080         unsigned int data_;
00081         std::string name_;
00082         std::string description_;
00083 };
00084 
00085 class OptionEntryHelper
00086 {
00087 public:
00088         static bool writeToBuffer(std::list<OptionEntry *> &options,
00089                                                           NetBuffer &buffer,
00090                                                           bool useprotected);
00091         static bool readFromBuffer(std::list<OptionEntry *> &options,
00092                                                            NetBufferReader &reader,
00093                                                            bool useprotected);
00094 
00095         static bool writeToXML(std::list<OptionEntry *> &options,
00096                                                                 XMLNode *node);
00097         static bool readFromXML(std::list<OptionEntry *> &options,
00098                                                                 XMLNode *node);
00099 
00100         static bool writeToFile(std::list<OptionEntry *> &options,
00101                 const std::string &fileName);
00102         static bool readFromFile(std::list<OptionEntry *> &options,
00103                 const std::string &fileName);
00104 
00105         static bool addToArgParser(std::list<OptionEntry *> &options,
00106                                                                 ARGParser &parser);
00107 
00108         static OptionEntry *getEntry(std::list<OptionEntry *> &options,
00109                 const char *name);
00110 };
00111 
00112 class OptionEntryInt : public OptionEntry, public ARGParserIntI
00113 {
00114 public:
00115         OptionEntryInt(std::list<OptionEntry *> &group,
00116                                    const char *name, 
00117                                    const char *description,
00118                                    unsigned int data,
00119                                    int defaultValue);
00120         virtual ~OptionEntryInt();
00121 
00122         virtual EntryType getEntryType() { return OptionEntryIntType; }
00123         virtual const char *getValueAsString();
00124         virtual const char *getDefaultValueAsString();
00125         virtual bool setValueFromString(const std::string &string);
00126 
00127         virtual int getValue();
00128         virtual bool setValue(int value);
00129 
00130         virtual bool addToArgParser(ARGParser &parser);
00131         virtual bool setIntArgument(int value);
00132         virtual bool isDefaultValue() { return value_ == defaultValue_; }
00133 
00134         operator int () { return value_; }
00135 
00136 protected:
00137         int defaultValue_;
00138         int value_;
00139 
00140 };
00141 
00142 class OptionEntryBoundedInt : public OptionEntryInt
00143 {
00144 public:
00145         OptionEntryBoundedInt(std::list<OptionEntry *> &group,
00146                                                   const char *name, 
00147                                                   const char *description,
00148                                                   unsigned int data,
00149                                                   int defaultValue,
00150                                                   int minValue, int maxValue, int stepValue);
00151         virtual ~OptionEntryBoundedInt();
00152 
00153         virtual const char *getDescription();
00154         virtual EntryType getEntryType() { return OptionEntryBoundedIntType; }
00155         virtual bool setValue(int value);
00156 
00157         int getMinValue() { return minValue_; }
00158         int getMaxValue() { return maxValue_; }
00159         int getStepValue() { return stepValue_; }
00160 
00161 protected:
00162         int minValue_, maxValue_;
00163         int stepValue_;
00164 
00165 };
00166 
00167 class OptionEntryEnum : public OptionEntryInt
00168 {
00169 public:
00170         struct EnumEntry
00171         {
00172                 char *description;
00173                 int value;
00174         };
00175 
00176         OptionEntryEnum(std::list<OptionEntry *> &group,
00177                                                   const char *name, 
00178                                                   const char *description,
00179                                                   unsigned int data,
00180                                                   int value,
00181                                                   OptionEntryEnum::EnumEntry enums[]);
00182         virtual ~OptionEntryEnum();
00183 
00184         virtual EntryType getEntryType() { return OptionEntryEnumType; }
00185         virtual bool setValue(int value);
00186         virtual const char *getDescription();
00187 
00188         virtual const char *getDefaultValueAsString();
00189         virtual const char *getValueAsString();
00190         virtual bool setValueFromString(const std::string &string);
00191 
00192         virtual const char *getComsBufferValue() 
00193                 { return OptionEntryInt::getValueAsString(); }
00194         virtual bool setComsBufferValue(const char *string) 
00195                 { return OptionEntryInt::setValueFromString(string); }
00196 
00197         OptionEntryEnum::EnumEntry *getEnums() { return enums_; }
00198 
00199 protected:
00200         EnumEntry *enums_;
00201 
00202 };
00203 
00204 class OptionEntryBool : public OptionEntry, public ARGParserBoolI
00205 {
00206 public:
00207         OptionEntryBool(std::list<OptionEntry *> &group,
00208                                         const char *name, 
00209                                         const char *description,
00210                                         unsigned int data,
00211                                         bool defaultValue);
00212         virtual ~OptionEntryBool();
00213 
00214         virtual EntryType getEntryType() { return OptionEntryBoolType; }
00215         virtual const char *getValueAsString();
00216         virtual const char *getDefaultValueAsString();
00217         virtual bool setValueFromString(const std::string &string);
00218 
00219         virtual bool setValue(bool value);
00220         virtual bool getValue();
00221 
00222         virtual bool addToArgParser(ARGParser &parser);
00223         virtual bool setBoolArgument(bool value);
00224         virtual bool isDefaultValue() { return value_ == defaultValue_; }
00225 
00226         operator bool () { return value_; }
00227 
00228 protected:
00229         bool defaultValue_;
00230         bool value_;
00231 };
00232 
00233 class OptionEntryString : public OptionEntry, public ARGParserStringI
00234 {
00235 public:
00236         OptionEntryString(std::list<OptionEntry *> &group,
00237                                           const char *name,
00238                                           const char *description,
00239                                           unsigned int data,
00240                                           const char *defaultValue,
00241                                           bool multiline = false);
00242         virtual ~OptionEntryString();
00243 
00244         virtual EntryType getEntryType() { return (multiline_?OptionEntryTextType:OptionEntryStringType); }
00245         virtual const char *getValueAsString();
00246         virtual const char *getDefaultValueAsString();
00247         virtual bool setValueFromString(const std::string &string);
00248 
00249         virtual const char *getValue();
00250         virtual bool setValue(const std::string &value);
00251 
00252         virtual bool addToArgParser(ARGParser &parser);
00253         virtual bool setStringArgument(const char* value);
00254 
00255         operator const char *() { return value_.c_str(); }
00256         virtual bool isDefaultValue() { return value_ == defaultValue_; }
00257 
00258 protected:
00259         std::string value_;
00260         std::string defaultValue_;
00261         bool multiline_;
00262 
00263 };
00264 
00265 class OptionEntryStringEnum : public OptionEntryString
00266 {
00267 public:
00268         struct EnumEntry
00269         {
00270                 char *value;
00271         };
00272 
00273         OptionEntryStringEnum(std::list<OptionEntry *> &group,
00274                                                   const char *name, 
00275                                                   const char *description,
00276                                                   unsigned int data,
00277                                                   const char *value,
00278                                                   OptionEntryStringEnum::EnumEntry enums[]);
00279         virtual ~OptionEntryStringEnum();
00280 
00281         virtual EntryType getEntryType() { return OptionEntryStringEnumType; }
00282         virtual bool setValue(const std::string &value);
00283         virtual const char *getDescription();
00284 
00285         virtual bool setValueFromString(const std::string &string);
00286 
00287         OptionEntryStringEnum::EnumEntry *getEnums() { return enums_; }
00288 
00289 protected:
00290         EnumEntry *enums_;
00291 
00292 };
00293 
00294 class OptionEntryFloat : public OptionEntry
00295 {
00296 public:
00297         OptionEntryFloat(std::list<OptionEntry *> &group,
00298                                    const char *name, 
00299                                    const char *description,
00300                                    unsigned int data,
00301                                    float defaultValue,
00302                                    bool truncate = false);
00303         virtual ~OptionEntryFloat();
00304 
00305         virtual EntryType getEntryType() { return OptionEntryFloatType; }
00306         virtual const char *getValueAsString();
00307         virtual const char *getDefaultValueAsString();
00308         virtual bool setValueFromString(const std::string &string);
00309 
00310         virtual float getValue();
00311         virtual bool setValue(float value);
00312 
00313         virtual bool addToArgParser(ARGParser &parser);
00314         virtual bool isDefaultValue() { return value_ == defaultValue_; }
00315 
00316         operator float () { return value_; }
00317 
00318 protected:
00319         bool truncate_;
00320         float defaultValue_;
00321         float value_;
00322 
00323 };
00324 
00325 class OptionEntryVector : public OptionEntry
00326 {
00327 public:
00328         OptionEntryVector(std::list<OptionEntry *> &group,
00329                                    const char *name, 
00330                                    const char *description,
00331                                    unsigned int data,
00332                                    Vector defaultValue,
00333                                    bool truncate = false);
00334         virtual ~OptionEntryVector();
00335 
00336         virtual EntryType getEntryType() { return OptionEntryVectorType; }
00337         virtual const char *getValueAsString();
00338         virtual const char *getDefaultValueAsString();
00339         virtual bool setValueFromString(const std::string &string);
00340 
00341         virtual Vector &getValue();
00342         virtual bool setValue(Vector value);
00343 
00344         virtual bool addToArgParser(ARGParser &parser);
00345         virtual bool isDefaultValue() { return value_ == defaultValue_; }
00346 
00347         operator Vector() { return value_; }
00348 
00349 protected:
00350         bool truncate_;
00351         Vector defaultValue_;
00352         Vector value_;
00353 
00354 };
00355 
00356 class OptionEntryFixed : public OptionEntry
00357 {
00358 public:
00359         OptionEntryFixed(std::list<OptionEntry *> &group,
00360                                    const char *name, 
00361                                    const char *description,
00362                                    unsigned int data,
00363                                    fixed defaultValue);
00364         virtual ~OptionEntryFixed();
00365 
00366         virtual EntryType getEntryType() { return OptionEntryFixedType; }
00367         virtual const char *getValueAsString();
00368         virtual const char *getDefaultValueAsString();
00369         virtual bool setValueFromString(const std::string &string);
00370 
00371         virtual fixed getValue();
00372         virtual bool setValue(fixed value);
00373 
00374         virtual bool addToArgParser(ARGParser &parser);
00375         virtual bool isDefaultValue() { return value_ == defaultValue_; }
00376 
00377         operator fixed () { return value_; }
00378 
00379 protected:
00380         fixed defaultValue_;
00381         fixed value_;
00382 
00383 };
00384 
00385 class OptionEntryFixedVector : public OptionEntry
00386 {
00387 public:
00388         OptionEntryFixedVector(std::list<OptionEntry *> &group,
00389                                    const char *name, 
00390                                    const char *description,
00391                                    unsigned int data,
00392                                    FixedVector defaultValue);
00393         virtual ~OptionEntryFixedVector();
00394 
00395         virtual EntryType getEntryType() { return OptionEntryFixedVectorType; }
00396         virtual const char *getValueAsString();
00397         virtual const char *getDefaultValueAsString();
00398         virtual bool setValueFromString(const std::string &string);
00399 
00400         virtual FixedVector &getValue();
00401         virtual bool setValue(FixedVector value);
00402 
00403         virtual bool addToArgParser(ARGParser &parser);
00404         virtual bool isDefaultValue() { return value_ == defaultValue_; }
00405 
00406         operator FixedVector() { return value_; }
00407 
00408 protected:
00409         FixedVector defaultValue_;
00410         FixedVector value_;
00411 
00412 };
00413 
00414 #endif

Generated on Mon Feb 16 15:14:49 2009 for Scorched3D by  doxygen 1.5.3