MyGUI  3.2.0
MyGUI_Version.h
Go to the documentation of this file.
00001 
00006 /*
00007     This file is part of MyGUI.
00008 
00009     MyGUI is free software: you can redistribute it and/or modify
00010     it under the terms of the GNU Lesser General Public License as published by
00011     the Free Software Foundation, either version 3 of the License, or
00012     (at your option) any later version.
00013 
00014     MyGUI is distributed in the hope that it will be useful,
00015     but WITHOUT ANY WARRANTY; without even the implied warranty of
00016     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017     GNU Lesser General Public License for more details.
00018 
00019     You should have received a copy of the GNU Lesser General Public License
00020     along with MyGUI.  If not, see <http://www.gnu.org/licenses/>.
00021 */
00022 #ifndef __MYGUI_VERSION_H__
00023 #define __MYGUI_VERSION_H__
00024 
00025 #include "MyGUI_Prerequest.h"
00026 #include "MyGUI_Types.h"
00027 #include "MyGUI_StringUtility.h"
00028 
00029 namespace MyGUI
00030 {
00031 
00032     class MYGUI_EXPORT Version
00033     {
00034     public:
00035 
00036         Version(unsigned int _major = 0, unsigned int _minor = 0, unsigned int _patch = 0) :
00037             mMajor(_major),
00038             mMinor(_minor),
00039             mPatch(_patch)
00040         {
00041         }
00042 
00043         friend bool operator < (Version const& a, Version const& b)
00044         {
00045             return (a.mMajor < b.mMajor) ? true : (a.mMinor < b.mMinor);
00046         }
00047 
00048         friend bool operator >= (Version const& a, Version const& b)
00049         {
00050             return !(a < b);
00051         }
00052 
00053         friend bool operator > (Version const& a, Version const& b)
00054         {
00055             return (b < a);
00056         }
00057 
00058         friend bool operator <= (Version const& a, Version const& b)
00059         {
00060             return !(a > b);
00061         }
00062 
00063         friend bool operator == (Version const& a, Version const& b)
00064         {
00065             return !(a < b) && !(a > b);
00066         }
00067 
00068         friend bool operator != (Version const& a, Version const& b)
00069         {
00070             return !(a == b);
00071         }
00072 
00073         friend std::ostream& operator << (std::ostream& _stream, const Version&  _value)
00074         {
00075             _stream << _value.print();
00076             return _stream;
00077         }
00078 
00079         friend std::istream& operator >> (std::istream& _stream, Version&  _value)
00080         {
00081             std::string value;
00082             _stream >> value;
00083             _value = parse(value);
00084             return _stream;
00085         }
00086 
00087         unsigned int getMajor() const
00088         {
00089             return mMajor;
00090         }
00091 
00092         unsigned int getMinor() const
00093         {
00094             return mMinor;
00095         }
00096 
00097         unsigned int getPatch() const
00098         {
00099             return mPatch;
00100         }
00101 
00102         std::string print() const
00103         {
00104             if (mPatch == 0)
00105                 return utility::toString(mMajor, ".", mMinor);
00106             return utility::toString(mMajor, ".", mMinor, ".", mPatch);
00107         }
00108 
00109         static Version parse(const std::string& _value)
00110         {
00111             const std::vector<std::string>& vec = utility::split(_value, ".");
00112             if (vec.empty())
00113                 return Version();
00114 
00115             unsigned int major = utility::parseValue<unsigned int>(vec[0]);
00116             unsigned int minor = vec.size() > 1 ? utility::parseValue<unsigned int>(vec[1]) : 0;
00117             unsigned int patch = vec.size() > 2 ? utility::parseValue<unsigned int>(vec[2]) : 0;
00118 
00119             return Version(major, minor, patch);
00120         }
00121 
00122     private:
00123         unsigned mMajor : 8;
00124         unsigned mMinor : 8;
00125         unsigned mPatch : 16;
00126     };
00127 
00128 } // namespace MyGUI
00129 
00130 #endif // __MYGUI_VERSION_H__