update dev300-m58
[ooovba.git] / autodoc / inc / ary / symtreenode.hxx
blobcb4288dd3e1f43eb0de780759f0613bc8648d992
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: symtreenode.hxx,v $
10 * $Revision: 1.3 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #ifndef ARY_SYMTREE_NODE_HXX
32 #define ARY_SYMTREE_NODE_HXX
35 // USED SERVICES
36 // BASE CLASSES
37 // OTHER
41 namespace ary
43 namespace symtree
48 /** Represents a node in a tree of symbols like a namespace tree or a
49 directory tree.
51 @tpl NODE_TRAITS
52 Needs to define the types:
53 entity_base_type: The type of the entities in that storage,
54 e.g. ->ary::cpp::CodeEntity.
55 id_type: The type of the ids of those entities,
56 e.g. ->ary::cpp::Ce_id.
58 Needs to define the functions:
59 1. static entity_base_type &
60 EntityOf_(
61 id_type i_id );
62 2. static symtree::Node<LeNode_Traits> *
63 NodeOf_(
64 const entity_base_type &
65 i_entity );
66 3. static const String &
67 LocalNameOf_(
68 const entity_base_type &
69 i_entity );
70 4. static entity_base_type *
71 ParentOf_(
72 const entity_base_type &
73 i_entity );
74 5. template <class KEY>
75 static id_t Search_(
76 const entity_base_type &
77 i_entity,
78 const KEY & i_localKey );
80 template <class NODE_TRAITS>
81 class Node
83 public:
84 typedef Node<NODE_TRAITS> node_self;
85 typedef typename NODE_TRAITS::entity_base_type entity_t;
86 typedef typename NODE_TRAITS::id_type id_t;
89 // LIFECYCLE
90 /// @attention Always needs to be followed by ->Assign_Entity()!
91 Node();
92 explicit Node(
93 entity_t & i_entity );
94 void Assign_Entity(
95 entity_t & i_entity );
96 ~Node();
97 // INQUIRY
98 id_t Id();
99 const String Name() const;
100 int Depth() const;
101 const entity_t & Entity() const;
102 const node_self * Parent() const;
104 /** Gets a child with a specific name and of a specific type.
106 There may be more childs with the same name.
108 @return id_t(0), if no matching child is found.
110 template <class KEY>
111 typename NODE_TRAITS::id_type
112 Search(
113 const KEY & i_localKey ) const
115 // Inline here to workaround SUNW8 compiler bug, works in SUNW12.
116 return NODE_TRAITS::Search_(Entity(), i_localKey);
120 /** Gets a child with a specific qualified name below this node.
122 The child may not exists.
124 template <class KEY>
125 void SearchBelow(
126 id_t & o_return, // Workaround SUNW8 compiler bug
127 StringVector::const_iterator
128 i_qualifiedSearchedName_begin,
129 StringVector::const_iterator
130 i_qualifiedSearchedName_end,
131 const KEY & i_localKey ) const;
133 /** Gets a child with a specific qualified name, either below this node
134 or below any of the parent nodes.
136 The child may not exists.
138 template <class KEY>
139 void SearchUp(
140 id_t & o_return, // Workaround SUNW8 compiler bug
141 StringVector::const_iterator
142 i_qualifiedSearchedName_begin,
143 StringVector::const_iterator
144 i_qualifiedSearchedName_end,
145 const KEY & i_localKey ) const;
146 // ACCESS
147 entity_t & Entity();
148 node_self * Parent();
150 private:
151 // Forbid copying:
152 Node(const node_self&);
153 node_self& operator=(const node_self&);
155 // Locals
156 void InitDepth();
157 node_self * Get_Parent() const;
158 node_self * NodeOf(
159 id_t i_id ) const;
161 // DATA
162 entity_t * pEntity;
163 int nDepth;
169 // IMPLEMENTATION
171 template <class NODE_TRAITS>
172 inline const typename Node<NODE_TRAITS>::entity_t &
173 Node<NODE_TRAITS>::Entity() const
175 csv_assert(pEntity != 0);
176 return *pEntity;
179 template <class NODE_TRAITS>
180 inline Node<NODE_TRAITS> *
181 Node<NODE_TRAITS>::NodeOf(id_t i_id) const
183 if (i_id.IsValid())
184 return NODE_TRAITS::NodeOf_(NODE_TRAITS::EntityOf_(i_id));
185 return 0;
188 template <class NODE_TRAITS>
189 inline Node<NODE_TRAITS> *
190 Node<NODE_TRAITS>::Get_Parent() const
192 entity_t *
193 parent = NODE_TRAITS::ParentOf_(Entity());
194 if (parent != 0)
195 return NODE_TRAITS::NodeOf_(*parent);
196 return 0;
199 template <class NODE_TRAITS>
200 Node<NODE_TRAITS>::Node()
201 : pEntity(0),
202 nDepth(0)
206 template <class NODE_TRAITS>
207 Node<NODE_TRAITS>::Node(entity_t & i_entity)
208 : pEntity(&i_entity),
209 nDepth(0)
211 InitDepth();
214 template <class NODE_TRAITS>
215 void
216 Node<NODE_TRAITS>::Assign_Entity(entity_t & i_entity)
218 pEntity = &i_entity;
219 InitDepth();
222 template <class NODE_TRAITS>
223 Node<NODE_TRAITS>::~Node()
227 template <class NODE_TRAITS>
228 inline typename Node<NODE_TRAITS>::id_t
229 Node<NODE_TRAITS>::Id()
231 return NODE_TRAITS::IdOf(Entity());
234 template <class NODE_TRAITS>
235 inline const String
236 Node<NODE_TRAITS>::Name() const
238 return NODE_TRAITS::LocalNameOf_(Entity());
241 template <class NODE_TRAITS>
242 inline int
243 Node<NODE_TRAITS>::Depth() const
245 return nDepth;
248 template <class NODE_TRAITS>
249 inline const Node<NODE_TRAITS> *
250 Node<NODE_TRAITS>::Parent() const
252 return Get_Parent();
255 template <class NODE_TRAITS>
256 template <class KEY>
257 void
258 Node<NODE_TRAITS>::SearchBelow(
259 id_t & o_return, // Workaround SUNW8 compiler bug
260 StringVector::const_iterator i_qualifiedSearchedName_begin,
261 StringVector::const_iterator i_qualifiedSearchedName_end,
262 const KEY & i_localKey ) const
264 if (i_qualifiedSearchedName_begin != i_qualifiedSearchedName_end)
266 id_t
267 next = Search(*i_qualifiedSearchedName_begin);
268 if (next.IsValid())
270 const node_self *
271 subnode = NodeOf(next);
272 if (subnode != 0)
274 subnode->SearchBelow( o_return,
275 i_qualifiedSearchedName_begin+1,
276 i_qualifiedSearchedName_end ,
277 i_localKey );
278 return;
281 o_return = id_t(0);
282 return;
285 o_return = Search(i_localKey);
288 template <class NODE_TRAITS>
289 template <class KEY>
290 void
291 Node<NODE_TRAITS>::SearchUp(
292 id_t & o_return, // Workaround SUNW8 compiler bug
293 StringVector::const_iterator i_qualifiedSearchedName_begin,
294 StringVector::const_iterator i_qualifiedSearchedName_end,
295 const KEY & i_localKey ) const
297 SearchBelow( o_return,
298 i_qualifiedSearchedName_begin,
299 i_qualifiedSearchedName_end,
300 i_localKey );
301 if (o_return.IsValid())
302 return;
304 node_self *
305 parent = Get_Parent();
306 if (parent != 0)
308 parent->SearchUp( o_return,
309 i_qualifiedSearchedName_begin,
310 i_qualifiedSearchedName_end,
311 i_localKey );
315 template <class NODE_TRAITS>
316 typename Node<NODE_TRAITS>::entity_t &
317 Node<NODE_TRAITS>::Entity()
319 csv_assert(pEntity != 0);
320 return *pEntity;
323 template <class NODE_TRAITS>
324 inline Node<NODE_TRAITS> *
325 Node<NODE_TRAITS>::Parent()
327 return Get_Parent();
330 template <class NODE_TRAITS>
331 void
332 Node<NODE_TRAITS>::InitDepth()
334 Node<NODE_TRAITS> *
335 pp = Get_Parent();
336 if (pp != 0)
337 nDepth = pp->Depth() + 1;
338 else
339 nDepth = 0;
345 } // namespace symtree
346 } // namespace ary
347 #endif