www.cemf.ir
subscriber.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 
22 #include "subscriber.hpp"
23 #include "anyList.hpp"
24 #include "observer.hpp"
25 #include "message.hpp"
26 
28 :
29  subName_(src.subName_)
30 {
31 }
32 
34 :
35  observerList_(std::move(src.observerList_)),
36  subName_(std::move(src.subName_))
37 {
38 
39  for(size_t i=0; i<observerList_.size(); i++)
40  {
41  for( auto obsvr: observerList_[i] )
42  {
43  if(obsvr) obsvr->changeSubscriber(this);
44  }
45  }
46 }
47 
49 {
50  this->subName_ = rhs.subName_;
51  return *this;
52 }
53 
55 {
56  this->subName_ = std::move(rhs.subName_);
57  this->observerList_ = std::move(rhs.observerList_);
58 
59  for(size_t i=0; i<observerList_.size(); i++)
60  {
61  for( auto obsvr: observerList_[i] )
62  {
63  if(obsvr) obsvr->changeSubscriber(this);
64  }
65  }
66 
67  return *this;
68 }
69 
71 {
72  for(size_t i=0; i<observerList_.size(); i++)
73  {
74  for( auto obsvr: observerList_[i] )
75  {
76  if(obsvr)
77  {
78  obsvr->invalidateSubscriber();
79  }
80  }
81  }
82 }
83 
85 (
86  message msg,
87  observer* obsevr
88 )const
89 {
90  // do not subscribe nullptr
91  if(!obsevr)return false;
92 
93  for(size_t i=0; i<msg.size(); i++)
94  {
95  if(msg.equivalentTo(i))
96  {
97  if(!observerList_[i].search(obsevr))
98  {
99  observerList_[i].push_back(obsevr);
100  }
101  }
102  }
103  return true;
104 }
105 
107 (
108  observer* obsrvr
109 )const
110 {
111  if(obsrvr)
112  {
113  for(size_t i=0; i<observerList_.size(); i++)
114  {
115  if( auto iter = observerList_[i].find(obsrvr); iter!= observerList_[i].end())
116  {
117  (*iter)->invalidateSubscriber();
118  observerList_[i].erase(iter);
119  }
120  }
121  }
122  return true;
123 }
124 
126 (
127  uint32 iter,
128  real t,
129  real dt,
130  const message msg,
131  const anyList& varList
132 )
133 {
134 
135  for(size_t i=0; i<msg.size(); i++)
136  {
137  if(msg.equivalentTo(i))
138  {
139  for( auto obsvr: observerList_[i] )
140  {
141  obsvr->hearChanges
142  (
143  t,
144  dt,
145  iter,
146  message(i),
147  varList
148  );
149  }
150  }
151  }
152 
153  return true;
154 }
155 
156 bool pFlow::subscriber::notify(const timeInfo &ti, const message msg, const anyList &varList)
157 {
158  return notify(ti.iter(), ti.t(), ti.dt(), msg, varList);
159 }
pFlow::message::equivalentTo
bool equivalentTo(EVENT evnt) const
Definition: message.hpp:131
pFlow::observer
Definition: observer.hpp:33
pFlow::subscriber::notify
bool notify(uint32 iter, real t, real dt, const message msg, const anyList &varList)
Definition: subscriber.cpp:126
subscriber.hpp
anyList.hpp
pFlow::message
Definition: message.hpp:33
pFlow::subscriber::unsubscribe
virtual bool unsubscribe(observer *obsevr) const
Definition: subscriber.cpp:107
pFlow::real
float real
Definition: builtinTypes.hpp:45
message.hpp
pFlow::subscriber::subscriber
subscriber(const word &name)
Definition: subscriber.hpp:48
pFlow::find
int64 find(Vector< T, Allocator > &vec, const T &val)
Definition: VectorAlgorithm.hpp:69
observer.hpp
pFlow::subscriber::observerList_
std::array< List< observer * >, message::numEvents()> observerList_
Definition: subscriber.hpp:42
pFlow::uint32
unsigned int uint32
Definition: builtinTypes.hpp:56
pFlow::message::size
auto size() const
Definition: message.hpp:146
pFlow::subscriber::subName_
word subName_
Definition: subscriber.hpp:44
pFlow::subscriber
Definition: subscriber.hpp:37
pFlow::timeInfo
Definition: timeInfo.hpp:28
pFlow::subscriber::subscribe
virtual bool subscribe(message msg, observer *obsevr) const
Definition: subscriber.cpp:85
pFlow::anyList
Definition: anyList.hpp:35
pFlow::timeInfo::dt
const real & dt() const
Definition: timeInfo.hpp:51
pFlow::subscriber::~subscriber
virtual ~subscriber()
Definition: subscriber.cpp:70
pFlow::subscriber::operator=
subscriber & operator=(const subscriber &)
Copy assignemnt, only assign the name, do not assign the list.
Definition: subscriber.cpp:48
pFlow::timeInfo::t
const real & t() const
Definition: timeInfo.hpp:47
pFlow::timeInfo::iter
const uint32 & iter() const
Definition: timeInfo.hpp:55