Bump version to 5.0-14
[LibreOffice.git] / dbaccess / source / ui / misc / propertystorage.cxx
blob7fb7c99c3a27b572e5dd2cd3fc64d4ae82df6f31
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 "propertystorage.hxx"
22 #include <svl/itemset.hxx>
23 #include <svl/stritem.hxx>
24 #include <svl/eitem.hxx>
26 #include <osl/diagnose.h>
28 #include <cassert>
29 #include <boost/scoped_ptr.hpp>
31 namespace dbaui
34 using ::com::sun::star::uno::Reference;
35 using ::com::sun::star::uno::XInterface;
36 using ::com::sun::star::uno::UNO_QUERY;
37 using ::com::sun::star::uno::Exception;
38 using ::com::sun::star::uno::RuntimeException;
39 using ::com::sun::star::uno::Any;
40 using ::com::sun::star::uno::makeAny;
42 // PropertyStorage
43 PropertyStorage::~PropertyStorage()
47 // helper
48 namespace
50 #undef UNOTYPE
51 template < class ITEMTYPE, class UNOTYPE >
52 class ItemAdapter
54 public:
55 static bool trySet( SfxItemSet& _rSet, ItemId _nItemId, const Any& _rValue )
57 const SfxPoolItem& rItem( _rSet.Get( _nItemId ) );
58 const ITEMTYPE* pTypedItem = dynamic_cast< const ITEMTYPE* >( &rItem );
59 if ( !pTypedItem )
60 return false;
62 UNOTYPE aValue( pTypedItem->GetValue() );
63 OSL_VERIFY( _rValue >>= aValue );
64 // TODO: one could throw an IllegalArgumentException here - finally, this method
65 // is (to be) used from within an XPropertySet::setPropertyValue implementation,
66 // where this would be the appropriate reaction on wrong value types
67 ITEMTYPE* pCloneItem = dynamic_cast< ITEMTYPE* >( pTypedItem->Clone() );
68 if(!pCloneItem)
70 return false;
72 boost::scoped_ptr< ITEMTYPE > pClone( pCloneItem);
73 assert(pClone.get());
74 pClone->SetValue( aValue );
75 _rSet.Put( *pClone );
76 return true;
79 static bool tryGet( const SfxPoolItem& _rItem, Any& _out_rValue )
81 const ITEMTYPE* pTypedItem = dynamic_cast< const ITEMTYPE* >( &_rItem );
82 if ( !pTypedItem )
83 return false;
85 _out_rValue <<= UNOTYPE( pTypedItem->GetValue() );
86 return true;
91 // SetItemPropertyStorage
92 void SetItemPropertyStorage::getPropertyValue( Any& _out_rValue ) const
94 const SfxPoolItem& rItem( m_rItemSet.Get( m_nItemID ) );
96 // try some known item types
97 if ( ItemAdapter< SfxBoolItem, sal_Bool >::tryGet( rItem, _out_rValue )
98 || ItemAdapter< SfxStringItem, OUString >::tryGet( rItem, _out_rValue )
100 return;
102 OSL_FAIL( "SetItemPropertyStorage::getPropertyValue: unsupported item type!" );
105 void SetItemPropertyStorage::setPropertyValue( const Any& _rValue )
107 // try some known item types
108 if ( ItemAdapter< SfxBoolItem, sal_Bool >::trySet( m_rItemSet, m_nItemID, _rValue )
109 || ItemAdapter< SfxStringItem, OUString >::trySet( m_rItemSet, m_nItemID, _rValue )
111 return;
113 OSL_FAIL( "SetItemPropertyStorage::setPropertyValue: unsupported item type!" );
116 } // namespace dbaui
118 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */