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 #if ! defined(COMPHELPER_UNWRAPARGS_HXX_INCLUDED)
25 #define COMPHELPER_UNWRAPARGS_HXX_INCLUDED
26 
27 #if ! defined(_RTL_USTRBUF_HXX_)
28 #include "rtl/ustrbuf.hxx"
29 #endif
30 #include "com/sun/star/uno/Sequence.hxx"
31 #if ! defined(_COM_SUN_STAR_LANG_ILLEGALARGUMENTEXCEPTION_HPP_)
32 #include "com/sun/star/lang/IllegalArgumentException.hpp"
33 #endif
34 #include "boost/optional.hpp"
35 #include "boost/preprocessor/cat.hpp"
36 #include "boost/preprocessor/repetition.hpp"
37 #include "boost/preprocessor/arithmetic/add.hpp"
38 #include "cppu/unotype.hxx"
39 
40 namespace comphelper {
41 
42 //
43 // generating helper functions to unwrap the service's argument sequence:
44 //
45 
46 /// @internal
47 namespace detail {
48 
49 template <typename T>
extract(::com::sun::star::uno::Sequence<::com::sun::star::uno::Any> const & seq,sal_Int32 nArg,T & v,::com::sun::star::uno::Reference<::com::sun::star::uno::XInterface> const & xErrorContext)50 inline void extract(
51     ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any> const& seq,
52     sal_Int32 nArg, T & v,
53     ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface>
54     const& xErrorContext )
55 {
56     if (nArg >= seq.getLength()) {
57         throw ::com::sun::star::lang::IllegalArgumentException(
58             ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(
59                                  "No such argument available!") ),
60             xErrorContext, static_cast<sal_Int16>(nArg) );
61     }
62     if (! (seq[nArg] >>= v)) {
63         ::rtl::OUStringBuffer buf;
64         buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("Cannot extract ANY { ") );
65         buf.append( seq[nArg].getValueType().getTypeName() );
66         buf.appendAscii( RTL_CONSTASCII_STRINGPARAM(" } to ") );
67         buf.append( ::cppu::UnoType<T>::get().getTypeName() );
68         buf.append( static_cast<sal_Unicode>('!') );
69         throw ::com::sun::star::lang::IllegalArgumentException(
70             buf.makeStringAndClear(), xErrorContext,
71             static_cast<sal_Int16>(nArg) );
72     }
73 }
74 
75 template <typename T>
extract(::com::sun::star::uno::Sequence<::com::sun::star::uno::Any> const & seq,sal_Int32 nArg,::boost::optional<T> & v,::com::sun::star::uno::Reference<::com::sun::star::uno::XInterface> const & xErrorContext)76 inline void extract(
77     ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any> const& seq,
78     sal_Int32 nArg, ::boost::optional<T> & v,
79     ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface>
80     const& xErrorContext )
81 {
82     if (nArg < seq.getLength()) {
83         T t;
84         extract( seq, nArg, t, xErrorContext );
85         v.reset( t );
86     }
87 }
88 
89 } // namespace detail
90 
91 #define COMPHELPER_UNWRAPARGS_extract(z_, n_, unused_) \
92     detail::extract( seq, n_, BOOST_PP_CAT(v, n_), xErrorContext );
93 #define COMPHELPER_UNWRAPARGS_args(z_, n_, unused_) \
94     BOOST_PP_CAT(T, n_) & BOOST_PP_CAT(v, n_)
95 
96 /** The following preprocessor repetitions generate functions like
97 
98     <pre>
99         template <typename T0, typename T1, ...>
100         inline void unwrapArgs(
101             uno::Sequence<uno::Any> const& seq,
102             T0 & v0, T1 & v1, ...,
103             css::uno::Reference<css::uno::XInterface> const& xErrorContext =
104             css::uno::Reference<css::uno::XInterface>() );
105     </pre>
106     (full namespace qualification ::com::sun::star has been omitted
107     for brevity)
108 
109     which unwraps the passed sequence's elements, assigning them to the
110     referenced values.  Specify optional arguments as boost::optional<T>.
111     If the length of the sequence is greater than the count of arguments,
112     then the latter sequence elements are ignored.
113     If too few arguments are given in the sequence and a missing argument is
114     no boost::optional<T>, then an lang::IllegalArgumentException is thrown
115     with the specified xErrorContext (defaults to null-ref).
116 
117     The maximum number of service declarations can be set by defining
118     COMPHELPER_UNWRAPARGS_MAX_ARGS; its default is 12.
119 */
120 #define COMPHELPER_UNWRAPARGS_make(z_, n_, unused_) \
121 template < BOOST_PP_ENUM_PARAMS( BOOST_PP_ADD(n_, 1), typename T) > \
122 inline void unwrapArgs( \
123     ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any > const& seq, \
124     BOOST_PP_ENUM(BOOST_PP_ADD(n_, 1), COMPHELPER_UNWRAPARGS_args, ~), \
125     ::com::sun::star::uno::Reference< \
126     ::com::sun::star::uno::XInterface> const& xErrorContext = \
127     ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface>() ) \
128 { \
129     BOOST_PP_REPEAT(BOOST_PP_ADD(n_, 1), COMPHELPER_UNWRAPARGS_extract, ~) \
130 }
131 
132 #if ! defined(COMPHELPER_UNWRAPARGS_MAX_ARGS)
133 #define COMPHELPER_UNWRAPARGS_MAX_ARGS 12
134 #endif
135 
136 BOOST_PP_REPEAT(COMPHELPER_UNWRAPARGS_MAX_ARGS, COMPHELPER_UNWRAPARGS_make, ~)
137 
138 #undef COMPHELPER_UNWRAPARGS_MAX_ARGS
139 #undef COMPHELPER_UNWRAPARGS_make
140 #undef COMPHELPER_UNWRAPARGS_args
141 #undef COMPHELPER_UNWRAPARGS_extract
142 
143 } // namespace comphelper
144 
145 #endif //  ! defined(COMPHELPER_UNWRAPARGS_HXX_INCLUDED)
146 
147