1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 #ifndef INCLUDED_COMPHELPER_INLINE_CONTAINER_HXX
24 #define INCLUDED_COMPHELPER_INLINE_CONTAINER_HXX
25 
26 #include <com/sun/star/uno/Sequence.hxx>
27 
28 #include <vector>
29 #include <map>
30 #include <set>
31 
32 namespace comphelper
33 {
34 
35 /** Creates a UNO-Sequence which contains an arbitrary number of elements.
36     Notice, that every call of the operator() issues a realloc, so this is not
37     suitable to create very large sequences.
38 
39     usage:
40 
41     uno::Sequence< t >( MakeSequence< t >( t_1 )( t_2 )...( t_n ) );
42  */
43 template < typename T >
44 class MakeSequence : public ::com::sun::star::uno::Sequence< T >
45 {
46 public:
MakeSequence(const T & a)47     explicit MakeSequence(const T &a)
48         : ::com::sun::star::uno::Sequence< T >( 1 )
49     {
50         this->operator[](0) = a;
51     }
operator ()(const T & a)52     MakeSequence& operator()(const T &a)
53     {
54         this->realloc( this->getLength() + 1 );
55         this->operator[]( this->getLength() - 1 ) = a;
56         return *this;
57     }
58 };
59 
60 // ----------------------------------------
61 
62 /** Creates a vector which contains an arbitrary number of elements.
63 
64     usage:
65 
66     vector< t > aVec( MakeVector< t >( t_1 )( t_2 )...( t_n ) );
67  */
68 template < typename T >
69 class MakeVector : public ::std::vector< T >
70 {
71 public:
MakeVector(const T & a)72     explicit MakeVector(const T &a)
73         : ::std::vector< T >(1, a)
74     {
75     }
operator ()(const T & a)76     MakeVector &operator()(const T &a)
77     {
78         this->push_back(a);
79         return *this;
80     }
81 };
82 
83 // ----------------------------------------
84 
85 /** Creates a set which contains an arbitrary number of elements.
86 
87     usage:
88 
89     set< t > aSet( MakeSet< t >( t_1 )( t_2 )...( t_n ) );
90  */
91 template < typename T >
92 class MakeSet : public ::std::set< T >
93 {
94 public:
MakeSet(const T & a)95     explicit MakeSet(const T &a)
96         : ::std::set< T >()
97     {
98         this->insert(this->end(), a);
99     }
operator ()(const T & a)100     MakeSet &operator()(const T &a)
101     {
102         this->insert(this->end(), a);
103         return *this;
104     }
105 };
106 
107 // ----------------------------------------
108 
109 /** usage:
110 
111     map< k, v > aMap( MakeMap< k, v >
112     ( key_1, value_1 )
113     ( key_2, value_2 )
114     ( key_3, value_3 )
115     ...
116     ( key_n, value_n )
117     );
118  */
119 template < typename Key, typename Value >
120 class MakeMap : public ::std::map< Key, Value >
121 {
122 private:
123     typedef typename ::std::map< Key, Value >::value_type value_type;
124 public:
MakeMap(const Key & k,const Value & v)125     explicit MakeMap( const Key &k, const Value &v )
126     {
127         this->insert( value_type( k, v ) );
128     }
operator ()(const Key & k,const Value & v)129     MakeMap &operator()( const Key &k, const Value &v )
130     {
131         this->insert( value_type( k, v ) );
132         return *this;
133     }
134 
operator ()(const MakeMap & rSource)135     MakeMap &operator()( const MakeMap& rSource )
136     {
137         this->insert(rSource.begin(),rSource.end());
138         return *this;
139     }
140 };
141 
142 } // namespace comphelper
143 
144 #endif
145 // INCLUDED_COMPHELPER_INLINE_CONTAINER_HXX
146