Test-PtrList.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | Copyright (C) 2011 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
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
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
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.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 Application
25 
26 Description
27 
28 \*---------------------------------------------------------------------------*/
29 
30 #include "OSspecific.H"
31 
32 #include "scalar.H"
33 #include "IOstreams.H"
34 #include "PtrList.H"
35 #include "plane.H"
36 #include "DynamicList.H"
37 
38 using namespace Foam;
39 
40 class Scalar
41 {
42  scalar data_;
43 
44 public:
45 
46  Scalar()
47  :
48  data_(0)
49  {}
50 
51  Scalar(scalar val)
52  :
53  data_(val)
54  {}
55 
56  ~Scalar()
57  {
58  Info<<"delete Scalar: " << data_ << endl;
59  }
60 
61  autoPtr<Scalar> clone() const
62  {
63  return autoPtr<Scalar>(new Scalar(data_));
64  }
65 
66  friend Ostream& operator<<(Ostream& os, const Scalar& val)
67  {
68  os << val.data_;
69  return os;
70  }
71 };
72 
73 
74 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
75 // Main program:
76 
77 int main(int argc, char *argv[])
78 {
79  PtrList<Scalar> list1(10);
80  PtrList<Scalar> list2(15);
81  PtrList<Scalar> listApp;
82 
83  forAll(list1, i)
84  {
85  list1.set(i, new Scalar(1.3*i));
86  }
87 
88  forAll(list2, i)
89  {
90  list2.set(i, new Scalar(10 + 1.3*i));
91  }
92 
93  for (label i = 0; i < 5; ++i)
94  {
95  listApp.append(new Scalar(1.3*i));
96  }
97 
98  Info<<"list1: " << list1 << endl;
99  Info<<"list2: " << list2 << endl;
100  Info<<"listApp: " << listApp << endl;
101 
102  Info<<"indirectly delete some items via set(.., 0) :" << endl;
103  for (label i = 0; i < 3; i++)
104  {
105  list1.set(i, 0);
106  }
107 
108  Info<<"transfer list2 -> list1:" << endl;
109  list1.transfer(list2);
110 
111  Info<<"list1: " << list1 << endl;
112  Info<<"list2: " << list2 << endl;
113 
114  Info<<"indirectly delete some items via setSize :" << endl;
115  list1.setSize(4);
116 
117  Info<<"list1: " << list1 << endl;
118 
119  PtrList<Scalar> list3(list1.xfer());
120  Info<< "Transferred via the xfer() method" << endl;
121 
122  Info<<"list1: " << list1 << endl;
123  Info<<"list2: " << list2 << endl;
124  Info<<"list3: " << list3 << endl;
125 
126  PtrList<plane> planes;
127  planes.append(new plane(vector::one, vector::one));
128  planes.append(new plane(vector(1,2,3), vector::one));
129 
130  forAll(planes, p)
131  Info<< "plane " << planes[p] << endl;
132 
133  Info<< nl << "Done." << endl;
134  return 0;
135 }
136 
137 
138 // ************************************************************************* //
OSspecific.H
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
IOstreams.H
Useful combination of include files which define Sin, Sout and Serr and the use of IO streams general...
p
p
Definition: pEqn.H:62
Scalar::Scalar
Scalar()
Definition: Test-PtrList.C:43
Foam::PtrList::append
void append(T *)
Append an element at the end of the list.
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:406
Scalar
Definition: Test-Dictionary.C:65
Foam::PtrList::xfer
Xfer< PtrList< T > > xfer()
Transfer contents to the Xfer container.
Definition: PtrListI.H:145
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
Scalar
#define Scalar
Definition: doubleScalar.C:33
Scalar::~Scalar
~Scalar()
Definition: Test-PtrList.C:53
Foam::plane
Geometric class that creates a 2D plane and can return the intersection point between a line and the ...
Definition: plane.H:60
Foam::PtrList::set
bool set(const label) const
Is element set.
Foam::label
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
plane.H
Foam::Vector< scalar >::one
static const Vector one
Definition: Vector.H:81
Foam::nl
static const char nl
Definition: Ostream.H:260
Foam::Info
messageStream Info
Foam::PtrList
A templated 1D list of pointers to objects of type <T>, where the size of the array is known and used...
Definition: List.H:61
Foam::PtrList::transfer
void transfer(PtrList< T > &)
Transfer the contents of the argument PtrList into this PtrList.
Definition: PtrList.C:200
Foam::operator<<
Ostream & operator<<(Ostream &, const edgeMesh &)
Definition: edgeMeshIO.C:130
scalar.H
Foam
Namespace for OpenFOAM.
Definition: combustionModel.C:30
Foam::vector
Vector< scalar > vector
A scalar version of the templated Vector.
Definition: vector.H:49
Foam::autoPtr
An auto-pointer similar to the STL auto_ptr but with automatic casting to a reference to the type and...
Definition: PtrList.H:117
Scalar::clone
autoPtr< Scalar > clone() const
Definition: Test-PtrList.C:58
Scalar::Scalar
Scalar(scalar val)
Definition: Test-PtrList.C:48
Foam::PtrList::setSize
void setSize(const label)
Reset size of PtrList. If extending the PtrList, new entries are.
Definition: PtrList.C:142
main
int main(int argc, char *argv[])
Definition: Test-PtrList.C:74
PtrList.H
DynamicList.H
Scalar::operator<<
friend Ostream & operator<<(Ostream &os, const Scalar &val)
Definition: Test-PtrList.C:63
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:53
Scalar::data_
scalar data_
Definition: Test-Dictionary.C:67