1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: ContainerHelper.hxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
30 #ifndef CHART2_CONTAINERHELPER_HXX
31 #define CHART2_CONTAINERHELPER_HXX
42 namespace ContainerHelper
45 /** converts a standard container into a sequence of the same type
47 input: standard container
48 output: css::uno::Sequence< container::value_type >
52 ::std::vector< sal_Int32 > aVector;
53 Sequence< sal_Int32 > aSequence( ContainerHelper::ContainerToSequence( aVector ));
55 template< class Container
>
56 ::com::sun::star::uno::Sequence
< typename
Container::value_type
>
57 ContainerToSequence( const Container
& rCont
)
59 ::com::sun::star::uno::Sequence
< typename
Container::value_type
> aResult( rCont
.size());
60 ::std::copy( rCont
.begin(), rCont
.end(), aResult
.getArray());
64 /** converts a UNO sequence into a standard "Sequence" container. For
65 convenience see the methods SequenceToVector, etc. below.
68 output: a standard container of the same value type implementing the Concept
69 of a Sequence (vector, deque, list, slist)
73 Sequence< sal_Int32 > aSequence;
74 ::std::vector< sal_Int32 > aVector(
75 ContainerToSequence::SequenceToSTLSequenceContainer< ::std::vector< sal_Int32 > >( aSequence );
77 template< class Container
>
79 SequenceToSTLSequenceContainer( const ::com::sun::star::uno::Sequence
< typename
Container::value_type
> & rSeq
)
81 Container
aResult( rSeq
.getLength());
82 ::std::copy( rSeq
.getConstArray(), rSeq
.getConstArray() + rSeq
.getLength(),
87 /** converts a UNO sequence into a standard container. For convenience see the
88 methods SequenceToVector, etc. below. (In contrast to
89 SequenceToSTLSequenceContainer this works for all standard containers)
92 output: a standard container that has an insert( iterator, key ) method (all
94 note: for containers implementing the Concept of a Sequence (vector, deque,
95 list, slist) use SequenceToSTLSequenceContainer for better speed
99 Sequence< sal_Int32 > aSequence;
100 ::std::set< sal_Int32 > aVector(
101 ContainerToSequence::SequenceToSTLContainer< ::std::set< sal_Int32 > >( aSequence );
103 template< class Container
>
105 SequenceToSTLContainer( const ::com::sun::star::uno::Sequence
< typename
Container::value_type
> & rSeq
)
108 ::std::copy( rSeq
.getConstArray(), rSeq
.getConstArray() + rSeq
.getLength(),
109 ::std::inserter
< Container
>( aResult
, aResult
.begin()));
113 // concrete container methods for convenience
115 /** converts a UNO sequence into a standard vector of same value type
119 Sequence< sal_Int32 > aSequence;
120 ::std::vector< sal_Int32 > aVector( ContainerHelper::SequenceToVector( aSequence ));
122 template< typename T
>
124 SequenceToVector( const ::com::sun::star::uno::Sequence
< T
> & rSeq
)
126 return SequenceToSTLSequenceContainer
< ::std::vector
< T
> >( rSeq
);
129 /** converts a UNO sequence into a standard set of same value type
133 Sequence< sal_Int32 > aSequence;
134 ::std::set< sal_Int32 > aVector( ContainerHelper::SequenceToSet( aSequence ));
136 template< typename T
>
138 SequenceToSet( const ::com::sun::star::uno::Sequence
< T
> & rSeq
)
140 return SequenceToSTLContainer
< ::std::set
< T
> >( rSeq
);
143 // ----------------------------------------
145 /** converts the keys of a Pair Associative Container into a UNO sequence
149 ::std::multimap< sal_Int32, ::rtl::OUString > aMyMultiMap;
150 uno::Sequence< sal_Int32 > aMyKeys( ContainerHelper::MapKeysToSequence( aMyMultiMap ));
151 // note: aMyKeys may contain duplicate keys here
153 template< class Map
>
154 ::com::sun::star::uno::Sequence
< typename
Map::key_type
> MapKeysToSequence(
157 ::com::sun::star::uno::Sequence
< typename
Map::key_type
> aResult( rCont
.size());
158 ::std::transform( rCont
.begin(), rCont
.end(), aResult
.getArray(),
159 ::std::select1st
< typename
Map::value_type
>());
163 /** converts the values of a Pair Associative Container into a UNO sequence
167 ::std::map< sal_Int32, ::rtl::OUString > aMyMultiMap;
168 uno::Sequence< ::rtl::OUString > aMyValues( ContainerHelper::MapValuesToSequence( aMyMultiMap ));
170 template< class Map
>
171 ::com::sun::star::uno::Sequence
< typename
Map::mapped_type
> MapValuesToSequence(
174 ::com::sun::star::uno::Sequence
< typename
Map::mapped_type
> aResult( rCont
.size());
175 ::std::transform( rCont
.begin(), rCont
.end(), aResult
.getArray(),
176 ::std::select2nd
< typename
Map::value_type
>());
180 } // namespace ContainerHelper
183 // CHART2_CONTAINERHELPER_HXX