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 {
40 inline css::uno::Any int2enum(sal_Int32 nEnum, const css::uno::Type& rType)
41 {
42  assert(rType.getTypeClass() == css::uno::TypeClass_ENUM);
43  return css::uno::Any(&nEnum, rType);
44 }
45 
54 inline bool enum2int(sal_Int32& rnEnum, const css::uno::Any& rAny)
55 {
56  if (rAny.getValueTypeClass() == css::uno::TypeClass_ENUM)
57  {
58  rnEnum = *static_cast<const sal_Int32*>(rAny.getValue());
59  return true;
60  }
61 
62  return rAny >>= rnEnum;
63 }
64 
73 template <typename E> inline void any2enum(E& eRet, const css::uno::Any& rAny)
74 {
75  // check for typesafe enum
76  if (!(rAny >>= eRet))
77  {
78  // if not enum, maybe integer?
79  sal_Int32 nValue = 0;
80  if (!(rAny >>= nValue))
81  throw css::lang::IllegalArgumentException();
82 
83  eRet = static_cast<E>(nValue);
84  }
85 }
86 
94 inline bool any2bool(const css::uno::Any& rAny)
95 {
96  bool b;
97  if (rAny >>= b)
98  {
99  return b;
100  }
101  else
102  {
103  sal_Int32 nValue = 0;
104  if (!(rAny >>= nValue))
105  throw css::lang::IllegalArgumentException();
106  return nValue != 0;
107  }
108 }
109 }
110 
111 #endif
112 
113 /* 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:73
const BorderLinePrimitive2D *pCandidateB assert(pCandidateA)
bool enum2int(sal_Int32 &rnEnum, const css::uno::Any &rAny)
Sets int32 from enum or int in any.
Definition: extract.hxx:54
css::uno::Any int2enum(sal_Int32 nEnum, const css::uno::Type &rType)
Sets enum from int32 value.
Definition: extract.hxx:40
bool any2bool(const css::uno::Any &rAny)
Extracts a boolean either as a bool or an integer from an any.
Definition: extract.hxx:94
sal_Int16 nValue