www.cemf.ir
IOobject.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 "IOobject.hpp"
22 #include "repository.hpp"
23 
24 
26 (
27  const objectFile& objf,
28  const IOPattern& iop,
29  repository* owner
30 )
31 :
32  IOfileHeader(objf),
33  ioPattern_(iop),
34  owner_(owner)
35 {
36 
37  if(owner_&& !owner->addToRepository(this))
38  {
40  "failed to add object "<< objf.name()<<
41  " to repository "<< owner->name()<<endl;
42  fatalExit;
43  }
44 }
45 
47 {
48  if(owner_)
49  {
51  }
52 }
53 
55 (
56  bool fromOwner
57 )
58 {
59  auto* old = owner_;
60  if(old && !fromOwner)
61  {
62  old->removeFromRepository(this);
63  }
64  owner_ = nullptr;
65  return old;
66 }
67 
68 bool pFlow::IOobject::isIncluded(const word& objName)const
69 {
70  if(owner_)
71  return owner_->isIncluded(objName);
72  return false;
73 }
74 
75 
76 bool pFlow::IOobject::isExcluded(const word& objName)const
77 {
78  if(owner_)
79  return owner_->isExcluded(objName);
80  return false;
81 }
82 
84 {
85  if(!implyRead())return true;
86 
87  if( rdHdr && ioPattern().thisCallRead())
88  {
89  if( auto ptrIS = inStream(); ptrIS )
90  {
91  if(!readHeader(ptrIS()))return false;
92  }
93  else
94  {
96  "could not open file " << path() <<endl;
97  return false;
98  }
99  }
100 
101  if(ioPattern().thisCallRead())
102  {
103  if( auto ptrIS = inStream(); ptrIS )
104  {
105  if(!readObject(ptrIS(), rdHdr))return false;
106  }
107  else
108  {
110  "could not open file " << path() <<endl;
111  return false;
112  }
113  }
114 
115  return true;
116 }
117 
118 
120 {
121  if(!implyWrite()) return true;
122 
123  if(ioPattern().thisCallWrite())
124  {
125  if( ioPattern().thisProcWriteData())
126  {
127  if(auto ptrOS = outStream(); ptrOS )
128  {
129  return writeObject(ptrOS());
130  }
131  else
132  {
134  "error in opening file "<< path() <<endl;
135  return false;
136  }
137  }
138  else
139  {
140 
141  if(auto ptrOS = dummyOutStream(); ptrOS )
142  {
143  return writeObject(ptrOS());
144  }
145  else
146  {
148  "error in opening file "<< path() <<endl;
149  return false;
150  }
151  }
152 
153 
154  }
155 
156  return true;
157 }
158 
159 
161 {
162  if(rdHdr &&
163  ioPattern().thisCallRead() &&
164  !readHeader(is)) return false;
165 
166  if(ioPattern().thisCallRead())
167  {
168  return read(is, ioPattern());
169  }
170  else
171  {
172  return true;
173  }
174 
175 }
176 
177 
179 {
180  if(this->writeHeader() && ioPattern().thisProcWriteHeader())
181  {
182  writeHeader(os, typeName());
183  }
184 
185  if(ioPattern().thisCallWrite())
186  {
187  return write(os, ioPattern() );
188  }
189  else
190  return true;
191 }
pFlow::IOobject::isExcluded
bool isExcluded(const word &objName) const override
Definition: IOobject.cpp:76
fatalExit
#define fatalExit
Fatal exit.
Definition: error.hpp:98
IOobject.hpp
pFlow::IOobject::~IOobject
~IOobject() override
Definition: IOobject.cpp:46
warningInFunction
#define warningInFunction
Report a warning.
Definition: error.hpp:95
pFlow::IOobject::isIncluded
bool isIncluded(const word &objName) const override
Definition: IOobject.cpp:68
pFlow::word
std::string word
Definition: builtinTypes.hpp:64
pFlow::endl
iOstream & endl(iOstream &os)
Add newline and flush stream.
Definition: iOstream.hpp:341
repository.hpp
pFlow::repository::name
word name() const
Definition: repository.cpp:60
pFlow::iIstream
Interface class for any input stream
Definition: iIstream.hpp:37
pFlow::IOobject::readObject
bool readObject(bool rdHdr=true)
Definition: IOobject.cpp:83
fatalErrorInFunction
#define fatalErrorInFunction
Report a fatal error and function name and exit the application.
Definition: error.hpp:77
pFlow::objectFile::name
virtual const word & name() const
Definition: objectFile.hpp:101
pFlow::IOobject::IOobject
IOobject(const objectFile &objf, const IOPattern &iop, repository *owner)
Definition: IOobject.cpp:26
pFlow::IOPattern
Definition: IOPattern.hpp:32
pFlow::repository::addToRepository
bool addToRepository(repository *rep)
add repository to this repository return false if the name already exists
Definition: repository.cpp:102
pFlow::objectFile
Definition: objectFile.hpp:30
pFlow::IOobject::releaseOwner
repository * releaseOwner(bool fromOwner=false)
Definition: IOobject.cpp:55
pFlow::repository::removeFromRepository
bool removeFromRepository(repository *rep)
remove rep from the list of repositories
Definition: repository.cpp:118
pFlow::IOfileHeader
Definition: IOfileHeader.hpp:35
pFlow::repository
Definition: repository.hpp:34
pFlow::IOobject::owner_
repository * owner_
Definition: IOobject.hpp:43
pFlow::IOobject::writeObject
bool writeObject() const
Definition: IOobject.cpp:119
pFlow::iOstream
Interface class for any output stream.
Definition: iOstream.hpp:59