www.cemf.ir
repository.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 "repository.hpp"
22 
23 
25 (
26  const word& name,
27  const fileSystem& localPath,
28  repository* owner
29 )
30 :
31  name_(name),
32  localPath_(localPath),
33  owner_(owner)
34 {
35 
36  if(owner)
37  {
38  owner->addToRepository(this);
39  }
40 }
41 
43 {
44  for(auto& objcs: objects_)
45  {
46  objcs.second->releaseOwner(true);
47  }
48  for(auto& reps: repositories_)
49  {
50  reps.second->releaseOwner(true);
51  }
52 
53  if(owner_)
54  {
56  }
57 
58 }
59 
61 {
62  return name_;
63 }
64 
66 {
67  return localPath_;
68 }
69 
71 {
72  if(owner_)
73  {
74  return owner_->path()/localPath();
75  }
76  else
77  {
78  return localPath();
79  }
80 }
81 
83 {
84  return owner_;
85 }
86 
88 {
89  return owner_;
90 }
91 
93 {
94  return *this;
95 }
96 
98 {
99  return *this;
100 }
101 
103 {
104  if( !repositories_.insertIf(rep->name(), rep) )
105  {
106 
108  "Failed to add repository " << rep->name() <<
109  " to repository " << this->name() <<
110  ". It is already in this repository. \n";
111  return false;
112 
113  }
114 
115  return true;
116 }
117 
119 {
120  auto name = rep->name();
121  return repositories_.erase(name) == 1;
122 }
123 
125 {
126  auto name = io->name();
127  return objects_.erase(name) == 1;
128 
129 }
130 
132 {
133  auto* old = owner_;
134  if(old && !fromOwner)
135  {
136  old->removeFromRepository(this);
137  }
138  owner_ = nullptr;
139  return old;
140 }
141 
143 {
144  if( !objects_.insertIf(io->name(), io ) )
145  {
147  "Failed to add object " << io->name() <<
148  " with type "<< lookupObjectTypeName(io->name())<<
149  " to repository " << this->name() <<
150  ". It is already in this repository. \n";
151  return false;
152  }
153 
154  return true;
155 }
156 
158 {
159  return objects_.search(nm);
160 }
161 
163 (
164  const word& nm
165 )const
166 {
167  if(auto [iter, found] = objects_.findIf(nm); found)
168  {
169  return iter->second->typeName();
170  }
171  else
172  {
174  "Object name " << nm << " is not found in repository "<< name() <<endl;
175  fatalExit;
176  return "";
177  }
178 }
179 
181 (
182  const word& nm,
183  bool downward
184 )const
185 {
186 
187  if(!downward)
188  {
189  // the object to start search and its owner
190  auto* object = this;
191  auto* owner = object->owner();
192 
193  // get to the top-most repository
194  while(!owner)
195  {
196  object = owner;
197  owner = object->owner();
198  }
199 
200  return object->globalLookupObjectName(nm, true);
201  }
202 
203  if( lookupObjectName(nm) ) return true;
204 
205  for(auto& rep:repositories_ )
206  {
207  if(rep.second)
208  {
209  if(rep.second->globalLookupObjectName(nm, true))return true;
210  }
211  }
212 
213  return false;
214 
215 }
216 
218 {
219  return repositories_.search(nm);
220 }
221 
223 {
224  if(lookupObjectName(nm)) return true;
225  if(lookupRepositoryName(nm))return true;
226  return false;
227 }
228 
230 {
231  return objects_.size();
232 }
233 
235 {
236  return repositories_.size();
237 }
238 
240 {
241 
242  if( auto [iter, success] = repositories_.findIf(name); success )
243  {
244  return *iter->second;
245  }
246  else
247  {
249  "repository with name " << name << " is not found in repository " << this->name()<<endl <<
250  "list of avaiable repositories is \n" << repositoryNames();
251  fatalExit;
252  return *iter->second;
253  }
254 }
255 
257 {
258  wordList names;
259  for(auto& ob:objects_)
260  {
261  names.push_back(ob.first);
262  }
263  return names;
264 }
265 
267 {
268  wordList names;
269  for(auto& rep:repositories_)
270  {
271  names.push_back(rep.first);
272  }
273  return names;
274 }
275 
276 
278 (
279  bool verbose
280 ) const
281 {
282 
283  for(const auto& obj:objects_)
284  {
285  if(verbose)
286  {
287  if(obj.second->implyWrite())
288  REPORT(1)<< "Writing to " << obj.second->path()<<END_REPORT;
289  }
290 
291  if(!obj.second->writeObject())
292  {
293  return false;
294  }
295  }
296 
297  // - write sub-repositories
298  for(auto& rep:repositories_)
299  {
300 
301  if( rep.second )
302  {
303  if(! rep.second->write(verbose) )
304  {
306  " error in writing repository " << rep.first <<endl;
307  return false;
308  }
309  }
310  else
311  {
313  " repository " << rep.first << " is not a valid object to be referenced. \n";
314  return false;
315  }
316  }
317 
318  return true;
319 }
pFlow::repository::releaseOwner
repository * releaseOwner(bool fromOwner=false)
Definition: repository.cpp:131
pFlow::List< word >
pFlow::repository::localPath
virtual fileSystem localPath() const
Definition: repository.cpp:65
fatalExit
#define fatalExit
Fatal exit.
Definition: error.hpp:98
REPORT
#define REPORT(n)
Definition: streams.hpp:39
warningInFunction
#define warningInFunction
Report a warning.
Definition: error.hpp:95
pFlow::repository::owner_
repository * owner_
Definition: repository.hpp:45
pFlow::repository::repositories_
wordMap< repository * > repositories_
Definition: repository.hpp:52
pFlow::repository::~repository
virtual ~repository()
Definition: repository.cpp:42
pFlow::repository::owner
const repository * owner() const
Definition: repository.cpp:82
pFlow::repository::lookupObjectName
bool lookupObjectName(const word &nm) const
Definition: repository.cpp:157
pFlow::word
std::string word
Definition: builtinTypes.hpp:64
pFlow::repository::lookupObjectTypeName
word lookupObjectTypeName(const word &nm) const
Definition: repository.cpp:163
pFlow::endl
iOstream & endl(iOstream &os)
Add newline and flush stream.
Definition: iOstream.hpp:341
pFlow::repository::numRepositories
size_t numRepositories() const
Definition: repository.cpp:234
pFlow::repository::write
virtual bool write(bool verbose=false) const
Definition: repository.cpp:278
pFlow::IOobject
Definition: IOobject.hpp:35
pFlow::repository::lookupRepositoryName
bool lookupRepositoryName(const word &nm) const
Definition: repository.cpp:217
pFlow::fileSystem
Manages file pathes, manupulate and combines them.
Definition: fileSystem.hpp:71
repository.hpp
pFlow::repository::name
word name() const
Definition: repository.cpp:60
pFlow::repository::globalLookupObjectName
bool globalLookupObjectName(const word &nm, bool downward=false) const
Definition: repository.cpp:181
fatalErrorInFunction
#define fatalErrorInFunction
Report a fatal error and function name and exit the application.
Definition: error.hpp:77
pFlow::fileSystem::path
const pathType & path() const
Const access to path.
Definition: fileSystem.hpp:154
pFlow::repository::repositoryNames
wordList repositoryNames() const
Definition: repository.cpp:266
pFlow::objectFile::name
virtual const word & name() const
Definition: objectFile.hpp:101
pFlow::repository::thisRepository
const repository & thisRepository() const
Definition: repository.cpp:92
pFlow::repository::objects_
wordMap< IOobject * > objects_
Definition: repository.hpp:48
pFlow::repository::repository
repository(const word &name, const fileSystem &localPath, repository *owner=nullptr)
Definition: repository.cpp:25
END_REPORT
#define END_REPORT
Definition: streams.hpp:40
pFlow::repository::addToRepository
bool addToRepository(repository *rep)
add repository to this repository return false if the name already exists
Definition: repository.cpp:102
pFlow::repository::path
virtual fileSystem path() const
Definition: repository.cpp:70
pFlow::repository::removeFromRepository
bool removeFromRepository(repository *rep)
remove rep from the list of repositories
Definition: repository.cpp:118
pFlow::repository::lookupName
bool lookupName(const word nm) const
Definition: repository.cpp:222
pFlow::repository::lookupRepository
repository & lookupRepository(const word &name)
Definition: repository.cpp:239
pFlow::repository::numObjects
size_t numObjects() const
Definition: repository.cpp:229
pFlow::repository::objectNames
wordList objectNames() const
Definition: repository.cpp:256
pFlow::repository
Definition: repository.hpp:34