OpenClonk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
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); }
38 public:
39  static const C4ID None; // Invalid ID
40  DEPRECATED static const C4ID Flag;
41  DEPRECATED static const C4ID Conkit; // Construction kit
42  static const C4ID Clonk;
43  static const C4ID Melee;
44  static const C4ID Bubble;
45  static const C4ID EditorBase;
46 
47  C4ID(): v(None.v) {}
48  C4ID(const C4ID &other) = default;
49  C4ID &operator =(const C4ID &other) = default;
50 
51  explicit C4ID(const std::string &s);
52  explicit C4ID(const StdStrBuf &s) { assign(s.getData()); }
53 
54  explicit inline C4ID(Handle i): v(i)
55  {
56  assert(v < names.size());
57  }
58 
59  inline const char *ToString() const
60  {
61  assert(v < names.size());
62  return names[v].c_str();
63  }
64  inline operator std::string () const
65  {
66  assert(v < names.size());
67  return names[v];
68  }
69  inline Handle GetHandle() const
70  {
71  return v;
72  }
73 
74  void CompileFunc(StdCompiler *pComp);
75 
76  // Compare names instead of v directly so that a sequence of IDs is synchronous
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  inline bool operator >(const C4ID &other) const { return names[v] > names[other.v]; }
81  inline bool operator <=(const C4ID &other) const { return names[v] <= names[other.v]; }
82  inline bool operator >=(const C4ID &other) const { return names[v] >= names[other.v]; }
83 
84  // Safe bool
85  typedef size_t C4ID::*safe_bool_type;
86  inline operator safe_bool_type() const { return v == None.v ? nullptr : &C4ID::v; }
87 };
88 
89 #endif
const char * getData() const
Definition: StdBuf.h:442
bool operator<=(const C4ID &other) const
Definition: C4Id.h:81
size_t C4ID::* safe_bool_type
Definition: C4Id.h:85
#define DEPRECATED
C4ID & operator=(const C4ID &other)=default
bool operator>=(const C4ID &other) const
Definition: C4Id.h:82
C4ID(Handle i)
Definition: C4Id.h:54
static DEPRECATED const C4ID Flag
Definition: C4Id.h:40
C4ID()
Definition: C4Id.h:47
bool operator>(const C4ID &other) const
Definition: C4Id.h:80
static const C4ID None
Definition: C4Id.h:39
static DEPRECATED const C4ID Conkit
Definition: C4Id.h:41
static const C4ID Melee
Definition: C4Id.h:43
Definition: C4Id.h:25
bool operator!=(const C4ID &other) const
Definition: C4Id.h:78
bool operator==(const C4ID &other) const
Definition: C4Id.h:77
bool operator<(const C4ID &other) const
Definition: C4Id.h:79
void CompileFunc(StdCompiler *pComp)
Definition: C4Id.cpp:53
C4ID(const StdStrBuf &s)
Definition: C4Id.h:52
const char * ToString() const
Definition: C4Id.h:59
static const C4ID Clonk
Definition: C4Id.h:42
size_t Handle
Definition: C4Id.h:28
static const C4ID EditorBase
Definition: C4Id.h:45
static const C4ID Bubble
Definition: C4Id.h:44
Handle GetHandle() const
Definition: C4Id.h:69
#define s