1*9877b273SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*9877b273SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*9877b273SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*9877b273SAndrew Rist  * distributed with this work for additional information
6*9877b273SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*9877b273SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*9877b273SAndrew Rist  * "License"); you may not use this file except in compliance
9*9877b273SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*9877b273SAndrew Rist  *
11*9877b273SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*9877b273SAndrew Rist  *
13*9877b273SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*9877b273SAndrew Rist  * software distributed under the License is distributed on an
15*9877b273SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*9877b273SAndrew Rist  * KIND, either express or implied.  See the License for the
17*9877b273SAndrew Rist  * specific language governing permissions and limitations
18*9877b273SAndrew Rist  * under the License.
19*9877b273SAndrew Rist  *
20*9877b273SAndrew Rist  *************************************************************/
21*9877b273SAndrew Rist 
22*9877b273SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #if ! defined(COMPHELPER_UNWRAPARGS_HXX_INCLUDED)
25cdf0e10cSrcweir #define COMPHELPER_UNWRAPARGS_HXX_INCLUDED
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #if ! defined(_RTL_USTRBUF_HXX_)
28cdf0e10cSrcweir #include "rtl/ustrbuf.hxx"
29cdf0e10cSrcweir #endif
30cdf0e10cSrcweir #include "com/sun/star/uno/Sequence.hxx"
31cdf0e10cSrcweir #if ! defined(_COM_SUN_STAR_LANG_ILLEGALARGUMENTEXCEPTION_HPP_)
32cdf0e10cSrcweir #include "com/sun/star/lang/IllegalArgumentException.hpp"
33cdf0e10cSrcweir #endif
34cdf0e10cSrcweir #include "boost/optional.hpp"
35cdf0e10cSrcweir #include "boost/preprocessor/cat.hpp"
36cdf0e10cSrcweir #include "boost/preprocessor/repetition.hpp"
37cdf0e10cSrcweir #include "boost/preprocessor/arithmetic/add.hpp"
38cdf0e10cSrcweir #include "cppu/unotype.hxx"
39cdf0e10cSrcweir 
40cdf0e10cSrcweir namespace comphelper {
41cdf0e10cSrcweir 
42cdf0e10cSrcweir //
43cdf0e10cSrcweir // generating helper functions to unwrap the service's argument sequence:
44cdf0e10cSrcweir //
45cdf0e10cSrcweir 
46cdf0e10cSrcweir /// @internal
47cdf0e10cSrcweir namespace detail {
48cdf0e10cSrcweir 
49cdf0e10cSrcweir 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)50cdf0e10cSrcweir inline void extract(
51cdf0e10cSrcweir     ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any> const& seq,
52cdf0e10cSrcweir     sal_Int32 nArg, T & v,
53cdf0e10cSrcweir     ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface>
54cdf0e10cSrcweir     const& xErrorContext )
55cdf0e10cSrcweir {
56cdf0e10cSrcweir     if (nArg >= seq.getLength()) {
57cdf0e10cSrcweir         throw ::com::sun::star::lang::IllegalArgumentException(
58cdf0e10cSrcweir             ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(
59cdf0e10cSrcweir                                  "No such argument available!") ),
60cdf0e10cSrcweir             xErrorContext, static_cast<sal_Int16>(nArg) );
61cdf0e10cSrcweir     }
62cdf0e10cSrcweir     if (! (seq[nArg] >>= v)) {
63cdf0e10cSrcweir         ::rtl::OUStringBuffer buf;
64cdf0e10cSrcweir         buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("Cannot extract ANY { ") );
65cdf0e10cSrcweir         buf.append( seq[nArg].getValueType().getTypeName() );
66cdf0e10cSrcweir         buf.appendAscii( RTL_CONSTASCII_STRINGPARAM(" } to ") );
67cdf0e10cSrcweir         buf.append( ::cppu::UnoType<T>::get().getTypeName() );
68cdf0e10cSrcweir         buf.append( static_cast<sal_Unicode>('!') );
69cdf0e10cSrcweir         throw ::com::sun::star::lang::IllegalArgumentException(
70cdf0e10cSrcweir             buf.makeStringAndClear(), xErrorContext,
71cdf0e10cSrcweir             static_cast<sal_Int16>(nArg) );
72cdf0e10cSrcweir     }
73cdf0e10cSrcweir }
74cdf0e10cSrcweir 
75cdf0e10cSrcweir 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)76cdf0e10cSrcweir inline void extract(
77cdf0e10cSrcweir     ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any> const& seq,
78cdf0e10cSrcweir     sal_Int32 nArg, ::boost::optional<T> & v,
79cdf0e10cSrcweir     ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface>
80cdf0e10cSrcweir     const& xErrorContext )
81cdf0e10cSrcweir {
82cdf0e10cSrcweir     if (nArg < seq.getLength()) {
83cdf0e10cSrcweir         T t;
84cdf0e10cSrcweir         extract( seq, nArg, t, xErrorContext );
85cdf0e10cSrcweir         v.reset( t );
86cdf0e10cSrcweir     }
87cdf0e10cSrcweir }
88cdf0e10cSrcweir 
89cdf0e10cSrcweir } // namespace detail
90cdf0e10cSrcweir 
91cdf0e10cSrcweir #define COMPHELPER_UNWRAPARGS_extract(z_, n_, unused_) \
92cdf0e10cSrcweir     detail::extract( seq, n_, BOOST_PP_CAT(v, n_), xErrorContext );
93cdf0e10cSrcweir #define COMPHELPER_UNWRAPARGS_args(z_, n_, unused_) \
94cdf0e10cSrcweir     BOOST_PP_CAT(T, n_) & BOOST_PP_CAT(v, n_)
95cdf0e10cSrcweir 
96cdf0e10cSrcweir /** The following preprocessor repetitions generate functions like
97cdf0e10cSrcweir 
98cdf0e10cSrcweir     <pre>
99cdf0e10cSrcweir         template <typename T0, typename T1, ...>
100cdf0e10cSrcweir         inline void unwrapArgs(
101cdf0e10cSrcweir             uno::Sequence<uno::Any> const& seq,
102cdf0e10cSrcweir             T0 & v0, T1 & v1, ...,
103cdf0e10cSrcweir             css::uno::Reference<css::uno::XInterface> const& xErrorContext =
104cdf0e10cSrcweir             css::uno::Reference<css::uno::XInterface>() );
105cdf0e10cSrcweir     </pre>
106cdf0e10cSrcweir     (full namespace qualification ::com::sun::star has been omitted
107cdf0e10cSrcweir     for brevity)
108cdf0e10cSrcweir 
109cdf0e10cSrcweir     which unwraps the passed sequence's elements, assigning them to the
110cdf0e10cSrcweir     referenced values.  Specify optional arguments as boost::optional<T>.
111cdf0e10cSrcweir     If the length of the sequence is greater than the count of arguments,
112cdf0e10cSrcweir     then the latter sequence elements are ignored.
113cdf0e10cSrcweir     If too few arguments are given in the sequence and a missing argument is
114cdf0e10cSrcweir     no boost::optional<T>, then an lang::IllegalArgumentException is thrown
115cdf0e10cSrcweir     with the specified xErrorContext (defaults to null-ref).
116cdf0e10cSrcweir 
117cdf0e10cSrcweir     The maximum number of service declarations can be set by defining
118cdf0e10cSrcweir     COMPHELPER_UNWRAPARGS_MAX_ARGS; its default is 12.
119cdf0e10cSrcweir */
120cdf0e10cSrcweir #define COMPHELPER_UNWRAPARGS_make(z_, n_, unused_) \
121cdf0e10cSrcweir template < BOOST_PP_ENUM_PARAMS( BOOST_PP_ADD(n_, 1), typename T) > \
122cdf0e10cSrcweir inline void unwrapArgs( \
123cdf0e10cSrcweir     ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any > const& seq, \
124cdf0e10cSrcweir     BOOST_PP_ENUM(BOOST_PP_ADD(n_, 1), COMPHELPER_UNWRAPARGS_args, ~), \
125cdf0e10cSrcweir     ::com::sun::star::uno::Reference< \
126cdf0e10cSrcweir     ::com::sun::star::uno::XInterface> const& xErrorContext = \
127cdf0e10cSrcweir     ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface>() ) \
128cdf0e10cSrcweir { \
129cdf0e10cSrcweir     BOOST_PP_REPEAT(BOOST_PP_ADD(n_, 1), COMPHELPER_UNWRAPARGS_extract, ~) \
130cdf0e10cSrcweir }
131cdf0e10cSrcweir 
132cdf0e10cSrcweir #if ! defined(COMPHELPER_UNWRAPARGS_MAX_ARGS)
133cdf0e10cSrcweir #define COMPHELPER_UNWRAPARGS_MAX_ARGS 12
134cdf0e10cSrcweir #endif
135cdf0e10cSrcweir 
136cdf0e10cSrcweir BOOST_PP_REPEAT(COMPHELPER_UNWRAPARGS_MAX_ARGS, COMPHELPER_UNWRAPARGS_make, ~)
137cdf0e10cSrcweir 
138cdf0e10cSrcweir #undef COMPHELPER_UNWRAPARGS_MAX_ARGS
139cdf0e10cSrcweir #undef COMPHELPER_UNWRAPARGS_make
140cdf0e10cSrcweir #undef COMPHELPER_UNWRAPARGS_args
141cdf0e10cSrcweir #undef COMPHELPER_UNWRAPARGS_extract
142cdf0e10cSrcweir 
143cdf0e10cSrcweir } // namespace comphelper
144cdf0e10cSrcweir 
145cdf0e10cSrcweir #endif //  ! defined(COMPHELPER_UNWRAPARGS_HXX_INCLUDED)
146cdf0e10cSrcweir 
147