Test-Dictionary.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 
34 #include "IOstreams.H"
35 #include "Dictionary.H"
36 #include "PtrDictionary.H"
37 
38 using namespace Foam;
39 
40 class ent
41 :
42  public Dictionary<ent>::link
43 {
44  word keyword_;
45  int i_;
46 
47 public:
48 
49  ent(const word& keyword, int i)
50  :
51  keyword_(keyword),
52  i_(i)
53  {}
54 
55  const word& keyword() const
56  {
57  return keyword_;
58  }
59 
60  friend Ostream& operator<<(Ostream& os, const ent& e)
61  {
62  os << e.keyword_ << ' ' << e.i_ << endl;
63  return os;
64  }
65 };
66 
67 
68 class Scalar
69 {
70  scalar data_;
71 
72 public:
73 
74  Scalar()
75  :
76  data_(0)
77  {}
78 
79  Scalar(scalar val)
80  :
81  data_(val)
82  {}
83 
84  ~Scalar()
85  {
86  Info<<"delete Scalar: " << data_ << endl;
87  }
88 
89  friend Ostream& operator<<(Ostream& os, const Scalar& val)
90  {
91  os << val.data_;
92  return os;
93  }
94 };
95 
96 
97 
98 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
99 // Main program:
100 
101 int main(int argc, char *argv[])
102 {
103  Dictionary<ent>* dictPtr = new Dictionary<ent>;
104  Dictionary<ent>& dict = *dictPtr;
105 
106  for (int i = 0; i<10; i++)
107  {
108  ent* ePtr = new ent(word("ent") + name(i), i);
109  dict.append(ePtr->keyword(), ePtr);
110  dict.swapUp(ePtr);
111  }
112 
113  Info<< dict << endl;
114 
115  dict.swapDown(dict.first());
116 
118  {
119  Info<< "element : " << *iter;
120  }
121 
122  Info<< "keys: " << dict.toc() << endl;
123 
124  delete dictPtr;
125 
126  Dictionary<ent> dict2;
127 
128  for (int i = 0; i<10; i++)
129  {
130  ent* ePtr = new ent(word("ent") + name(i), i);
131  dict2.append(ePtr->keyword(), ePtr);
132  dict2.swapUp(ePtr);
133  }
134 
135  Info<< "dict:\n" << dict2 << endl;
136 
137  Info<< nl << "Testing transfer: " << nl << endl;
138  Info<< "original: " << dict2 << endl;
139 
140  Dictionary<ent> newDict;
141  newDict.transfer(dict2);
142 
143  Info<< nl << "source: " << dict2 << nl
144  << "keys: " << dict2.toc() << nl
145  << "target: " << newDict << nl
146  << "keys: " << newDict.toc() << endl;
147 
148 
149  PtrDictionary<Scalar> scalarDict;
150  for (int i = 0; i<10; i++)
151  {
152  word key("ent" + name(i));
153  scalarDict.insert(key, new Scalar(1.3*i));
154  }
155 
156  Info<< nl << "scalarDict1: " << endl;
157  forAllConstIter(PtrDictionary<Scalar>, scalarDict, iter)
158  {
159  Info<< " = " << iter() << endl;
160  }
161 
162  PtrDictionary<Scalar> scalarDict2;
163  for (int i = 8; i<15; i++)
164  {
165  word key("ent" + name(i));
166  scalarDict2.insert(key, new Scalar(1.3*i));
167  }
168  Info<< nl << "scalarDict2: " << endl;
169  forAllConstIter(PtrDictionary<Scalar>, scalarDict2, iter)
170  {
171  Info<< "elem = " << *iter << endl;
172  }
173 
174  scalarDict.transfer(scalarDict2);
175 
176 
177  Scalar* p = scalarDict.lookupPtr("ent8");
178 
179  // This does not (yet) work
180  // Scalar* q = scalarDict.remove("ent10");
181 
182  if (p)
183  {
184  Info<< "found: " << *p << endl;
185  }
186  else
187  {
188  Info<< "no p: " << endl;
189  }
190 
191  scalarDict.clear();
192 
193  // Info<< " = " << *iter << endl;
194 
195 
196 
197  Info<< nl << "Done." << endl;
198  return 0;
199 }
200 
201 
202 // ************************************************************************* //
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-Dictionary.C:71
Foam::word
A class for handling words, derived from string.
Definition: word.H:59
Foam::DictionaryBase< IDLList< T >, T >::toc
wordList toc() const
Return the table of contents.
Definition: DictionaryBase.C:172
Foam::PtrDictionary
Template dictionary class which manages the storage associated with it.
Definition: PtrDictionary.H:53
PtrDictionary.H
Foam::DictionaryBase< DLPtrList< 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
ent::keyword_
word keyword_
Definition: Test-Dictionary.C:41
Scalar
#define Scalar
Definition: doubleScalar.C:33
Scalar::~Scalar
~Scalar()
Definition: Test-Dictionary.C:81
Foam::DictionaryBase< IDLList< T >, T >::append
void append(const word &, T *)
Add at tail of dictionary.
Definition: DictionaryBase.C:203
forAllConstIter
forAllConstIter(PtrDictionary< phaseModel >, mixture.phases(), phase)
Definition: pEqn.H:39
ent::ent
ent(const word &keyword, int i)
Definition: Test-Dictionary.C:46
Foam::DictionaryBase< DLPtrList< T >, T >::insert
void insert(const word &, T *)
Add at head of dictionary.
Definition: DictionaryBase.C:193
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
ent::i_
int i_
Definition: Test-Dictionary.C:42
dict
dictionary dict
Definition: searchingEngine.H:14
scalar.H
Foam
Namespace for OpenFOAM.
Definition: combustionModel.C:30
Foam::e
const double e
Elementary charge.
Definition: doubleFloat.H:94
ent::keyword
const word & keyword() const
Definition: Test-Dictionary.C:52
main
int main(int argc, char *argv[])
Definition: Test-Dictionary.C:98
Dictionary.H
Scalar::Scalar
Scalar(scalar val)
Definition: Test-Dictionary.C:76
Foam::DictionaryBase< DLPtrList< T >, T >::clear
void clear()
Clear the dictionary.
Definition: DictionaryBase.C:230
Foam::Dictionary
Gerneral purpose template dictionary class which manages the storage associated with it.
Definition: Dictionary.H:54
ent::operator<<
friend Ostream & operator<<(Ostream &os, const ent &e)
Definition: Test-Dictionary.C:57
Foam::dictionary::toc
wordList toc() const
Return the table of contents.
Definition: dictionary.C:697
Scalar::operator<<
friend Ostream & operator<<(Ostream &os, const Scalar &val)
Definition: Test-Dictionary.C:86
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:53
Foam::DictionaryBase< IDLList< T >, T >::transfer
void transfer(DictionaryBase< IDLListType, T > &)
Transfer the contents of the argument into this DictionaryBase.
Definition: DictionaryBase.C:239
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
ent
Definition: Test-Dictionary.C:37