1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
19 #ifndef INCLUDED_CHART2_SOURCE_INC_CONTAINERHELPER_HXX
20 #define INCLUDED_CHART2_SOURCE_INC_CONTAINERHELPER_HXX
28 #include <o3tl/compat_functional.hxx>
32 namespace ContainerHelper
35 /** converts a standard container into a sequence of the same type
37 input: standard container
38 output: css::uno::Sequence< container::value_type >
42 ::std::vector< sal_Int32 > aVector;
43 Sequence< sal_Int32 > aSequence( ContainerHelper::ContainerToSequence( aVector ));
45 template< class Container
>
46 ::com::sun::star::uno::Sequence
< typename
Container::value_type
>
47 ContainerToSequence( const Container
& rCont
)
49 ::com::sun::star::uno::Sequence
< typename
Container::value_type
> aResult( rCont
.size());
50 ::std::copy( rCont
.begin(), rCont
.end(), aResult
.getArray());
54 /** converts a UNO sequence into a standard "Sequence" container. For
55 convenience see the methods SequenceToVector, etc. below.
58 output: a standard container of the same value type implementing the Concept
59 of a Sequence (vector, deque, list, slist)
63 Sequence< sal_Int32 > aSequence;
64 ::std::vector< sal_Int32 > aVector(
65 ContainerToSequence::SequenceToSTLSequenceContainer< ::std::vector< sal_Int32 > >( aSequence );
67 template< class Container
>
69 SequenceToSTLSequenceContainer( const ::com::sun::star::uno::Sequence
< typename
Container::value_type
> & rSeq
)
71 Container
aResult( rSeq
.getLength());
72 ::std::copy( rSeq
.getConstArray(), rSeq
.getConstArray() + rSeq
.getLength(),
77 /** converts a UNO sequence into a standard container. For convenience see the
78 methods SequenceToVector, etc. below. (In contrast to
79 SequenceToSTLSequenceContainer this works for all standard containers)
82 output: a standard container that has an insert( iterator, key ) method (all
84 note: for containers implementing the Concept of a Sequence (vector, deque,
85 list, slist) use SequenceToSTLSequenceContainer for better speed
89 Sequence< sal_Int32 > aSequence;
90 ::std::set< sal_Int32 > aVector(
91 ContainerToSequence::SequenceToSTLContainer< ::std::set< sal_Int32 > >( aSequence );
93 template< class Container
>
95 SequenceToSTLContainer( const ::com::sun::star::uno::Sequence
< typename
Container::value_type
> & rSeq
)
98 ::std::copy( rSeq
.getConstArray(), rSeq
.getConstArray() + rSeq
.getLength(),
99 ::std::inserter
< Container
>( aResult
, aResult
.begin()));
103 // concrete container methods for convenience
105 /** converts a UNO sequence into a standard vector of same value type
109 Sequence< sal_Int32 > aSequence;
110 ::std::vector< sal_Int32 > aVector( ContainerHelper::SequenceToVector( aSequence ));
112 template< typename T
>
114 SequenceToVector( const ::com::sun::star::uno::Sequence
< T
> & rSeq
)
116 return SequenceToSTLSequenceContainer
< ::std::vector
< T
> >( rSeq
);
119 /** converts a UNO sequence into a standard set of same value type
123 Sequence< sal_Int32 > aSequence;
124 ::std::set< sal_Int32 > aVector( ContainerHelper::SequenceToSet( aSequence ));
126 template< typename T
>
128 SequenceToSet( const ::com::sun::star::uno::Sequence
< T
> & rSeq
)
130 return SequenceToSTLContainer
< ::std::set
< T
> >( rSeq
);
133 /** converts the keys of a Pair Associative Container into a UNO sequence
137 ::std::multimap< sal_Int32, OUString > aMyMultiMap;
138 uno::Sequence< sal_Int32 > aMyKeys( ContainerHelper::MapKeysToSequence( aMyMultiMap ));
139 // note: aMyKeys may contain duplicate keys here
141 template< class Map
>
142 ::com::sun::star::uno::Sequence
< typename
Map::key_type
> MapKeysToSequence(
145 ::com::sun::star::uno::Sequence
< typename
Map::key_type
> aResult( rCont
.size());
146 ::std::transform( rCont
.begin(), rCont
.end(), aResult
.getArray(),
147 ::o3tl::select1st
< typename
Map::value_type
>());
151 /** converts the values of a Pair Associative Container into a UNO sequence
155 ::std::map< sal_Int32, OUString > aMyMultiMap;
156 uno::Sequence< OUString > aMyValues( ContainerHelper::MapValuesToSequence( aMyMultiMap ));
158 template< class Map
>
159 ::com::sun::star::uno::Sequence
< typename
Map::mapped_type
> MapValuesToSequence(
162 ::com::sun::star::uno::Sequence
< typename
Map::mapped_type
> aResult( rCont
.size());
163 ::std::transform( rCont
.begin(), rCont
.end(), aResult
.getArray(),
164 ::o3tl::select2nd
< typename
Map::value_type
>());
168 } // namespace ContainerHelper
171 // INCLUDED_CHART2_SOURCE_INC_CONTAINERHELPER_HXX
174 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */