xref: /trunk/main/registry/inc/registry/reader.hxx (revision 5a5f4a75)
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 INCLUDED_registry_reader_hxx
25 #define INCLUDED_registry_reader_hxx
26 
27 #include "registry/reader.h"
28 #include "registry/refltype.hxx"
29 #include "registry/types.h"
30 #include "registry/version.h"
31 
32 #include "rtl/ustring.hxx"
33 #include "sal/types.h"
34 
35 #include <algorithm>
36 #include <new>
37 
38 namespace typereg {
39 
40 /// @HTML
41 
42 /**
43    A type reader working on a binary blob that represents a UNOIDL type.
44 
45    <p>Instances of this class are not multi-thread&ndash;safe.</p>
46 
47    @since UDK 3.2.0
48  */
49 class Reader {
50 public:
51     /**
52        Creates an invalid type reader.
53      */
Reader()54     Reader(): m_handle(0) {}
55 
56     /**
57        Creates a type reader.
58 
59        <p>If the given binary blob is malformed, or of a version larger than
60        <code>maxVersion</code>, the created type reader is flagged as
61        invalid.</p>
62 
63        @param buffer the binary blob representing the type; must point to at
64        least <code>length</code> bytes, and need only be byte-aligned
65 
66        @param length the size in bytes of the binary blob representing the type
67 
68        @param copy if true, the type reader creates an internal copy of the
69        given buffer, and the given buffer is not accessed after this constructor
70        returns; if false, the type reader works directly on the given buffer,
71        which must remain available unmodified until the underlying type reader
72        is destroyed (note that the lifetime of the underlying type reader can be
73        different from the lifetime of this <code>Reader</code> instance)
74 
75        @param maxVersion the maximum binary blob version the client is prepared
76        to handle; must not be negative
77 
78        @exception std::bad_alloc is raised if an out-of-memory condition occurs
79      */
Reader(void const * buffer,sal_uInt32 length,bool copy,typereg_Version maxVersion)80     Reader(
81         void const * buffer, sal_uInt32 length, bool copy,
82         typereg_Version maxVersion)
83     {
84         if (!typereg_reader_create(buffer, length, copy, maxVersion, &m_handle))
85         {
86             throw std::bad_alloc();
87         }
88     }
89 
90     /**
91        Shares a type reader between two <code>Reader</code> instances.
92 
93        @param other another <code>Reader</code> instance
94      */
Reader(Reader const & other)95     Reader(Reader const & other): m_handle(other.m_handle) {
96         typereg_reader_acquire(m_handle);
97     }
98 
99     /**
100        Destroys this <code>Reader</code> instance.
101 
102        <p>The underlying type reader is only destroyed if this instance was its
103        last user.</p>
104      */
~Reader()105     ~Reader() {
106         typereg_reader_release(m_handle);
107     }
108 
109     /**
110        Replaces the underlying type reader.
111 
112        @param other any <code>Reader</code> instance
113 
114        @return this <code>Reader</code> instance
115      */
operator =(Reader const & other)116     Reader & operator =(Reader const & other) {
117         Reader temp(other);
118         std::swap(this->m_handle, temp.m_handle);
119         return *this;
120     }
121 
122     /**
123        Returns whether this type reader is valid.
124 
125        @return true iff this type reader is valid
126      */
isValid() const127     bool isValid() const {
128         return m_handle != 0;
129     }
130 
131     /**
132        Returns the binary blob version of this type reader.
133 
134        @return the version of the binary blob from which this type reader was
135        constructed; if this type reader is invalid,
136        <code>TYPEREG_VERSION_0</code> is returned
137      */
getVersion() const138     typereg_Version getVersion() const {
139         return typereg_reader_getVersion(m_handle);
140     }
141 
142     /**
143        Returns the documentation of this type reader.
144 
145        @return the documentation of this type reader; if this type reader is
146        invalid, an empty string is returned
147 
148        @exception std::bad_alloc is raised if an out-of-memory condition occurs
149      */
getDocumentation() const150     rtl::OUString getDocumentation() const {
151         rtl_uString * s = 0;
152         typereg_reader_getDocumentation(m_handle, &s);
153         if (s == 0) {
154             throw std::bad_alloc();
155         }
156         return rtl::OUString(s, SAL_NO_ACQUIRE);
157     }
158 
159     /**
160        Returns the file name of this type reader.
161 
162        @return the file name of this type reader; if this type reader is
163        invalid, an empty string is returned
164 
165        @exception std::bad_alloc is raised if an out-of-memory condition occurs
166        @deprecated
167      */
getFileName() const168     rtl::OUString getFileName() const {
169         rtl_uString * s = 0;
170         typereg_reader_getFileName(m_handle, &s);
171         if (s == 0) {
172             throw std::bad_alloc();
173         }
174         return rtl::OUString(s, SAL_NO_ACQUIRE);
175     }
176 
177     /**
178        Returns the type class of this type reader.
179 
180        <p>This function will always return the type class without the internal
181        <code>RT_TYPE_PUBLISHED</code> flag set.  Use <code>isPublished</code> to
182        determine whether this type reader is published.</p>
183 
184        @return the type class of this type reader; if this type reader is
185        invalid, <code>RT_TYPE_INVALID</code> is returned
186      */
getTypeClass() const187     RTTypeClass getTypeClass() const {
188         return typereg_reader_getTypeClass(m_handle);
189     }
190 
191     /**
192        Returns whether this type reader is published.
193 
194        @return whether this type reader is published; if this type reader is
195        invalid, <code>false</code> is returned
196      */
isPublished() const197     bool isPublished() const {
198         return typereg_reader_isPublished(m_handle);
199     }
200 
201     /**
202        Returns the type name of this type reader.
203 
204        @return the type name of this type reader; if this type reader is
205        invalid, an empty string is returned
206 
207        @exception std::bad_alloc is raised if an out-of-memory condition occurs
208      */
getTypeName() const209     rtl::OUString getTypeName() const {
210         rtl_uString * s = 0;
211         typereg_reader_getTypeName(m_handle, &s);
212         if (s == 0) {
213             throw std::bad_alloc();
214         }
215         return rtl::OUString(s, SAL_NO_ACQUIRE);
216     }
217 
218     /**
219        Returns the number of super types of this type reader.
220 
221        @return the number of super types of this type reader; if this type
222        reader is invalid, zero is returned
223      */
getSuperTypeCount() const224     sal_uInt16 getSuperTypeCount() const {
225         return typereg_reader_getSuperTypeCount(m_handle);
226     }
227 
228     /**
229        Returns the type name of a super type of this type reader.
230 
231        @param index a valid index into the range of super types of this type
232        reader
233 
234        @return the type name of the given super type
235 
236        @exception std::bad_alloc is raised if an out-of-memory condition occurs
237      */
getSuperTypeName(sal_uInt16 index) const238     rtl::OUString getSuperTypeName(sal_uInt16 index) const {
239         rtl_uString * s = 0;
240         typereg_reader_getSuperTypeName(m_handle, &s, index);
241         if (s == 0) {
242             throw std::bad_alloc();
243         }
244         return rtl::OUString(s, SAL_NO_ACQUIRE);
245     }
246 
247     /**
248        Returns the number of fields of this type reader.
249 
250        @return the number of fields of this type reader; if this type reader is
251        invalid, zero is returned
252      */
getFieldCount() const253     sal_uInt16 getFieldCount() const {
254         return typereg_reader_getFieldCount(m_handle);
255     }
256 
257     /**
258        Returns the documentation of a field of this type reader.
259 
260        @param index a valid index into the range of fields of this type reader
261 
262        @return the documentation of the given field
263 
264        @exception std::bad_alloc is raised if an out-of-memory condition occurs
265      */
getFieldDocumentation(sal_uInt16 index) const266     rtl::OUString getFieldDocumentation(sal_uInt16 index) const {
267         rtl_uString * s = 0;
268         typereg_reader_getFieldDocumentation(m_handle, &s, index);
269         if (s == 0) {
270             throw std::bad_alloc();
271         }
272         return rtl::OUString(s, SAL_NO_ACQUIRE);
273     }
274 
275     /**
276        Returns the file name of a field of this type reader.
277 
278        @param index a valid index into the range of fields of this type reader
279 
280        @return the file name of the given field
281 
282        @exception std::bad_alloc is raised if an out-of-memory condition occurs
283        @deprecated
284      */
getFieldFileName(sal_uInt16 index) const285     rtl::OUString getFieldFileName(sal_uInt16 index) const {
286         rtl_uString * s = 0;
287         typereg_reader_getFieldFileName(m_handle, &s, index);
288         if (s == 0) {
289             throw std::bad_alloc();
290         }
291         return rtl::OUString(s, SAL_NO_ACQUIRE);
292     }
293 
294     /**
295        Returns the flags of a field of this type reader.
296 
297        @param index a valid index into the range of fields of this type reader
298 
299        @return the flags of the given field
300      */
getFieldFlags(sal_uInt16 index) const301     RTFieldAccess getFieldFlags(sal_uInt16 index) const {
302         return typereg_reader_getFieldFlags(m_handle, index);
303     }
304 
305     /**
306        Returns the name of a field of this type reader.
307 
308        @param index a valid index into the range of fields of this type reader
309 
310        @return the name of the given field
311 
312        @exception std::bad_alloc is raised if an out-of-memory condition occurs
313      */
getFieldName(sal_uInt16 index) const314     rtl::OUString getFieldName(sal_uInt16 index) const {
315         rtl_uString * s = 0;
316         typereg_reader_getFieldName(m_handle, &s, index);
317         if (s == 0) {
318             throw std::bad_alloc();
319         }
320         return rtl::OUString(s, SAL_NO_ACQUIRE);
321     }
322 
323     /**
324        Returns the type name of a field of this type reader.
325 
326        @param index a valid index into the range of fields of this type reader
327 
328        @return the type name of the given field
329 
330        @exception std::bad_alloc is raised if an out-of-memory condition occurs
331      */
getFieldTypeName(sal_uInt16 index) const332     rtl::OUString getFieldTypeName(sal_uInt16 index) const {
333         rtl_uString * s = 0;
334         typereg_reader_getFieldTypeName(m_handle, &s, index);
335         if (s == 0) {
336             throw std::bad_alloc();
337         }
338         return rtl::OUString(s, SAL_NO_ACQUIRE);
339     }
340 
341     /**
342        Returns the value of a field of this type reader.
343 
344        @param index a valid index into the range of fields of this type reader
345 
346        @return the value of the given field
347 
348        @exception std::bad_alloc is raised if an out-of-memory condition occurs
349      */
getFieldValue(sal_uInt16 index) const350     RTConstValue getFieldValue(sal_uInt16 index) const {
351         RTConstValue v;
352         if (!typereg_reader_getFieldValue(
353                 m_handle, index, &v.m_type, &v.m_value))
354         {
355             throw std::bad_alloc();
356         }
357         return v;
358     }
359 
360     /**
361        Returns the number of methods of this type reader.
362 
363        @return the number of methods of this type reader; if this type reader is
364        invalid, zero is returned
365      */
getMethodCount() const366     sal_uInt16 getMethodCount() const {
367         return typereg_reader_getMethodCount(m_handle);
368     }
369 
370     /**
371        Returns the documentation of a method of this type reader.
372 
373        @param index a valid index into the range of methods of this type reader
374 
375        @return the documentation of the given method
376 
377        @exception std::bad_alloc is raised if an out-of-memory condition occurs
378      */
getMethodDocumentation(sal_uInt16 index) const379     rtl::OUString getMethodDocumentation(sal_uInt16 index) const {
380         rtl_uString * s = 0;
381         typereg_reader_getMethodDocumentation(m_handle, &s, index);
382         if (s == 0) {
383             throw std::bad_alloc();
384         }
385         return rtl::OUString(s, SAL_NO_ACQUIRE);
386     }
387 
388     /**
389        Returns the flags of a method of this type reader.
390 
391        @param index a valid index into the range of methods of this type reader
392 
393        @return the flags of the given method
394      */
getMethodFlags(sal_uInt16 index) const395     RTMethodMode getMethodFlags(sal_uInt16 index) const {
396         return typereg_reader_getMethodFlags(m_handle, index);
397     }
398 
399     /**
400        Returns the name of a method of this type reader.
401 
402        @param index a valid index into the range of methods of this type reader
403 
404        @return the name of the given method
405 
406        @exception std::bad_alloc is raised if an out-of-memory condition occurs
407      */
getMethodName(sal_uInt16 index) const408     rtl::OUString getMethodName(sal_uInt16 index) const {
409         rtl_uString * s = 0;
410         typereg_reader_getMethodName(m_handle, &s, index);
411         if (s == 0) {
412             throw std::bad_alloc();
413         }
414         return rtl::OUString(s, SAL_NO_ACQUIRE);
415     }
416 
417     /**
418        Returns the return type name of a method of this type reader.
419 
420        @param index a valid index into the range of methods of this type reader
421 
422        @return the return type name of the given method
423 
424        @exception std::bad_alloc is raised if an out-of-memory condition occurs
425      */
getMethodReturnTypeName(sal_uInt16 index) const426     rtl::OUString getMethodReturnTypeName(sal_uInt16 index) const {
427         rtl_uString * s = 0;
428         typereg_reader_getMethodReturnTypeName(m_handle, &s, index);
429         if (s == 0) {
430             throw std::bad_alloc();
431         }
432         return rtl::OUString(s, SAL_NO_ACQUIRE);
433     }
434 
435     /**
436        Returns the number of parameters of a method of this type reader.
437 
438        @param index a valid index into the range of methods of this type reader
439 
440        @return the number of parameters of the given method
441      */
getMethodParameterCount(sal_uInt16 index) const442     sal_uInt16 getMethodParameterCount(sal_uInt16 index) const {
443         return typereg_reader_getMethodParameterCount(m_handle, index);
444     }
445 
446     /**
447        Returns the flags of a parameter of a method of this type reader.
448 
449        @param methodIndex a valid index into the range of methods of this type
450        reader
451 
452        @param parameterIndex a valid index into the range of parameters of the
453        given method
454 
455        @return the flags of the given method parameter
456      */
getMethodParameterFlags(sal_uInt16 methodIndex,sal_uInt16 parameterIndex) const457     RTParamMode getMethodParameterFlags(
458         sal_uInt16 methodIndex, sal_uInt16 parameterIndex) const
459     {
460         return typereg_reader_getMethodParameterFlags(
461             m_handle, methodIndex, parameterIndex);
462     }
463 
464     /**
465        Returns the name of a parameter of a method of this type reader.
466 
467        @param methodIndex a valid index into the range of methods of this type
468        reader
469 
470        @param parameterIndex a valid index into the range of parameters of the
471        given method
472 
473        @return the name of the given method parameter
474 
475        @exception std::bad_alloc is raised if an out-of-memory condition occurs
476      */
getMethodParameterName(sal_uInt16 methodIndex,sal_uInt16 parameterIndex) const477     rtl::OUString getMethodParameterName(
478         sal_uInt16 methodIndex, sal_uInt16 parameterIndex) const
479     {
480         rtl_uString * s = 0;
481         typereg_reader_getMethodParameterName(
482             m_handle, &s, methodIndex, parameterIndex);
483         if (s == 0) {
484             throw std::bad_alloc();
485         }
486         return rtl::OUString(s, SAL_NO_ACQUIRE);
487     }
488 
489     /**
490        Returns the type name of a parameter of a method of this type reader.
491 
492        @param methodIndex a valid index into the range of methods of this type
493        reader
494 
495        @param parameterIndex a valid index into the range of parameters of the
496        given method
497 
498        @return the type name of the given method parameter
499 
500        @exception std::bad_alloc is raised if an out-of-memory condition occurs
501      */
getMethodParameterTypeName(sal_uInt16 methodIndex,sal_uInt16 parameterIndex) const502     rtl::OUString getMethodParameterTypeName(
503         sal_uInt16 methodIndex, sal_uInt16 parameterIndex) const
504     {
505         rtl_uString * s = 0;
506         typereg_reader_getMethodParameterTypeName(
507             m_handle, &s, methodIndex, parameterIndex);
508         if (s == 0) {
509             throw std::bad_alloc();
510         }
511         return rtl::OUString(s, SAL_NO_ACQUIRE);
512     }
513 
514     /**
515        Returns the number of exceptions of a method of this type reader.
516 
517        @param index a valid index into the range of methods of this type reader
518 
519        @return the number of exceptions of the given method
520      */
getMethodExceptionCount(sal_uInt16 index) const521     sal_uInt16 getMethodExceptionCount(sal_uInt16 index) const {
522         return typereg_reader_getMethodExceptionCount(m_handle, index);
523     }
524 
525     /**
526        Returns the type name of an exception of a method of this type reader.
527 
528        @param methodIndex a valid index into the range of methods of this type
529        reader
530 
531        @param exceptionIndex a valid index into the range of exceptions of the
532        given method
533 
534        @return the type name of the given method exception
535 
536        @exception std::bad_alloc is raised if an out-of-memory condition occurs
537      */
getMethodExceptionTypeName(sal_uInt16 methodIndex,sal_uInt16 exceptionIndex) const538     rtl::OUString getMethodExceptionTypeName(
539         sal_uInt16 methodIndex, sal_uInt16 exceptionIndex) const
540     {
541         rtl_uString * s = 0;
542         typereg_reader_getMethodExceptionTypeName(
543             m_handle, &s, methodIndex, exceptionIndex);
544         if (s == 0) {
545             throw std::bad_alloc();
546         }
547         return rtl::OUString(s, SAL_NO_ACQUIRE);
548     }
549 
550     /**
551        Returns the number of references of this type reader.
552 
553        @return the number of references of this type reader; if this type reader
554        is invalid, zero is returned
555      */
getReferenceCount() const556     sal_uInt16 getReferenceCount() const {
557         return typereg_reader_getReferenceCount(m_handle);
558     }
559 
560     /**
561        Returns the documentation of a reference of this type reader.
562 
563        @param index a valid index into the range of references of this type
564        reader
565 
566        @return the documentation of the given reference
567 
568        @exception std::bad_alloc is raised if an out-of-memory condition occurs
569      */
getReferenceDocumentation(sal_uInt16 index) const570     rtl::OUString getReferenceDocumentation(sal_uInt16 index) const {
571         rtl_uString * s = 0;
572         typereg_reader_getReferenceDocumentation(m_handle, &s, index);
573         if (s == 0) {
574             throw std::bad_alloc();
575         }
576         return rtl::OUString(s, SAL_NO_ACQUIRE);
577     }
578 
579     /**
580        Returns the flags of a reference of this type reader.
581 
582        @param index a valid index into the range of references of this type
583        reader
584 
585        @return the flags of the given reference
586      */
getReferenceFlags(sal_uInt16 index) const587     RTFieldAccess getReferenceFlags(sal_uInt16 index) const {
588         return typereg_reader_getReferenceFlags(m_handle, index);
589     }
590 
591     /**
592        Returns the sort of a reference of this type reader.
593 
594        @param index a valid index into the range of references of this type
595        reader
596 
597        @return the sort of the given reference
598      */
getReferenceSort(sal_uInt16 index) const599     RTReferenceType getReferenceSort(sal_uInt16 index) const {
600         return typereg_reader_getReferenceSort(m_handle, index);
601     }
602 
603     /**
604        Returns the type name of a reference of this type reader.
605 
606        @param index a valid index into the range of references of this type
607        reader
608 
609        @return the type name of the given reference
610 
611        @exception std::bad_alloc is raised if an out-of-memory condition occurs
612      */
getReferenceTypeName(sal_uInt16 index) const613     rtl::OUString getReferenceTypeName(sal_uInt16 index) const {
614         rtl_uString * s = 0;
615         typereg_reader_getReferenceTypeName(m_handle, &s, index);
616         if (s == 0) {
617             throw std::bad_alloc();
618         }
619         return rtl::OUString(s, SAL_NO_ACQUIRE);
620     }
621 
622 private:
623     void * m_handle;
624 };
625 
626 }
627 
628 #endif
629