xref: /trunk/main/autodoc/source/ary/idl/ia_type.cxx (revision d291ea28)
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 "ia_type.hxx"
24 
25 
26 // NOT FULLY DEFINED SERVICES
27 #include <ary/qualiname.hxx>
28 #include <ary/idl/i_module.hxx>
29 #include <ary/idl/i_type.hxx>
30 #include <ary/idl/ip_ce.hxx>
31 #include "ia_ce.hxx"
32 #include "is_type.hxx"
33 #include "it_builtin.hxx"
34 #include "it_ce.hxx"
35 #include "it_explicit.hxx"
36 #include "it_sequence.hxx"
37 #include "it_tplparam.hxx"
38 #include "it_xnameroom.hxx"
39 
40 
41 
42 namespace ary
43 {
44 namespace idl
45 {
46 
47 String              MakeTemplateName(
48                         const String &      i_localName,
49                         const std::vector<Type_id> &
50                                             i_templateParameters );
51 
52 
53 
54 inline CeAdmin &
my_Ces() const55 TypeAdmin::my_Ces() const
56     { return *pCes; }
57 
58 inline void
lhf_Put2Storage_and_AssignId(DYN Type & pass_io_rType)59 TypeAdmin::lhf_Put2Storage_and_AssignId( DYN Type & pass_io_rType )
60     { // This also assigns an ID to pass_io_rType:
61       Storage().Store_Entity(pass_io_rType); }
62 
63 inline Type_id
lhf_findBuiltInType(const String & i_sName)64 TypeAdmin::lhf_findBuiltInType( const String & i_sName )
65     { return ary_cast<ExplicitNameRoom>(Storage()[nXNameRoom_Root])
66                         .Search_Name(i_sName); }
67 
68 inline const ExplicitNameRoom &
find_ExplicitNameRoom(Type_id i_nType) const69 TypeAdmin::find_ExplicitNameRoom( Type_id i_nType ) const
70 {
71     return ary_cast<ExplicitNameRoom>(Storage()[i_nType]);
72 }
73 
74 inline ExplicitNameRoom &
find_ExplicitNameRoom(Type_id i_nType)75 TypeAdmin::find_ExplicitNameRoom( Type_id i_nType )
76 {
77     return ary_cast<ExplicitNameRoom>(Storage()[i_nType]);
78 }
79 
80 ExplicitNameRoom &
lhf_CheckIn_XNameRoom(const QualifiedName & i_rName,Ce_id i_nModuleOfOccurrence)81 TypeAdmin::lhf_CheckIn_XNameRoom( const QualifiedName & i_rName,
82                                        Ce_id                 i_nModuleOfOccurrence )
83 {
84     Type_id nRoot = i_rName.IsAbsolute()
85                         ?   Type_id( predefined::type_GlobalXNameRoom )
86                         :   lhf_Get_NameRoomRoot_forModuleofOccurrence( i_nModuleOfOccurrence ).TypeId();
87 
88     if ( i_rName.NamespaceDepth() == 0 )
89         return find_ExplicitNameRoom(nRoot);
90 
91     QualifiedName::namespace_iterator it = i_rName.first_namespace();
92     ExplicitNameRoom *
93         ret = & find_ExplicitNameRoom(nRoot);
94     for ( ; it != i_rName.end_namespace(); ++it )
95     {
96         Type_id
97             found = ret->Search_Name(*it);
98         if (found.IsValid())
99         {
100             ret = & find_ExplicitNameRoom(found);
101         }
102         else
103         {
104             ExplicitNameRoom &
105                 rNew = *new ExplicitNameRoom(*it, *ret);
106             lhf_Put2Storage_and_AssignId(rNew);
107             ret->Add_Name( rNew.Name(), rNew.TypeId() );
108             ret = &rNew;
109         }
110 
111     }   // end for
112     return *ret;
113 }
114 
115 Type_id
lhf_CheckIn_TypeName(const String & i_sLocalName,ExplicitNameRoom & io_rXNameRoom,Ce_id i_nModuleOfOccurrence,const std::vector<Type_id> * i_templateParameters)116 TypeAdmin::lhf_CheckIn_TypeName( const String &        i_sLocalName,
117                                       ExplicitNameRoom &    io_rXNameRoom,
118                                       Ce_id                 i_nModuleOfOccurrence,
119                                  const std::vector<Type_id> *   i_templateParameters )
120 {
121     String sSearchLocalName( i_sLocalName );
122     if ( i_templateParameters != 0
123          ?  i_templateParameters->size() > 0
124          :  false )
125     {
126         sSearchLocalName = MakeTemplateName(
127                                 i_sLocalName,
128                                 *i_templateParameters);
129     }
130 
131     Type_id
132         ret = io_rXNameRoom.Search_Name(sSearchLocalName);
133     if (NOT ret.IsValid())
134     {
135         DYN Type &
136             rNewType = *new ExplicitType(  i_sLocalName,
137                                            io_rXNameRoom.TypeId(),
138                                            i_nModuleOfOccurrence,
139                                            i_templateParameters );
140         lhf_Put2Storage_and_AssignId(rNewType);
141         ret = rNewType.TypeId();
142         io_rXNameRoom.Add_Name( sSearchLocalName, ret );
143     }
144     return ret;
145 }
146 
147 Type_id
lhf_CheckIn_Sequence(Type_id i_nType)148 TypeAdmin::lhf_CheckIn_Sequence(Type_id i_nType)
149 {
150     Type_id
151         ret = Storage().Search_SequenceOf(i_nType);
152 
153     if (NOT ret.IsValid())
154     {
155         DYN Type &
156             rNewSeq = *new Sequence(i_nType);
157         lhf_Put2Storage_and_AssignId(rNewSeq);
158         ret = rNewSeq.Id();
159         Storage().Add_Sequence(i_nType, ret);
160     }
161     return ret;
162 }
163 
164 void
lhf_CheckIn_BuiltInType(const char * i_sName,Rid i_nId)165 TypeAdmin::lhf_CheckIn_BuiltInType( const char *       i_sName,
166                                     Rid                i_nId )
167 {
168     DYN BuiltInType &
169         rNewType = *new BuiltInType(i_sName);
170     Storage().Set_Reserved(i_nId, rNewType);
171 
172     // Put them into both roots, to catch the syntactically correct
173     //   (though unlikely) ::Any, ::long etc.
174     Type_id
175         nId(i_nId);
176     find_ExplicitNameRoom(nXNameRoom_Root).Add_Name(i_sName, nId);
177     find_ExplicitNameRoom(nXNameRoom_Global).Add_Name(i_sName, nId);
178 }
179 
180 ExplicitNameRoom &
lhf_Get_NameRoomRoot_forModuleofOccurrence(Ce_id i_nModuleOfOccurrence)181 TypeAdmin::lhf_Get_NameRoomRoot_forModuleofOccurrence( Ce_id i_nModuleOfOccurrence )
182 {
183     const Type_id *
184         pFound = csv::find_in_map( aMap_ModuleOfOccurrence2NameRoomRoot,
185                                    i_nModuleOfOccurrence );
186     if (pFound != 0)
187         return find_ExplicitNameRoom(*pFound);
188 
189     ExplicitNameRoom &
190         ret = *new ExplicitNameRoom;
191     lhf_Put2Storage_and_AssignId(ret);
192     aMap_ModuleOfOccurrence2NameRoomRoot.insert(std::pair< const Ce_id, Type_id>(i_nModuleOfOccurrence,ret.TypeId()));
193     return ret;
194 }
195 
TypeAdmin()196 TypeAdmin::TypeAdmin()
197     :   pStorage(new Type_Storage),
198         pCes(0),    // Needs to be set directly after creation.
199         nXNameRoom_Root( static_cast<ary::Rid>(predefined::type_Root_ofXNameRooms) ),
200         nXNameRoom_Global( static_cast<ary::Rid>(predefined::type_GlobalXNameRoom) ),
201         aMap_ModuleOfOccurrence2NameRoomRoot()
202 {
203     DYN ExplicitNameRoom &
204         drRoot = *new ExplicitNameRoom;
205     Storage().Set_Reserved( nXNameRoom_Root.Value(), drRoot );
206 
207     DYN ExplicitNameRoom &
208         drGlobal = *new ExplicitNameRoom(String::Null_(), drRoot);
209     Storage().Set_Reserved( nXNameRoom_Global.Value(), drGlobal );
210     drRoot.Add_Name( drGlobal.Name(), nXNameRoom_Global );
211 
212     lhf_Setup_BuildInTypes();
213 }
214 
~TypeAdmin()215 TypeAdmin::~TypeAdmin()
216 {
217 }
218 
219 void
lhf_Setup_BuildInTypes()220 TypeAdmin::lhf_Setup_BuildInTypes()
221 {
222     lhf_CheckIn_BuiltInType("any", predefined::type_any);
223     lhf_CheckIn_BuiltInType("boolean", predefined::type_boolean);
224     lhf_CheckIn_BuiltInType("byte", predefined::type_byte);
225     lhf_CheckIn_BuiltInType("char", predefined::type_char);
226     lhf_CheckIn_BuiltInType("double", predefined::type_double);
227     lhf_CheckIn_BuiltInType("float", predefined::type_float);
228     lhf_CheckIn_BuiltInType("hyper", predefined::type_hyper);
229     lhf_CheckIn_BuiltInType("long", predefined::type_long);
230     lhf_CheckIn_BuiltInType("short", predefined::type_short);
231     lhf_CheckIn_BuiltInType("string", predefined::type_string);
232     lhf_CheckIn_BuiltInType("type", predefined::type_type);
233     lhf_CheckIn_BuiltInType("void", predefined::type_void);
234     lhf_CheckIn_BuiltInType("unsigned hyper", predefined::type_u_hyper);
235     lhf_CheckIn_BuiltInType("unsigned long", predefined::type_u_long);
236     lhf_CheckIn_BuiltInType("unsigned short", predefined::type_u_short);
237 }
238 
239 const Type &
CheckIn_Type(QualifiedName & i_rFullName,uintt i_nSequenceCount,Ce_id i_nModuleOfOccurrence,const std::vector<Type_id> * i_templateParameters)240 TypeAdmin::CheckIn_Type( QualifiedName &     i_rFullName,
241                                  uintt               i_nSequenceCount,
242                                  Ce_id               i_nModuleOfOccurrence,
243                          const std::vector<Type_id> *   i_templateParameters )
244 {
245     // Look in built-in types:
246     Type_id
247         nType = lhf_findBuiltInType(i_rFullName.LocalName());
248     if (NOT nType.IsValid())
249     {   // No built-in type:
250         ExplicitNameRoom &
251             rNameRoom = lhf_CheckIn_XNameRoom(i_rFullName,i_nModuleOfOccurrence);
252         nType = lhf_CheckIn_TypeName( i_rFullName.LocalName(),
253                                       rNameRoom,
254                                       i_nModuleOfOccurrence,
255                                       i_templateParameters );
256     }   // endif
257 
258     for ( uintt s = 0; s < i_nSequenceCount; ++s )
259     {
260         nType = lhf_CheckIn_Sequence(nType);
261     }
262 
263     return Storage()[nType];
264 }
265 
266 TemplateParamType &
Store_TemplateParamType(String i_sName)267 TypeAdmin::Store_TemplateParamType( String i_sName )
268 {
269     DYN TemplateParamType &
270         ret = *new TemplateParamType( i_sName );
271     lhf_Put2Storage_and_AssignId(ret);
272     return ret;
273 }
274 
275 const Type &
Find_Type(Type_id i_nType) const276 TypeAdmin::Find_Type( Type_id i_nType ) const
277 {
278     return Storage()[i_nType];
279 }
280 
281 String
Search_LocalNameOf(Type_id i_nType) const282 TypeAdmin::Search_LocalNameOf( Type_id i_nType ) const
283 {
284     const Type *
285         pType = Storage().Exists(i_nType)
286                     ? 0
287                     : & Storage()[i_nType];
288     if (pType != 0)
289     {
290         switch (pType->AryClass())
291         {
292             case Ce_Type::class_id:
293             case ExplicitType::class_id:
294             case BuiltInType::class_id:
295                     return static_cast< const Named_Type& >(*pType).Name();
296         }
297     }
298     return String::Null_();
299 }
300 
301 Ce_id
Search_CeRelatedTo(Type_id i_nType) const302 TypeAdmin::Search_CeRelatedTo( Type_id i_nType ) const
303 {
304     const Ce_Type *
305         ret = ary_cast<Ce_Type>( & Storage()[i_nType] );
306     return ret != 0
307             ?   ret->RelatedCe()
308             :   Ce_id_Null();
309 }
310 
311 const ExplicitNameRoom &
Find_XNameRoom(Type_id i_nType) const312 TypeAdmin::Find_XNameRoom( Type_id i_nType ) const
313 {
314     return find_ExplicitNameRoom(i_nType);
315 }
316 
317 bool
IsBuiltInOrRelated(const Type & i_rType) const318 TypeAdmin::IsBuiltInOrRelated( const Type & i_rType ) const
319 {
320     if ( is_type<BuiltInType>(i_rType) )
321         return true;
322     else
323     {
324         const Type *
325             pType = &i_rType;
326         while (is_type<Sequence>(*pType))
327         {
328             Type_id
329                 nt = ary_cast<Sequence>(pType)->RelatedType();
330             if (NOT nt.IsValid())
331                 return false;
332             pType = & Storage()[nt];
333         }
334         return is_type<BuiltInType>(*pType);
335     }
336 }
337 
338 
339 String
MakeTemplateName(const String & i_localName,const std::vector<Type_id> &)340 MakeTemplateName( const String &                i_localName,
341                   const std::vector<Type_id> &  )
342 {
343     StreamLock
344         sl(200);
345 
346     // This is the simple solution, assuming that there is only
347     // one version of templatisation allowed with a given name.
348     return
349         sl()
350             << i_localName
351             << C_cTemplateDelimiter
352             << c_str;
353 }
354 
355 
356 
357 }   // namespace idl
358 }   // namespace ary
359