OpenClonk
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  {
36  nptr = data.ptr->Number;
37  }
38  pComp->Value(nptr);
39  if (pComp->isDeserializer())
40  {
41  data.nptr = nptr;
42 #ifndef NDEBUG
43  // After having read a value the pointer is enumerated
44  fDenumerated = false;
45 #endif
46  }
47 }
48 
50 {
51  assert(!fDenumerated || !data.ptr);
52 
53  assert(data.nptr < std::numeric_limits<int32_t>::max());
54  data.ptr = ::Objects.ObjectPointer(static_cast<int32_t>(data.nptr));
55 
56 #ifndef NDEBUG
57  fDenumerated = true;
58 #endif
59 }
60 
C4GameObjects Objects
Definition: C4Globals.cpp:48
C4Object * ObjectPointer(int32_t object_number)
bool fDenumerated
Definition: C4ObjectPtr.h:64
union C4ObjectPtr::@80 data
void CompileFunc(StdCompiler *pComp)
Definition: C4ObjectPtr.cpp:28
void DenumeratePointers()
Definition: C4ObjectPtr.cpp:49
static const C4ObjectPtr Null
Definition: C4ObjectPtr.h:28
void Value(const T &rStruct)
Definition: StdCompiler.h:161
virtual bool isDeserializer()
Definition: StdCompiler.h:53