Branch data Line data Source code
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 : : #ifndef _NAMEDCOLLECTION_HXX
21 : : #define _NAMEDCOLLECTION_HXX
22 : :
23 : : #include <collection.hxx>
24 : : #include <cppuhelper/implbase1.hxx>
25 : : #include <com/sun/star/container/XNameAccess.hpp>
26 : :
27 : : #include <algorithm>
28 : :
29 : : template<class T>
30 : : class NamedCollection : public cppu::ImplInheritanceHelper1<
31 : : Collection<T>,
32 : : com::sun::star::container::XNameAccess>
33 : : {
34 : : using Collection<T>::maItems;
35 : : using Collection<T>::getItem;
36 : : using Collection<T>::hasItem;
37 : :
38 : : public:
39 : 0 : NamedCollection() {}
40 [ # # ]: 0 : virtual ~NamedCollection() {}
41 : :
42 : 0 : const T& getItem( const rtl::OUString& rName ) const
43 : : {
44 : : OSL_ENSURE( hasItem( rName ), "invalid name" );
45 : 0 : return *findItem( rName );
46 : : }
47 : :
48 : 0 : bool hasItem( const rtl::OUString& rName ) const
49 : : {
50 [ # # ][ # # ]: 0 : return findItem( rName ) != maItems.end();
51 : : }
52 : :
53 : : typedef com::sun::star::uno::Sequence<rtl::OUString> Names_t;
54 : 0 : Names_t getNames() const
55 : : {
56 : : // iterate over members, and collect all those that have names
57 [ # # ]: 0 : std::vector<rtl::OUString> aNames;
58 [ # # ][ # # ]: 0 : for( typename std::vector<T>::const_iterator aIter = maItems.begin();
59 : : aIter != maItems.end();
60 : : ++aIter )
61 : : {
62 : : com::sun::star::uno::Reference<com::sun::star::container::XNamed>
63 [ # # ]: 0 : xNamed( *aIter, com::sun::star::uno::UNO_QUERY );
64 [ # # ]: 0 : if( xNamed.is() )
65 [ # # ][ # # ]: 0 : aNames.push_back( xNamed->getName() );
[ # # ]
66 : : }
67 : :
68 : : // copy names to Sequence and return
69 [ # # ]: 0 : Names_t aResult( aNames.size() );
70 [ # # ]: 0 : rtl::OUString* pStrings = aResult.getArray();
71 [ # # ]: 0 : std::copy( aNames.begin(), aNames.end(), pStrings );
72 : :
73 : 0 : return aResult;
74 : : }
75 : :
76 : : protected:
77 : 0 : typename std::vector<T>::const_iterator findItem( const rtl::OUString& rName ) const
78 : : {
79 [ # # ][ # # ]: 0 : for( typename std::vector<T>::const_iterator aIter = maItems.begin();
80 : : aIter != maItems.end();
81 : : ++aIter )
82 : : {
83 : : com::sun::star::uno::Reference<com::sun::star::container::XNamed>
84 [ # # ]: 0 : xNamed( *aIter, com::sun::star::uno::UNO_QUERY );
85 [ # # ][ # # ]: 0 : if( xNamed.is() && xNamed->getName() == rName )
[ # # ][ # # ]
[ # # ]
[ # # # # ]
86 [ # # ]: 0 : return aIter;
87 : : }
88 : 0 : return maItems.end();
89 : : }
90 : :
91 : : public:
92 : :
93 : : // XElementAccess
94 : 0 : virtual typename Collection<T>::Type_t SAL_CALL getElementType()
95 : : throw( typename Collection<T>::RuntimeException_t )
96 : : {
97 : 0 : return Collection<T>::getElementType();
98 : : }
99 : :
100 : 0 : virtual sal_Bool SAL_CALL hasElements()
101 : : throw( typename Collection<T>::RuntimeException_t )
102 : : {
103 : 0 : return Collection<T>::hasElements();
104 : : }
105 : :
106 : : // XNameAccess : XElementAccess
107 : 0 : virtual typename Collection<T>::Any_t SAL_CALL getByName(
108 : : const rtl::OUString& aName )
109 : : throw( typename Collection<T>::NoSuchElementException_t,
110 : : typename Collection<T>::WrappedTargetException_t,
111 : : typename Collection<T>::RuntimeException_t )
112 : : {
113 [ # # ]: 0 : if( hasItem( aName ) )
114 : 0 : return com::sun::star::uno::makeAny( getItem( aName ) );
115 : : else
116 [ # # ]: 0 : throw typename Collection<T>::NoSuchElementException_t();
117 : :
118 : : }
119 : :
120 : 0 : virtual Names_t SAL_CALL getElementNames()
121 : : throw( typename Collection<T>::RuntimeException_t )
122 : : {
123 : 0 : return getNames();
124 : : }
125 : :
126 : 0 : virtual sal_Bool SAL_CALL hasByName(
127 : : const rtl::OUString& aName )
128 : : throw( typename Collection<T>::RuntimeException_t )
129 : : {
130 [ # # ]: 0 : return hasItem( aName ) ? sal_True : sal_False;
131 : : }
132 : : };
133 : :
134 : : #endif
135 : :
136 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|