1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #ifndef _NAMEDCOLLECTION_HXX
25 #define _NAMEDCOLLECTION_HXX
26 
27 #include <collection.hxx>
28 #include <cppuhelper/implbase1.hxx>
29 #include <com/sun/star/container/XNameAccess.hpp>
30 
31 #include <algorithm>
32 
33 template<class T>
34 class NamedCollection : public cppu::ImplInheritanceHelper1<
35                             Collection<T>,
36                             com::sun::star::container::XNameAccess>
37 {
38     using Collection<T>::maItems;
39     using Collection<T>::getItem;
40     using Collection<T>::hasItem;
41 
42 public:
NamedCollection()43     NamedCollection() {}
~NamedCollection()44     virtual ~NamedCollection() {}
45 
getItem(const rtl::OUString & rName) const46     const T& getItem( const rtl::OUString& rName ) const
47     {
48         OSL_ENSURE( hasItem( rName ), "invalid name" );
49         return *findItem( rName );
50     }
51 
hasItem(const rtl::OUString & rName) const52     bool hasItem( const rtl::OUString& rName ) const
53     {
54         return findItem( rName ) != maItems.end();
55     }
56 
57     typedef com::sun::star::uno::Sequence<rtl::OUString> Names_t;
getNames() const58     Names_t getNames() const
59     {
60         // iterate over members, and collect all those that have names
61         std::vector<rtl::OUString> aNames;
62         for( typename std::vector<T>::const_iterator aIter = maItems.begin();
63              aIter != maItems.end();
64              aIter++ )
65         {
66             com::sun::star::uno::Reference<com::sun::star::container::XNamed>
67                 xNamed( *aIter, com::sun::star::uno::UNO_QUERY );
68             if( xNamed.is() )
69                 aNames.push_back( xNamed->getName() );
70         }
71 
72         // copy names to Sequence and return
73         Names_t aResult( aNames.size() );
74         rtl::OUString* pStrings = aResult.getArray();
75         std::copy( aNames.begin(), aNames.end(), pStrings );
76 
77         return aResult;
78     }
79 
80 protected:
findItem(const rtl::OUString & rName) const81     typename std::vector<T>::const_iterator findItem( const rtl::OUString& rName ) const
82     {
83         for( typename std::vector<T>::const_iterator aIter = maItems.begin();
84              aIter != maItems.end();
85              aIter++ )
86         {
87             com::sun::star::uno::Reference<com::sun::star::container::XNamed>
88                 xNamed( *aIter, com::sun::star::uno::UNO_QUERY );
89             if( xNamed.is()  &&  xNamed->getName() == rName )
90                 return aIter;
91         }
92         return maItems.end();
93     }
94 
95 public:
96 
97     // XElementAccess
getElementType()98     virtual typename Collection<T>::Type_t SAL_CALL getElementType()
99         throw( typename Collection<T>::RuntimeException_t )
100     {
101         return Collection<T>::getElementType();
102     }
103 
hasElements()104     virtual sal_Bool SAL_CALL hasElements()
105         throw( typename Collection<T>::RuntimeException_t )
106     {
107         return Collection<T>::hasElements();
108     }
109 
110     // XNameAccess : XElementAccess
getByName(const rtl::OUString & aName)111     virtual typename Collection<T>::Any_t SAL_CALL getByName(
112         const rtl::OUString& aName )
113         throw( typename Collection<T>::NoSuchElementException_t,
114                typename Collection<T>::WrappedTargetException_t,
115                typename Collection<T>::RuntimeException_t )
116     {
117         if( hasItem( aName ) )
118             return com::sun::star::uno::makeAny( getItem( aName ) );
119         else
120             throw typename Collection<T>::NoSuchElementException_t();
121 
122     }
123 
getElementNames()124     virtual Names_t SAL_CALL getElementNames()
125         throw( typename Collection<T>::RuntimeException_t )
126     {
127         return getNames();
128     }
129 
hasByName(const rtl::OUString & aName)130     virtual sal_Bool SAL_CALL hasByName(
131         const rtl::OUString& aName )
132         throw( typename Collection<T>::RuntimeException_t )
133     {
134         return hasItem( aName ) ? sal_True : sal_False;
135     }
136 };
137 
138 #endif
139