LibreOffice Module dbaccess (master) 1
propertystorage.cxx
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#include <propertystorage.hxx>
21
22#include <svl/itemset.hxx>
23#include <svl/stritem.hxx>
24#include <svl/eitem.hxx>
25
26#include <osl/diagnose.h>
27
28#include <cassert>
29#include <memory>
30
31namespace dbaui
32{
33
34 using ::com::sun::star::uno::Any;
35
36 // helper
37 namespace
38 {
39 template < class ITEMTYPE, class UNOTYPE >
40 class ItemAdapter
41 {
42 public:
43 static bool trySet( SfxItemSet& _rSet, sal_uInt16 _nItemId, const Any& _rValue )
44 {
45 const SfxPoolItem& rItem( _rSet.Get( _nItemId ) );
46 const ITEMTYPE* pTypedItem = dynamic_cast< const ITEMTYPE* >( &rItem );
47 if ( !pTypedItem )
48 return false;
49
50 UNOTYPE aValue( pTypedItem->GetValue() );
51 OSL_VERIFY( _rValue >>= aValue );
52 // TODO: one could throw an IllegalArgumentException here - finally, this method
53 // is (to be) used from within an XPropertySet::setPropertyValue implementation,
54 // where this would be the appropriate reaction on wrong value types
55 ITEMTYPE* pCloneItem = dynamic_cast< ITEMTYPE* >( pTypedItem->Clone() );
56 if(!pCloneItem)
57 {
58 return false;
59 }
60 std::unique_ptr< ITEMTYPE > pClone( pCloneItem);
61 assert(pClone.get());
62 pClone->SetValue( aValue );
63 _rSet.Put( std::move(pClone) );
64 return true;
65 }
66
67 static bool tryGet( const SfxPoolItem& _rItem, Any& _out_rValue )
68 {
69 const ITEMTYPE* pTypedItem = dynamic_cast< const ITEMTYPE* >( &_rItem );
70 if ( !pTypedItem )
71 return false;
72
73 _out_rValue <<= UNOTYPE( pTypedItem->GetValue() );
74 return true;
75 }
76 };
77 }
78
79 // SetItemPropertyStorage
80 void SetItemPropertyStorage::getPropertyValue( Any& _out_rValue ) const
81 {
82 const SfxPoolItem& rItem( m_rItemSet.Get( m_nItemID ) );
83
84 // try some known item types
85 if ( ItemAdapter< SfxBoolItem, bool >::tryGet( rItem, _out_rValue )
86 || ItemAdapter< SfxStringItem, OUString >::tryGet( rItem, _out_rValue )
87 )
88 return;
89
90 OSL_FAIL( "SetItemPropertyStorage::getPropertyValue: unsupported item type!" );
91 }
92
94 {
95 // try some known item types
96 if ( ItemAdapter< SfxBoolItem, bool >::trySet( m_rItemSet, m_nItemID, _rValue )
97 || ItemAdapter< SfxStringItem, OUString >::trySet( m_rItemSet, m_nItemID, _rValue )
98 )
99 return;
100
101 OSL_FAIL( "SetItemPropertyStorage::setPropertyValue: unsupported item type!" );
102 }
103
104} // namespace dbaui
105
106/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
const SfxPoolItem * Put(const SfxPoolItem &rItem, sal_uInt16 nWhich)
const SfxPoolItem & Get(sal_uInt16 nWhich, bool bSrchInParent=true) const
void getPropertyValue(css::uno::Any &_out_rValue) const
void setPropertyValue(const css::uno::Any &_rValue)