LibreOffice Module forms (master) 1
NameContainer.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
24#include <map>
25
26#include <com/sun/star/container/XNameContainer.hpp>
27#include <com/sun/star/container/NoSuchElementException.hpp>
28#include <com/sun/star/lang/IllegalArgumentException.hpp>
29#include <com/sun/star/uno/Any.hxx>
30#include <com/sun/star/uno/Type.hxx>
31#include <osl/diagnose.h>
32
33typedef cppu::WeakImplHelper<
34 css::container::XNameContainer
36
37template<class T>
39{
40 typedef std::map<OUString,T> map_t;
42
43protected:
44 typename map_t::const_iterator findItem( const OUString& rName )
45 {
46 return maItems.find( rName );
47 }
48
49 bool hasItem( const OUString& rName )
50 {
51 return findItem( rName ) != maItems.end();
52 }
53
54 void replace( const OUString& rName,
55 const T& aElement )
56 {
57 OSL_ENSURE( hasItem( rName ), "unknown item" );
58 maItems[ rName ] = aElement;
59 }
60
61 void insert( const OUString& rName,
62 const T& aElement )
63 {
64 OSL_ENSURE( ! hasItem( rName ), "item already in set" );
65 maItems[ rName ] = aElement;
66 }
67
68 void remove( const OUString& rName )
69 {
70 OSL_ENSURE( hasItem( rName ), "item not in set" );
71 maItems.erase( rName );
72 }
73
74
75public:
76
78
79
80 // methods for XElementAccess
81
82
83 virtual css::uno::Type SAL_CALL getElementType() override
84 {
85 return cppu::UnoType<T>::get();
86 }
87
88 virtual sal_Bool SAL_CALL hasElements() override
89 {
90 return ! maItems.empty();
91 }
92
93
94 // methods for XNameAccess (inherits XElementAccess)
95
96
97 virtual css::uno::Any SAL_CALL getByName(
98 const OUString& rName ) override
99 {
100 typename map_t::const_iterator aIter = findItem( rName );
101 if( aIter == maItems.end() )
102 throw css::container::NoSuchElementException();
103 return css::uno::Any( aIter->second );
104 }
105
106 virtual css::uno::Sequence<OUString> SAL_CALL getElementNames() override
107 {
109 }
110
111 virtual sal_Bool SAL_CALL hasByName(
112 const OUString& rName ) override
113 {
114 return hasItem( rName );
115 }
116
117
118 // methods for XNameReplace (inherits XNameAccess)
119
120
121 virtual void SAL_CALL replaceByName(
122 const OUString& rName,
123 const css::uno::Any& aElement ) override
124 {
125 T aItem;
126 if( !(aElement >>= aItem) )
127 throw css::lang::IllegalArgumentException();
128 if( !hasByName( rName ) )
129 throw css::container::NoSuchElementException();
130 replace( rName, aItem );
131 }
132
133
134 // methods for XNameContainer (inherits XNameReplace)
135
136
137 virtual void SAL_CALL insertByName(
138 const OUString& rName,
139 const css::uno::Any& aElement ) override
140 {
141 T aItem;
142 if( !(aElement >>= aItem) )
143 throw css::lang::IllegalArgumentException();
144 if( hasByName( rName ) )
145 throw css::container::ElementExistException();
146 insert( rName, aItem );
147 }
148
149 virtual void SAL_CALL removeByName(
150 const OUString& rName ) override
151 {
152 if( !hasByName( rName ) )
153 throw css::container::NoSuchElementException();
154 remove( rName );
155 }
156
157};
158
159/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
cppu::WeakImplHelper< css::container::XNameContainer > NameContainer_t
virtual void SAL_CALL insertByName(const OUString &rName, const css::uno::Any &aElement) override
virtual void SAL_CALL replaceByName(const OUString &rName, const css::uno::Any &aElement) override
std::map< OUString, T > map_t
virtual css::uno::Any SAL_CALL getByName(const OUString &rName) override
virtual css::uno::Type SAL_CALL getElementType() override
void replace(const OUString &rName, const T &aElement)
virtual sal_Bool SAL_CALL hasElements() override
map_t::const_iterator findItem(const OUString &rName)
virtual void SAL_CALL removeByName(const OUString &rName) override
void insert(const OUString &rName, const T &aElement)
bool hasItem(const OUString &rName)
void remove(const OUString &rName)
virtual css::uno::Sequence< OUString > SAL_CALL getElementNames() override
virtual sal_Bool SAL_CALL hasByName(const OUString &rName) override
css::uno::Type const & get()
css::uno::Sequence< typename M::key_type > mapKeysToSequence(M const &map)
unsigned char sal_Bool