xref: /aoo42x/main/configmgr/source/type.cxx (revision 3a7cf181)
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 #include "precompiled_configmgr.hxx"
25 #include "sal/config.h"
26 
27 #include "com/sun/star/uno/Any.hxx"
28 #include "com/sun/star/uno/Reference.hxx"
29 #include "com/sun/star/uno/RuntimeException.hpp"
30 #include "com/sun/star/uno/Sequence.hxx"
31 #include "com/sun/star/uno/Type.hxx"
32 #include "com/sun/star/uno/TypeClass.hpp"
33 #include "com/sun/star/uno/XInterface.hpp"
34 #include "cppu/unotype.hxx"
35 #include "osl/diagnose.h"
36 #include "rtl/string.h"
37 #include "rtl/ustring.h"
38 #include "rtl/ustring.hxx"
39 #include "sal/types.h"
40 
41 #include "type.hxx"
42 
43 namespace configmgr {
44 
45 namespace {
46 
47 namespace css = com::sun::star;
48 
49 }
50 
isListType(Type type)51 bool isListType(Type type) {
52     return type >= TYPE_BOOLEAN_LIST;
53 }
54 
elementType(Type type)55 Type elementType(Type type) {
56     switch (type) {
57     case TYPE_BOOLEAN_LIST:
58         return TYPE_BOOLEAN;
59     case TYPE_SHORT_LIST:
60         return TYPE_SHORT;
61     case TYPE_INT_LIST:
62         return TYPE_INT;
63     case TYPE_LONG_LIST:
64         return TYPE_LONG;
65     case TYPE_DOUBLE_LIST:
66         return TYPE_DOUBLE;
67     case TYPE_STRING_LIST:
68         return TYPE_STRING;
69     case TYPE_HEXBINARY_LIST:
70         return TYPE_HEXBINARY;
71     default:
72         OSL_ASSERT(false);
73         throw css::uno::RuntimeException(
74             rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("this cannot happen")),
75             css::uno::Reference< css::uno::XInterface >());
76     }
77 }
78 
mapType(Type type)79 css::uno::Type mapType(Type type) {
80     switch (type) {
81     case TYPE_ANY:
82         return cppu::UnoType< css::uno::Any >::get();
83     case TYPE_BOOLEAN:
84         return cppu::UnoType< sal_Bool >::get();
85     case TYPE_SHORT:
86         return cppu::UnoType< sal_Int16 >::get();
87     case TYPE_INT:
88         return cppu::UnoType< sal_Int32 >::get();
89     case TYPE_LONG:
90         return cppu::UnoType< sal_Int64 >::get();
91     case TYPE_DOUBLE:
92         return cppu::UnoType< double >::get();
93     case TYPE_STRING:
94         return cppu::UnoType< rtl::OUString >::get();
95     case TYPE_HEXBINARY:
96         return cppu::UnoType< css::uno::Sequence< sal_Int8 > >::get();
97     case TYPE_BOOLEAN_LIST:
98         return cppu::UnoType< css::uno::Sequence< sal_Bool > >::get();
99     case TYPE_SHORT_LIST:
100         return cppu::UnoType< css::uno::Sequence< sal_Int16 > >::get();
101     case TYPE_INT_LIST:
102         return cppu::UnoType< css::uno::Sequence< sal_Int32 > >::get();
103     case TYPE_LONG_LIST:
104         return cppu::UnoType< css::uno::Sequence< sal_Int64 > >::get();
105     case TYPE_DOUBLE_LIST:
106         return cppu::UnoType< css::uno::Sequence< double > >::get();
107     case TYPE_STRING_LIST:
108         return cppu::UnoType< css::uno::Sequence< rtl::OUString > >::get();
109     case TYPE_HEXBINARY_LIST:
110         return cppu::UnoType<
111             css::uno::Sequence< css::uno::Sequence< sal_Int8 > > >::get();
112     default:
113         OSL_ASSERT(false);
114         throw css::uno::RuntimeException(
115             rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("this cannot happen")),
116             css::uno::Reference< css::uno::XInterface >());
117     }
118 }
119 
getDynamicType(css::uno::Any const & value)120 Type getDynamicType(css::uno::Any const & value) {
121     switch (value.getValueType().getTypeClass()) {
122     case css::uno::TypeClass_VOID:
123         return TYPE_NIL;
124     case css::uno::TypeClass_BOOLEAN:
125         return TYPE_BOOLEAN;
126     case css::uno::TypeClass_BYTE:
127         return TYPE_SHORT;
128     case css::uno::TypeClass_SHORT:
129         return TYPE_SHORT;
130     case css::uno::TypeClass_UNSIGNED_SHORT:
131         return value.has< sal_Int16 >() ? TYPE_SHORT : TYPE_INT;
132     case css::uno::TypeClass_LONG:
133         return TYPE_INT;
134     case css::uno::TypeClass_UNSIGNED_LONG:
135         return value.has< sal_Int32 >() ? TYPE_INT : TYPE_LONG;
136     case css::uno::TypeClass_HYPER:
137         return TYPE_LONG;
138     case css::uno::TypeClass_UNSIGNED_HYPER:
139         return value.has< sal_Int64 >() ? TYPE_LONG : TYPE_ERROR;
140     case css::uno::TypeClass_FLOAT:
141     case css::uno::TypeClass_DOUBLE:
142         return TYPE_DOUBLE;
143     case css::uno::TypeClass_STRING:
144         return TYPE_STRING;
145     case css::uno::TypeClass_SEQUENCE: //TODO
146         {
147             rtl::OUString name(value.getValueType().getTypeName());
148             if (name.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("[]byte"))) {
149                 return TYPE_HEXBINARY;
150             } else if (name.equalsAsciiL(
151                            RTL_CONSTASCII_STRINGPARAM("[]boolean")))
152             {
153                 return TYPE_BOOLEAN_LIST;
154             } else if (name.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("[]short")))
155             {
156                 return TYPE_SHORT_LIST;
157             } else if (name.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("[]long")))
158             {
159                 return TYPE_INT_LIST;
160             } else if (name.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("[]hyper")))
161             {
162                 return TYPE_LONG_LIST;
163             } else if (name.equalsAsciiL(
164                            RTL_CONSTASCII_STRINGPARAM("[]double")))
165             {
166                 return TYPE_DOUBLE_LIST;
167             } else if (name.equalsAsciiL(
168                            RTL_CONSTASCII_STRINGPARAM("[]string")))
169             {
170                 return TYPE_STRING_LIST;
171             } else if (name.equalsAsciiL(
172                            RTL_CONSTASCII_STRINGPARAM("[][]byte")))
173             {
174                 return TYPE_HEXBINARY_LIST;
175             }
176         }
177         // fall through
178     default:
179         return TYPE_ERROR;
180     }
181 }
182 
183 }
184