OpenClonk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
C4MeshDenumerator.cpp
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 #include "C4Include.h"
20 #include "object/C4Object.h"
21 #include "object/C4Def.h"
22 
23 const StdMeshInstance::AttachedMesh::DenumeratorFactoryFunc C4MeshDenumeratorFactory = StdMeshInstance::AttachedMesh::DenumeratorFactory<C4MeshDenumerator>;
24 
26 {
27  if(pComp->isDeserializer())
28  {
29  int32_t def;
30  pComp->Value(mkNamingCountAdapt(def, "ChildInstance"));
31 
32  if(def)
33  {
34  C4DefGraphics* pGfx = nullptr;
35  pComp->Value(mkNamingAdapt(C4DefGraphicsAdapt(pGfx), "ChildMesh"));
36  Def = pGfx->pDef;
37 
38  if(pGfx->Type != C4DefGraphics::TYPE_Mesh)
39  pComp->excCorrupt("ChildMesh points to non-mesh graphics");
40  assert(!attach->Child);
41  pComp->Value(mkParAdapt(mkNamingContextPtrAdapt(attach->Child, *pGfx->Mesh, "ChildInstance"), C4MeshDenumeratorFactory));
42  assert(attach->Child != nullptr);
43  attach->OwnChild = true; // Delete the newly allocated child instance when the parent instance is gone
44 
45  // TODO: Do we leak pGfx?
46  }
47  else
48  {
49  pComp->Value(mkNamingAdapt(Object, "ChildObject"));
50  attach->OwnChild = false; // Keep child instance when parent instance is gone since it belongs to a different object
51  }
52  }
53  else
54  {
55  int32_t def = 0;
56  if(Def) ++def;
57  pComp->Value(mkNamingCountAdapt(def, "ChildInstance"));
58 
59  if(Def)
60  {
61  assert(attach->OwnChild);
62  C4DefGraphics* pGfx = &Def->Graphics;
63  assert(pGfx->Type == C4DefGraphics::TYPE_Mesh);
64  pComp->Value(mkNamingAdapt(C4DefGraphicsAdapt(pGfx), "ChildMesh"));
65  pComp->Value(mkParAdapt(mkNamingContextPtrAdapt(attach->Child, *pGfx->Mesh, "ChildInstance"), C4MeshDenumeratorFactory));
66  }
67  else
68  {
69  assert(!attach->OwnChild);
70  pComp->Value(mkNamingAdapt(Object, "ChildObject"));
71  }
72  }
73 }
74 
76 {
77  Object.DenumeratePointers();
78 
79  // Set child instance of attach after denumeration
80  if(Object)
81  {
82  assert(!attach->OwnChild);
83  assert(!attach->Child || attach->Child == Object->pMeshInstance);
84  if(!attach->Child)
85  attach->Child = Object->pMeshInstance;
86  }
87 }
88 
90 {
91  if(Object == pObj)
92  {
93  Object = nullptr;
94  // Return false causes the attached mesh to be deleted by StdMeshInstance
95  return false;
96  }
97 
98  return true;
99 }
StdNamingCountAdapt< int_t > mkNamingCountAdapt(int_t &iCount, const char *szName)
Definition: StdAdaptors.h:977
void excCorrupt(const char *szMessage,...)
Definition: StdCompiler.h:259
const StdMeshInstance::AttachedMesh::DenumeratorFactoryFunc C4MeshDenumeratorFactory
StdContextPtrAdapt< T, ContextT > mkNamingContextPtrAdapt(T *&rpObj, const ContextT &ctx, const char *szNaming)
Definition: StdAdaptors.h:612
StdMeshInstance * Child
Definition: StdMesh.h:513
void DenumeratePointers()
Definition: C4ObjectPtr.cpp:46
StdNamingAdapt< T > mkNamingAdapt(T &&rValue, const char *szName)
Definition: StdAdaptors.h:93
virtual void CompileFunc(StdCompiler *pComp, StdMeshInstance::AttachedMesh *attach)
Denumerator *(* DenumeratorFactoryFunc)()
Definition: StdMesh.h:501
virtual void DenumeratePointers(StdMeshInstance::AttachedMesh *attach)
void Value(const T &rStruct)
Definition: StdCompiler.h:171
virtual bool ClearPointers(C4Object *pObj)
virtual bool isDeserializer()
Definition: StdCompiler.h:63
GraphicsType Type
Definition: C4DefGraphics.h:48
StdParameterAdapt< T, P > mkParAdapt(T &&rObj, P &&rPar)
Definition: StdAdaptors.h:459
C4DefGraphics Graphics
Definition: C4Def.h:194
StdMeshInstance * pMeshInstance
Definition: C4Object.h:155