OpenAstra
 
Loading...
Searching...
No Matches
gameid.hpp
1#ifndef OBJECTID_H
2#define OBJECTID_H
3
4#include "json.hpp"
5#include "randomizer.hpp"
6#include "timer.h"
7
8class ObjectId
9{
10public:
11 static ObjectId create() {
12 ObjectId new_id;
13 new_id._value = ((uint64_t)((uint16_t)(Randomizer<uint16_t>().getRandom(0x0000, 0xFFFF))) << 48 ) |
14 (Timer::getTimeEpocUS() & 0x0000FFFFFFFFFFFFULL);
15 return new_id;
16 }
17
18 ObjectId()
19 : _value( _invalidId ) {}
20
21 ObjectId( const ObjectId& other )
22 : _value( other._value ) {}
23
24 bool isValid() const {
25 return _value != _invalidId;
26 }
27
28 const ObjectId &operator =( const ObjectId &b) {
29 _value = b._value;
30 return *this;
31 }
32
33 operator std::string() const {
34 return std::to_string( _value );
35 }
36
37 friend void to_json(nlohmann::json& j, const ObjectId& s) {
38 j = s._value;
39 }
40
41 friend void from_json(const nlohmann::json& j, ObjectId& s) {
42 s._value = j.get<uint64_t>();
43 }
44
45 friend bool operator == (const ObjectId &a, const ObjectId &b) {
46 return a._value == b._value;
47 }
48
49 friend bool operator != (const ObjectId &a, const ObjectId &b) {
50 return a._value != b._value;
51 }
52
53 friend bool operator < (const ObjectId &a, const ObjectId &b) {
54 return a._value < b._value;
55 }
56
57private:
58 static constexpr uint64_t _invalidId = 0;
59 uint64_t _value;
60
61};
62
63#endif // OBJECTID_H
Definition randomizer.hpp:8