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