LibreOffice Module comphelper (master)  1
unwrapargs.hxx
Go to the documentation of this file.
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 <sal/config.h>
24 
25 #include <optional>
26 
27 #include <rtl/ustrbuf.hxx>
28 #include <com/sun/star/uno/Sequence.hxx>
29 #include <com/sun/star/uno/XInterface.hpp>
30 #include <com/sun/star/lang/IllegalArgumentException.hpp>
31 #include <cppu/unotype.hxx>
32 
33 namespace comphelper {
34 
36 namespace detail {
37  inline void unwrapArgsError(
38  const OUString& str, sal_Int32 nArg,
39  const css::uno::Reference< css::uno::XInterface >& xErrorContext =
40  css::uno::Reference< css::uno::XInterface >() )
41  {
42  throw css::lang::IllegalArgumentException(
43  str, xErrorContext, static_cast< sal_Int16 >( nArg ) );
44  }
45 
46  template< typename T, typename... Args >
47  inline void unwrapArgsError( const OUString& str, sal_Int32 nArg, T&, Args&... args )
48  {
49  return unwrapArgsError( str, nArg, args... );
50  }
51 
52  inline void unwrapArgs(
53  const css::uno::Sequence< css::uno::Any >&,
54  sal_Int32,
55  const css::uno::Reference< css::uno::XInterface >& )
56  {
57  return;
58  }
59 
60  inline void unwrapArgs(
61  const css::uno::Sequence< css::uno::Any >&,
62  sal_Int32 )
63  {
64  return;
65  }
66 
67  template< typename T, typename... Args >
68  inline void unwrapArgs(
69  const css::uno::Sequence< css::uno::Any >& seq,
70  sal_Int32 nArg, ::std::optional< T >& v, Args&... args );
71 
72  template< typename T, typename... Args >
73  inline void unwrapArgs(
74  const css::uno::Sequence< css::uno::Any >& seq,
75  sal_Int32 nArg, T& v, Args&... args )
76  {
77  if( seq.getLength() <= nArg )
78  {
79  return unwrapArgsError( OUString( "No such argument available!"),
80  nArg, args... );
81  }
82  if( !fromAny( seq[nArg], &v ) )
83  {
84  OUString msg =
85  "Cannot extract ANY { " +
86  seq[nArg].getValueType().getTypeName() +
87  " } to " +
88  ::cppu::UnoType<T>::get().getTypeName() +
89  "!";
90  return unwrapArgsError( msg, nArg, args... );
91  }
92  return unwrapArgs( seq, ++nArg, args... );
93  }
94 
95  template< typename T, typename... Args >
96  inline void unwrapArgs(
97  const css::uno::Sequence< css::uno::Any >& seq,
98  sal_Int32 nArg, ::std::optional< T >& v, Args&... args )
99  {
100  if( nArg < seq.getLength() )
101  {
102  T t;
103  unwrapArgs( seq, nArg, t, args... );
104  v = t;
105  } else {
106  unwrapArgs( seq, ++nArg, args... );
107  }
108  }
109 }
110 
111 template< typename... Args >
112 inline void unwrapArgs(
113  const css::uno::Sequence< css::uno::Any >& seq,
114  Args&... args )
115 {
116  return detail::unwrapArgs( seq, 0, args... );
117 }
118 
119 } // namespace comphelper
120 
121 #endif // ! defined( INCLUDED_COMPHELPER_UNWRAPARGS_HXX)
122 
123 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void unwrapArgsError(const OUString &str, sal_Int32 nArg, const css::uno::Reference< css::uno::XInterface > &xErrorContext=css::uno::Reference< css::uno::XInterface >())
Definition: unwrapargs.hxx:37
css::uno::Type const & get()
XPropertyListType t
void unwrapArgs(const css::uno::Sequence< css::uno::Any > &, sal_Int32, const css::uno::Reference< css::uno::XInterface > &)
Definition: unwrapargs.hxx:52
void unwrapArgs(const css::uno::Sequence< css::uno::Any > &seq, Args &...args)
Definition: unwrapargs.hxx:112