Gorgon Game Engine
SGuid.h
Go to the documentation of this file.
1 
3 #pragma once
4 
5 #include <iostream>
6 #include <time.h>
7 #include <fstream>
8 #include <stdlib.h>
9 #include <string>
10 #include <cstring>
11 #include <stdlib.h>
12 #include <iomanip>
13 #include <stdint.h>
14 
15 #include "Types.h"
16 
17 namespace Gorgon {
18 
22  class SGuid {
23  public:
24 
25  union {
27  Byte Bytes[8];
29  uint64_t Integer;
30  };
31 
33  static const struct CreateNewTag { } CreateNew;
34 
36  SGuid() : Integer(0) { }
37 
40  explicit SGuid(const CreateNewTag&) {
41  New();
42  }
43 
44  // cppcheck-suppress noExplicitConstructor
46  SGuid(const Byte data[8]) {
47  if(data==nullptr) {
48  Integer=0;
49  }
50  else {
51 #ifdef MSVC
52  memcpy_s(Bytes, 8, data, 8);
53 #else
54  std::memcpy(Bytes, data, 8);
55 #endif
56  checknewserial();
57  }
58  }
59 
60  // cppcheck-suppress noExplicitConstructor
62  SGuid(unsigned long long data) {
63  Integer=data;
64  checknewserial();
65  }
66 
68  SGuid(unsigned serial, unsigned random, unsigned time) {
69  Set(serial, random, time);
70  }
71 
73  explicit SGuid(std::istream &in) {
74  Load(in);
75  }
76 
78  bool operator ==(const SGuid &right) const {
79  return Integer==right.Integer;
80  }
81 
83  bool operator !=(const SGuid &right) const {
84  return Integer!=right.Integer;
85  }
86 
88  bool operator <(const SGuid &g) const {
89  return Integer<g.Integer;
90  }
91 
93  void Set(unsigned serial, unsigned random, unsigned time) {
94  //time component - 2
95  /*ints[0] = (time & 0xffff);
96 
97  //random component - 3
98  *(int*)(bytes+2) = random & 0xffffff; //this part is 3 bytes, not 4
99 
100  //serial component - 3
101  bytes[5] = Byte( serial & 0xff);
102  bytes[6] = Byte((serial>>8 ) & 0xff);
103  bytes[7] = Byte((serial>>16) & 0xff);*/
104 
105  memcpy(Bytes+0, &time , 2);
106  memcpy(Bytes+2, &random, 3);
107  memcpy(Bytes+5, &serial, 3);
108 
109  checknewserial();
110  }
111 
113  void New() {
114  time_t t=time(nullptr);
115  int random=rand();
116  serial++;
117 
118  Set(serial, random, (unsigned)t);
119  }
120 
122  operator std::string() const {
123  static char hexlist[]="0123456789abcdef";
124  std::string str;
125 
126  str.resize(18);
127  int j=0;
128  for(int i=7;i>=0;i--) {
129  str[j++]=hexlist[Bytes[i]/16];
130  str[j++]=hexlist[Bytes[i]%16];
131  if(i==5 || i==2) {
132  str[j++]='-';
133  }
134  }
135 
136  return str;
137  }
138 
140  void Load(std::istream &Data) {
141  Data.read((char*)Bytes, 8);
142 
143  checknewserial();
144  }
145 
147  void LoadLong(std::istream &file) {
148  file.read((char*)Bytes, 8);
149  file.read((char*)Bytes, 8);
150  }
151 
153  void Save(std::ostream &file) const {
154  file.write((char*)Bytes, 8);
155  }
156 
158  bool IsEmpty() const {
159  return Integer==0;
160  }
161 
163  operator bool() const {
164  return !IsEmpty();
165  }
166 
168  static const SGuid Empty;
169 
170 
171  private:
172  void checknewserial() const {
173  unsigned s=((Integer>>40) & 0xffffff);
174  if(s>serial)
175  serial=s;
176  }
177 
178  static unsigned serial;
179  };
180 
182  inline std::ostream &operator <<(std::ostream &stream, const SGuid &guid) {
183  stream<<(std::string)guid;
184 
185  return stream;
186  }
187 
189  inline std::istream &operator>>(std::istream &in, SGuid &guid) {
190  guid.Integer=0;
191 
192  while(in.peek()==' ' || in.peek()=='\t' || in.peek()=='\n')
193  in.ignore();
194 
195  if(in.peek()=='%')
196  in.ignore();
197 
198  for(int i=15; i>=0; i--) {
199  char c;
200  c=in.get();
201  c=tolower(c);
202 
203  if(c>='0' && c<='9') {
204  guid.Bytes[i/2]+=(c-'0')<<((i%2) ? 4 : 0);
205  }
206  else if(c>='a' && c<='f') {
207  guid.Bytes[i/2]+=(c-'a'+10)<<((i%2) ? 4 : 0);
208  }
209  else if(c=='-' || c==' ' || c=='\t') i++;
210 
211  if(!in.good()) break;
212  }
213 
214  return in;
215  }
216 
217 }
Gorgon::SGuid::CreateNew
static const struct Gorgon::SGuid::CreateNewTag CreateNew
Definition: SGuid.cpp:9
Gorgon::SGuid::Empty
static const SGuid Empty
Value for empty GUID.
Definition: SGuid.h:168
Gorgon::SGuid::operator<
bool operator<(const SGuid &g) const
Compares two GUIDs.
Definition: SGuid.h:88
Gorgon::Resource::GID::Data
constexpr Type Data
Data resource.
Definition: GID.h:164
Gorgon::SGuid::SGuid
SGuid(const Byte data[8])
Creates a new GUID from the given data.
Definition: SGuid.h:46
Gorgon::SGuid::New
void New()
Generates a new GUID and assign that GUID to this one.
Definition: SGuid.h:113
Gorgon::SGuid::IsEmpty
bool IsEmpty() const
Returns whether this GUID is empty.
Definition: SGuid.h:158
Gorgon::SGuid::SGuid
SGuid(unsigned long long data)
Creates a new GUID from the given data.
Definition: SGuid.h:62
Gorgon::SGuid::Set
void Set(unsigned serial, unsigned random, unsigned time)
Sets the GUID to the given components.
Definition: SGuid.h:93
SGuid.h
contains implementation of a 8-byte short GUID.
Gorgon::SGuid::Integer
uint64_t Integer
Single integer value representing this guid.
Definition: SGuid.h:29
Gorgon::SGuid::LoadLong
void LoadLong(std::istream &file)
Loads a full length 16bit GUID from the given file. Used for back compatibility.
Definition: SGuid.h:147
Gorgon
Root namespace for Gorgon Game Engine.
Definition: Any.h:19
Gorgon::SGuid::SGuid
SGuid(std::istream &in)
Reads a new GUID from the given stream.
Definition: SGuid.h:73
Gorgon::SGuid::operator==
bool operator==(const SGuid &right) const
Compares two GUIDs.
Definition: SGuid.h:78
Gorgon::operator>>
std::istream & operator>>(std::istream &in, TextualProperty< C_, T_, G_, S_ > &p)
Definition: Property.h:837
Gorgon::SGuid::Bytes
Byte Bytes[8]
Allows byte-by-byte addressing of the guid.
Definition: SGuid.h:27
Gorgon::Resource::GID::SGuid
constexpr Type SGuid
Identifies resources.
Definition: GID.h:115
Gorgon::SGuid::SGuid
SGuid(const CreateNewTag &)
Constructor to create a new guid.
Definition: SGuid.h:40
Types.h
contains type definitions for Gorgon system
Gorgon::Byte
unsigned char Byte
Represents smallest cell in memory.
Definition: Types.h:9
Gorgon::SGuid::SGuid
SGuid(unsigned serial, unsigned random, unsigned time)
Creates a new GUID from the given data.
Definition: SGuid.h:68
Gorgon::operator<<
std::ostream & operator<<(std::ostream &out, const TextualProperty< C_, T_, G_, S_ > &p)
Definition: Property.h:823
Gorgon::SGuid
This class represents a short globally unique identifier.
Definition: SGuid.h:22
Gorgon::SGuid::Load
void Load(std::istream &Data)
Loads the GUID from a given stream.
Definition: SGuid.h:140
Gorgon::SGuid::Save
void Save(std::ostream &file) const
Saves this GUID to file.
Definition: SGuid.h:153
Gorgon::SGuid::SGuid
SGuid()
Constructor for an empty guid.
Definition: SGuid.h:36
Gorgon::SGuid::operator!=
bool operator!=(const SGuid &right) const
Compares two GUIDs.
Definition: SGuid.h:83