Honeycomb  0.1
Component-Model Framework
Id.h
Go to the documentation of this file.
1 // Honeycomb, Copyright (C) 2015 NewGamePlus Inc. Distributed under the Boost Software License v1.0.
2 #pragma once
3 
4 #include "Honey/String/String.h"
5 #include "Honey/String/Hash.h"
6 
7 namespace honey
8 {
9 
19 
21 class IdLiteral;
22 class ByteStream;
23 
25 class Id
26 {
27  friend class IdLiteral;
28 public:
29  Id() : _hash(0) {}
30  Id(const String& name) : debug_if(_name(name),) _hash(hash::fast(name)) {}
32  Id(const String& name, szt hash) : debug_if(_name(name),) _hash(hash) { assert(_hash == hash::fast(_name)); }
33  Id(const IdLiteral& rhs);
34  Id(const Id& rhs) : debug_if(_name(rhs._name),) _hash(rhs._hash) {}
35  Id(Id&& rhs) : debug_if(_name(move(rhs._name)),) _hash(rhs._hash) {}
36 
37  Id& operator=(const Id& rhs) { debug_if(_name = rhs._name;) _hash = rhs._hash; return *this; }
38  Id& operator=(Id&& rhs) { debug_if(_name = move(rhs._name);) _hash = rhs._hash; return *this; }
39  Id& operator=(const IdLiteral& rhs);
40 
41  bool operator==(const Id& rhs) const { return _hash == rhs._hash; }
42  bool operator!=(const Id& rhs) const { return _hash != rhs._hash; }
43  bool operator< (const Id& rhs) const { return _hash < rhs._hash; }
44  bool operator> (const Id& rhs) const { return _hash > rhs._hash; }
45  bool operator<=(const Id& rhs) const { return _hash <= rhs._hash; }
46  bool operator>=(const Id& rhs) const { return _hash >= rhs._hash; }
47 
48  bool operator==(const IdLiteral& rhs) const;
49  bool operator!=(const IdLiteral& rhs) const;
50  bool operator< (const IdLiteral& rhs) const;
51  bool operator> (const IdLiteral& rhs) const;
52  bool operator<=(const IdLiteral& rhs) const;
53  bool operator>=(const IdLiteral& rhs) const;
54 
55  #ifndef FINAL
56  const String& name() const { return _name; }
58  #endif
59  szt hash() const { return _hash; }
61 
63  operator szt() const { return _hash; }
64 
65  #ifndef FINAL
66  friend ostream& operator<<(ostream& os, const Id& val) { return val._hash ? (val._name.length() ? os << val._name : os << val._hash) : os << "idnull"; }
67  #else
68  friend ostream& operator<<(ostream& os, const Id& val) { return val._hash ? os << val._hash : os << "idnull"; }
69  #endif
70  friend ByteStream& operator<<(ByteStream& os, const Id& val);
71  friend ByteStream& operator>>(ByteStream& is, Id& val);
72 
73 protected:
74  #ifndef FINAL
76  #endif
78 };
79 
81 class IdLiteral
82 {
83  friend class Id;
84 public:
85  constexpr IdLiteral() : debug_if(_name(""),) _hash(0) {}
86  constexpr IdLiteral(const char* str, szt len) : debug_if(_name(str),) _hash(hash::fast_(str, len)) {}
87 
88  constexpr bool operator==(const IdLiteral& rhs) const { return _hash == rhs._hash; }
89  constexpr bool operator!=(const IdLiteral& rhs) const { return _hash != rhs._hash; }
90  constexpr bool operator< (const IdLiteral& rhs) const { return _hash < rhs._hash; }
91  constexpr bool operator> (const IdLiteral& rhs) const { return _hash > rhs._hash; }
92  constexpr bool operator<=(const IdLiteral& rhs) const { return _hash <= rhs._hash; }
93  constexpr bool operator>=(const IdLiteral& rhs) const { return _hash >= rhs._hash; }
94 
95  bool operator==(const Id& rhs) const { return _hash == rhs._hash; }
96  bool operator!=(const Id& rhs) const { return _hash != rhs._hash; }
97  bool operator< (const Id& rhs) const { return _hash < rhs._hash; }
98  bool operator> (const Id& rhs) const { return _hash > rhs._hash; }
99  bool operator<=(const Id& rhs) const { return _hash <= rhs._hash; }
100  bool operator>=(const Id& rhs) const { return _hash >= rhs._hash; }
101 
102  #ifndef FINAL
103  constexpr const char* name() const { return _name; }
104  #endif
105  constexpr szt hash() const { return _hash; }
106 
107  constexpr operator szt() const { return _hash; }
108 
109  #ifndef FINAL
110  friend ostream& operator<<(ostream& os, const IdLiteral& val) { return val._hash ? os << val._name : os << "idnull"; }
111  #else
112  friend ostream& operator<<(ostream& os, const IdLiteral& val) { return val._hash ? os << val._hash : os << "idnull"; }
113  #endif
114  friend ByteStream& operator<<(ByteStream& os, const IdLiteral& val);
115 
116 private:
117  #ifndef FINAL
118  const char* _name;
119  #endif
120  szt _hash;
121 };
122 
124 #define idnull IdLiteral()
125 
127 
130 constexpr IdLiteral operator"" _id(const char* str, szt len) { return IdLiteral(str, len); }
131 
133 inline Id::Id(const IdLiteral& rhs) : debug_if(_name(rhs._name),) _hash(rhs._hash) {}
134 inline Id& Id::operator=(const IdLiteral& rhs) { debug_if(_name = rhs._name;) _hash = rhs._hash; return *this; }
135 inline bool Id::operator==(const IdLiteral& rhs) const { return _hash == rhs._hash; }
136 inline bool Id::operator!=(const IdLiteral& rhs) const { return _hash != rhs._hash; }
137 inline bool Id::operator<=(const IdLiteral& rhs) const { return _hash <= rhs._hash; }
138 inline bool Id::operator>=(const IdLiteral& rhs) const { return _hash >= rhs._hash; }
139 inline bool Id::operator< (const IdLiteral& rhs) const { return _hash < rhs._hash; }
140 inline bool Id::operator> (const IdLiteral& rhs) const { return _hash > rhs._hash; }
143 class NameId : public Id
145 {
146 public:
147  NameId() = default;
148  NameId(const String& name) : Id(name), _name(name) {}
149  NameId(const char* name) : Id(name), _name(name) {}
151  explicit NameId(const Id& id) : Id(id) {}
153  NameId(const String& name, const Id& id) : Id(id), _name(name) { assert(_name == Id::name()); }
154 
155  const String& name() const { return _name; }
156 
157  friend ostream& operator<<(ostream& os, const NameId& val) { return os << val._name; }
158  friend ByteStream& operator<<(ByteStream& os, const NameId& val);
159  friend ByteStream& operator>>(ByteStream& is, NameId& val);
160 
161 private:
162  String _name;
163 };
165 
166 }
167 
169 template<>
171 struct std::hash<honey::Id>
172 {
173  size_t operator()(const honey::Id& val) const { return val.hash(); }
174 };
175 
176 template<>
177 struct std::hash<honey::IdLiteral>
178 {
179  size_t operator()(const honey::IdLiteral& val) const { return val.hash(); }
180 };
181 
182 template<>
183 struct std::hash<honey::NameId>
184 {
185  size_t operator()(const honey::NameId& val) const { return val.hash(); }
186 };
szt fast(ByteBufConst bs, szt seed)
Quickly generate a small hash value. Each seed value produces a unique hash from the same data...
Definition: Hash.cpp:99
Id(const String &name)
Definition: Id.h:30
friend ostream & operator<<(ostream &os, const IdLiteral &val)
Definition: Id.h:110
istream & operator>>(istream &is, Bytes &val)
Definition: Bytes.cpp:16
friend ostream & operator<<(ostream &os, const NameId &val)
Definition: Id.h:157
constexpr szt hash() const
Definition: Id.h:105
Holds both a name string and its hashed value, and unlike Id the name is never compiled out...
Definition: Id.h:144
bool operator>(const String &lhs, const String &rhs)
Definition: String.h:148
constexpr IdLiteral()
Definition: Id.h:85
bool operator>(const Id &rhs) const
Definition: Id.h:44
szt hash() const
Get hashed integral value of name.
Definition: Id.h:60
friend ByteStream & operator>>(ByteStream &is, Id &val)
Id from bytes.
Definition: ByteStream.h:344
bool operator!=(const String &lhs, const String &rhs)
Definition: String.h:142
Id(const String &name, szt hash)
Construct with precalculated hash.
Definition: Id.h:32
Id & operator=(Id &&rhs)
Definition: Id.h:38
Id created from a string literal at compile-time.
Definition: Id.h:81
constexpr bool operator<=(const IdLiteral &rhs) const
Definition: Id.h:92
bool operator<=(const Id &rhs) const
Definition: Id.h:99
bool operator>=(const Id &rhs) const
Definition: Id.h:100
constexpr const char * name() const
Definition: Id.h:103
NameId(const char *name)
Definition: Id.h:149
Id(Id &&rhs)
Definition: Id.h:35
bool operator>=(const Id &rhs) const
Definition: Id.h:46
constexpr szt fast_(const char *str, szt len, szt seed=0)
Compile-time version of fast() for UTF-8 strings.
Definition: Hash.h:96
NameId(const String &name)
Definition: Id.h:148
Id & operator=(const Id &rhs)
Definition: Id.h:37
bool operator<(const String &lhs, const String &rhs)
Definition: String.h:145
bool operator!=(const Id &rhs) const
Definition: Id.h:42
const String & name() const
Get name string that this id represents.
Definition: Id.h:57
#define assert(...)
Forwards to assert_#args. See assert_1(), assert_2().
Definition: Debug.h:24
bool operator<=(const Id &rhs) const
Definition: Id.h:45
Unicode UTF-16 string class, wrapper around std::u16string.
Definition: String.h:23
An I/O stream into which objects may be serialized and subsequently deserialized. ...
Definition: ByteStream.h:63
bool operator<(const Id &rhs) const
Definition: Id.h:43
Id()
Definition: Id.h:29
bool operator==(const Id &rhs) const
Definition: Id.h:95
constexpr bool operator!=(const IdLiteral &rhs) const
Definition: Id.h:89
size_t szt
Size type, shorthand for size_t.
Definition: Core.h:90
constexpr bool operator>(const IdLiteral &rhs) const
Definition: Id.h:91
NameId(const Id &id)
Create with id, leaving name empty.
Definition: Id.h:151
szt _hash
Definition: Id.h:77
constexpr bool operator>=(const IdLiteral &rhs) const
Definition: Id.h:93
Id(const Id &rhs)
Definition: Id.h:34
constexpr bool operator==(const IdLiteral &rhs) const
Definition: Id.h:88
#define debug_if(...)
Evaluate expression in debug mode only, does nothing in final mode.
Definition: Debug.h:30
constexpr IdLiteral(const char *str, szt len)
Definition: Id.h:86
bool operator<=(const String &lhs, const String &rhs)
Definition: String.h:151
NameId(const String &name, const Id &id)
Create with name and precalculated id.
Definition: Id.h:153
constexpr bool operator<(const IdLiteral &rhs) const
Definition: Id.h:90
bool operator==(const Id &rhs) const
Definition: Id.h:41
Holds a name string and its hashed value for fast comparison ops. See String Identifier.
Definition: Id.h:25
bool operator!=(const Id &rhs) const
Definition: Id.h:96
bool operator>=(const String &lhs, const String &rhs)
Definition: String.h:154
ostream & operator<<(ostream &os, const Bytes &val)
Definition: Bytes.cpp:9
const String & name() const
Definition: Id.h:155
String _name
Definition: Id.h:75
Global Honeycomb namespace.
friend ostream & operator<<(ostream &os, const Id &val)
Definition: Id.h:66