initial commit for version 1.6.x patch release
[OpenFOAM-1.6.x.git] / applications / test / PtrList / PtrListTest.C
blob5413a662b889fed52c2c8b6bc218a3315eee3446
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
6      \\/     M anipulation  |
7 -------------------------------------------------------------------------------
8 License
9     This file is part of OpenFOAM.
11     OpenFOAM is free software; you can redistribute it and/or modify it
12     under the terms of the GNU General Public License as published by the
13     Free Software Foundation; either version 2 of the License, or (at your
14     option) any later version.
16     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
19     for more details.
21     You should have received a copy of the GNU General Public License
22     along with OpenFOAM; if not, write to the Free Software Foundation,
23     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 Application
27 Description
29 \*---------------------------------------------------------------------------*/
31 #include "OSspecific.H"
33 #include "scalar.H"
34 #include "IOstreams.H"
35 #include "PtrList.H"
37 using namespace Foam;
39 class Scalar
41     scalar data_;
43 public:
45     Scalar()
46     :
47         data_(0)
48     {}
50     Scalar(scalar val)
51     :
52         data_(val)
53     {}
55     ~Scalar()
56     {
57         Info <<"delete Scalar: " << data_ << endl;
58     }
60     friend Ostream& operator<<(Ostream& os, const Scalar& val)
61     {
62         os << val.data_;
63         return os;
64     }
68 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
69 //  Main program:
71 int main(int argc, char *argv[])
73     PtrList<Scalar> list1(10);
74     PtrList<Scalar> list2(15);
76     forAll(list1, i)
77     {
78         list1.set(i, new Scalar(1.3*i));
79     }
81     forAll(list2, i)
82     {
83         list2.set(i, new Scalar(10 + 1.3*i));
84     }
87     Info<<"list1: " << list1 << endl;
88     Info<<"list2: " << list2 << endl;
90     Info<<"indirectly delete some items via set(.., 0) :" << endl;
91     for (label i = 0; i < 3; i++)
92     {
93         list1.set(i, 0);
94     }
96     Info<<"transfer list2 -> list1:" << endl;
97     list1.transfer(list2);
99     Info<<"list1: " << list1 << endl;
100     Info<<"list2: " << list2 << endl;
102     Info<<"indirectly delete some items via setSize :" << endl;
103     list1.setSize(4);
105     Info<<"list1: " << list1 << endl;
107     PtrList<Scalar> list3(list1.xfer());
108     Info<< "Transferred via the xfer() method" << endl;
110     Info<<"list1: " << list1 << endl;
111     Info<<"list2: " << list2 << endl;
112     Info<<"list3: " << list3 << endl;
114     Info<< nl << "Done." << endl;
115     return 0;
119 // ************************************************************************* //