version.hpp

Go to the documentation of this file.
00001 /* $Id: version.hpp 52533 2012-01-07 02:35:17Z shadowmaster $ */
00002 /*
00003    Copyright (C) 2008 - 2012 by Ignacio R. Morelle <shadowm2006@gmail.com>
00004    Part of the Battle for Wesnoth Project http://www.wesnoth.org/
00005 
00006    This program 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    This program is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY.
00012 
00013    See the COPYING file for more details.
00014 */
00015 
00016 #ifndef VERSION_HPP_INCLUDED
00017 #define VERSION_HPP_INCLUDED
00018 
00019 #include <string>
00020 #include <vector>
00021 
00022 /**
00023  * @file
00024  * Interfaces for manipulating version numbers of engine,
00025  * add-ons, etc.
00026  *
00027  * This class assumes versions are in the format "X.Y.Z", with
00028  * additional components not being associated to canonical
00029  * names.
00030  */
00031 class version_info
00032 {
00033 public:
00034     /** Thrown when trying to compare an non-sane version_info object. */
00035     struct not_sane_exception {};
00036 
00037     version_info();                    /**< Default constructor. */
00038     version_info(const version_info&); /**< Copy constructor. */
00039     version_info(const std::string&);  /**< String constructor. */
00040 
00041     /** Simple list constructor. */
00042     version_info(unsigned int major, unsigned int minor, unsigned int revision_level, bool sane = true,
00043                  char special_separator='\0', const std::string& special=std::string());
00044 
00045     /** Assignment operator. */
00046     version_info& operator=(const version_info& o) {
00047         this->assign(o);
00048         return *this;
00049     }
00050 
00051     bool good() const { return this->sane_; }
00052     bool is_canonical() const;
00053 
00054     // Good old setters and getters for this class. Their names should be
00055     // pretty self-descriptive. I couldn't use shorter names such as
00056     // major() or minor() because sys/sysmacros.h reserves them by defining
00057     // some backwards-compatibility macros for stuff, and they cause
00058     // conflicts in the C/C++ preprocessor on GNU/Linux (GCC).
00059 
00060     // Canonical version components.
00061 
00062     unsigned int major_version() const;  /**< Retrieves the major version number (X in "X.Y.Z"). */
00063     unsigned int minor_version() const;  /**< Retrieves the minor version number (Y in "X.Y.Z"). */
00064     unsigned int revision_level() const; /**< Retrieves the revision level (Z in "X.Y.Z"). */
00065 
00066     /**
00067      * It is sometimes useful so append special build/distribution
00068      * information to version numbers, in the form of "X.Y.Z+svn",
00069      * "X.Y.Za", etcetera. This member function retrieves such if
00070      * available.
00071      */
00072     const std::string& special_version() const {
00073         return this->special_;
00074     }
00075 
00076     /**
00077      * Retrieves the special version separator. For the "X.Y.Z+blah"
00078      * string, it would be '+'. On the other hand, it would be a null
00079      * (ASCII 00) character if the string was "X.Y.Za".
00080      */
00081     char special_version_separator() const {
00082         return this->special_separator_;
00083     }
00084 
00085     void set_major_version(unsigned int);  /**< Set major version number. */
00086     void set_minor_version(unsigned int);  /**< Set minor version number. */
00087     void set_revision_level(unsigned int); /**< Set revision level. */
00088 
00089     /** Set special version string. */
00090     void set_special_version(const std::string& str) {
00091         this->special_ = str;
00092     }
00093 
00094     // Non-canonical version strings components.
00095 
00096     /**
00097      * Returns a component from a non-canonically formatted
00098      * version string (i.e. 'D' from A.B.C.D is index 3).
00099      * The index may be in the [0,3) range; in such case, this
00100      * function works identically to the canonical version
00101      * numbers extractors.
00102      *
00103      * @note If the number of components is smaller than index-1,
00104      *       a std::out_of_range exception may be thrown.
00105      */
00106     unsigned int get_component(size_t index) const {
00107         return nums_.at(index);
00108     }
00109 
00110     /**
00111      * Sets a component in a non-canonically formatted
00112      * version string (i.e. 'D' from A.B.C.D is index 3).
00113      * The index may be in the [0,3) range; in such case, this
00114      * function works identically to the canonical version
00115      * numbers setters.
00116      *
00117      * @note If the number of components is smaller than index-1,
00118      *       new ones are added and initialized as 0 to make room.
00119      */
00120     void set_component(size_t index, unsigned int value) {
00121         nums_.at(index) = value;
00122     }
00123 
00124     /** Read-only access to complete vector of components. */
00125     const std::vector<unsigned int>& components() const {
00126         return this->nums_;
00127     }
00128 
00129     std::string str() const; /**<
00130                                * Returns a formatted string of the form
00131                                * A.B.C[.x1[.x2[...]]]kS, where xN stand for
00132                                * non-canonical version components, k for the
00133                                * suffix separator, and S for the suffix string.
00134                                */
00135 
00136     /** Syntactic shortcut for str(). */
00137     operator std::string() const { return this->str(); }
00138 
00139 protected:
00140     /** Assign from other version_info object. */
00141     void assign(const version_info&);
00142 
00143 private:
00144     std::vector<unsigned int> nums_;
00145     std::string               special_;
00146     char                      special_separator_;
00147     bool                      sane_;
00148 };
00149 
00150 /** Equality operator for version_info. */
00151 bool operator==(const version_info&, const version_info&);
00152 /** Inequality operator for version_info. */
00153 bool operator!=(const version_info&, const version_info&);
00154 /** Greater-than operator for version_info. */
00155 bool operator>(const version_info&, const version_info&);
00156 /** Less-than operator for version_info. */
00157 bool operator<(const version_info&, const version_info&);
00158 /** Greater-than-or-equal operator for version_info. */
00159 bool operator>=(const version_info&, const version_info&);
00160 /** Less-than-or-equal operator for version_info. */
00161 bool operator<=(const version_info&, const version_info&);
00162 
00163 enum VERSION_COMP_OP {
00164     OP_INVALID,
00165     OP_EQUAL,
00166     OP_NOT_EQUAL,
00167     OP_LESS,
00168     OP_LESS_OR_EQUAL,
00169     OP_GREATER,
00170     OP_GREATER_OR_EQUAL
00171 };
00172 
00173 VERSION_COMP_OP parse_version_op(const std::string& op_str);
00174 bool do_version_check(const version_info& a, VERSION_COMP_OP op, const version_info& b);
00175 
00176 #endif /* !VERSION_HPP_INCLUDED */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated by doxygen 1.7.1 on Fri May 25 2012 01:03:14 for The Battle for Wesnoth
Gna! | Forum | Wiki | CIA | devdocs