Version 6.4.0.3, tag libreoffice-6.4.0.3
[LibreOffice.git] / binaryurp / source / binaryany.cxx
blobaf14933311608a469324cb85db5968604cb92d1b
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <sal/config.h>
22 #include <cassert>
23 #include <utility>
25 #include <typelib/typeclass.h>
26 #include <typelib/typedescription.hxx>
27 #include <uno/any2.h>
29 #include "binaryany.hxx"
31 namespace binaryurp {
33 namespace {
35 // Cf. com::sun::star::uno::detail::moveAnyInternals in
36 // include/com/sun/star/uno/Any.hxx:
37 void moveInternals(uno_Any & from, uno_Any & to) {
38 uno_any_construct(&to, nullptr, nullptr, nullptr);
39 std::swap(from.pType, to.pType);
40 std::swap(from.pData, to.pData);
41 std::swap(from.pReserved, to.pReserved);
42 if (to.pData == &from.pReserved) {
43 to.pData = &to.pReserved;
45 // This leaves to.pData (where "to" is now VOID) dangling to somewhere (cf.
46 // CONSTRUCT_EMPTY_ANY, cppu/source/uno/prim.hxx), but what's relevant is
47 // only that it isn't a nullptr (as e.g. >>= -> uno_type_assignData ->
48 // _assignData takes a null pSource to mean "construct a default value").
53 BinaryAny::BinaryAny() throw () {
54 uno_any_construct(&data_, nullptr, nullptr, nullptr);
57 BinaryAny::BinaryAny(css::uno::TypeDescription const & type, void * value)
58 throw ()
60 assert(type.is());
61 uno_any_construct(&data_, value, type.get(), nullptr);
64 BinaryAny::BinaryAny(uno_Any const & raw) throw () {
65 assert(raw.pType != nullptr);
66 data_.pType = raw.pType;
67 typelib_typedescriptionreference_acquire(data_.pType);
68 data_.pData = raw.pData == &raw.pReserved ? &data_.pReserved : raw.pData;
69 data_.pReserved = raw.pReserved;
72 BinaryAny::BinaryAny(BinaryAny const & other) throw () {
73 uno_type_any_construct(&data_, other.data_.pData, other.data_.pType, nullptr);
76 BinaryAny::BinaryAny(BinaryAny && other) throw () {
77 moveInternals(other.data_, data_);
80 BinaryAny::~BinaryAny() throw () {
81 uno_any_destruct(&data_, nullptr);
84 BinaryAny & BinaryAny::operator =(BinaryAny const & other) throw () {
85 if (&other != this) {
86 uno_type_any_assign(&data_, other.data_.pData, other.data_.pType, nullptr, nullptr);
88 return *this;
91 BinaryAny & BinaryAny::operator =(BinaryAny && other) throw () {
92 uno_any_destruct(&data_, nullptr);
93 moveInternals(other.data_, data_);
94 return *this;
97 css::uno::TypeDescription BinaryAny::getType() const throw () {
98 return css::uno::TypeDescription(data_.pType);
101 void * BinaryAny::getValue(css::uno::TypeDescription const & type) const
102 throw ()
104 assert(type.is());
105 assert(
106 type.get()->eTypeClass == typelib_TypeClass_ANY ||
107 type.equals(css::uno::TypeDescription(data_.pType)));
108 return type.get()->eTypeClass == typelib_TypeClass_ANY
109 ? &data_ : data_.pData;
114 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */