Developer Documentation
BaseObjectDataT_impl.hh
1 /*===========================================================================*\
2 * *
3 * OpenFlipper *
4  * Copyright (c) 2001-2015, RWTH-Aachen University *
5  * Department of Computer Graphics and Multimedia *
6  * All rights reserved. *
7  * www.openflipper.org *
8  * *
9  *---------------------------------------------------------------------------*
10  * This file is part of OpenFlipper. *
11  *---------------------------------------------------------------------------*
12  * *
13  * Redistribution and use in source and binary forms, with or without *
14  * modification, are permitted provided that the following conditions *
15  * are met: *
16  * *
17  * 1. Redistributions of source code must retain the above copyright notice, *
18  * this list of conditions and the following disclaimer. *
19  * *
20  * 2. Redistributions in binary form must reproduce the above copyright *
21  * notice, this list of conditions and the following disclaimer in the *
22  * documentation and/or other materials provided with the distribution. *
23  * *
24  * 3. Neither the name of the copyright holder nor the names of its *
25  * contributors may be used to endorse or promote products derived from *
26  * this software without specific prior written permission. *
27  * *
28  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
29  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
30  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
31  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
32  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, *
33  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, *
34  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR *
35  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
36  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
37  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
38  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
39 * *
40 \*===========================================================================*/
41 
42 
43 //=============================================================================
44 //
45 // MyTypes
46 //
47 //=============================================================================
48 
49 #define BASEOBJECTDATA_C
50 
51 
52 //== INCLUDES =================================================================
53 
54 #include "Types.hh"
55 #include <stack>
56 #include <ACG/Scenegraph/BaseNode.hh>
57 
58 //== TYPEDEFS =================================================================
59 
60 //== CLASS DEFINITION =========================================================
61 
62 // ===============================================================================
63 // Additional Nodes
64 // ===============================================================================
65 
66 template< typename NodeT >
67 bool BaseObjectData::addAdditionalNode(NodeT* _node , QString _pluginName, QString _nodeName , int _id )
68 {
69  if ( hasAdditionalNode(_pluginName,_nodeName,_id) ) {
70  std::cerr << "Trying to inserted additional node twice in " << _pluginName.toStdString()
71  << " with Name " << _nodeName.toStdString() << " and id " << _id << std::endl;
72  return false;
73  }
74 
75  QString name = _pluginName+"#"+_nodeName+"#"+QString::number(_id);
76 
77  std::pair <BaseNode*,QString> newNode(dynamic_cast<BaseNode*>(_node) , name);
78  additionalNodes_.push_back(newNode);
79 
80  return true;
81 }
82 
83 template< typename NodeT >
84 bool BaseObjectData::getAdditionalNode(NodeT*& _node , QString _pluginName, QString _nodeName , int _id )
85 {
86  QString searchname = _pluginName + "#" + _nodeName + "#" + QString::number(_id);
87 
88  for ( uint i =0 ; i < additionalNodes_.size() ; ++i ) {
89  if ( additionalNodes_[i].second == searchname ) {
90  _node = dynamic_cast<NodeT*>(additionalNodes_[i].first);
91  return ( _node != NULL);
92  }
93  }
94 
95  return false;
96 }
97 
98 template< typename NodeT >
99 bool BaseObjectData::removeAdditionalNode(NodeT*& _node, QString _pluginName, QString _nodeName , int _id )
100 {
101  QString searchname = _pluginName + "#" + _nodeName + "#" + QString::number(_id);
102 
103  for ( uint i =0 ; i < additionalNodes_.size() ; ++i ) {
104  if ( additionalNodes_[i].second == searchname ) {
105  _node = dynamic_cast<NodeT*>(additionalNodes_[i].first);
106  if (_node != NULL) //valid node delete subtree
107  {
108  // Delete parent node from additional nodes list
109  additionalNodes_.erase (additionalNodes_.begin()+i);
110 
111  // Remove children from list, too, since the objects will be deleted
112  // on delete_subtree() but (invalid) pointers reside in the additionalNodes_ list
113  // if not removed manually. We need to do this recursively:
114  std::stack<BaseNode *> children;
115  children.push(_node);
116  while(!children.empty())
117  {
118  BaseNode *current = children.top();
119  children.pop();
120 
121  for(BaseNode::ChildIter it = current->childrenBegin(); it != current->childrenEnd(); ++it)
122  children.push(*it);
123 
124  for(int j = additionalNodes_.size()-1; j >= 0; --j)
125  if(additionalNodes_[j].first == current)
126  additionalNodes_.erase(additionalNodes_.begin()+j);
127  }
128  // Delete nodes recursively
129  _node->delete_subtree();
130  }
131  return true;
132  }
133  }
134 
135  return false;
136 }
137 
138 //=============================================================================
ChildIter childrenEnd()
Returns: end-iterator of children.
Definition: BaseNode.hh:298
bool getAdditionalNode(NodeT *&_node, QString _pluginName, QString _nodeName, int _id=0)
get an addition node from the object
ChildIter childrenBegin()
Returns: begin-iterator of children.
Definition: BaseNode.hh:294
bool addAdditionalNode(NodeT *_node, QString _pluginName, QString _nodeName, int _id=0)
add an additional node to the object
QString name() const
return the name of the object. The name defaults to NONAME if unset.
Definition: BaseObject.cc:730
bool removeAdditionalNode(NodeT *&_node, QString _pluginName, QString _nodeName, int _id=0)
remove an additional node from the object
std::vector< BaseNode * >::iterator ChildIter
allows to iterate over children
Definition: BaseNode.hh:286
bool hasAdditionalNode(QString _pluginName, QString _nodeName, int _id=0)
check if an object has the additional node
std::vector< std::pair< BaseNode *, QString > > additionalNodes_