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 #include <map>
25 #include <string>
26 #include <vector>
27 
28 class C4ID
29 {
30 public:
31  typedef size_t Handle;
32 private:
33  Handle v;
34  typedef std::map<std::string, Handle> LookupTable;
35  typedef std::vector<std::string> NamesList;
36  static LookupTable lookup;
37  static NamesList names;
38  void assign(const std::string &s);
39  template<size_t N>
40  explicit C4ID(const char (&s)[N]) { assign(s); }
41 public:
42  static const C4ID None; // Invalid ID
43  DEPRECATED static const C4ID Flag;
44  DEPRECATED static const C4ID Conkit; // Construction kit
45  static const C4ID Clonk;
46  static const C4ID Melee;
47  static const C4ID Bubble;
48  static const C4ID EditorBase;
49 
50  C4ID(): v(None.v) {}
51  C4ID(const C4ID &other): v(other.v) {}
52  C4ID &operator =(const C4ID &other) { v = other.v; return *this; }
53 
54  explicit C4ID(const std::string &s);
55  explicit C4ID(const StdStrBuf &s) { assign(s.getData()); }
56 
57  explicit inline C4ID(Handle i): v(i)
58  {
59  assert(v < names.size());
60  }
61 
62  inline const char *ToString() const
63  {
64  assert(v < names.size());
65  return names[v].c_str();
66  }
67  inline operator std::string () const
68  {
69  assert(v < names.size());
70  return names[v];
71  }
72  inline Handle GetHandle() const
73  {
74  return v;
75  }
76 
77  void CompileFunc(StdCompiler *pComp);
78 
79  // Compare names instead of v directly so that a sequence of IDs is synchronous
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  inline bool operator >(const C4ID &other) const { return names[v] > names[other.v]; }
84  inline bool operator <=(const C4ID &other) const { return names[v] <= names[other.v]; }
85  inline bool operator >=(const C4ID &other) const { return names[v] >= names[other.v]; }
86 
87  // Safe bool
88  typedef size_t C4ID::*safe_bool_type;
89  inline operator safe_bool_type() const { return v == None.v ? 0 : &C4ID::v; }
90 };
91 
92 #endif
const char * getData() const
Definition: StdBuf.h:450
C4ID(const C4ID &other)
Definition: C4Id.h:51
bool operator<=(const C4ID &other) const
Definition: C4Id.h:84
size_t C4ID::* safe_bool_type
Definition: C4Id.h:88
#define DEPRECATED
bool operator>=(const C4ID &other) const
Definition: C4Id.h:85
C4ID(Handle i)
Definition: C4Id.h:57
static DEPRECATED const C4ID Flag
Definition: C4Id.h:43
C4ID()
Definition: C4Id.h:50
bool operator>(const C4ID &other) const
Definition: C4Id.h:83
static const C4ID None
Definition: C4Id.h:42
static DEPRECATED const C4ID Conkit
Definition: C4Id.h:44
static const C4ID Melee
Definition: C4Id.h:46
Definition: C4Id.h:28
bool operator!=(const C4ID &other) const
Definition: C4Id.h:81
bool operator==(const C4ID &other) const
Definition: C4Id.h:80
bool operator<(const C4ID &other) const
Definition: C4Id.h:82
void CompileFunc(StdCompiler *pComp)
Definition: C4Id.cpp:54
C4ID(const StdStrBuf &s)
Definition: C4Id.h:55
const char * ToString() const
Definition: C4Id.h:62
static const C4ID Clonk
Definition: C4Id.h:45
C4ID & operator=(const C4ID &other)
Definition: C4Id.h:52
size_t Handle
Definition: C4Id.h:31
static const C4ID EditorBase
Definition: C4Id.h:48
static const C4ID Bubble
Definition: C4Id.h:47
Handle GetHandle() const
Definition: C4Id.h:72
#define s