Test-PtrListDictionary.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) 2015 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 
34 #include "IOstreams.H"
35 #include "PtrListDictionary.H"
36 
37 using namespace Foam;
38 
39 class Scalar
40 {
41  scalar data_;
42 
43 public:
44 
45  Scalar()
46  :
47  data_(0)
48  {}
49 
50  Scalar(scalar val)
51  :
52  data_(val)
53  {}
54 
55  ~Scalar()
56  {
57  Info<<"delete Scalar: " << data_ << endl;
58  }
59 
60  friend Ostream& operator<<(Ostream& os, const Scalar& val)
61  {
62  os << val.data_;
63  return os;
64  }
65 };
66 
67 
68 
69 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
70 // Main program:
71 
72 int main(int argc, char *argv[])
73 {
74  PtrListDictionary<Scalar> scalarDict(10);
75  forAll(scalarDict, i)
76  {
77  word key("ent" + name(i));
78  scalarDict.set(i, key, new Scalar(1.3*i));
79  }
80 
81  Info<< nl << "scalarDict1: " << endl;
82  forAll(scalarDict, i)
83  {
84  Info<< "elem " << i << " = " << scalarDict[i] << endl;
85  }
86 
87  Scalar* ent8Ptr = scalarDict.lookupPtr("ent8");
88 
89  Info<< "ent8 = " << *ent8Ptr << endl;
90 
91  PtrListDictionary<Scalar> scalarDict2(15);
92  forAll(scalarDict2, i)
93  {
94  word key("ent" + name(i));
95  scalarDict2.set(i, key, new Scalar(1.3*i));
96  }
97  Info<< nl << "scalarDict2: " << endl;
98  forAll(scalarDict2, i)
99  {
100  Info<< "elem " << i << " = " << scalarDict2[i] << endl;
101  }
102 
103  scalarDict.transfer(scalarDict2);
104 
105  Scalar* p = scalarDict.lookupPtr("ent8");
106 
107  if (p)
108  {
109  Info<< "found: " << *p << endl;
110  }
111  else
112  {
113  Info<< "no p: " << endl;
114  }
115 
116  scalarDict.clear();
117 
118  Info<< nl << "Done." << endl;
119  return 0;
120 }
121 
122 
123 // ************************************************************************* //
PtrListDictionary.H
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-PtrListDictionary.C:42
Foam::word
A class for handling words, derived from string.
Definition: word.H:59
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:406
Foam::DictionaryBase< PtrList< T >, T >::lookupPtr
const T * lookupPtr(const word &) const
Find and return an entry if present, otherwise return NULL.
Scalar
Definition: Test-Dictionary.C:65
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-PtrListDictionary.C:52
Foam::PtrListDictionary
Template dictionary class which manages the storage associated with it.
Definition: PtrListDictionary.H:53
Foam::PtrListDictionary::set
autoPtr< T > set(const label, const word &key, T *)
Set element to pointer provided and return old element.
Definition: PtrListDictionary.C:65
Foam::nl
static const char nl
Definition: Ostream.H:260
Foam::Info
messageStream Info
Foam::operator<<
Ostream & operator<<(Ostream &, const edgeMesh &)
Definition: edgeMeshIO.C:130
scalar.H
Foam
Namespace for OpenFOAM.
Definition: combustionModel.C:30
Scalar::Scalar
Scalar(scalar val)
Definition: Test-PtrListDictionary.C:47
Foam::DictionaryBase< PtrList< T >, T >::clear
void clear()
Clear the dictionary.
Definition: DictionaryBase.C:230
Scalar::operator<<
friend Ostream & operator<<(Ostream &os, const Scalar &val)
Definition: Test-PtrListDictionary.C:57
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:53
Foam::DictionaryBase< PtrList< T >, T >::transfer
void transfer(DictionaryBase< IDLListType, T > &)
Transfer the contents of the argument into this DictionaryBase.
Definition: DictionaryBase.C:239
main
int main(int argc, char *argv[])
Definition: Test-PtrListDictionary.C:69
Foam::name
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
Scalar::data_
scalar data_
Definition: Test-Dictionary.C:67