1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*************************************************************************
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* Copyright 2000, 2010 Oracle and/or its affiliates.
*
* OpenOffice.org - a multi-platform office productivity suite
*
* This file is part of OpenOffice.org.
*
* OpenOffice.org is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License version 3
* only, as published by the Free Software Foundation.
*
* OpenOffice.org is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License version 3 for more details
* (a copy is included in the LICENSE file that accompanied this code).
*
* You should have received a copy of the GNU Lesser General Public License
* version 3 along with OpenOffice.org. If not, see
* <http://www.openoffice.org/license.html>
* for a copy of the LGPLv3 License.
*
************************************************************************/
#include "comphelper_module.hxx"
#include "comphelper/anytostring.hxx"
#include "comphelper/anycompare.hxx"
#include "comphelper/componentbase.hxx"
#include "comphelper/componentcontext.hxx"
#include "comphelper/extract.hxx"
/** === begin UNO includes === **/
#include <com/sun/star/container/XEnumerableMap.hpp>
#include <com/sun/star/lang/XInitialization.hpp>
#include <com/sun/star/ucb/AlreadyInitializedException.hpp>
#include <com/sun/star/beans/Pair.hpp>
#include <com/sun/star/lang/XServiceInfo.hpp>
/** === end UNO includes === **/
#include <cppuhelper/compbase3.hxx>
#include <cppuhelper/implbase1.hxx>
#include <rtl/math.hxx>
#include <rtl/ustrbuf.hxx>
#include <typelib/typedescription.hxx>
#include <map>
#include <boost/shared_ptr.hpp>
//........................................................................
namespace comphelper
{
//........................................................................
/** === begin UNO using === **/
using ::com::sun::star::uno::Reference;
using ::com::sun::star::uno::XInterface;
using ::com::sun::star::uno::UNO_QUERY;
using ::com::sun::star::uno::UNO_QUERY_THROW;
using ::com::sun::star::uno::UNO_SET_THROW;
using ::com::sun::star::uno::Exception;
using ::com::sun::star::uno::RuntimeException;
using ::com::sun::star::uno::Any;
using ::com::sun::star::uno::makeAny;
using ::com::sun::star::uno::Sequence;
using ::com::sun::star::uno::Type;
using ::com::sun::star::container::XEnumerableMap;
using ::com::sun::star::lang::NoSupportException;
using ::com::sun::star::beans::IllegalTypeException;
using ::com::sun::star::container::NoSuchElementException;
using ::com::sun::star::lang::IllegalArgumentException;
using ::com::sun::star::lang::XInitialization;
using ::com::sun::star::ucb::AlreadyInitializedException;
using ::com::sun::star::beans::Pair;
using ::com::sun::star::uno::TypeClass;
using ::com::sun::star::uno::TypeClass_VOID;
using ::com::sun::star::uno::TypeClass_UNKNOWN;
using ::com::sun::star::uno::TypeClass_ANY;
using ::com::sun::star::uno::TypeClass_EXCEPTION;
using ::com::sun::star::uno::TypeClass_STRUCT;
using ::com::sun::star::uno::TypeClass_UNION;
using ::com::sun::star::uno::TypeClass_FLOAT;
using ::com::sun::star::uno::TypeClass_DOUBLE;
using ::com::sun::star::uno::TypeClass_INTERFACE;
using ::com::sun::star::lang::XServiceInfo;
using ::com::sun::star::uno::XComponentContext;
using ::com::sun::star::container::XEnumeration;
using ::com::sun::star::uno::TypeDescription;
using ::com::sun::star::lang::WrappedTargetException;
using ::com::sun::star::lang::DisposedException;
/** === end UNO using === **/
//====================================================================
//= MapData
//====================================================================
class IMapModificationListener;
typedef ::std::vector< IMapModificationListener* > MapListeners;
typedef ::std::map< Any, Any, LessPredicateAdapter > KeyedValues;
struct MapData
{
Type m_aKeyType;
Type m_aValueType;
::std::auto_ptr< KeyedValues > m_pValues;
::boost::shared_ptr< IKeyPredicateLess > m_pKeyCompare;
bool m_bMutable;
MapListeners m_aModListeners;
MapData()
:m_bMutable( true )
{
}
MapData( const MapData& _source )
:m_aKeyType( _source.m_aKeyType )
,m_aValueType( _source.m_aValueType )
,m_pValues( new KeyedValues( *_source.m_pValues ) )
,m_pKeyCompare( _source.m_pKeyCompare )
,m_bMutable( false )
,m_aModListeners()
{
}
private:
MapData& operator=( const MapData& _source ); // not implemented
};
//====================================================================
//= IMapModificationListener
//====================================================================
/** implemented by components who want to be notified of modifications in the MapData they work with
*/
class SAL_NO_VTABLE IMapModificationListener
{
public:
/// called when the map was modified
virtual void mapModified() = 0;
virtual ~IMapModificationListener()
{
}
};
//====================================================================
//= MapData helpers
//====================================================================
//--------------------------------------------------------------------
static void lcl_registerMapModificationListener( MapData& _mapData, IMapModificationListener& _listener )
{
#if OSL_DEBUG_LEVEL > 0
for ( MapListeners::const_iterator lookup = _mapData.m_aModListeners.begin();
lookup != _mapData.m_aModListeners.end();
++lookup
)
{
OSL_ENSURE( *lookup != &_listener, "lcl_registerMapModificationListener: this listener is already registered!" );
}
#endif
_mapData.m_aModListeners.push_back( &_listener );
}
//--------------------------------------------------------------------
static void lcl_revokeMapModificationListener( MapData& _mapData, IMapModificationListener& _listener )
{
for ( MapListeners::iterator lookup = _mapData.m_aModListeners.begin();
lookup != _mapData.m_aModListeners.end();
++lookup
)
{
if ( *lookup == &_listener )
{
_mapData.m_aModListeners.erase( lookup );
return;
}
}
OSL_FAIL( "lcl_revokeMapModificationListener: the listener is not registered!" );
}
//--------------------------------------------------------------------
static void lcl_notifyMapDataListeners_nothrow( const MapData& _mapData )
{
for ( MapListeners::const_iterator loop = _mapData.m_aModListeners.begin();
loop != _mapData.m_aModListeners.end();
++loop
)
{
(*loop)->mapModified();
}
}
//====================================================================
//= EnumerableMap
//====================================================================
typedef ::cppu::WeakAggComponentImplHelper3 < XInitialization
, XEnumerableMap
, XServiceInfo
> Map_IFace;
class COMPHELPER_DLLPRIVATE EnumerableMap :public Map_IFace
,public ComponentBase
{
protected:
EnumerableMap( const ComponentContext& _rContext );
virtual ~EnumerableMap();
// XInitialization
virtual void SAL_CALL initialize( const Sequence< Any >& aArguments ) throw (Exception, RuntimeException);
// XEnumerableMap
virtual ::com::sun::star::uno::Reference< ::com::sun::star::container::XEnumeration > SAL_CALL createKeyEnumeration( ::sal_Bool _Isolated ) throw (::com::sun::star::lang::NoSupportException, ::com::sun::star::uno::RuntimeException);
virtual ::com::sun::star::uno::Reference< ::com::sun::star::container::XEnumeration > SAL_CALL createValueEnumeration( ::sal_Bool _Isolated ) throw (::com::sun::star::lang::NoSupportException, ::com::sun::star::uno::RuntimeException);
virtual ::com::sun::star::uno::Reference< ::com::sun::star::container::XEnumeration > SAL_CALL createElementEnumeration( ::sal_Bool _Isolated ) throw (::com::sun::star::lang::NoSupportException, ::com::sun::star::uno::RuntimeException);
// XMap
virtual Type SAL_CALL getKeyType() throw (RuntimeException);
virtual Type SAL_CALL getValueType() throw (RuntimeException);
virtual void SAL_CALL clear( ) throw (NoSupportException, RuntimeException);
virtual ::sal_Bool SAL_CALL containsKey( const Any& _key ) throw (IllegalTypeException, IllegalArgumentException, RuntimeException);
virtual ::sal_Bool SAL_CALL containsValue( const Any& _value ) throw (IllegalTypeException, IllegalArgumentException, RuntimeException);
virtual Any SAL_CALL get( const Any& _key ) throw (IllegalTypeException, IllegalArgumentException, NoSuchElementException, RuntimeException);
virtual Any SAL_CALL put( const Any& _key, const Any& _value ) throw (NoSupportException, IllegalTypeException, IllegalArgumentException, RuntimeException);
virtual Any SAL_CALL remove( const Any& _key ) throw (NoSupportException, IllegalTypeException, IllegalArgumentException, NoSuchElementException, RuntimeException);
// XElementAccess (base of XMap)
virtual Type SAL_CALL getElementType() throw (RuntimeException);
virtual ::sal_Bool SAL_CALL hasElements() throw (RuntimeException);
// XServiceInfo
virtual ::rtl::OUString SAL_CALL getImplementationName( ) throw (RuntimeException);
virtual ::sal_Bool SAL_CALL supportsService( const ::rtl::OUString& ServiceName ) throw (RuntimeException);
virtual Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames( ) throw (RuntimeException);
public:
// XServiceInfo, static version (used for component registration)
static ::rtl::OUString SAL_CALL getImplementationName_static( );
static Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames_static( );
static Reference< XInterface > SAL_CALL Create( const Reference< XComponentContext >& );
private:
void impl_initValues_throw( const Sequence< Pair< Any, Any > >& _initialValues );
/// throws a IllegalTypeException if the given value is not compatible with our ValueType
void impl_checkValue_throw( const Any& _value ) const;
void impl_checkKey_throw( const Any& _key ) const;
void impl_checkNaN_throw( const Any& _keyOrValue, const Type& _keyOrValueType ) const;
void impl_checkMutable_throw() const;
private:
::osl::Mutex m_aMutex;
ComponentContext m_aContext;
MapData m_aData;
::std::vector< ::com::sun::star::uno::WeakReference< XInterface > >
m_aDependentComponents;
};
//====================================================================
//= EnumerationType
//====================================================================
enum EnumerationType
{
eKeys, eValues, eBoth
};
//====================================================================
//= MapEnumerator
//====================================================================
class MapEnumerator : public IMapModificationListener
{
public:
MapEnumerator( ::cppu::OWeakObject& _rParent, MapData& _mapData, const EnumerationType _type )
:m_rParent( _rParent )
,m_rMapData( _mapData )
,m_eType( _type )
,m_mapPos( _mapData.m_pValues->begin() )
,m_disposed( false )
{
lcl_registerMapModificationListener( m_rMapData, *this );
}
virtual ~MapEnumerator()
{
dispose();
}
void dispose()
{
if ( !m_disposed )
{
lcl_revokeMapModificationListener( m_rMapData, *this );
m_disposed = true;
}
}
// XEnumeration equivalents
::sal_Bool hasMoreElements();
Any nextElement();
// IMapModificationListener
virtual void mapModified();
private:
::cppu::OWeakObject& m_rParent;
MapData& m_rMapData;
const EnumerationType m_eType;
KeyedValues::const_iterator m_mapPos;
bool m_disposed;
private:
MapEnumerator(); // not implemented
MapEnumerator( const MapEnumerator& ); // not implemented
MapEnumerator& operator=( const MapEnumerator& ); // not implemented
};
//====================================================================
//= MapEnumeration
//====================================================================
typedef ::cppu::WeakImplHelper1 < XEnumeration
> MapEnumeration_Base;
class MapEnumeration :public ComponentBase
,public MapEnumeration_Base
{
public:
MapEnumeration( ::cppu::OWeakObject& _parentMap, MapData& _mapData, ::cppu::OBroadcastHelper& _rBHelper,
const EnumerationType _type, const bool _isolated )
:ComponentBase( _rBHelper, ComponentBase::NoInitializationNeeded() )
,m_xKeepMapAlive( _parentMap )
,m_pMapDataCopy( _isolated ? new MapData( _mapData ) : NULL )
,m_aEnumerator( *this, _isolated ? *m_pMapDataCopy : _mapData, _type )
{
}
// XEnumeration
virtual ::sal_Bool SAL_CALL hasMoreElements( ) throw (RuntimeException);
virtual Any SAL_CALL nextElement( ) throw (NoSuchElementException, WrappedTargetException, RuntimeException);
protected:
virtual ~MapEnumeration()
{
acquire();
{
::osl::MutexGuard aGuard( getMutex() );
m_aEnumerator.dispose();
m_pMapDataCopy.reset();
}
}
private:
// sicne we share our mutex with the main map, we need to keep it alive as long as we live
Reference< XInterface > m_xKeepMapAlive;
::std::auto_ptr< MapData > m_pMapDataCopy;
MapEnumerator m_aEnumerator;
};
//====================================================================
//= EnumerableMap
//====================================================================
//--------------------------------------------------------------------
EnumerableMap::EnumerableMap( const ComponentContext& _rContext )
:Map_IFace( m_aMutex )
,ComponentBase( Map_IFace::rBHelper )
,m_aContext( _rContext )
{
}
//--------------------------------------------------------------------
EnumerableMap::~EnumerableMap()
{
if ( !impl_isDisposed() )
{
acquire();
dispose();
}
}
//--------------------------------------------------------------------
void SAL_CALL EnumerableMap::initialize( const Sequence< Any >& _arguments ) throw (Exception, RuntimeException)
{
ComponentMethodGuard aGuard( *this, ComponentMethodGuard::WithoutInit );
if ( impl_isInitialized_nothrow() )
throw AlreadyInitializedException();
sal_Int32 nArgumentCount = _arguments.getLength();
if ( ( nArgumentCount != 2 ) && ( nArgumentCount != 3 ) )
throw IllegalArgumentException();
Type aKeyType, aValueType;
if ( !( _arguments[0] >>= aKeyType ) )
throw IllegalArgumentException( ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.uno.Type expected.")), *this, 1 );
if ( !( _arguments[1] >>= aValueType ) )
throw IllegalArgumentException( ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.uno.Type expected.")), *this, 2 );
Sequence< Pair< Any, Any > > aInitialValues;
bool bMutable = true;
if ( nArgumentCount == 3 )
{
if ( !( _arguments[2] >>= aInitialValues ) )
throw IllegalArgumentException( ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("[]com.sun.star.beans.Pair<any,any> expected.")), *this, 2 );
bMutable = false;
}
// for the value, anything is allowed, except VOID
if ( ( aValueType.getTypeClass() == TypeClass_VOID ) || ( aValueType.getTypeClass() == TypeClass_UNKNOWN ) )
throw IllegalTypeException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Unsupported value type." ) ), *this );
// create the comparator for the KeyType, and throw if the type is not supported
::std::auto_ptr< IKeyPredicateLess > pComparator( getStandardLessPredicate( aKeyType, NULL ) );
if ( !pComparator.get() )
throw IllegalTypeException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Unsupported key type." ) ), *this );
// init members
m_aData.m_aKeyType = aKeyType;
m_aData.m_aValueType = aValueType;
m_aData.m_pKeyCompare = pComparator;
m_aData.m_pValues.reset( new KeyedValues( *m_aData.m_pKeyCompare ) );
m_aData.m_bMutable = bMutable;
if ( aInitialValues.getLength() )
impl_initValues_throw( aInitialValues );
setInitialized();
}
//--------------------------------------------------------------------
void EnumerableMap::impl_initValues_throw( const Sequence< Pair< Any, Any > >& _initialValues )
{
OSL_PRECOND( m_aData.m_pValues.get() && m_aData.m_pValues->empty(), "EnumerableMap::impl_initValues_throw: illegal call!" );
if ( !m_aData.m_pValues.get() || !m_aData.m_pValues->empty() )
throw RuntimeException();
const Pair< Any, Any >* mapping = _initialValues.getConstArray();
const Pair< Any, Any >* mappingEnd = mapping + _initialValues.getLength();
Any normalizedValue;
for ( ; mapping != mappingEnd; ++mapping )
{
impl_checkValue_throw( mapping->Second );
(*m_aData.m_pValues)[ mapping->First ] = mapping->Second;
}
}
//--------------------------------------------------------------------
void EnumerableMap::impl_checkValue_throw( const Any& _value ) const
{
if ( !_value.hasValue() )
// nothing to do, NULL values are always allowed, regardless of the ValueType
return;
TypeClass eAllowedTypeClass = m_aData.m_aValueType.getTypeClass();
bool bValid = false;
switch ( eAllowedTypeClass )
{
default:
bValid = ( _value.getValueTypeClass() == eAllowedTypeClass );
break;
case TypeClass_ANY:
bValid = true;
break;
case TypeClass_INTERFACE:
{
// special treatment: _value might contain the proper type, but the interface
// might actually be NULL. Which is still valid ...
if ( m_aData.m_aValueType.isAssignableFrom( _value.getValueType() ) )
// this also catches the special case where XFoo is our value type,
// and _value contains a NULL-reference to XFoo, or a derived type
bValid = true;
else
{
Reference< XInterface > xValue( _value, UNO_QUERY );
Any aTypedValue;
if ( xValue.is() )
// XInterface is not-NULL, but is X(ValueType) not-NULL, too?
xValue.set( xValue->queryInterface( m_aData.m_aValueType ), UNO_QUERY );
bValid = xValue.is();
}
}
break;
case TypeClass_EXCEPTION:
case TypeClass_STRUCT:
case TypeClass_UNION:
{
// values are accepted if and only if their type equals, or is derived from, our value type
if ( _value.getValueTypeClass() != eAllowedTypeClass )
bValid = false;
else
{
const TypeDescription aValueTypeDesc( _value.getValueType() );
const TypeDescription aRequiredTypeDesc( m_aData.m_aValueType );
const _typelib_CompoundTypeDescription* pValueCompoundTypeDesc =
reinterpret_cast< const _typelib_CompoundTypeDescription* >( aValueTypeDesc.get() );
while ( pValueCompoundTypeDesc )
{
if ( typelib_typedescription_equals( &pValueCompoundTypeDesc->aBase, aRequiredTypeDesc.get() ) )
break;
pValueCompoundTypeDesc = pValueCompoundTypeDesc->pBaseTypeDescription;
}
bValid = ( pValueCompoundTypeDesc != NULL );
}
}
break;
}
if ( !bValid )
{
::rtl::OUStringBuffer aMessage;
aMessage.appendAscii( "Incompatible value type. Found '" );
aMessage.append( _value.getValueTypeName() );
aMessage.appendAscii( "', where '" );
aMessage.append( m_aData.m_aValueType.getTypeName() );
aMessage.appendAscii( "' (or compatible type) is expected." );
throw IllegalTypeException( aMessage.makeStringAndClear(), *const_cast< EnumerableMap* >( this ) );
}
impl_checkNaN_throw( _value, m_aData.m_aValueType );
}
//--------------------------------------------------------------------
void EnumerableMap::impl_checkNaN_throw( const Any& _keyOrValue, const Type& _keyOrValueType ) const
{
if ( ( _keyOrValueType.getTypeClass() == TypeClass_DOUBLE )
|| ( _keyOrValueType.getTypeClass() == TypeClass_FLOAT )
)
{
double nValue(0);
if ( _keyOrValue >>= nValue )
if ( ::rtl::math::isNan( nValue ) )
throw IllegalArgumentException(
::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "NaN (not-a-number) not supported by this implementation." ) ),
*const_cast< EnumerableMap* >( this ), 0 );
// (note that the case of _key not containing a float/double value is handled in the
// respective IKeyPredicateLess implementation, so there's no need to handle this here.)
}
}
//--------------------------------------------------------------------
void EnumerableMap::impl_checkKey_throw( const Any& _key ) const
{
if ( !_key.hasValue() )
throw IllegalArgumentException(
::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "NULL keys not supported by this implementation." ) ),
*const_cast< EnumerableMap* >( this ), 0 );
impl_checkNaN_throw( _key, m_aData.m_aKeyType );
}
//--------------------------------------------------------------------
void EnumerableMap::impl_checkMutable_throw() const
{
if ( !m_aData.m_bMutable )
throw NoSupportException(
::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "The map is immutable." ) ),
*const_cast< EnumerableMap* >( this ) );
}
//--------------------------------------------------------------------
Reference< XEnumeration > SAL_CALL EnumerableMap::createKeyEnumeration( ::sal_Bool _Isolated ) throw (NoSupportException, RuntimeException)
{
ComponentMethodGuard aGuard( *this );
return new MapEnumeration( *this, m_aData, getBroadcastHelper(), eKeys, _Isolated );
}
//--------------------------------------------------------------------
Reference< XEnumeration > SAL_CALL EnumerableMap::createValueEnumeration( ::sal_Bool _Isolated ) throw (NoSupportException, RuntimeException)
{
ComponentMethodGuard aGuard( *this );
return new MapEnumeration( *this, m_aData, getBroadcastHelper(), eValues, _Isolated );
}
//--------------------------------------------------------------------
Reference< XEnumeration > SAL_CALL EnumerableMap::createElementEnumeration( ::sal_Bool _Isolated ) throw (NoSupportException, RuntimeException)
{
ComponentMethodGuard aGuard( *this );
return new MapEnumeration( *this, m_aData, getBroadcastHelper(), eBoth, _Isolated );
}
//--------------------------------------------------------------------
Type SAL_CALL EnumerableMap::getKeyType() throw (RuntimeException)
{
ComponentMethodGuard aGuard( *this );
return m_aData.m_aKeyType;
}
//--------------------------------------------------------------------
Type SAL_CALL EnumerableMap::getValueType() throw (RuntimeException)
{
ComponentMethodGuard aGuard( *this );
return m_aData.m_aValueType;
}
//--------------------------------------------------------------------
void SAL_CALL EnumerableMap::clear( ) throw (NoSupportException, RuntimeException)
{
ComponentMethodGuard aGuard( *this );
impl_checkMutable_throw();
m_aData.m_pValues->clear();
lcl_notifyMapDataListeners_nothrow( m_aData );
}
//--------------------------------------------------------------------
::sal_Bool SAL_CALL EnumerableMap::containsKey( const Any& _key ) throw (IllegalTypeException, IllegalArgumentException, RuntimeException)
{
ComponentMethodGuard aGuard( *this );
impl_checkKey_throw( _key );
KeyedValues::const_iterator pos = m_aData.m_pValues->find( _key );
return ( pos != m_aData.m_pValues->end() );
}
//--------------------------------------------------------------------
::sal_Bool SAL_CALL EnumerableMap::containsValue( const Any& _value ) throw (IllegalTypeException, IllegalArgumentException, RuntimeException)
{
ComponentMethodGuard aGuard( *this );
impl_checkValue_throw( _value );
for ( KeyedValues::const_iterator mapping = m_aData.m_pValues->begin();
mapping != m_aData.m_pValues->end();
++mapping
)
{
if ( mapping->second == _value )
return sal_True;
}
return sal_False;
}
//--------------------------------------------------------------------
Any SAL_CALL EnumerableMap::get( const Any& _key ) throw (IllegalTypeException, IllegalArgumentException, NoSuchElementException, RuntimeException)
{
ComponentMethodGuard aGuard( *this );
impl_checkKey_throw( _key );
KeyedValues::const_iterator pos = m_aData.m_pValues->find( _key );
if ( pos == m_aData.m_pValues->end() )
throw NoSuchElementException( anyToString( _key ), *this );
return pos->second;
}
//--------------------------------------------------------------------
Any SAL_CALL EnumerableMap::put( const Any& _key, const Any& _value ) throw (NoSupportException, IllegalTypeException, IllegalArgumentException, RuntimeException)
{
ComponentMethodGuard aGuard( *this );
impl_checkMutable_throw();
impl_checkKey_throw( _key );
impl_checkValue_throw( _value );
Any previousValue;
KeyedValues::iterator pos = m_aData.m_pValues->find( _key );
if ( pos != m_aData.m_pValues->end() )
{
previousValue = pos->second;
pos->second = _value;
}
else
{
(*m_aData.m_pValues)[ _key ] = _value;
}
lcl_notifyMapDataListeners_nothrow( m_aData );
return previousValue;
}
//--------------------------------------------------------------------
Any SAL_CALL EnumerableMap::remove( const Any& _key ) throw (NoSupportException, IllegalTypeException, IllegalArgumentException, NoSuchElementException, RuntimeException)
{
ComponentMethodGuard aGuard( *this );
impl_checkMutable_throw();
impl_checkKey_throw( _key );
Any previousValue;
KeyedValues::iterator pos = m_aData.m_pValues->find( _key );
if ( pos != m_aData.m_pValues->end() )
{
previousValue = pos->second;
m_aData.m_pValues->erase( pos );
}
lcl_notifyMapDataListeners_nothrow( m_aData );
return previousValue;
}
//--------------------------------------------------------------------
Type SAL_CALL EnumerableMap::getElementType() throw (RuntimeException)
{
return ::cppu::UnoType< Pair< Any, Any > >::get();
}
//--------------------------------------------------------------------
::sal_Bool SAL_CALL EnumerableMap::hasElements() throw (RuntimeException)
{
ComponentMethodGuard aGuard( *this );
return m_aData.m_pValues->empty();
}
//--------------------------------------------------------------------
::rtl::OUString SAL_CALL EnumerableMap::getImplementationName( ) throw (RuntimeException)
{
return getImplementationName_static();
}
//--------------------------------------------------------------------
::sal_Bool SAL_CALL EnumerableMap::supportsService( const ::rtl::OUString& _serviceName ) throw (RuntimeException)
{
Sequence< ::rtl::OUString > aServices( getSupportedServiceNames() );
for ( sal_Int32 i=0; i<aServices.getLength(); ++i )
if ( _serviceName == aServices[i] )
return sal_True;
return sal_False;
}
//--------------------------------------------------------------------
Sequence< ::rtl::OUString > SAL_CALL EnumerableMap::getSupportedServiceNames( ) throw (RuntimeException)
{
return getSupportedServiceNames_static();
}
//--------------------------------------------------------------------
::rtl::OUString SAL_CALL EnumerableMap::getImplementationName_static( )
{
return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "org.openoffice.comp.comphelper.EnumerableMap" ) );
}
//--------------------------------------------------------------------
Sequence< ::rtl::OUString > SAL_CALL EnumerableMap::getSupportedServiceNames_static( )
{
Sequence< ::rtl::OUString > aServiceNames(1);
aServiceNames[0] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.container.EnumerableMap" ) );
return aServiceNames;
}
//--------------------------------------------------------------------
Reference< XInterface > SAL_CALL EnumerableMap::Create( const Reference< XComponentContext >& _context )
{
return *new EnumerableMap( ComponentContext( _context ) );
}
//====================================================================
//= MapEnumerator
//====================================================================
//--------------------------------------------------------------------
::sal_Bool MapEnumerator::hasMoreElements()
{
if ( m_disposed )
throw DisposedException( ::rtl::OUString(), m_rParent );
return m_mapPos != m_rMapData.m_pValues->end();
}
//--------------------------------------------------------------------
Any MapEnumerator::nextElement()
{
if ( m_disposed )
throw DisposedException( ::rtl::OUString(), m_rParent );
if ( m_mapPos == m_rMapData.m_pValues->end() )
throw NoSuchElementException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "No more elements." ) ), m_rParent );
Any aNextElement;
switch ( m_eType )
{
case eKeys: aNextElement = m_mapPos->first; break;
case eValues: aNextElement = m_mapPos->second; break;
case eBoth: aNextElement <<= Pair< Any, Any >( m_mapPos->first, m_mapPos->second ); break;
}
++m_mapPos;
return aNextElement;
}
//--------------------------------------------------------------------
void MapEnumerator::mapModified()
{
m_disposed = true;
}
//====================================================================
//= MapEnumeration - implementation
//====================================================================
//--------------------------------------------------------------------
::sal_Bool SAL_CALL MapEnumeration::hasMoreElements( ) throw (RuntimeException)
{
ComponentMethodGuard aGuard( *this );
return m_aEnumerator.hasMoreElements();
}
//--------------------------------------------------------------------
Any SAL_CALL MapEnumeration::nextElement( ) throw (NoSuchElementException, WrappedTargetException, RuntimeException)
{
ComponentMethodGuard aGuard( *this );
return m_aEnumerator.nextElement();
}
//........................................................................
} // namespace comphelper
//........................................................................
void createRegistryInfo_Map()
{
::comphelper::module::OAutoRegistration< ::comphelper::EnumerableMap > aAutoRegistration;
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|