fixed: auto_ptr -> unique_ptr
[opensg.git] / Source / System / FieldContainer / Misc / OSGSceneGraphUtils.cpp
blobfffda5dcbe52ffc3e98db04ac8317cd6ede10c6b
1 /*---------------------------------------------------------------------------*\
2 * OpenSG *
3 * *
4 * *
5 * Copyright (C) 2008 by the OpenSG Forum *
6 * *
7 * www.opensg.org *
8 * *
9 * contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de *
10 * *
11 \*---------------------------------------------------------------------------*/
12 /*---------------------------------------------------------------------------*\
13 * License *
14 * *
15 * This library is free software; you can redistribute it and/or modify it *
16 * under the terms of the GNU Library General Public License as published *
17 * by the Free Software Foundation, version 2. *
18 * *
19 * This library is distributed in the hope that it will be useful, but *
20 * WITHOUT ANY WARRANTY; without even the implied warranty of *
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
22 * Library General Public License for more details. *
23 * *
24 * You should have received a copy of the GNU Library General Public *
25 * License along with this library; if not, write to the Free Software *
26 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
27 * *
28 \*---------------------------------------------------------------------------*/
29 /*---------------------------------------------------------------------------*\
30 * Changes *
31 * *
32 * *
33 * *
34 * *
35 * *
36 * *
37 \*---------------------------------------------------------------------------*/
39 #include "OSGSceneGraphUtils.h"
40 #include "OSGFieldContainerSFields.h"
41 #include "OSGFieldContainerMFields.h"
42 #include "OSGAttachment.h"
43 #include "OSGAttachmentMapSFields.h"
44 #include "OSGNameAttachment.h"
46 #include <boost/bind.hpp>
47 #include <boost/format.hpp>
49 #include <ostream>
50 #include <set>
52 OSG_BEGIN_NAMESPACE
54 //---------------------------------------------------------------------------
55 // SceneGraphPrinter
56 //---------------------------------------------------------------------------
58 SceneGraphPrinter::SceneGraphPrinter(Node *root)
59 : _pRoot (root),
60 _pCurrNode (NULL),
61 _pStream (NULL),
62 _indent (0),
63 _printFuncMap()
65 // nothing to do
68 void SceneGraphPrinter::printDownTree(std::ostream &os)
70 _pStream = &os;
71 _indent = 0;
73 traverse(_pRoot,
74 boost::bind(&Self::traverseEnter, this, _1 ),
75 boost::bind(&Self::traverseLeave, this, _1, _2) );
77 os << std::flush;
80 void SceneGraphPrinter::printUpTree(std::ostream &os)
82 _pStream = &os;
83 _indent = 0;
85 Node *node = _pRoot;
87 while(node != NULL)
89 NodeCore *core = node->getCore();
90 _pCurrNode = node;
92 os << "[" << node
93 << "] [" << node->getId()
94 << "] [" << (getName(node) ? getName(node) : "<unnamed>")
95 << "] -- [" << core
96 << "] [" << core->getId()
97 << "] [" << core->getType().getCName()
98 << "]\n";
100 node = node->getParent();
103 os << std::flush;
106 void
107 SceneGraphPrinter::addPrintFunc(const FieldContainerType &fcType,
108 const CorePrintFunction &printFunc)
110 _printFuncMap[fcType.getId()] = printFunc;
113 void
114 SceneGraphPrinter::subPrintFunc(const FieldContainerType &fcType)
116 PrintFuncMapIt pfIt = _printFuncMap.find(fcType.getId());
118 if(pfIt != _printFuncMap.end())
119 _printFuncMap.erase(pfIt);
122 void
123 SceneGraphPrinter::incIndent(void)
125 _indent += 2;
128 void
129 SceneGraphPrinter::decIndent(void)
131 _indent -= 2;
134 std::ostream &
135 SceneGraphPrinter::indentStream(void)
137 for(UInt32 i = 0; i < _indent; ++i)
138 (*_pStream) << " ";
140 return *_pStream;
143 std::ostream &
144 SceneGraphPrinter::getStream(void)
146 return *_pStream;
149 Node *
150 SceneGraphPrinter::getCurrNode(void)
152 return _pCurrNode;
155 Action::ResultE SceneGraphPrinter::traverseEnter(Node *node)
157 if(node == NULL)
158 return Action::Continue;
160 _pCurrNode = node;
162 std::ostream &os = getStream();
163 incIndent();
165 indentStream()
166 << "[" << node
167 << "] [" << node->getId()
168 << "] [" << (getName(node) ? getName(node) : "<unnamed>")
169 << "]";
171 NodeCore *core = node->getCore();
173 if(core == NULL)
175 os << " -- !! MISSING CORE !!\n";
176 return Action::Continue;
179 os << " -- [" << core
180 << "] [" << core->getId()
181 << "] [" << core->getType().getCName()
182 << "]";
184 os << " [" << (getName(core) ? getName(core) : "<unnamed>")
185 << "]";
187 NodeCore::MFParentsType::const_iterator pIt = core->getMFParents()->begin();
188 NodeCore::MFParentsType::const_iterator pEnd = core->getMFParents()->end ();
190 if(core->getMFParents()->size() > 1)
192 os << " # parents [" << core->getMFParents()->size() << "] #";
194 for(; pIt != pEnd; ++pIt)
196 Node *parent = dynamic_cast<Node *>(*pIt);
198 os << " [" << *pIt
199 << "] [" << (getName(parent) ? getName(parent) : "<unnamed>")
200 << "]";
204 os << "\n";
206 // if there is a print function registered for this core type call it
207 PrintFuncMapConstIt pfIt = _printFuncMap.find(core->getType().getId());
209 if(pfIt != _printFuncMap.end())
211 incIndent();
212 (pfIt->second)(this, core);
213 decIndent();
216 return Action::Continue;
219 Action::ResultE SceneGraphPrinter::traverseLeave(
220 Node *node, Action::ResultE res)
222 if(node == NULL)
223 return Action::Continue;
225 decIndent();
227 return Action::Continue;
230 OSG_END_NAMESPACE