Test-callback.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-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  callBackTest
26 
27 Description
28 
29 \*---------------------------------------------------------------------------*/
30 
31 #include "Callback.H"
32 
33 using namespace Foam;
34 
35 class callback
36 :
37  public Callback<callback>
38 {
39 public:
40 
42  :
44  {}
45 
47  {}
48 
49  virtual const word& name() const
50  {
51  return word::null;
52  }
53 
54  void testCallbackFunction() const
55  {
56  Info<< "calling testCallbackFunction for object " << name() << endl;
57  }
58 };
59 
60 
61 class callbackRegistry
62 :
63  public CallbackRegistry<callback>
64 {
65 public:
66 
68  {}
69 
71  {}
72 
73  void testCallbackFunction() const
74  {
75  forAllConstIter(callbackRegistry, *this, iter)
76  {
77  iter().testCallbackFunction();
78  }
79  }
80 };
81 
82 
84 :
85  public callback
86 {
87  word name_;
88 
89 public:
90 
92  :
93  callback(cbr),
94  name_(n)
95  {}
96 
97  virtual const word& name() const
98  {
99  return name_;
100  }
101 };
102 
103 
104 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
105 // Main program:
106 
107 int main(int argc, char *argv[])
108 {
109  callbackRegistry cbr;
110 
111  objectWithCallback ob1("ob1", cbr);
112  objectWithCallback ob2("ob2", cbr);
113 
114  cbr.testCallbackFunction();
115 
116  {
117  objectWithCallback ob1("ob1", cbr);
118  cbr.testCallbackFunction();
119  }
120 
121  cbr.testCallbackFunction();
122 
123  Info<< "End\n" << endl;
124 
125  return 0;
126 }
127 
128 
129 // ************************************************************************* //
Foam::CallbackRegistry< callback >
callbackRegistry
Definition: Test-callback.C:58
Foam::word
A class for handling words, derived from string.
Definition: word.H:59
objectWithCallback
Definition: Test-callback.C:80
callbackRegistry::~callbackRegistry
~callbackRegistry()
Definition: Test-callback.C:67
callback::callback
callback(CallbackRegistry< callback > &cbr)
Definition: Test-callback.C:38
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
objectWithCallback::name_
word name_
Definition: Test-callback.C:84
forAllConstIter
forAllConstIter(PtrDictionary< phaseModel >, mixture.phases(), phase)
Definition: pEqn.H:39
n
label n
Definition: TABSMDCalcMethod2.H:31
main
int main(int argc, char *argv[])
Definition: Test-callback.C:104
Foam::Info
messageStream Info
Callback.H
callback::name
virtual const word & name() const
Definition: Test-callback.C:46
objectWithCallback::objectWithCallback
objectWithCallback(const word &n, callbackRegistry &cbr)
Definition: Test-callback.C:88
Foam
Namespace for OpenFOAM.
Definition: combustionModel.C:30
callback
Definition: Test-callback.C:32
objectWithCallback::name
virtual const word & name() const
Definition: Test-callback.C:94
Foam::word::null
static const word null
An empty word.
Definition: word.H:77
Foam::Callback
Abstract class to handle automatic call-back registration with the CallbackRegistry....
Definition: Callback.H:51
callback::testCallbackFunction
void testCallbackFunction() const
Definition: Test-callback.C:51
callbackRegistry::callbackRegistry
callbackRegistry()
Definition: Test-callback.C:64
callback::~callback
~callback()
Definition: Test-callback.C:43
Foam::name
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
callbackRegistry::testCallbackFunction
void testCallbackFunction() const
Definition: Test-callback.C:70