/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ /************************************************************************* * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * Copyright 2000, 2010 Oracle and/or its affiliates. * * OpenOffice.org - a multi-platform office productivity suite * * This file is part of OpenOffice.org. * * OpenOffice.org is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License version 3 * only, as published by the Free Software Foundation. * * OpenOffice.org is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License version 3 for more details * (a copy is included in the LICENSE file that accompanied this code). * * You should have received a copy of the GNU Lesser General Public License * version 3 along with OpenOffice.org. If not, see * * for a copy of the LGPLv3 License. * ************************************************************************/ #include #include #include #include #include #include #include #include #include using namespace ::comphelper; using namespace ::com::sun::star::uno; using namespace ::rtl; using namespace ::std; // some helpers for testing (imperative) void fill_testdata(Sequence& seq) { OUStringBuffer buf; for(sal_Int32 i=0; i& seq) { cout << "Sequence of " << seq.getLength() << " OUStrings: " << endl; for(sal_Int32 i=0; i& stl_seq) { generate(stl_seq.begin(), stl_seq.end(), TestdataGenerator()); } void print_sequence_stl(const StlUnoSequence& stl_seq) { cout << "Sequence of " << stl_seq.size() << " OUStrings: " << endl; for_each(stl_seq.begin(), stl_seq.end(), &print_oustring); cout << endl; } // code samples // imperative loops (just to show they work, using for_each would be better most of the time void classic_loops() { Sequence s(10); fill_testdata(s); StlUnoSequence::iterator stl_s_it; cout << "for iteration" << endl; for(stl_s_it = stl_begin(s); stl_s_it != stl_end(s); ++stl_s_it) cout << OUStringToOString(*stl_s_it, RTL_TEXTENCODING_ASCII_US).getStr() << endl; cout << "reverse for iteration" << endl; for(stl_s_it = stl_end(s); stl_s_it != stl_begin(s); --stl_s_it) cout << OUStringToOString(*(stl_s_it-1), RTL_TEXTENCODING_ASCII_US).getStr() << endl; cout << "skipping for iteration" << endl; for(stl_s_it = stl_begin(s); stl_s_it != stl_end(s); stl_s_it+=2) cout << OUStringToOString(*stl_s_it, RTL_TEXTENCODING_ASCII_US).getStr() << endl; cout << "skipping reverse for iteration" << endl; for(stl_s_it = stl_end(s); stl_s_it != stl_begin(s); stl_s_it-=2) std::cout << OUStringToOString(*(stl_s_it-1), RTL_TEXTENCODING_ASCII_US).getStr() << endl; } void stl_algos() { Sequence s(10); fill_testdata(s); random_shuffle(stl_begin(s), stl_end(s)); cout << "shuffed" << std::endl; print_sequence(s); sort(stl_begin(s), stl_end(s)); cout << "sorted" << std::endl; print_sequence(s); } void stl_conversions() { Sequence s(10); fill_testdata(s); StlUnoSequence stl_s = StlUnoSequence::createInstance(s); // convert to stl::vector, modify in vector, copy back, print cout << "vector conversion" << endl; vector vec(stl_s.begin(), stl_s.end()); vec[2] = OUString( RTL_CONSTASCII_USTRINGPARAM( "changed in vector" )); copy(vec.begin(), vec.end(), stl_s.begin()); print_sequence(s); // convert to stl::list, modify in list, copy back, print cout << "list conversion" << endl; list l(stl_s.begin(), stl_s.end()); l.pop_back(); l.push_back(OUString( RTL_CONSTASCII_USTRINGPARAM( "changed in list" ))); copy(l.begin(), l.end(), stl_s.begin()); print_sequence(s); } // inserts the second half of the second sequence after the first element of the first sequence void stl_inserting() { Sequence s1(10); Sequence s2(10); Sequence result(15); StlUnoSequence stl_s1 = StlUnoSequence::createInstance(s1); StlUnoSequence stl_s2 = StlUnoSequence::createInstance(s2); StlUnoSequence stl_result = StlUnoSequence::createInstance(result); fill_testdata(s1); fill_testdata(s2); list temp(stl_s1.begin(), stl_s1.end()); copy(stl_s2.begin()+5, stl_s2.end(), insert_iterator >(temp, ++temp.begin())); copy(temp.begin(), temp.end(), stl_result.begin()); print_sequence(result); } void stl_compare() { Sequence s1(10); Sequence s2(10); StlUnoSequence stl_s1 = StlUnoSequence::createInstance(s1); StlUnoSequence stl_s2 = StlUnoSequence::createInstance(s2); if (stl_s1 == stl_s2) cout << "sequences are equal." << endl; s2[9] = OUString( RTL_CONSTASCII_USTRINGPARAM( "ZZZZZ" )); if(stl_s1 < stl_s2) cout << "first sequence is smaller." << endl; } void stl_const_sequence() { const Sequence s(10); for(StlUnoSequence::const_iterator stl_s_it = stl_begin(s); stl_s_it != stl_end(s); ++stl_s_it) cout << OUStringToOString(*stl_s_it, RTL_TEXTENCODING_ASCII_US).getStr() << endl; } void stl_helpers() { Sequence s(10); StlUnoSequence stl_s = StlUnoSequence::createInstance(s); fill_testdata_stl(stl_s); print_sequence_stl(stl_s); } int main() { cout << "--- CLASSIC LOOPS" << endl; classic_loops(); cout << "--- SOME STL ALGORITHMS" << endl; stl_algos(); cout << "--- SOME STL CONVERSIONS" << endl; stl_conversions(); cout << "--- INSERTING IN SEQUENCE" << endl; stl_inserting(); cout << "--- COMPARING" << endl; stl_compare(); cout << "--- CONST SEQUENCE" << endl; stl_const_sequence(); cout << "--- HELPERS IN STL-STYLE" << endl; stl_helpers(); } /* vim:set shiftwidth=4 softtabstop=4 expandtab: */