MyGUI  3.0.1
MyGUI_Guid.h
Go to the documentation of this file.
1 
7 /*
8  This file is part of MyGUI.
9 
10  MyGUI is free software: you can redistribute it and/or modify
11  it under the terms of the GNU Lesser General Public License as published by
12  the Free Software Foundation, either version 3 of the License, or
13  (at your option) any later version.
14 
15  MyGUI is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  GNU Lesser General Public License for more details.
19 
20  You should have received a copy of the GNU Lesser General Public License
21  along with MyGUI. If not, see <http://www.gnu.org/licenses/>.
22 */
23 #ifndef __MYGUI_GUID_H__
24 #define __MYGUI_GUID_H__
25 
26 #include "MyGUI_Prerequest.h"
27 #include "MyGUI_Types.h"
28 #include <memory.h>
29 
30 namespace MyGUI
31 {
32 
34  {
35  public:
36  Guid() { fast._data1 = fast._data2 = fast._data3 = fast._data4 = 0; }
37  Guid( Guid const& _value ) { *this = _value; }
38  explicit Guid(const std::string& _value) { *this = parse(_value); }
39  explicit Guid(unsigned char(&_id)[16]) { ::memcpy((void*)&vec._data1[0], (void*)&_id[0], 16); }
40 
41  bool operator == (Guid const& _comp) const
42  {
43  return _comp.fast._data1 == fast._data1
44  && _comp.fast._data2 == fast._data2
45  && _comp.fast._data3 == fast._data3
46  && _comp.fast._data4 == fast._data4;
47  }
48 
49  bool operator != ( Guid const& _comp ) const
50  {
51  return ! (*this == _comp);
52  }
53 
54  bool operator < ( Guid const& _comp ) const
55  {
56  if (_comp.fast._data1 < fast._data1) return true;
57  else if (_comp.fast._data1 > fast._data1) return false;
58  if (_comp.fast._data2 < fast._data2) return true;
59  else if (_comp.fast._data2 > fast._data2) return false;
60  if (_comp.fast._data3 < fast._data3) return true;
61  else if (_comp.fast._data3 > fast._data3) return false;
62  if (_comp.fast._data4 < fast._data4) return true;
63  return false;
64  }
65 
66  Guid& operator = (Guid const& _rvalue)
67  {
68  fast._data1 = _rvalue.fast._data1;
69  fast._data2 = _rvalue.fast._data2;
70  fast._data3 = _rvalue.fast._data3;
71  fast._data4 = _rvalue.fast._data4;
72  return *this;
73  }
74 
75  bool empty() const
76  {
77  return fast._data1 == 0
78  && fast._data2 == 0
79  && fast._data3 == 0
80  && fast._data4 == 0;
81  }
82 
83  void clear()
84  {
85  fast._data1 = fast._data2 = fast._data3 = fast._data4 = 0;
86  }
87 
88  std::string print() const;
89  static Guid parse(const std::string& _value);
90  static Guid generate();
91 
92  friend std::ostream& operator << ( std::ostream& _stream, const Guid& _value )
93  {
94  _stream << _value.print();
95  return _stream;
96  }
97 
98  friend std::istream& operator >> ( std::istream& _stream, Guid& _value )
99  {
100  std::string value;
101  _stream >> value;
102  if (_stream.fail()) _value.clear();
103  else _value = Guid::parse(value);
104  return _stream;
105  }
106 
107  private:
108  // массив для быстрой конвертации
109  static const char convert_hex[64];
110 
111  struct _original
112  {
113  uint32 data1;
114  uint16 data2, data3;
115  uint8 data4[8];
116  };
117  struct _fast
118  {
119  uint32 _data1, _data2, _data3, _data4;
120  };
121  struct _vec
122  {
123  unsigned char _data1[16];
124  };
125 
126  union
127  {
128  _original original;
129  _fast fast;
130  _vec vec;
131  };
132 
133  };
134 
135 } // namespace MyGUI
136 
137 #endif // __MYGUI_GUID_H__