MyGUI  3.0.3
MyGUI_Guid.h
Go to the documentation of this file.
00001 
00007 /*
00008     This file is part of MyGUI.
00009 
00010     MyGUI is free software: you can redistribute it and/or modify
00011     it under the terms of the GNU Lesser General Public License as published by
00012     the Free Software Foundation, either version 3 of the License, or
00013     (at your option) any later version.
00014 
00015     MyGUI 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 Lesser General Public License for more details.
00019 
00020     You should have received a copy of the GNU Lesser General Public License
00021     along with MyGUI.  If not, see <http://www.gnu.org/licenses/>.
00022 */
00023 #ifndef __MYGUI_GUID_H__
00024 #define __MYGUI_GUID_H__
00025 
00026 #include "MyGUI_Prerequest.h"
00027 #include "MyGUI_Types.h"
00028 #include <memory.h>
00029 
00030 namespace MyGUI
00031 {
00032 
00033     class MYGUI_EXPORT Guid
00034     {
00035     public:
00036         Guid() { fast._data1 = fast._data2 = fast._data3 = fast._data4 = 0; }
00037         Guid( Guid const& _value ) { *this = _value; }
00038         explicit Guid(const std::string& _value) { *this = parse(_value); }
00039         explicit Guid(unsigned char(&_id)[16]) { ::memcpy((void*)&vec._data1[0], (void*)&_id[0], 16); }
00040 
00041         bool operator == (Guid const& _comp) const
00042         {
00043             return _comp.fast._data1 == fast._data1
00044                 && _comp.fast._data2 == fast._data2
00045                 && _comp.fast._data3 == fast._data3
00046                 && _comp.fast._data4 == fast._data4;
00047         }
00048 
00049         bool operator != ( Guid const& _comp ) const
00050         {
00051             return ! (*this == _comp);
00052         }
00053 
00054         bool operator < ( Guid const& _comp ) const
00055         {
00056             if (_comp.fast._data1 < fast._data1) return true;
00057             else if (_comp.fast._data1 > fast._data1) return false;
00058             if (_comp.fast._data2 < fast._data2) return true;
00059             else if (_comp.fast._data2 > fast._data2) return false;
00060             if (_comp.fast._data3 < fast._data3) return true;
00061             else if (_comp.fast._data3 > fast._data3) return false;
00062             if (_comp.fast._data4 < fast._data4) return true;
00063             return false;
00064         }
00065 
00066         Guid& operator = (Guid const& _rvalue)
00067         {
00068             fast._data1 = _rvalue.fast._data1;
00069             fast._data2 = _rvalue.fast._data2;
00070             fast._data3 = _rvalue.fast._data3;
00071             fast._data4 = _rvalue.fast._data4;
00072             return *this;
00073         }
00074 
00075         bool empty() const
00076         {
00077             return fast._data1 == 0
00078                 && fast._data2 == 0
00079                 && fast._data3 == 0
00080                 && fast._data4 == 0;
00081         }
00082 
00083         void clear()
00084         {
00085             fast._data1 = fast._data2 = fast._data3 = fast._data4 = 0;
00086         }
00087 
00088         std::string print() const;
00089         static Guid parse(const std::string& _value);
00090         static Guid generate();
00091 
00092         friend std::ostream& operator << ( std::ostream& _stream, const Guid&  _value )
00093         {
00094             _stream << _value.print();
00095             return _stream;
00096         }
00097 
00098         friend std::istream& operator >> ( std::istream& _stream, Guid&  _value )
00099         {
00100             std::string value;
00101             _stream >> value;
00102             if (_stream.fail()) _value.clear();
00103             else _value = Guid::parse(value);
00104             return _stream;
00105         }
00106 
00107     private:
00108         // массив для быстрой конвертации
00109         static const char convert_hex[64];
00110 
00111         struct _original
00112         {
00113             uint32 data1;
00114             uint16 data2, data3;
00115             uint8 data4[8];
00116         };
00117         struct _fast
00118         {
00119             uint32 _data1, _data2, _data3, _data4;
00120         };
00121         struct _vec
00122         {
00123             unsigned char _data1[16];
00124         };
00125 
00126         union
00127         {
00128             _original original;
00129             _fast fast;
00130             _vec vec;
00131         };
00132 
00133     };
00134 
00135 } // namespace MyGUI
00136 
00137 #endif // __MYGUI_GUID_H__