www.cemf.ir
observer.cpp
Go to the documentation of this file.
1 /*------------------------------- phasicFlow ---------------------------------
2  O C enter of
3  O O E ngineering and
4  O O M ultiscale modeling of
5  OOOOOOO F luid flow
6 ------------------------------------------------------------------------------
7  Copyright (C): www.cemf.ir
8  email: hamid.r.norouzi AT gmail.com
9 ------------------------------------------------------------------------------
10 Licence:
11  This file is part of phasicFlow code. It is a free software for simulating
12  granular and multiphase flows. You can redistribute it and/or modify it under
13  the terms of GNU General Public License v3 or any other later versions.
14 
15  phasicFlow is distributed to help others in their research in the field of
16  granular and multiphase flows, but WITHOUT ANY WARRANTY; without even the
17  implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
18 
19 -----------------------------------------------------------------------------*/
20 
21 #include "observer.hpp"
22 #include "subscriber.hpp"
23 
25 {
26  const subscriber* old = subscriber_;
27  subscriber_ = newSub;
28  return old;
29 }
30 
31 pFlow::observer::observer(message msg) : subscriber_(nullptr),
32  message_(msg)
33 {}
34 
36 (
37  const subscriber* subscrbr,
38  message msg
39 )
40 {
41  addToSubscriber(subscrbr, msg);
42 }
43 
45 {
46  if(subscriber_)
47  subscriber_->unsubscribe(this);
48  invalidateSubscriber();
49 }
50 
52 {
53  if( !message_.equivalentTo(event) )
54  {
55  message_.add(event);
56  if(!subscriber_->subscribe(message(event),this))
57  {
59  "error when subcribing event from observer "<<endl;
60  fatalExit;
61  }
62  }
63  return *this;
64 }
65 
67 (
68  const subscriber* subscrbr,
69  message msg
70 )
71 {
72  if(subscriber_)
73  subscriber_->unsubscribe(this);
74  invalidateSubscriber();
75 
76  subscriber_ = subscrbr;
77  message_ = msg;
78 
79  if(subscriber_)
80  {
81  if(!subscriber_->subscribe(message_, this))
82  {
84  "error in subscribing an observer"<<endl;
85  fatalExit;
86  }
87  }
88 }
89 
91 {
92  if(subscriber_)
93  subscriber_->unsubscribe(this);
94  invalidateSubscriber();
95 
96  subscriber_ = &subscrbr;
97  return subscriber_->subscribe(message_, this);
98 }
pFlow::observer
Definition: observer.hpp:33
subscriber.hpp
pFlow::message
Definition: message.hpp:33
fatalExit
#define fatalExit
Fatal exit.
Definition: error.hpp:98
observer.hpp
pFlow::observer::observer
observer(message msg)
Definition: observer.cpp:31
pFlow::observer::addEvent
observer & addEvent(message::EVENT event)
Definition: observer.cpp:51
pFlow::endl
iOstream & endl(iOstream &os)
Add newline and flush stream.
Definition: iOstream.hpp:341
pFlow::subscriber
Definition: subscriber.hpp:37
pFlow::message::EVENT
EVENT
Definition: message.hpp:36
pFlow::subscriber::subscribe
virtual bool subscribe(message msg, observer *obsevr) const
Definition: subscriber.cpp:85
fatalErrorInFunction
#define fatalErrorInFunction
Report a fatal error and function name and exit the application.
Definition: error.hpp:77
pFlow::observer::subscriber_
const subscriber * subscriber_
pointer to subscriber
Definition: observer.hpp:39
pFlow::observer::addToSubscriber
void addToSubscriber(const subscriber *subscrbr, message msg)
Definition: observer.cpp:67
pFlow::observer::~observer
virtual ~observer()
Definition: observer.cpp:44
pFlow::observer::changeSubscriber
const subscriber * changeSubscriber(const subscriber *newSub)
Definition: observer.cpp:24