1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #ifndef ARY_NAMETREENODE_HXX
25 #define ARY_NAMETREENODE_HXX
26 //  KORR_DEPRECATED_3.0
27 //      Replace by ::ary::symtree::Node.
28 
29 // USED SERVICES
30 #include <cosv/tpl/tpltools.hxx>
31 #include <sci_impl.hxx>
32 // HACK because of SunPro 5.2 compiler bug with templates:
33 #include <ary/idl/i_module.hxx>
34 
35 
36 
37 
38 namespace ary
39 {
40 
41 
42 /** Implementation of a node in a namespace-tree.
43 */
44 template<class ITEM_ID>
45 class NameTreeNode
46 {
47   public:
48     typedef NameTreeNode                    self;
49     typedef ITEM_ID                         item_id;
50     typedef StringVector::const_iterator    name_iterator;
51     typedef std::map<String, item_id>       Map_LocalNames;
52 
53     // LIFECYCLE
54                         NameTreeNode();
55                         NameTreeNode(
56                             const String &      i_sName,
57                             const self &        i_rParent,
58                             ITEM_ID             i_nParentId );
59     virtual             ~NameTreeNode();
60 
61     // OPERATIONS
62     void                Add_Name(
63                             const String &      i_sName,
64                             item_id             i_nId );
65     // INQUIRY
Name() const66     const String &      Name() const            { return Depth() > 0 ? aCompleteNameChain.back() : String::Null_(); }
Parent() const67     item_id             Parent() const          { return nParent; }
Depth() const68     intt                Depth() const           { return aCompleteNameChain.size(); }
69 
70     bool                IsEquivalent(
71                             const NameTreeNode &
72                                                 i_rNode ) const;
NameChain_Begin() const73     name_iterator       NameChain_Begin() const { return aCompleteNameChain.begin(); }
NameChain_End() const74     name_iterator       NameChain_End() const   { return aCompleteNameChain.end(); }
75 
76     item_id             Search_Name(
77                             const String &      i_sName ) const;
78     void                Get_Names(
79                             Dyn_StdConstIterator<ITEM_ID> &
80                                                 o_rResult ) const;
81     const Map_LocalNames &
LocalNames() const82                         LocalNames() const      { return aLocalNames; }
83   private:
84     // Locals
LocalNames()85     Map_LocalNames &    LocalNames()            { return aLocalNames; }
86 
87     // DATA
88     Map_LocalNames      aLocalNames;
89     StringVector        aCompleteNameChain;
90     item_id             nParent;
91 };
92 
93 
94 
95 
96 // IMPLEMENTATION
97 template<class ITEM_ID>
NameTreeNode()98 NameTreeNode<ITEM_ID>::NameTreeNode()
99     :   aLocalNames(),
100         aCompleteNameChain(),
101         nParent(0)
102 {
103 }
104 
105 template<class ITEM_ID>
NameTreeNode(const String & i_sName,const self & i_rParent,ITEM_ID i_nParentId)106 NameTreeNode<ITEM_ID>::NameTreeNode( const String &      i_sName,
107                                      const self &        i_rParent,
108                                      ITEM_ID             i_nParentId )
109     :   aLocalNames(),
110         aCompleteNameChain(),
111         nParent(i_nParentId)
112 {
113     aCompleteNameChain.reserve(i_rParent.Depth()+1);
114     for ( name_iterator it = i_rParent.NameChain_Begin();
115           it != i_rParent.NameChain_End();
116           ++it )
117     {
118         aCompleteNameChain.push_back(*it);
119     }
120     aCompleteNameChain.push_back(i_sName);
121 }
122 
123 template<class ITEM_ID>
~NameTreeNode()124 NameTreeNode<ITEM_ID>::~NameTreeNode()
125 {
126 }
127 
128 
129 template<class ITEM_ID>
130 inline void
Add_Name(const String & i_sName,item_id i_nId)131 NameTreeNode<ITEM_ID>::Add_Name( const String &      i_sName,
132                                  item_id             i_nId )
133 {
134     LocalNames().insert( typename Map_LocalNames::value_type(i_sName, i_nId) );
135 }
136 
137 
138 template<class ITEM_ID>
139 inline bool
IsEquivalent(const NameTreeNode & i_rNode) const140 NameTreeNode<ITEM_ID>::IsEquivalent( const NameTreeNode & i_rNode ) const
141 {
142     return aCompleteNameChain == i_rNode.aCompleteNameChain;
143 }
144 
145 template<class ITEM_ID>
146 inline ITEM_ID
Search_Name(const String & i_sName) const147 NameTreeNode<ITEM_ID>::Search_Name( const String & i_sName ) const
148 {
149     return csv::value_from_map(LocalNames(),i_sName, ITEM_ID(0));
150 }
151 
152 template<class ITEM_ID>
153 inline void
Get_Names(Dyn_StdConstIterator<ITEM_ID> & o_rResult) const154 NameTreeNode<ITEM_ID>::Get_Names( Dyn_StdConstIterator<ITEM_ID> & o_rResult ) const
155 {
156     o_rResult = new SCI_DataInMap<String,item_id>(LocalNames());
157 }
158 
159 
160 // HACK because of SunPro 5.2 compiler bug with templates:
161 //   ary::idl::Module has to be "FIND_NODE::node_type"
162 //   must be solved later somehow.
163 template <class FIND_NODE>
164 typename FIND_NODE::id_type
Search_SubTree(const ary::idl::Module & i_rStart,const FIND_NODE & i_rNodeFinder)165 Search_SubTree( const ary::idl::Module &    i_rStart,
166                 const FIND_NODE &           i_rNodeFinder )
167 {
168     const ary::idl::Module *
169         ret = &i_rStart;
170 
171     for ( StringVector::const_iterator  it = i_rNodeFinder.Begin();
172           it != i_rNodeFinder.End() AND ret != 0;
173           ++it )
174     {
175         ret = i_rNodeFinder(ret->Search_Name(*it));
176     }
177 
178     typename FIND_NODE::id_type nret(0);
179     return ret != 0
180             ?   ret->Search_Name(i_rNodeFinder.Name2Search())
181             :   nret;
182 }
183 
184 template <class  FIND_NODE>
185 typename FIND_NODE::id_type
Search_SubTree_UpTillRoot(const ary::idl::Module & i_rStart,const FIND_NODE & i_rNodeFinder)186 Search_SubTree_UpTillRoot( const ary::idl::Module &    i_rStart,
187                            const FIND_NODE &           i_rNodeFinder )
188 {
189     typename FIND_NODE::id_type
190         ret(0);
191     for ( const ary::idl::Module * start = &i_rStart;
192           start != 0 AND NOT ret.IsValid();
193           start = i_rNodeFinder(start->Owner()) )
194     {
195         ret = Search_SubTree( *start,
196                               i_rNodeFinder );
197     }
198     return ret;
199 }
200 // END Hack for SunPro 5.2 compiler bug.
201 
202 
203 
204 
205 }   // namespace ary
206 #endif
207