summaryrefslogtreecommitdiff
path: root/include/o3tl
diff options
context:
space:
mode:
authorStephan Bergmann <sbergman@redhat.com>2016-06-06 09:44:12 +0200
committerStephan Bergmann <sbergman@redhat.com>2016-06-06 09:47:32 +0200
commite5d45064fc2e858c27ab7d97a7a7fa024d210133 (patch)
tree25f8b444fc956489b772625abe1055711129088a /include/o3tl
parent6aa46396f6cb3de4e34a7684c59162bb6a3f37fe (diff)
Improve o3tl/any.hxx
* Ensure tryGet<XFoo> (instead of tryGet<css::uno::Reference<XFoo>>) doesn't compile. * Add forceGet (as discussed at 0d7c5823124696f80583ac2a5f0e28f329f6f786 "New o3tl::try/doGet to obtain value from Any", to be used momentarily). Change-Id: I57eb6ef18a6ab0d52bb26df7eb4e51b485fc83ed
Diffstat (limited to 'include/o3tl')
-rw-r--r--include/o3tl/any.hxx41
1 files changed, 35 insertions, 6 deletions
diff --git a/include/o3tl/any.hxx b/include/o3tl/any.hxx
index 268ae208137f..023f46886953 100644
--- a/include/o3tl/any.hxx
+++ b/include/o3tl/any.hxx
@@ -12,6 +12,7 @@
#include <sal/config.h>
+#include <cassert>
#include <type_traits>
#include <utility>
@@ -20,6 +21,7 @@
#include <com/sun/star/uno/Any.hxx>
#include <com/sun/star/uno/RuntimeException.hpp>
#include <com/sun/star/uno/Reference.hxx>
+#include <com/sun/star/uno/XInterface.hpp>
#include <cppu/unotype.hxx>
#include <rtl/ustring.hxx>
#include <sal/types.h>
@@ -27,10 +29,6 @@
// Some functionality related to css::uno::Any that would ideally be part of
// <com/sun/star/uno/Any.hxx>, but (for now) cannot be for some reason.
-namespace com { namespace sun { namespace star { namespace uno {
- class XInterface;
-} } } }
-
namespace o3tl {
namespace detail {
@@ -116,7 +114,8 @@ template<typename T> inline boost::optional<T> tryGetConverted(
@tparam T the C++ representation of a UNO type that can be contained in a
UNO ANY (i.e., any UNO type other than ANY itself). The legacy C++
representations sal_Bool, cppu::UnoVoidType, cppu::UnoUnsignedShortType,
- cppu::UnoCharType, and cppu::UnoSequenceType are not supported.
+ cppu::UnoCharType, and cppu::UnoSequenceType are not supported. Must be a
+ complete type or void.
@param any an Any value.
@@ -126,7 +125,8 @@ template<typename T> inline boost::optional<T> tryGetConverted(
template<typename T> inline
typename std::enable_if<
!(detail::IsDerivedReference<T>::value
- || detail::IsUnoSequenceType<T>::value),
+ || detail::IsUnoSequenceType<T>::value
+ || std::is_base_of<css::uno::XInterface, T>::value),
typename detail::Optional<T>::type>::type
tryGet(css::uno::Any const & any) {
// CHAR, STRING, TYPE, sequence types, enum types, struct types, exception
@@ -209,6 +209,11 @@ template<> detail::Optional<css::uno::Any>::type tryGet<css::uno::Any>(
template<> detail::Optional<sal_Bool>::type tryGet<sal_Bool>(
css::uno::Any const &) = delete;
+/*
+
+// Already prevented by std::is_base_of<css::uno::XInterface, T> requiring T to
+// be complete:
+
template<> detail::Optional<cppu::UnoVoidType>::type tryGet<cppu::UnoVoidType>(
css::uno::Any const &) = delete;
@@ -218,6 +223,8 @@ tryGet<cppu::UnoUnsignedShortType>(css::uno::Any const &) = delete;
template<> detail::Optional<cppu::UnoCharType>::type tryGet<cppu::UnoCharType>(
css::uno::Any const &) = delete;
+*/
+
template<typename T> inline
typename std::enable_if<
detail::IsDerivedReference<T>::value,
@@ -257,6 +264,28 @@ template<typename T> inline typename detail::Optional<T>::type doGet(
return opt;
}
+/** Get the value of a specific type from an Any, knowing the Any contains a
+ value of a matching type.
+
+ @note Ideally this would be a public member function of css::uno::Any. See
+ tryGet for details.
+
+ @tparam T the C++ representation of a UNO type that can be contained in a
+ UNO ANY. See tryGet for details.
+
+ @param any an Any value.
+
+ @return a positive proxy for the value of the specfied type obtained from
+ the given Any. See tryGet for details.
+*/
+template<typename T> inline typename detail::Optional<T>::type forceGet(
+ css::uno::Any const & any)
+{
+ auto opt = tryGet<T>(any);
+ assert(opt);
+ return opt;
+}
+
}
#endif