xref: /AOO41X/main/configmgr/source/type.cxx (revision 1ecadb572e7010ff3b3382ad9bf179dbc6efadbb)
1 /*************************************************************************
2 *
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
6 *
7 * OpenOffice.org - a multi-platform office productivity suite
8 *
9 * This file is part of OpenOffice.org.
10 *
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
14 *
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
20 *
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org.  If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
25 *
26 ************************************************************************/
27 
28 #include "precompiled_configmgr.hxx"
29 #include "sal/config.h"
30 
31 #include "com/sun/star/uno/Any.hxx"
32 #include "com/sun/star/uno/Reference.hxx"
33 #include "com/sun/star/uno/RuntimeException.hpp"
34 #include "com/sun/star/uno/Sequence.hxx"
35 #include "com/sun/star/uno/Type.hxx"
36 #include "com/sun/star/uno/TypeClass.hpp"
37 #include "com/sun/star/uno/XInterface.hpp"
38 #include "cppu/unotype.hxx"
39 #include "osl/diagnose.h"
40 #include "rtl/string.h"
41 #include "rtl/ustring.h"
42 #include "rtl/ustring.hxx"
43 #include "sal/types.h"
44 
45 #include "type.hxx"
46 
47 namespace configmgr {
48 
49 namespace {
50 
51 namespace css = com::sun::star;
52 
53 }
54 
55 bool isListType(Type type) {
56     return type >= TYPE_BOOLEAN_LIST;
57 }
58 
59 Type elementType(Type type) {
60     switch (type) {
61     case TYPE_BOOLEAN_LIST:
62         return TYPE_BOOLEAN;
63     case TYPE_SHORT_LIST:
64         return TYPE_SHORT;
65     case TYPE_INT_LIST:
66         return TYPE_INT;
67     case TYPE_LONG_LIST:
68         return TYPE_LONG;
69     case TYPE_DOUBLE_LIST:
70         return TYPE_DOUBLE;
71     case TYPE_STRING_LIST:
72         return TYPE_STRING;
73     case TYPE_HEXBINARY_LIST:
74         return TYPE_HEXBINARY;
75     default:
76         OSL_ASSERT(false);
77         throw css::uno::RuntimeException(
78             rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("this cannot happen")),
79             css::uno::Reference< css::uno::XInterface >());
80     }
81 }
82 
83 css::uno::Type mapType(Type type) {
84     switch (type) {
85     case TYPE_ANY:
86         return cppu::UnoType< css::uno::Any >::get();
87     case TYPE_BOOLEAN:
88         return cppu::UnoType< sal_Bool >::get();
89     case TYPE_SHORT:
90         return cppu::UnoType< sal_Int16 >::get();
91     case TYPE_INT:
92         return cppu::UnoType< sal_Int32 >::get();
93     case TYPE_LONG:
94         return cppu::UnoType< sal_Int64 >::get();
95     case TYPE_DOUBLE:
96         return cppu::UnoType< double >::get();
97     case TYPE_STRING:
98         return cppu::UnoType< rtl::OUString >::get();
99     case TYPE_HEXBINARY:
100         return cppu::UnoType< css::uno::Sequence< sal_Int8 > >::get();
101     case TYPE_BOOLEAN_LIST:
102         return cppu::UnoType< css::uno::Sequence< sal_Bool > >::get();
103     case TYPE_SHORT_LIST:
104         return cppu::UnoType< css::uno::Sequence< sal_Int16 > >::get();
105     case TYPE_INT_LIST:
106         return cppu::UnoType< css::uno::Sequence< sal_Int32 > >::get();
107     case TYPE_LONG_LIST:
108         return cppu::UnoType< css::uno::Sequence< sal_Int64 > >::get();
109     case TYPE_DOUBLE_LIST:
110         return cppu::UnoType< css::uno::Sequence< double > >::get();
111     case TYPE_STRING_LIST:
112         return cppu::UnoType< css::uno::Sequence< rtl::OUString > >::get();
113     case TYPE_HEXBINARY_LIST:
114         return cppu::UnoType<
115             css::uno::Sequence< css::uno::Sequence< sal_Int8 > > >::get();
116     default:
117         OSL_ASSERT(false);
118         throw css::uno::RuntimeException(
119             rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("this cannot happen")),
120             css::uno::Reference< css::uno::XInterface >());
121     }
122 }
123 
124 Type getDynamicType(css::uno::Any const & value) {
125     switch (value.getValueType().getTypeClass()) {
126     case css::uno::TypeClass_VOID:
127         return TYPE_NIL;
128     case css::uno::TypeClass_BOOLEAN:
129         return TYPE_BOOLEAN;
130     case css::uno::TypeClass_BYTE:
131         return TYPE_SHORT;
132     case css::uno::TypeClass_SHORT:
133         return TYPE_SHORT;
134     case css::uno::TypeClass_UNSIGNED_SHORT:
135         return value.has< sal_Int16 >() ? TYPE_SHORT : TYPE_INT;
136     case css::uno::TypeClass_LONG:
137         return TYPE_INT;
138     case css::uno::TypeClass_UNSIGNED_LONG:
139         return value.has< sal_Int32 >() ? TYPE_INT : TYPE_LONG;
140     case css::uno::TypeClass_HYPER:
141         return TYPE_LONG;
142     case css::uno::TypeClass_UNSIGNED_HYPER:
143         return value.has< sal_Int64 >() ? TYPE_LONG : TYPE_ERROR;
144     case css::uno::TypeClass_FLOAT:
145     case css::uno::TypeClass_DOUBLE:
146         return TYPE_DOUBLE;
147     case css::uno::TypeClass_STRING:
148         return TYPE_STRING;
149     case css::uno::TypeClass_SEQUENCE: //TODO
150         {
151             rtl::OUString name(value.getValueType().getTypeName());
152             if (name.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("[]byte"))) {
153                 return TYPE_HEXBINARY;
154             } else if (name.equalsAsciiL(
155                            RTL_CONSTASCII_STRINGPARAM("[]boolean")))
156             {
157                 return TYPE_BOOLEAN_LIST;
158             } else if (name.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("[]short")))
159             {
160                 return TYPE_SHORT_LIST;
161             } else if (name.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("[]long")))
162             {
163                 return TYPE_INT_LIST;
164             } else if (name.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("[]hyper")))
165             {
166                 return TYPE_LONG_LIST;
167             } else if (name.equalsAsciiL(
168                            RTL_CONSTASCII_STRINGPARAM("[]double")))
169             {
170                 return TYPE_DOUBLE_LIST;
171             } else if (name.equalsAsciiL(
172                            RTL_CONSTASCII_STRINGPARAM("[]string")))
173             {
174                 return TYPE_STRING_LIST;
175             } else if (name.equalsAsciiL(
176                            RTL_CONSTASCII_STRINGPARAM("[][]byte")))
177             {
178                 return TYPE_HEXBINARY_LIST;
179             }
180         }
181         // fall through
182     default:
183         return TYPE_ERROR;
184     }
185 }
186 
187 }
188