OpenClonk
C4Id.h
Go to the documentation of this file.
1 /*
2  * OpenClonk, http://www.openclonk.org
3  *
4  * Copyright (c) 1998-2000, Matthes Bender
5  * Copyright (c) 2001-2009, RedWolf Design GmbH, http://www.clonk.de/
6  * Copyright (c) 2009-2016, The OpenClonk Team and contributors
7  *
8  * Distributed under the terms of the ISC license; see accompanying file
9  * "COPYING" for details.
10  *
11  * "Clonk" is a registered trademark of Matthes Bender, used with permission.
12  * See accompanying file "TRADEMARK" for details.
13  *
14  * To redistribute this file separately, substitute the full license texts
15  * for the above references.
16  */
17 
18 /* Value to identify object definitions */
19 
20 #ifndef INC_C4Id
21 #define INC_C4Id
22 
23 #include "lib/StdAdaptors.h"
24 
25 class C4ID
26 {
27 public:
28  typedef size_t Handle;
29 private:
30  Handle v;
31  typedef std::map<std::string, Handle> LookupTable;
32  typedef std::vector<std::string> NamesList;
33  static LookupTable lookup;
34  static NamesList names;
35  void assign(const std::string &s);
36  template<size_t N>
37  explicit C4ID(const char (&s)[N]) { assign(s); } // @suppress("Class members should be properly initialized"): The call to assign() initializes 'v'
38 public:
39  static const C4ID None; // Invalid ID
40  static const C4ID Clonk;
41  static const C4ID Bubble;
42  static const C4ID EditorBase;
43 
44  C4ID(): v(None.v) {}
45  C4ID(const C4ID &other) = default;
46  C4ID &operator =(const C4ID &other) = default;
47 
48  explicit C4ID(const std::string &s);
49  explicit C4ID(const StdStrBuf &s) { assign(s.getData()); }
50 
51  explicit inline C4ID(Handle i): v(i)
52  {
53  assert(v < names.size());
54  }
55 
56  inline const char *ToString() const
57  {
58  assert(v < names.size());
59  return names[v].c_str();
60  }
61  inline operator std::string () const
62  {
63  assert(v < names.size());
64  return names[v];
65  }
66  inline Handle GetHandle() const
67  {
68  return v;
69  }
70 
71  void CompileFunc(StdCompiler *pComp);
72 
73  // Compare names instead of v directly so that a sequence of IDs is synchronous
74  inline bool operator ==(const C4ID &other) const { return names[v] == names[other.v]; }
75  inline bool operator !=(const C4ID &other) const { return names[v] != names[other.v]; }
76  inline bool operator <(const C4ID &other) const { return names[v] < names[other.v]; }
77  inline bool operator >(const C4ID &other) const { return names[v] > names[other.v]; }
78  inline bool operator <=(const C4ID &other) const { return names[v] <= names[other.v]; }
79  inline bool operator >=(const C4ID &other) const { return names[v] >= names[other.v]; }
80 
81  // Safe bool
82  typedef size_t C4ID::*safe_bool_type;
83  inline operator safe_bool_type() const { return v == None.v ? nullptr : &C4ID::v; }
84 };
85 
86 #endif
#define s
Definition: C4Id.h:26
Handle GetHandle() const
Definition: C4Id.h:66
static const C4ID Clonk
Definition: C4Id.h:40
size_t Handle
Definition: C4Id.h:28
static const C4ID Bubble
Definition: C4Id.h:41
static const C4ID EditorBase
Definition: C4Id.h:42
C4ID(const StdStrBuf &s)
Definition: C4Id.h:49
C4ID(const C4ID &other)=default
C4ID()
Definition: C4Id.h:44
size_t C4ID::* safe_bool_type
Definition: C4Id.h:82
bool operator==(const C4ID &other) const
Definition: C4Id.h:74
void CompileFunc(StdCompiler *pComp)
Definition: C4Id.cpp:51
bool operator!=(const C4ID &other) const
Definition: C4Id.h:75
bool operator<(const C4ID &other) const
Definition: C4Id.h:76
bool operator>(const C4ID &other) const
Definition: C4Id.h:77
bool operator>=(const C4ID &other) const
Definition: C4Id.h:79
const char * ToString() const
Definition: C4Id.h:56
bool operator<=(const C4ID &other) const
Definition: C4Id.h:78
C4ID & operator=(const C4ID &other)=default
static const C4ID None
Definition: C4Id.h:39
C4ID(Handle i)
Definition: C4Id.h:51