LibreOffice Module comphelper (master)  1
extract.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 #ifndef INCLUDED_COMPHELPER_EXTRACT_HXX
20 #define INCLUDED_COMPHELPER_EXTRACT_HXX
21 
22 #include <sal/config.h>
23 
24 #include <cassert>
25 
26 #include <com/sun/star/lang/IllegalArgumentException.hpp>
27 #include <com/sun/star/uno/TypeClass.hpp>
28 #include <com/sun/star/uno/Type.h>
29 #include <com/sun/star/uno/Any.hxx>
30 
31 namespace cppu
32 {
33 
41 inline css::uno::Any int2enum(
42  sal_Int32 nEnum, const css::uno::Type & rType )
43 {
44  assert(rType.getTypeClass() == css::uno::TypeClass_ENUM);
45  return css::uno::Any( &nEnum, rType );
46 }
47 
56 inline bool enum2int( sal_Int32 & rnEnum, const css::uno::Any & rAny )
57 {
58  if (rAny.getValueTypeClass() == css::uno::TypeClass_ENUM)
59  {
60  rnEnum = * static_cast< const sal_Int32 * >( rAny.getValue() );
61  return true;
62  }
63 
64  return rAny >>= rnEnum;
65 }
66 
75 template< typename E >
76 inline void any2enum( E & eRet, const css::uno::Any & rAny )
77 {
78  // check for typesafe enum
79  if (! (rAny >>= eRet))
80  {
81  // if not enum, maybe integer?
82  sal_Int32 nValue = 0;
83  if (! (rAny >>= nValue))
84  throw css::lang::IllegalArgumentException();
85 
86  eRet = static_cast<E>(nValue);
87  }
88 }
89 
97 inline bool any2bool( const css::uno::Any & rAny )
98 {
99  bool b;
100  if (rAny >>= b)
101  {
102  return b;
103  }
104  else
105  {
106  sal_Int32 nValue = 0;
107  if (! (rAny >>= nValue))
108  throw css::lang::IllegalArgumentException();
109  return nValue != 0;
110  }
111 }
112 
113 }
114 
115 #endif
116 
117 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void any2enum(E &eRet, const css::uno::Any &rAny)
Sets int32 from enum or int in any with additional typecheck.
Definition: extract.hxx:76
bool enum2int(sal_Int32 &rnEnum, const css::uno::Any &rAny)
Sets int32 from enum or int in any.
Definition: extract.hxx:56
css::uno::Any int2enum(sal_Int32 nEnum, const css::uno::Type &rType)
Sets enum from int32 value.
Definition: extract.hxx:41
bool any2bool(const css::uno::Any &rAny)
Extracts a boolean either as a bool or an integer from an any.
Definition: extract.hxx:97