fixed: auto_ptr -> unique_ptr
[opensg.git] / Source / System / NodeCores / Groups / Light / testLightReferenceCountTest.cpp
blob43e7593c0e55d1bc9ab76eac8cf382cbfba55b5d
1 /*---------------------------------------------------------------------------*\
2 * Opensg *
3 * *
4 * *
5 * Copyright 2007 by OpenSG Forum *
6 * *
7 * contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de *
8 * *
9 \*---------------------------------------------------------------------------*/
10 /*---------------------------------------------------------------------------*\
11 * License *
12 * *
13 * This library is free software; you can redistribute it and/or modify it *
14 * under the terms of the GNU Library General Public License as published *
15 * by the Free Software Foundation, version 2. *
16 * *
17 * This library is distributed in the hope that it will be useful, but *
18 * WITHOUT ANY WARRANTY; without even the implied warranty of *
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
20 * Library General Public License for more details. *
21 * *
22 * You should have received a copy of the GNU Library General Public *
23 * License along with this library; if not, write to the Free Software *
24 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
25 * *
26 \*---------------------------------------------------------------------------*/
27 /*---------------------------------------------------------------------------*\
28 * Changes *
29 * *
30 * *
31 * *
32 * *
33 * *
34 * *
35 \*---------------------------------------------------------------------------*/
37 #if __GNUC__ >= 4 || __GNUC_MINOR__ >=3
38 #pragma GCC diagnostic ignored "-Wunused-variable"
39 #endif
41 #include "OSGFieldContainer.h"
42 #include "OSGNode.h"
43 #include "OSGNodeCore.h"
44 #include "OSGGroup.h"
45 #include "OSGDirectionalLight.h"
47 #include <iostream>
48 #include <string>
49 #include <vector>
51 #define CHECK_EQUAL(A, B) OSG_ASSERT((A) == (B))
53 void testLight(void)
55 // Test lights, as their beacons can introduce loops in the pointer
56 // structures (not really a problem, because the beacons ptrs are weak)
58 // np0[gp0] <------------+
59 // +----------+ |
60 // np1[gp1] np2[gp2] | beacon
61 // | |
62 // np3[lp0]--------------+
64 // create objects
66 OSG::NodeRecPtr nrcp0(OSG::Node ::create());
67 OSG::NodeRecPtr nrcp1(OSG::Node ::create());
68 OSG::NodeRecPtr nrcp2(OSG::Node ::create());
69 OSG::NodeRecPtr nrcp3(OSG::Node ::create());
70 OSG::GroupRecPtr grcp0(OSG::Group::create());
71 OSG::GroupRecPtr grcp1(OSG::Group::create());
72 OSG::GroupRecPtr grcp2(OSG::Group::create());
73 OSG::LightRecPtr lrcp0(OSG::DirectionalLight::create());
75 OSG::Node *np0 = nrcp0;
76 OSG::Node *np1 = nrcp1;
77 OSG::Node *np2 = nrcp2;
78 OSG::Node *np3 = nrcp3;
79 OSG::Group *gp0 = grcp0;
80 OSG::Group *gp1 = grcp1;
81 OSG::Group *gp2 = grcp2;
82 OSG::Light *lp0 = lrcp0;
84 OSG::UInt32 ni0 = nrcp0->getId();
85 OSG::UInt32 ni1 = nrcp1->getId();
86 OSG::UInt32 ni2 = nrcp2->getId();
87 OSG::UInt32 ni3 = nrcp3->getId();
88 OSG::UInt32 gi0 = grcp0->getId();
89 OSG::UInt32 gi1 = grcp1->getId();
90 OSG::UInt32 gi2 = grcp2->getId();
91 OSG::UInt32 li0 = lrcp0->getId();
93 nrcp0->addChild(nrcp1);
94 nrcp0->addChild(nrcp2);
95 nrcp1->addChild(nrcp3);
97 nrcp0->setCore(grcp0);
98 nrcp1->setCore(grcp1);
99 nrcp2->setCore(grcp2);
100 nrcp3->setCore(lrcp0);
102 lrcp0->setBeacon(nrcp0);
104 CHECK_EQUAL(1, nrcp0->getRefCount() );
105 CHECK_EQUAL(1, nrcp0->getWeakRefCount());
107 CHECK_EQUAL(2, nrcp1->getRefCount() );
108 CHECK_EQUAL(0, nrcp1->getWeakRefCount());
110 CHECK_EQUAL(2, nrcp2->getRefCount() );
111 CHECK_EQUAL(0, nrcp2->getWeakRefCount());
113 CHECK_EQUAL(2, grcp0->getRefCount() );
114 CHECK_EQUAL(0, grcp0->getWeakRefCount());
116 CHECK_EQUAL(2, grcp1->getRefCount() );
117 CHECK_EQUAL(0, grcp1->getWeakRefCount());
119 CHECK_EQUAL(2, grcp2->getRefCount() );
120 CHECK_EQUAL(0, grcp2->getWeakRefCount());
122 CHECK_EQUAL(2, lrcp0->getRefCount() );
123 CHECK_EQUAL(0, lrcp0->getWeakRefCount());
125 // remove all ref pointers (i.e. all external refs)
127 nrcp1 = NULL;
128 nrcp2 = NULL;
129 nrcp3 = NULL;
130 grcp0 = NULL;
131 grcp1 = NULL;
132 grcp2 = NULL;
133 lrcp0 = NULL;
135 OSG::commitChanges();
137 // only nrcp0 is still here, check everything is still alive and has
138 // the expected ref counts
140 CHECK_EQUAL(np0, OSG::FieldContainerFactory::the()->getContainer(ni0));
141 CHECK_EQUAL(np1, OSG::FieldContainerFactory::the()->getContainer(ni1));
142 CHECK_EQUAL(np2, OSG::FieldContainerFactory::the()->getContainer(ni2));
143 CHECK_EQUAL(np3, OSG::FieldContainerFactory::the()->getContainer(ni3));
144 CHECK_EQUAL(gp0, OSG::FieldContainerFactory::the()->getContainer(gi0));
145 CHECK_EQUAL(gp1, OSG::FieldContainerFactory::the()->getContainer(gi1));
146 CHECK_EQUAL(gp2, OSG::FieldContainerFactory::the()->getContainer(gi2));
147 CHECK_EQUAL(lp0, OSG::FieldContainerFactory::the()->getContainer(li0));
149 CHECK_EQUAL(1, np0->getRefCount() );
150 CHECK_EQUAL(2, np0->getWeakRefCount()); // light chunk keeps additional weak ref
152 CHECK_EQUAL(1, np1->getRefCount() );
153 CHECK_EQUAL(0, np1->getWeakRefCount());
155 CHECK_EQUAL(1, np2->getRefCount() );
156 CHECK_EQUAL(0, np2->getWeakRefCount());
158 CHECK_EQUAL(1, np3->getRefCount() );
159 CHECK_EQUAL(0, np3->getWeakRefCount());
161 // remove nrcp0 as well, everything should clean up
163 nrcp0 = NULL;
165 OSG::commitChanges();
167 CHECK_EQUAL(static_cast<OSG::FieldContainer *>(NULL), OSG::FieldContainerFactory::the()->getContainer(ni0));
168 CHECK_EQUAL(static_cast<OSG::FieldContainer *>(NULL), OSG::FieldContainerFactory::the()->getContainer(ni1));
169 CHECK_EQUAL(static_cast<OSG::FieldContainer *>(NULL), OSG::FieldContainerFactory::the()->getContainer(ni2));
170 CHECK_EQUAL(static_cast<OSG::FieldContainer *>(NULL), OSG::FieldContainerFactory::the()->getContainer(ni3));
171 CHECK_EQUAL(static_cast<OSG::FieldContainer *>(NULL), OSG::FieldContainerFactory::the()->getContainer(gi0));
172 CHECK_EQUAL(static_cast<OSG::FieldContainer *>(NULL), OSG::FieldContainerFactory::the()->getContainer(gi1));
173 CHECK_EQUAL(static_cast<OSG::FieldContainer *>(NULL), OSG::FieldContainerFactory::the()->getContainer(gi2));
174 CHECK_EQUAL(static_cast<OSG::FieldContainer *>(NULL), OSG::FieldContainerFactory::the()->getContainer(li0));
177 int main (int argc, char **argv)
179 OSG::osgInit(argc, argv);
181 testLight();
183 return 0;