string_db.h
1 /*************************************************************************/
2 /* string_db.h */
3 /*************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* http://www.godotengine.org */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2016 Juan Linietsky, Ariel Manzur. */
9 /* */
10 /* Permission is hereby granted, free of charge, to any person obtaining */
11 /* a copy of this software and associated documentation files (the */
12 /* "Software"), to deal in the Software without restriction, including */
13 /* without limitation the rights to use, copy, modify, merge, publish, */
14 /* distribute, sublicense, and/or sell copies of the Software, and to */
15 /* permit persons to whom the Software is furnished to do so, subject to */
16 /* the following conditions: */
17 /* */
18 /* The above copyright notice and this permission notice shall be */
19 /* included in all copies or substantial portions of the Software. */
20 /* */
21 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
22 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
23 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
24 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
25 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
26 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
27 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
28 /*************************************************************************/
29 #ifndef STRING_DB_H
30 #define STRING_DB_H
31 
32 #include "hash_map.h"
33 #include "ustring.h"
34 #include "safe_refcount.h"
35 
40 struct StaticCString {
41 
42  const char *ptr;
43  static StaticCString create(const char *p_ptr);
44 };
45 
46 
47 
48 class StringName {
49 
50 
51  enum {
52 
53  STRING_TABLE_BITS=12,
54  STRING_TABLE_LEN=1<<STRING_TABLE_BITS,
55  STRING_TABLE_MASK=STRING_TABLE_LEN-1
56  };
57 
58  struct _Data {
59  SafeRefCount refcount;
60  const char* cname;
61  String name;
62 
63  String get_name() const { return cname?String(cname):name; }
64  int idx;
65  uint32_t hash;
66  _Data *prev;
67  _Data *next;
68  _Data() { cname=NULL; next=prev=NULL; hash=0; }
69  };
70 
71 
72  static _Data *_table[STRING_TABLE_LEN];
73 
74  _Data *_data;
75 
76  union _HashUnion {
77 
78  _Data *ptr;
79  uint32_t hash;
80  };
81 
82  void unref();
83 friend void register_core_types();
84 friend void unregister_core_types();
85 
86  static void setup();
87  static void cleanup();
88  static bool configured;
89 
90  StringName(_Data *p_data) { _data=p_data; }
91 public:
92 
93 
94  operator const void*() const { return (_data && (_data->cname || !_data->name.empty()))?(void*)1:0; }
95 
96  bool operator==(const String& p_name) const;
97  bool operator==(const char* p_name) const;
98  bool operator!=(const String& p_name) const;
99  _FORCE_INLINE_ bool operator<(const StringName& p_name) const {
100 
101  return _data<p_name._data;
102  }
103  _FORCE_INLINE_ bool operator==(const StringName& p_name) const {
104  // the real magic of all this mess happens here.
105  // this is why path comparisons are very fast
106  return _data==p_name._data;
107  }
108  _FORCE_INLINE_ uint32_t hash() const {
109 
110  if (_data)
111  return _data->hash;
112  else
113  return 0;
114  }
115  bool operator!=(const StringName& p_name) const;
116 
117  _FORCE_INLINE_ operator String() const {
118 
119  if (_data) {
120  if (_data->cname )
121  return String(_data->cname);
122  else
123  return _data->name;
124  }
125 
126  return String();
127  }
128 
129  static StringName search(const char *p_name);
130  static StringName search(const CharType *p_name);
131  static StringName search(const String &p_name);
132 
133  struct AlphCompare {
134 
135  _FORCE_INLINE_ bool operator()(const StringName& l,const StringName& r) const {
136 
137  return l.operator String() < r.operator String();
138  }
139  };
140 
141  void operator=(const StringName& p_name);
142  StringName(const char *p_name);
143  StringName(const StringName& p_name);
144  StringName(const String& p_name);
145  StringName(const StaticCString& p_static_string);
146  StringName();
147  ~StringName();
148 };
149 
151 
152  static _FORCE_INLINE_ uint32_t hash(const StringName &p_string) { return p_string.hash(); }
153 };
154 
155 StringName _scs_create(const char *p_chr);
156 
157 //#define _SCS(m_cstr) (m_cstr[0]?StringName(StaticCString::create(m_cstr)):StringName())
158 #define _SCS(m_cstr) _scs_create(m_cstr)
159 
160 #endif
Definition: string_db.h:150
Definition: string_db.h:133
Definition: safe_refcount.h:336
Definition: string_db.h:48
Definition: string_db.h:40
Definition: ustring.h:64