Developer Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
BaseObjectDataT.cc
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 * $Revision$ *
45 * $LastChangedBy$ *
46 * $Date$ *
47 * *
48 \*===========================================================================*/
49 
50 
51 
52 //=============================================================================
53 //
54 // MyTypes
55 //
56 //=============================================================================
57 
58 #define BASEOBJECTDATA_C
59 
60 
61 //== INCLUDES =================================================================
62 
63 #include "Types.hh"
64 
65 //== TYPEDEFS =================================================================
66 
67 //== CLASS DEFINITION =========================================================
68 
69 // ===============================================================================
70 // Additional Nodes
71 // ===============================================================================
72 
73 template< typename NodeT >
74 bool BaseObjectData::addAdditionalNode(NodeT* _node , QString _pluginName, QString _nodeName , int _id )
75 {
76  if ( hasAdditionalNode(_pluginName,_nodeName,_id) ) {
77  std::cerr << "Trying to inserted additional node twice in " << _pluginName.toStdString()
78  << " with Name " << _nodeName.toStdString() << " and id " << _id << std::endl;
79  return false;
80  }
81 
82  QString name = _pluginName+"#"+_nodeName+"#"+QString::number(_id);
83 
84  std::pair <BaseNode*,QString> newNode(dynamic_cast<BaseNode*>(_node) , name);
85  additionalNodes_.push_back(newNode);
86 
87  return true;
88 }
89 
90 template< typename NodeT >
91 bool BaseObjectData::getAdditionalNode(NodeT*& _node , QString _pluginName, QString _nodeName , int _id )
92 {
93  QString searchname = _pluginName + "#" + _nodeName + "#" + QString::number(_id);
94 
95  for ( uint i =0 ; i < additionalNodes_.size() ; ++i ) {
96  if ( additionalNodes_[i].second == searchname ) {
97  _node = dynamic_cast<NodeT*>(additionalNodes_[i].first);
98  return ( _node != NULL);
99  }
100  }
101 
102  return false;
103 }
104 
105 template< typename NodeT >
106 bool BaseObjectData::removeAdditionalNode(NodeT*& _node, QString _pluginName, QString _nodeName , int _id )
107 {
108  QString searchname = _pluginName + "#" + _nodeName + "#" + QString::number(_id);
109 
110  for ( uint i =0 ; i < additionalNodes_.size() ; ++i ) {
111  if ( additionalNodes_[i].second == searchname ) {
112  _node = dynamic_cast<NodeT*>(additionalNodes_[i].first);
113  if (_node != NULL) //valid node delete subtree
114  {
115  // Delete parent node from additional nodes list
116  additionalNodes_.erase (additionalNodes_.begin()+i);
117 
118  // Remove children from list, too, since the objects will be deleted
119  // on delete_subtree() but (invalid) pointers reside in the additionalNodes_ list
120  // if not removed manually. We need to do this recursively:
121  std::stack<BaseNode *> children;
122  children.push(_node);
123  while(!children.empty())
124  {
125  BaseNode *current = children.top();
126  children.pop();
127 
128  for(BaseNode::ChildIter it = current->childrenBegin(); it != current->childrenEnd(); ++it)
129  children.push(*it);
130 
131  for(int j = additionalNodes_.size()-1; j >= 0; --j)
132  if(additionalNodes_[j].first == current)
133  additionalNodes_.erase(additionalNodes_.begin()+j);
134  }
135  // Delete nodes recursively
136  _node->delete_subtree();
137  }
138  return true;
139  }
140  }
141 
142  return false;
143 }
144 
145 //=============================================================================
bool removeAdditionalNode(NodeT *&_node, QString _pluginName, QString _nodeName, int _id=0)
remove an additional node from the object
QString name() const
return the name of the object. The name defaults to NONAME if unset.
Definition: BaseObject.cc:741
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:270
bool hasAdditionalNode(QString _pluginName, QString _nodeName, int _id=0)
check if an object has the additional node
std::list< BaseNode * >::iterator ChildIter
allows to iterate over children
Definition: BaseNode.hh:262
std::vector< std::pair< BaseNode *, QString > > additionalNodes_
bool addAdditionalNode(NodeT *_node, QString _pluginName, QString _nodeName, int _id=0)
add an additional node to the object
ChildIter childrenEnd()
Returns: end-iterator of children.
Definition: BaseNode.hh:274