1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #ifndef INCLUDED_COMPHELPER_UNWRAPARGS_HXX
21 #define INCLUDED_COMPHELPER_UNWRAPARGS_HXX
23 #include <rtl/ustrbuf.hxx>
24 #include <com/sun/star/uno/Sequence.hxx>
25 #include <com/sun/star/lang/IllegalArgumentException.hpp>
26 #include <boost/optional.hpp>
27 #include <boost/preprocessor/cat.hpp>
28 #include <boost/preprocessor/repetition.hpp>
29 #include <boost/preprocessor/arithmetic/add.hpp>
30 #include <cppu/unotype.hxx>
32 namespace comphelper
{
35 // generating helper functions to unwrap the service's argument sequence:
43 ::com::sun::star::uno::Sequence
< ::com::sun::star::uno::Any
> const& seq
,
44 sal_Int32 nArg
, T
& v
,
45 ::com::sun::star::uno::Reference
< ::com::sun::star::uno::XInterface
>
46 const& xErrorContext
)
48 if (nArg
>= seq
.getLength()) {
49 throw ::com::sun::star::lang::IllegalArgumentException(
50 OUString( "No such argument available!"),
51 xErrorContext
, static_cast<sal_Int16
>(nArg
) );
53 if (! (seq
[nArg
] >>= v
)) {
55 buf
.append( "Cannot extract ANY { " );
56 buf
.append( seq
[nArg
].getValueType().getTypeName() );
57 buf
.append( " } to " );
58 buf
.append( ::cppu::UnoType
<T
>::get().getTypeName() );
59 buf
.append( static_cast<sal_Unicode
>('!') );
60 throw ::com::sun::star::lang::IllegalArgumentException(
61 buf
.makeStringAndClear(), xErrorContext
,
62 static_cast<sal_Int16
>(nArg
) );
68 ::com::sun::star::uno::Sequence
< ::com::sun::star::uno::Any
> const& seq
,
69 sal_Int32 nArg
, ::boost::optional
<T
> & v
,
70 ::com::sun::star::uno::Reference
< ::com::sun::star::uno::XInterface
>
71 const& xErrorContext
)
73 if (nArg
< seq
.getLength()) {
75 extract( seq
, nArg
, t
, xErrorContext
);
82 #define COMPHELPER_UNWRAPARGS_extract(z_, n_, unused_) \
83 detail::extract( seq, n_, BOOST_PP_CAT(v, n_), xErrorContext );
84 #define COMPHELPER_UNWRAPARGS_args(z_, n_, unused_) \
85 BOOST_PP_CAT(T, n_) & BOOST_PP_CAT(v, n_)
87 /** The following preprocessor repetitions generate functions like
90 template <typename T0, typename T1, ...>
91 inline void unwrapArgs(
92 uno::Sequence<uno::Any> const& seq,
93 T0 & v0, T1 & v1, ...,
94 css::uno::Reference<css::uno::XInterface> const& xErrorContext =
95 css::uno::Reference<css::uno::XInterface>() );
97 (full namespace qualification ::com::sun::star has been omitted
100 which unwraps the passed sequence's elements, assigning them to the
101 referenced values. Specify optional arguments as boost::optional<T>.
102 If the length of the sequence is greater than the count of arguments,
103 then the latter sequence elements are ignored.
104 If too few arguments are given in the sequence and a missing argument is
105 no boost::optional<T>, then an lang::IllegalArgumentException is thrown
106 with the specified xErrorContext (defaults to null-ref).
108 The maximum number of service declarations can be set by defining
109 COMPHELPER_UNWRAPARGS_MAX_ARGS; its default is 12.
111 #define COMPHELPER_UNWRAPARGS_make(z_, n_, unused_) \
112 template < BOOST_PP_ENUM_PARAMS( BOOST_PP_ADD(n_, 1), typename T) > \
113 inline void unwrapArgs( \
114 ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any > const& seq, \
115 BOOST_PP_ENUM(BOOST_PP_ADD(n_, 1), COMPHELPER_UNWRAPARGS_args, ~), \
116 ::com::sun::star::uno::Reference< \
117 ::com::sun::star::uno::XInterface> const& xErrorContext = \
118 ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface>() ) \
120 BOOST_PP_REPEAT(BOOST_PP_ADD(n_, 1), COMPHELPER_UNWRAPARGS_extract, ~) \
123 #ifndef COMPHELPER_UNWRAPARGS_MAX_ARGS
124 #define COMPHELPER_UNWRAPARGS_MAX_ARGS 12
127 BOOST_PP_REPEAT(COMPHELPER_UNWRAPARGS_MAX_ARGS
, COMPHELPER_UNWRAPARGS_make
, ~)
129 #undef COMPHELPER_UNWRAPARGS_MAX_ARGS
130 #undef COMPHELPER_UNWRAPARGS_make
131 #undef COMPHELPER_UNWRAPARGS_args
132 #undef COMPHELPER_UNWRAPARGS_extract
134 } // namespace comphelper
136 #endif // ! defined( INCLUDED_COMPHELPER_UNWRAPARGS_HXX)
138 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */