OpenClonk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
C4ObjectPtr.cpp
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 #include "C4Include.h"
19 #include "object/C4ObjectPtr.h"
20 
21 #include "object/C4GameObjects.h"
22 #include "object/C4Object.h"
23 
24 #include <limits>
25 
26 const C4ObjectPtr C4ObjectPtr::Null(nullptr);
27 
29 {
30  // Pointer needs to be denumerated when decompiling
31  assert(fDenumerated);
32 
33  int32_t nptr = 0;
34  if (!pComp->isDeserializer() && data.ptr)
35  nptr = data.ptr->Number;
36  pComp->Value(nptr);
37  if (pComp->isDeserializer())
38  {
39  data.nptr = nptr;
40 #ifndef NDEBUG
41  // After having read a value the pointer is enumerated
42  fDenumerated = false;
43 #endif
44  }
45 }
46 
48 {
49  assert(!fDenumerated || !data.ptr);
50 
51  assert(data.nptr < std::numeric_limits<int32_t>::max());
52  data.ptr = ::Objects.ObjectPointer(static_cast<int32_t>(data.nptr));
53 
54 #ifndef NDEBUG
55  fDenumerated = true;
56 #endif
57 }
58 
bool fDenumerated
Definition: C4ObjectPtr.h:64
void DenumeratePointers()
Definition: C4ObjectPtr.cpp:47
static const C4ObjectPtr Null
Definition: C4ObjectPtr.h:28
union C4ObjectPtr::@78 data
void Value(const T &rStruct)
Definition: StdCompiler.h:161
void CompileFunc(StdCompiler *pComp)
Definition: C4ObjectPtr.cpp:28
virtual bool isDeserializer()
Definition: StdCompiler.h:53
C4Object * ObjectPointer(int32_t iNumber)
C4GameObjects Objects
Definition: C4Globals.cpp:48