1 /*************************************************************************
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 * Copyright 2008 by Sun Microsystems, Inc.
6 * OpenOffice.org - a multi-platform office productivity suite
8 * $RCSfile: uno_iterators.cxx,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.
28 ************************************************************************/
31 #include <comphelper/stlunosequence.hxx>
32 #include <com/sun/star/uno/Sequence.hxx>
35 #include <rtl/textenc.h>
36 #include <rtl/ustrbuf.hxx>
37 #include <rtl/ustring.hxx>
40 using namespace ::comphelper
;
41 using namespace ::com::sun::star::uno
;
42 using namespace ::rtl
;
43 using namespace ::std
;
45 // some helpers for testing (imperative)
46 void fill_testdata(Sequence
<OUString
>& seq
)
49 for(sal_Int32 i
=0; i
<seq
.getLength(); i
++)
51 buf
.appendAscii("Sampletext ");
53 seq
[i
] = buf
.makeStringAndClear();
57 void print_oustring(const OUString
& ustr
)
58 { cout
<< OUStringToOString(ustr
, RTL_TEXTENCODING_ASCII_US
).getStr() << endl
; }
60 void print_sequence(const Sequence
<OUString
>& seq
)
62 cout
<< "Sequence of " << seq
.getLength() << " OUStrings: " << endl
;
63 for(sal_Int32 i
=0; i
<seq
.getLength(); i
++)
64 print_oustring(seq
[i
]);
68 // some helpers for testing (functional)
69 class TestdataGenerator
72 TestdataGenerator() : m_Index(0) {};
73 OUString
operator()() { return m_Buf
.appendAscii("Sampletext ").append(m_Index
++).makeStringAndClear(); };
78 void fill_testdata_stl(StlUnoSequence
<OUString
>& stl_seq
)
80 generate(stl_seq
.begin(), stl_seq
.end(), TestdataGenerator());
83 void print_sequence_stl(const StlUnoSequence
<OUString
>& stl_seq
)
85 cout
<< "Sequence of " << stl_seq
.size() << " OUStrings: " << endl
;
86 for_each(stl_seq
.begin(), stl_seq
.end(), &print_oustring
);
93 // imperative loops (just to show they work, using for_each would be better most of the time
96 Sequence
<OUString
> s(10);
98 StlUnoSequence
<OUString
>::iterator stl_s_it
;
100 cout
<< "for iteration" << endl
;
101 for(stl_s_it
= stl_begin(s
); stl_s_it
!= stl_end(s
); stl_s_it
++)
102 cout
<< OUStringToOString(*stl_s_it
, RTL_TEXTENCODING_ASCII_US
).getStr() << endl
;
104 cout
<< "reverse for iteration" << endl
;
105 for(stl_s_it
= stl_end(s
); stl_s_it
!= stl_begin(s
); stl_s_it
--)
106 cout
<< OUStringToOString(*(stl_s_it
-1), RTL_TEXTENCODING_ASCII_US
).getStr() << endl
;
108 cout
<< "skipping for iteration" << endl
;
109 for(stl_s_it
= stl_begin(s
); stl_s_it
!= stl_end(s
); stl_s_it
+=2)
110 cout
<< OUStringToOString(*stl_s_it
, RTL_TEXTENCODING_ASCII_US
).getStr() << endl
;
112 cout
<< "skipping reverse for iteration" << endl
;
113 for(stl_s_it
= stl_end(s
); stl_s_it
!= stl_begin(s
); stl_s_it
-=2)
114 std::cout
<< OUStringToOString(*(stl_s_it
-1), RTL_TEXTENCODING_ASCII_US
).getStr() << endl
;
119 Sequence
<OUString
> s(10);
122 random_shuffle(stl_begin(s
), stl_end(s
));
123 cout
<< "shuffed" << std::endl
;
126 sort(stl_begin(s
), stl_end(s
));
127 cout
<< "sorted" << std::endl
;
131 void stl_conversions()
133 Sequence
<OUString
> s(10);
135 StlUnoSequence
<OUString
> stl_s
= StlUnoSequence
<OUString
>::createInstance(s
);
137 // convert to stl::vector, modify in vector, copy back, print
138 cout
<< "vector conversion" << endl
;
139 vector
<OUString
> vec(stl_s
.begin(), stl_s
.end());
140 vec
[2] = OUString::createFromAscii("changed in vector");
141 copy(vec
.begin(), vec
.end(), stl_s
.begin());
144 // convert to stl::list, modify in list, copy back, print
145 cout
<< "list conversion" << endl
;
146 list
<OUString
> l(stl_s
.begin(), stl_s
.end());
148 l
.push_back(OUString::createFromAscii("changed in list"));
149 copy(l
.begin(), l
.end(), stl_s
.begin());
153 // inserts the second half of the second sequence after the first element of the first sequence
156 Sequence
<OUString
> s1(10);
157 Sequence
<OUString
> s2(10);
158 Sequence
<OUString
> result(15);
159 StlUnoSequence
<OUString
> stl_s1
= StlUnoSequence
<OUString
>::createInstance(s1
);
160 StlUnoSequence
<OUString
> stl_s2
= StlUnoSequence
<OUString
>::createInstance(s2
);
161 StlUnoSequence
<OUString
> stl_result
= StlUnoSequence
<OUString
>::createInstance(result
);
165 list
<OUString
> temp(stl_s1
.begin(), stl_s1
.end());
166 copy(stl_s2
.begin()+5, stl_s2
.end(), insert_iterator
<list
<OUString
> >(temp
, ++temp
.begin()));
167 copy(temp
.begin(), temp
.end(), stl_result
.begin());
168 print_sequence(result
);
173 Sequence
<OUString
> s1(10);
174 Sequence
<OUString
> s2(10);
175 StlUnoSequence
<OUString
> stl_s1
= StlUnoSequence
<OUString
>::createInstance(s1
);
176 StlUnoSequence
<OUString
> stl_s2
= StlUnoSequence
<OUString
>::createInstance(s2
);
177 if (stl_s1
== stl_s2
)
178 cout
<< "sequences are equal." << endl
;
179 s2
[9] = OUString::createFromAscii("ZZZZZ");
181 cout
<< "first sequence is smaller." << endl
;
184 void stl_const_sequence()
186 const Sequence
<OUString
> s(10);
187 for(StlUnoSequence
<OUString
>::const_iterator stl_s_it
= stl_begin(s
); stl_s_it
!= stl_end(s
); stl_s_it
++)
188 cout
<< OUStringToOString(*stl_s_it
, RTL_TEXTENCODING_ASCII_US
).getStr() << endl
;
193 Sequence
<OUString
> s(10);
194 StlUnoSequence
<OUString
> stl_s
= StlUnoSequence
<OUString
>::createInstance(s
);
195 fill_testdata_stl(stl_s
);
196 print_sequence_stl(stl_s
);
201 cout
<< "--- CLASSIC LOOPS" << endl
;
204 cout
<< "--- SOME STL ALGORITHMS" << endl
;
207 cout
<< "--- SOME STL CONVERSIONS" << endl
;
210 cout
<< "--- INSERTING IN SEQUENCE" << endl
;
213 cout
<< "--- COMPARING" << endl
;
216 cout
<< "--- CONST SEQUENCE" << endl
;
217 stl_const_sequence();
219 cout
<< "--- HELPERS IN STL-STYLE" << endl
;