LibreOffice Module forms (master) 1
namedcollection.hxx
Go to the documentation of this file.
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#pragma once
21
22#include "collection.hxx"
25#include <com/sun/star/container/XNameAccess.hpp>
26#include <com/sun/star/container/XNamed.hpp>
27
28#include <algorithm>
29
30template<class T>
31class NamedCollection : public cppu::ImplInheritanceHelper<
32 Collection<T>,
33 css::container::XNameAccess>
34{
38
39public:
40
41 const T& getItem( const OUString& rName ) const
42 {
43 OSL_ENSURE( hasItem( rName ), "invalid name" );
44 return *findItem( rName );
45 }
46
47 bool hasItem( const OUString& rName ) const
48 {
49 return findItem( rName ) != maItems.end();
50 }
51
52 css::uno::Sequence<OUString> getNames() const
53 {
54 // iterate over members, and collect all those that have names
55 std::vector<OUString> aNames;
56 for( const T& rItem : maItems )
57 {
58 css::uno::Reference<css::container::XNamed>
59 xNamed( rItem, css::uno::UNO_QUERY );
60 if( xNamed.is() )
61 aNames.push_back( xNamed->getName() );
62 }
63
65 }
66
67protected:
68 typename std::vector<T>::const_iterator findItem( const OUString& rName ) const
69 {
70 return std::find_if(maItems.begin(), maItems.end(), [&rName](const T& rItem) {
71 css::uno::Reference<css::container::XNamed>
72 xNamed( rItem, css::uno::UNO_QUERY );
73 return xNamed.is() && xNamed->getName() == rName;
74 });
75 }
76
77public:
78
79 // XElementAccess
80 virtual css::uno::Type SAL_CALL getElementType() override
81 {
83 }
84
85 virtual sal_Bool SAL_CALL hasElements() override
86 {
88 }
89
90 // XNameAccess : XElementAccess
91 virtual css::uno::Any SAL_CALL getByName(
92 const OUString& aName ) override
93 {
94 if( !hasItem( aName ) )
95 throw css::container::NoSuchElementException();
96 return css::uno::Any( getItem( aName ) );
97 }
98
99 virtual css::uno::Sequence<OUString> SAL_CALL getElementNames() override
100 {
101 return getNames();
102 }
103
104 virtual sal_Bool SAL_CALL hasByName(
105 const OUString& aName ) override
106 {
107 return hasItem( aName );
108 }
109};
110
111/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual sal_Bool SAL_CALL hasElements() override
Definition: collection.hxx:149
virtual css::uno::Type SAL_CALL getElementType() override
Definition: collection.hxx:144
virtual css::uno::Type SAL_CALL getElementType() override
virtual css::uno::Any SAL_CALL getByName(const OUString &aName) override
const T & getItem(const OUString &rName) const
virtual sal_Bool SAL_CALL hasElements() override
std::vector< T >::const_iterator findItem(const OUString &rName) const
virtual sal_Bool SAL_CALL hasByName(const OUString &aName) override
bool hasItem(const OUString &rName) const
css::uno::Sequence< OUString > getNames() const
virtual css::uno::Sequence< OUString > SAL_CALL getElementNames() override
OUString aName
css::uno::Sequence< DstElementType > containerToSequence(const SrcType &i_Container)
unsigned char sal_Bool