xref: /trunk/main/sw/source/core/except/dbgloop.cxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_sw.hxx"
30 
31 #ifdef DBG_UTIL
32 
33 #include <tools/stream.hxx>
34 #include "dbgloop.hxx"
35 #include "errhdl.hxx"
36 
37 DbgLoopStack DbgLoop::aDbgLoopStack;
38 
39 /*************************************************************************
40  *						class DbgLoopStack
41  *************************************************************************/
42 
43 DbgLoopStack::DbgLoopStack()
44 {
45 	Reset();
46 }
47 
48 void DbgLoopStack::Reset()
49 {
50 	nPtr = 0;
51 	pDbg = 0;
52 	for( sal_uInt16 i = 0; i < DBG_MAX_STACK; ++i )
53 		aCount[i] = 0;
54 }
55 
56 /*************************************************************************
57  *						 DbgLoopStack::Push()
58  *************************************************************************/
59 
60 void DbgLoopStack::Push( const void *pThis )
61 {
62 	// Wir muessen irgendwie mitbekommen, wann die erste Stackposition
63 	// resettet werden soll, z.B. wenn wir einen Nullpointer uebergeben
64 	if( !nPtr && ( pDbg != pThis || !pThis ) )
65 	{
66 		aCount[1] = 0;
67 		pDbg = pThis;
68 	}
69 
70 	++nPtr;
71 	if( DBG_MAX_STACK > nPtr )
72 	{
73 		// Wenn eine loop entdeckt wird, wird der counter wieder zurueckgesetzt.
74 		ASSERT( DBG_MAX_LOOP > aCount[nPtr], "DbgLoopStack::Push: loop detected" );
75 		if( DBG_MAX_LOOP > aCount[nPtr] )
76 			++(aCount[nPtr]);
77 		else
78 			aCount[nPtr] = 0;
79 	}
80 }
81 
82 /*************************************************************************
83  *						 DbgLoopStack::Pop()
84  *************************************************************************/
85 
86 void DbgLoopStack::Pop()
87 {
88 	if( DBG_MAX_STACK > nPtr )
89 	{
90 		ASSERT( nPtr, "DbgLoopStack::Pop: can't pop the stack" );
91 
92 		ASSERT( aCount[nPtr], "DbgLoopStack::Pop: can't dec the count" );
93 		if( DBG_MAX_STACK > nPtr + 1 )
94 			aCount[nPtr + 1] = 0;
95 	}
96 	--nPtr;
97 }
98 
99 /*************************************************************************
100  *						 DbgLoopStack::Print()
101  *************************************************************************/
102 
103 void DbgLoopStack::Print( SvStream &rOS ) const
104 {
105 	rOS << "POS: " << nPtr << '\n';
106     sal_uInt16 i;
107 	for( i = 0; i < DBG_MAX_STACK; ++i )
108 		rOS << i << " ";
109 	rOS << '\n';
110 	for( i = 0; i < DBG_MAX_STACK; ++i )
111 		rOS << aCount[i] << " ";
112 	rOS << '\n';
113 }
114 
115 #ifdef STAND_ALONE
116 // compile with: cl /AL /DSTAND_ALONE dbgloop.cxx
117 
118 /*************************************************************************
119  *							main()
120  *************************************************************************/
121 
122 #include <stdlib.h>
123 
124 void AssertFail( const char *pErr, const char *pFile, sal_uInt16 nLine )
125 {
126 	cout << pErr << '\n';
127 	PrintLoopStack( cout );
128 	exit(0);
129 }
130 
131 class Test
132 {
133 public:
134 		void Run() const;
135 };
136 
137 void Test::Run() const
138 {
139 	cout << "---" << '\n';
140 	for( sal_uInt16 i = 0; i < 10; ++i )
141 	{
142 		cout << "i" << i;
143 		DBG_LOOP;
144 		PrintLoopStack( cout );
145 		for( sal_uInt16 j = 0; j < 10; ++j )
146 		{
147 			cout << " j" << j;
148 			DBG_LOOP;
149 			PrintLoopStack( cout );
150 		}
151 		cout << '\n';
152 	}
153 	PrintLoopStack( cout );
154 }
155 
156 int main()
157 {
158 	// unterschiedliche Instanzen waehlen wg. pDbg != pThis
159 	Test aTest1;
160 	aTest1.Run();
161 	Test aTest2;
162 	aTest2.Run();
163 	return 0;
164 }
165 #endif
166 
167 #endif // DBG_UTIL
168 
169