1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #ifndef _NAMEDCOLLECTION_HXX
29 #define _NAMEDCOLLECTION_HXX
30 
31 #include <collection.hxx>
32 #include <cppuhelper/implbase1.hxx>
33 #include <com/sun/star/container/XNameAccess.hpp>
34 
35 #include <algorithm>
36 
37 template<class T>
38 class NamedCollection : public cppu::ImplInheritanceHelper1<
39                             Collection<T>,
40                             com::sun::star::container::XNameAccess>
41 {
42     using Collection<T>::maItems;
43     using Collection<T>::getItem;
44     using Collection<T>::hasItem;
45 
46 public:
47     NamedCollection() {}
48     virtual ~NamedCollection() {}
49 
50     const T& getItem( const rtl::OUString& rName ) const
51     {
52         OSL_ENSURE( hasItem( rName ), "invalid name" );
53         return *findItem( rName );
54     }
55 
56     bool hasItem( const rtl::OUString& rName ) const
57     {
58         return findItem( rName ) != maItems.end();
59     }
60 
61     typedef com::sun::star::uno::Sequence<rtl::OUString> Names_t;
62     Names_t getNames() const
63     {
64         // iterate over members, and collect all those that have names
65         std::vector<rtl::OUString> aNames;
66         for( typename std::vector<T>::const_iterator aIter = maItems.begin();
67              aIter != maItems.end();
68              aIter++ )
69         {
70             com::sun::star::uno::Reference<com::sun::star::container::XNamed>
71                 xNamed( *aIter, com::sun::star::uno::UNO_QUERY );
72             if( xNamed.is() )
73                 aNames.push_back( xNamed->getName() );
74         }
75 
76         // copy names to Sequence and return
77         Names_t aResult( aNames.size() );
78         rtl::OUString* pStrings = aResult.getArray();
79         std::copy( aNames.begin(), aNames.end(), pStrings );
80 
81         return aResult;
82     }
83 
84 protected:
85     typename std::vector<T>::const_iterator findItem( const rtl::OUString& rName ) const
86     {
87         for( typename std::vector<T>::const_iterator aIter = maItems.begin();
88              aIter != maItems.end();
89              aIter++ )
90         {
91             com::sun::star::uno::Reference<com::sun::star::container::XNamed>
92                 xNamed( *aIter, com::sun::star::uno::UNO_QUERY );
93             if( xNamed.is()  &&  xNamed->getName() == rName )
94                 return aIter;
95         }
96         return maItems.end();
97     }
98 
99 public:
100 
101     // XElementAccess
102     virtual typename Collection<T>::Type_t SAL_CALL getElementType()
103         throw( typename Collection<T>::RuntimeException_t )
104     {
105         return Collection<T>::getElementType();
106     }
107 
108     virtual sal_Bool SAL_CALL hasElements()
109         throw( typename Collection<T>::RuntimeException_t )
110     {
111         return Collection<T>::hasElements();
112     }
113 
114     // XNameAccess : XElementAccess
115     virtual typename Collection<T>::Any_t SAL_CALL getByName(
116         const rtl::OUString& aName )
117         throw( typename Collection<T>::NoSuchElementException_t,
118                typename Collection<T>::WrappedTargetException_t,
119                typename Collection<T>::RuntimeException_t )
120     {
121         if( hasItem( aName ) )
122             return com::sun::star::uno::makeAny( getItem( aName ) );
123         else
124             throw typename Collection<T>::NoSuchElementException_t();
125 
126     }
127 
128     virtual Names_t SAL_CALL getElementNames()
129         throw( typename Collection<T>::RuntimeException_t )
130     {
131         return getNames();
132     }
133 
134     virtual sal_Bool SAL_CALL hasByName(
135         const rtl::OUString& aName )
136         throw( typename Collection<T>::RuntimeException_t )
137     {
138         return hasItem( aName ) ? sal_True : sal_False;
139     }
140 };
141 
142 #endif
143