xref: /trunk/main/comphelper/inc/comphelper/InlineContainer.hxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 #ifndef INCLUDED_COMPHELPER_INLINE_CONTAINER_HXX
28 #define INCLUDED_COMPHELPER_INLINE_CONTAINER_HXX
29 
30 #include <com/sun/star/uno/Sequence.hxx>
31 
32 #include <vector>
33 #include <map>
34 #include <set>
35 
36 namespace comphelper
37 {
38 
39 /** Creates a UNO-Sequence which contains an arbitrary number of elements.
40     Notice, that every call of the operator() issues a realloc, so this is not
41     suitable to create very large sequences.
42 
43     usage:
44 
45     uno::Sequence< t >( MakeSequence< t >( t_1 )( t_2 )...( t_n ) );
46  */
47 template < typename T >
48 class MakeSequence : public ::com::sun::star::uno::Sequence< T >
49 {
50 public:
51     explicit MakeSequence(const T &a)
52         : ::com::sun::star::uno::Sequence< T >( 1 )
53     {
54         this->operator[](0) = a;
55     }
56     MakeSequence& operator()(const T &a)
57     {
58         this->realloc( this->getLength() + 1 );
59         this->operator[]( this->getLength() - 1 ) = a;
60         return *this;
61     }
62 };
63 
64 // ----------------------------------------
65 
66 /** Creates a vector which contains an arbitrary number of elements.
67 
68     usage:
69 
70     vector< t > aVec( MakeVector< t >( t_1 )( t_2 )...( t_n ) );
71  */
72 template < typename T >
73 class MakeVector : public ::std::vector< T >
74 {
75 public:
76     explicit MakeVector(const T &a)
77         : ::std::vector< T >(1, a)
78     {
79     }
80     MakeVector &operator()(const T &a)
81     {
82         this->push_back(a);
83         return *this;
84     }
85 };
86 
87 // ----------------------------------------
88 
89 /** Creates a set which contains an arbitrary number of elements.
90 
91     usage:
92 
93     set< t > aSet( MakeSet< t >( t_1 )( t_2 )...( t_n ) );
94  */
95 template < typename T >
96 class MakeSet : public ::std::set< T >
97 {
98 public:
99     explicit MakeSet(const T &a)
100         : ::std::set< T >()
101     {
102         insert(this->end(), a);
103     }
104     MakeSet &operator()(const T &a)
105     {
106         this->insert(this->end(), a);
107         return *this;
108     }
109 };
110 
111 // ----------------------------------------
112 
113 /** usage:
114 
115     map< k, v > aMap( MakeMap< k, v >
116     ( key_1, value_1 )
117     ( key_2, value_2 )
118     ( key_3, value_3 )
119     ...
120     ( key_n, value_n )
121     );
122  */
123 template < typename Key, typename Value >
124 class MakeMap : public ::std::map< Key, Value >
125 {
126 private:
127     typedef typename ::std::map< Key, Value >::value_type value_type;
128 public:
129     explicit MakeMap( const Key &k, const Value &v )
130     {
131         this->insert( value_type( k, v ) );
132     }
133     MakeMap &operator()( const Key &k, const Value &v )
134     {
135         this->insert( value_type( k, v ) );
136         return *this;
137     }
138 
139     MakeMap &operator()( const MakeMap& rSource )
140     {
141         this->insert(rSource.begin(),rSource.end());
142         return *this;
143     }
144 };
145 
146 } // namespace comphelper
147 
148 #endif
149 // INCLUDED_COMPHELPER_INLINE_CONTAINER_HXX
150