LibreOffice Module comphelper (master)  1
IndexedPropertyValuesContainer.cxx
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 #include <com/sun/star/container/XIndexContainer.hpp>
21 #include <com/sun/star/uno/Sequence.h>
22 #include <com/sun/star/beans/PropertyValue.hpp>
23 #include <cppuhelper/implbase.hxx>
24 #include <com/sun/star/lang/IllegalArgumentException.hpp>
25 #include <com/sun/star/lang/IndexOutOfBoundsException.hpp>
26 #include <com/sun/star/lang/XServiceInfo.hpp>
28 
29 #include <vector>
30 
31 namespace com::sun::star::uno { class XComponentContext; }
32 
33 using namespace com::sun::star;
34 
35 typedef std::vector < uno::Sequence< beans::PropertyValue > > IndexedPropertyValues;
36 
37 namespace {
38 
39 class IndexedPropertyValuesContainer : public cppu::WeakImplHelper< container::XIndexContainer, lang::XServiceInfo >
40 {
41 public:
42  IndexedPropertyValuesContainer() throw();
43 
44  // XIndexContainer
45  virtual void SAL_CALL insertByIndex( sal_Int32 nIndex, const css::uno::Any& aElement ) override;
46  virtual void SAL_CALL removeByIndex( sal_Int32 nIndex ) override;
47 
48  // XIndexReplace
49  virtual void SAL_CALL replaceByIndex( sal_Int32 nIndex, const css::uno::Any& aElement ) override;
50 
51  // XIndexAccess
52  virtual sal_Int32 SAL_CALL getCount( ) override;
53  virtual css::uno::Any SAL_CALL getByIndex( sal_Int32 nIndex ) override;
54 
55  // XElementAccess
56  virtual css::uno::Type SAL_CALL getElementType( ) override;
57  virtual sal_Bool SAL_CALL hasElements( ) override;
58 
59  //XServiceInfo
60  virtual OUString SAL_CALL getImplementationName( ) override;
61  virtual sal_Bool SAL_CALL supportsService( const OUString& ServiceName ) override;
62  virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames( ) override;
63 
64 private:
66 };
67 
68 }
69 
70 IndexedPropertyValuesContainer::IndexedPropertyValuesContainer() throw()
71 {
72 }
73 
74 // XIndexContainer
75 void SAL_CALL IndexedPropertyValuesContainer::insertByIndex( sal_Int32 nIndex, const css::uno::Any& aElement )
76 {
77  sal_Int32 nSize(maProperties.size());
78  if ((nSize < nIndex) || (nIndex < 0))
79  throw lang::IndexOutOfBoundsException();
80 
81  uno::Sequence<beans::PropertyValue> aProps;
82  if (!(aElement >>= aProps))
83  throw lang::IllegalArgumentException();
84  if (nSize == nIndex)
85  maProperties.push_back(aProps);
86  else
87  maProperties.insert(maProperties.begin() + nIndex, aProps);
88 }
89 
90 void SAL_CALL IndexedPropertyValuesContainer::removeByIndex( sal_Int32 nIndex )
91 {
92  if ((nIndex >= sal_Int32(maProperties.size())) || (nIndex < 0))
93  throw lang::IndexOutOfBoundsException();
94 
95  maProperties.erase(maProperties.begin() + nIndex);
96 }
97 
98 // XIndexReplace
99 void SAL_CALL IndexedPropertyValuesContainer::replaceByIndex( sal_Int32 nIndex, const css::uno::Any& aElement )
100 {
101  sal_Int32 nSize(maProperties.size());
102  if ((nIndex >= nSize) || (nIndex < 0))
103  throw lang::IndexOutOfBoundsException();
104 
105  uno::Sequence<beans::PropertyValue> aProps;
106  if (!(aElement >>= aProps))
107  throw lang::IllegalArgumentException();
108  maProperties[nIndex] = aProps;
109 }
110 
111 // XIndexAccess
112 sal_Int32 SAL_CALL IndexedPropertyValuesContainer::getCount( )
113 {
114  return maProperties.size();
115 }
116 
117 css::uno::Any SAL_CALL IndexedPropertyValuesContainer::getByIndex( sal_Int32 nIndex )
118 {
119  sal_Int32 nSize(maProperties.size());
120  if ((nIndex >= nSize) || (nIndex < 0))
121  throw lang::IndexOutOfBoundsException();
122 
123  return uno::Any( maProperties[nIndex] );
124 }
125 
126 // XElementAccess
127 css::uno::Type SAL_CALL IndexedPropertyValuesContainer::getElementType( )
128 {
130 }
131 
132 sal_Bool SAL_CALL IndexedPropertyValuesContainer::hasElements( )
133 {
134  return !maProperties.empty();
135 }
136 
137 //XServiceInfo
138 OUString SAL_CALL IndexedPropertyValuesContainer::getImplementationName( )
139 {
140  return "IndexedPropertyValuesContainer";
141 }
142 
143 sal_Bool SAL_CALL IndexedPropertyValuesContainer::supportsService( const OUString& ServiceName )
144 {
145  return cppu::supportsService(this, ServiceName);
146 }
147 
148 css::uno::Sequence< OUString > SAL_CALL IndexedPropertyValuesContainer::getSupportedServiceNames( )
149 {
150  return { "com.sun.star.document.IndexedPropertyValues" };
151 }
152 
153 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
155  css::uno::XComponentContext *,
156  css::uno::Sequence<css::uno::Any> const &)
157 {
158  return cppu::acquire(new IndexedPropertyValuesContainer());
159 }
160 
161 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * IndexedPropertyValuesContainer_get_implementation(css::uno::XComponentContext *, css::uno::Sequence< css::uno::Any > const &)
sal_Int32 nIndex
std::vector< uno::Sequence< beans::PropertyValue > > IndexedPropertyValues
OUString getImplementationName()
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
SvGenericNameContainerMapImpl maProperties
css::uno::Sequence< OUString > getSupportedServiceNames()
unsigned char sal_Bool