Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #ifndef INCLUDED_COMPHELPER_UNWRAPARGS_HXX
21 : #define INCLUDED_COMPHELPER_UNWRAPARGS_HXX
22 :
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>
31 :
32 : namespace comphelper {
33 :
34 :
35 : // generating helper functions to unwrap the service's argument sequence:
36 :
37 :
38 : /// @internal
39 : namespace detail {
40 :
41 : template <typename T>
42 8371 : inline void extract(
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 )
47 : {
48 8371 : if (nArg >= seq.getLength()) {
49 0 : throw ::com::sun::star::lang::IllegalArgumentException(
50 : OUString( "No such argument available!"),
51 0 : xErrorContext, static_cast<sal_Int16>(nArg) );
52 : }
53 8371 : if (! (seq[nArg] >>= v)) {
54 0 : OUStringBuffer buf;
55 0 : buf.append( "Cannot extract ANY { " );
56 0 : buf.append( seq[nArg].getValueType().getTypeName() );
57 0 : buf.append( " } to " );
58 0 : buf.append( ::cppu::UnoType<T>::get().getTypeName() );
59 0 : buf.append( static_cast<sal_Unicode>('!') );
60 0 : throw ::com::sun::star::lang::IllegalArgumentException(
61 : buf.makeStringAndClear(), xErrorContext,
62 0 : static_cast<sal_Int16>(nArg) );
63 : }
64 8371 : }
65 :
66 : template <typename T>
67 5630 : inline void extract(
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 )
72 : {
73 5630 : if (nArg < seq.getLength()) {
74 2744 : T t;
75 5488 : extract( seq, nArg, t, xErrorContext );
76 5488 : v.reset( t );
77 : }
78 5630 : }
79 :
80 : } // namespace detail
81 :
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_)
86 :
87 : /** The following preprocessor repetitions generate functions like
88 :
89 : <pre>
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>() );
96 : </pre>
97 : (full namespace qualification ::com::sun::star has been omitted
98 : for brevity)
99 :
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).
107 :
108 : The maximum number of service declarations can be set by defining
109 : COMPHELPER_UNWRAPARGS_MAX_ARGS; its default is 12.
110 : */
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>() ) \
119 : { \
120 : BOOST_PP_REPEAT(BOOST_PP_ADD(n_, 1), COMPHELPER_UNWRAPARGS_extract, ~) \
121 : }
122 :
123 : #ifndef COMPHELPER_UNWRAPARGS_MAX_ARGS
124 : #define COMPHELPER_UNWRAPARGS_MAX_ARGS 12
125 : #endif
126 :
127 2884 : BOOST_PP_REPEAT(COMPHELPER_UNWRAPARGS_MAX_ARGS, COMPHELPER_UNWRAPARGS_make, ~)
128 :
129 : #undef COMPHELPER_UNWRAPARGS_MAX_ARGS
130 : #undef COMPHELPER_UNWRAPARGS_make
131 : #undef COMPHELPER_UNWRAPARGS_args
132 : #undef COMPHELPER_UNWRAPARGS_extract
133 :
134 : } // namespace comphelper
135 :
136 : #endif // ! defined( INCLUDED_COMPHELPER_UNWRAPARGS_HXX)
137 :
138 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|