OpenClonk
C4ObjectPtr.h
Go to the documentation of this file.
1 /*
2  * OpenClonk, http://www.openclonk.org
3  *
4  * Copyright (c) 2010-2016, The OpenClonk Team and contributors
5  *
6  * Distributed under the terms of the ISC license; see accompanying file
7  * "COPYING" for details.
8  *
9  * "Clonk" is a registered trademark of Matthes Bender, used with permission.
10  * See accompanying file "TRADEMARK" for details.
11  *
12  * To redistribute this file separately, substitute the full license texts
13  * for the above references.
14  */
15 
16 /* A convenient way to (de)serialize object pointers */
17 
18 #ifndef INC_C4ObjectPtr
19 #define INC_C4ObjectPtr
20 
21 // An enumerable C4Object pointer
23 {
24 public:
25  // For use with mkNamingAdapt because simply 0 becomes 0 (the int)
26  // which makes correct template deduction fail. This constant is a
27  // denumerated null pointer which can be repeatedly denumerated.
28  static const C4ObjectPtr Null;
29 
30  C4ObjectPtr() = default; // uninitialized
31 
33 #ifndef NDEBUG
34  : fDenumerated(true)
35 #endif
36  {
37  data.ptr = pObj;
38  }
39 
40  void CompileFunc(StdCompiler* pComp);
41  void DenumeratePointers();
42 
43  bool operator!() const { assert(fDenumerated); return !data.ptr; }
44  bool operator==(C4Object* other) const { assert(fDenumerated); return data.ptr == other; }
45  bool operator==(const C4ObjectPtr& other) const { assert(fDenumerated == other.fDenumerated); return data.ptr == other.data.ptr; }
46  bool operator!=(C4Object* other) const { assert(fDenumerated); return data.ptr != other; }
47  bool operator!=(const C4ObjectPtr& other) const { assert(fDenumerated == other.fDenumerated); return data.ptr != other.data.ptr; }
48 
50  {
51 #ifndef NDEBUG
52  fDenumerated = true;
53 #endif
54  data.ptr = object;
55  return *this;
56  }
57 
58  C4Object& operator*() const { assert(fDenumerated); return *data.ptr; }
59  C4Object* operator->() const { assert(fDenumerated); return data.ptr; }
60  operator C4Object*() const { assert(fDenumerated); return data.ptr; }
61 
62 protected:
63 #ifndef NDEBUG
65 #endif
66 
67  union
68  {
69  C4Object* ptr;
70  intptr_t nptr; // needs to have same size as ptr for operator== to work when enumerated
71  } data;
72 };
73 
74 #endif
bool fDenumerated
Definition: C4ObjectPtr.h:64
union C4ObjectPtr::@80 data
bool operator!() const
Definition: C4ObjectPtr.h:43
bool operator==(C4Object *other) const
Definition: C4ObjectPtr.h:44
C4ObjectPtr operator=(C4Object *object)
Definition: C4ObjectPtr.h:49
C4Object & operator*() const
Definition: C4ObjectPtr.h:58
C4Object * operator->() const
Definition: C4ObjectPtr.h:59
C4ObjectPtr()=default
bool operator==(const C4ObjectPtr &other) const
Definition: C4ObjectPtr.h:45
C4ObjectPtr(C4Object *pObj)
Definition: C4ObjectPtr.h:32
bool operator!=(const C4ObjectPtr &other) const
Definition: C4ObjectPtr.h:47
void CompileFunc(StdCompiler *pComp)
Definition: C4ObjectPtr.cpp:28
void DenumeratePointers()
Definition: C4ObjectPtr.cpp:49
bool operator!=(C4Object *other) const
Definition: C4ObjectPtr.h:46
static const C4ObjectPtr Null
Definition: C4ObjectPtr.h:28