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 #include <precomp.h>
23 #include <ary/cpp/c_traits.hxx>
24
25
26 // NOT FULLY DEFINED SERVICES
27 #include <ary/namesort.hxx>
28 #include <ary/cpp/c_class.hxx>
29 #include <ary/cpp/c_enuval.hxx>
30 #include <ary/cpp/c_namesp.hxx>
31 #include <ary/cpp/usedtype.hxx>
32 #include <ary/getncast.hxx>
33 #include "cs_ce.hxx"
34 #include "cs_def.hxx"
35 #include "cs_type.hxx"
36
37
38
39
40 namespace ary
41 {
42 namespace cpp
43 {
44
45
46
47 //******************** Ce_Traits ********************//
48 Ce_Traits::entity_base_type &
EntityOf_(id_type i_id)49 Ce_Traits::EntityOf_(id_type i_id)
50 {
51 csv_assert(i_id.IsValid());
52 return Ce_Storage::Instance_()[i_id];
53 }
54
55 //******************** CeNode_Traits ********************//
56 const symtree::Node<CeNode_Traits> *
NodeOf_(const entity_base_type & i_entity)57 CeNode_Traits::NodeOf_(const entity_base_type & i_entity)
58 {
59 if (is_type<Namespace>(i_entity))
60 return & ary_cast<Namespace>(i_entity).AsNode();
61 else if (is_type<Class>(i_entity))
62 return & ary_cast<Class>(i_entity).AsNode();
63 return 0;
64 }
65
66 symtree::Node<CeNode_Traits> *
NodeOf_(entity_base_type & io_entity)67 CeNode_Traits::NodeOf_(entity_base_type & io_entity)
68 {
69 if (is_type<Namespace>(io_entity))
70 return & ary_cast<Namespace>(io_entity).AsNode();
71 else if (is_type<Class>(io_entity))
72 return & ary_cast<Class>(io_entity).AsNode();
73 return 0;
74 }
75
76 Ce_Traits::entity_base_type *
ParentOf_(const entity_base_type & i_entity)77 CeNode_Traits::ParentOf_(const entity_base_type & i_entity)
78 {
79 Ce_Traits::id_type
80 ret = i_entity.Owner();
81 if (ret.IsValid())
82 {
83 if (is_type<EnumValue>(i_entity))
84 { // Return not the Enum, but the owner of the Enum:
85 ret = EntityOf_(ret).Owner();
86 csv_assert(ret.IsValid());
87 }
88 return &EntityOf_(ret);
89 }
90 return 0;
91 }
92
93 Ce_id
CeNode_Search(const CodeEntity & i_entity,const String & i_localKey)94 CeNode_Search( const CodeEntity & i_entity,
95 const String & i_localKey )
96 {
97 if (is_type<Namespace>(i_entity))
98 return ary_cast<Namespace>(i_entity).Search_Child(i_localKey);
99 else if (is_type<Class>(i_entity))
100 return ary_cast<Class>(i_entity).Search_Child(i_localKey);
101 return Ce_id(0);
102 }
103
104
105
106
107 //******************** Ce_Compare ********************//
108 const Ce_Compare::key_type &
KeyOf_(const entity_base_type & i_entity)109 Ce_Compare::KeyOf_(const entity_base_type & i_entity)
110 {
111 return i_entity.LocalName();
112 }
113
114 bool
Lesser_(const key_type & i_1,const key_type & i_2)115 Ce_Compare::Lesser_( const key_type & i_1,
116 const key_type & i_2 )
117 {
118 static ::ary::LesserName less_;
119 return less_(i_1,i_2);
120 }
121
122
123 //******************** Ce_GlobalCompare ********************//
124 void
Get_Qualified(StreamStr & o_out,const CodeEntity & i_ce)125 Get_Qualified( StreamStr & o_out,
126 const CodeEntity & i_ce )
127 {
128 if (i_ce.LocalName().empty())
129 return;
130 if (i_ce.Owner().IsValid())
131 Get_Qualified(o_out, Ce_Traits::EntityOf_(i_ce.Owner()));
132
133 o_out << i_ce.LocalName() << "::";
134 }
135
136
137 bool
Lesser_(const key_type & i_1,const key_type & i_2)138 Ce_GlobalCompare::Lesser_( const key_type & i_1,
139 const key_type & i_2 )
140 {
141 static ::ary::LesserName less_;
142
143 if (i_1.LocalName() != i_2.LocalName())
144 return less_(i_1.LocalName(), i_2.LocalName());
145
146 csv_assert(i_1.Owner().IsValid() AND i_2.Owner().IsValid());
147
148 static StreamStr
149 aBuffer1_(300);
150 static StreamStr
151 aBuffer2_(300);
152 aBuffer1_.reset();
153 aBuffer2_.reset();
154
155 Get_Qualified(aBuffer1_, Ce_Traits::EntityOf_(i_1.Owner()));
156 Get_Qualified(aBuffer2_, Ce_Traits::EntityOf_(i_2.Owner()));
157 if (aBuffer1_.size() >= 2)
158 aBuffer1_.pop_back(2);
159 if (aBuffer2_.size() >= 2)
160 aBuffer2_.pop_back(2);
161 return less_(aBuffer1_.c_str(), aBuffer2_.c_str());
162 }
163
164
165
166 //******************** Def_Traits ********************//
167 Def_Traits::entity_base_type &
EntityOf_(id_type i_id)168 Def_Traits::EntityOf_(id_type i_id)
169 {
170 csv_assert(i_id.IsValid());
171 return Def_Storage::Instance_()[i_id];
172 }
173
174 //******************** Def_Compare ********************//
175 const Def_Compare::key_type &
KeyOf_(const entity_base_type & i_entity)176 Def_Compare::KeyOf_(const entity_base_type & i_entity)
177 {
178 return i_entity.LocalName();
179 }
180
181 bool
Lesser_(const key_type & i_1,const key_type & i_2)182 Def_Compare::Lesser_( const key_type & i_1,
183 const key_type & i_2 )
184 {
185 static ::ary::LesserName less_;
186 return less_(i_1,i_2);
187 }
188
189
190
191 //******************** Type_Traits ********************//
192 Type_Traits::entity_base_type &
EntityOf_(id_type i_id)193 Type_Traits::EntityOf_(id_type i_id)
194 {
195 csv_assert(i_id.IsValid());
196 return Type_Storage::Instance_()[i_id];
197 }
198
199 //******************** Type_Compare ********************//
200 const UsedType_Compare::key_type &
KeyOf_(const entity_base_type & i_entity)201 UsedType_Compare::KeyOf_(const entity_base_type & i_entity)
202 {
203 csv_assert( is_type<UsedType>(i_entity) );
204 return ary_cast<UsedType>(i_entity);
205 }
206
207 bool
Lesser_(const key_type & i_1,const key_type & i_2)208 UsedType_Compare::Lesser_( const key_type & i_1,
209 const key_type & i_2 )
210 {
211 return i_1 < i_2;
212 }
213
214
215
216 } // namespace cpp
217 } // namespace ary
218