KiCad PCB EDA Suite
x3d_base.cpp
Go to the documentation of this file.
1 /*
2  * This program source code file is part of KiCad, a free EDA CAD application.
3  *
4  * Copyright (C) 2016 Cirilo Bernardo <cirilo.bernardo@gmail.com>
5  * Copyright (C) 2021 KiCad Developers, see AUTHORS.txt for contributors.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, you may find one here:
19  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20  * or you may search the http://www.gnu.org website for the version 2 license,
21  * or you may write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23  */
24 
25 
26 #include <iostream>
27 #include <sstream>
28 #include <utility>
29 #include <algorithm>
30 #include <wx/log.h>
31 #include "x3d_base.h"
32 #include "wrltypes.h"
33 
34 
35 bool X3D_DICT::AddName( const wxString& aName, X3DNODE* aNode )
36 {
37  if( aName.empty() )
38  return false;
39 
40  std::map< wxString, X3DNODE* >::iterator ir = reg.find( aName );
41 
42  if( ir != reg.end() )
43  reg.erase( ir );
44 
45  reg.insert( std::pair< wxString, X3DNODE* >( aName, aNode ) );
46 
47  return true;
48 }
49 
50 
51 bool X3D_DICT::DelName( const wxString& aName, X3DNODE* aNode )
52 {
53  if( aName.empty() )
54  return false;
55 
56  std::map< wxString, X3DNODE* >::iterator ir = reg.find( aName );
57 
58  if( ir != reg.end() && ir->second == aNode )
59  {
60  reg.erase( ir );
61  return true;
62  }
63 
64  return false;
65 }
66 
67 
68 X3DNODE* X3D_DICT::FindName( const wxString& aName )
69 {
70  if( aName.empty() )
71  return nullptr;
72 
73  std::map< wxString, X3DNODE* >::iterator ir = reg.find( aName );
74 
75  if( ir != reg.end() )
76  return ir->second;
77 
78  return nullptr;
79 }
80 
81 
83 {
85  m_Parent = nullptr;
86  m_sgNode = nullptr;
87  m_Dict = nullptr;
88 
89  return;
90 }
91 
92 
94 {
95  if( !m_Name.empty() && nullptr != m_Dict )
96  m_Dict->DelName( m_Name, this );
97 
98  return;
99 }
100 
101 
102 void X3DNODE::unlinkChildNode( const X3DNODE* aNode )
103 {
104  std::list< X3DNODE* >::iterator sL = m_Children.begin();
105  std::list< X3DNODE* >::iterator eL = m_Children.end();
106 
107  while( sL != eL )
108  {
109  if( *sL == aNode )
110  {
111  m_Children.erase( sL );
112  return;
113  }
114 
115  ++sL;
116  }
117 
118  return;
119 }
120 
121 
122 void X3DNODE::unlinkRefNode( const X3DNODE* aNode )
123 {
124  std::list< X3DNODE* >::iterator sL = m_Refs.begin();
125  std::list< X3DNODE* >::iterator eL = m_Refs.end();
126 
127  while( sL != eL )
128  {
129  if( *sL == aNode )
130  {
131  m_Refs.erase( sL );
132  return;
133  }
134 
135  ++sL;
136  }
137 
138  return;
139 }
140 
141 
143 {
144  // the parent node must never be added as a backpointer
145  if( aNode == m_Parent )
146  return;
147 
148  std::list< X3DNODE* >::iterator sR = m_BackPointers.begin();
149  std::list< X3DNODE* >::iterator eR = m_BackPointers.end();
150 
151  while( sR != eR )
152  {
153  if( *sR == aNode )
154  return;
155 
156  ++sR;
157  }
158 
159  m_BackPointers.push_back( aNode );
160 
161  return;
162 }
163 
164 
166 {
167  std::list< X3DNODE* >::iterator np =
168  std::find( m_BackPointers.begin(), m_BackPointers.end(), aNode );
169 
170  if( np != m_BackPointers.end() )
171  {
172  m_BackPointers.erase( np );
173  return;
174  }
175 
176  wxLogTrace( traceVrmlPlugin, wxT( "%s:%s:%d\n"
177  " * [BUG] delNodeRef() did not find its target." ),
178  __FILE__, __FUNCTION__, __LINE__ );
179 
180  return;
181 }
182 
183 
185 {
186  return m_Type;
187 }
188 
189 
191 {
192  return m_Parent;
193 }
194 
195 
196 wxString X3DNODE::GetName( void ) const
197 {
198  return m_Name;
199 }
200 
201 
202 std::string X3DNODE::GetError( void )
203 {
204  return m_error;
205 }
virtual ~X3DNODE()
Definition: x3d_base.cpp:93
std::list< X3DNODE * > m_BackPointers
Definition: x3d_base.h:163
declares base class of X3D tree
std::string GetError(void)
Definition: x3d_base.cpp:202
void delNodeRef(X3DNODE *aNode)
Remove a pointer to a node which references, but does not own, this node.
Definition: x3d_base.cpp:165
X3DNODE * FindName(const wxString &aName)
Definition: x3d_base.cpp:68
X3DNODES m_Type
Definition: x3d_base.h:160
X3DNODE * GetParent(void) const
Return a pointer to the parent node of this object or NULL if the object has no parent (ie.
Definition: x3d_base.cpp:190
bool DelName(const wxString &aName, X3DNODE *aNode)
Definition: x3d_base.cpp:51
wxString m_Name
Definition: x3d_base.h:168
std::string m_error
Definition: x3d_base.h:166
X3DNODE()
Definition: x3d_base.cpp:82
void addNodeRef(X3DNODE *aNode)
Add a pointer to a node which references, but does not own, this node.
Definition: x3d_base.cpp:142
std::list< X3DNODE * > m_Children
Definition: x3d_base.h:164
The base class of all X3D nodes.
Definition: x3d_base.h:74
virtual void unlinkRefNode(const X3DNODE *aNode)
Remove pointers to a referenced node; it is invoked by the referenced node upon destruction to ensure...
Definition: x3d_base.cpp:122
X3DNODES GetNodeType(void) const
Return the type of this node instance.
Definition: x3d_base.cpp:184
std::map< wxString, X3DNODE * > reg
Definition: x3d_base.h:56
virtual void unlinkChildNode(const X3DNODE *aNode)
Remove references to an owned child; it is invoked by the child upon destruction to ensure that the p...
Definition: x3d_base.cpp:102
const wxChar *const traceVrmlPlugin
Flag to enable VRML plugin trace output.
Definition: vrml.cpp:63
bool AddName(const wxString &aName, X3DNODE *aNode)
Definition: x3d_base.cpp:35
SGNODE * m_sgNode
Definition: x3d_base.h:169
X3DNODE * m_Parent
Definition: x3d_base.h:159
declares some compound types used for VRML
wxString GetName(void) const
Return the name of this object.
Definition: x3d_base.cpp:196
X3DNODES
Definition: x3d_base.h:59
X3D_DICT * m_Dict
Definition: x3d_base.h:161
std::list< X3DNODE * > m_Refs
Definition: x3d_base.h:165