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 
22 #include "repository.hpp"
23 
24 
26 (
27  const word& name,
28  const fileSystem& localPath,
29  repository* owner
30 )
31 :
32  name_(name),
33  localPath_(localPath),
34  owner_(owner)
35 {
36 
37  if(owner)
38  {
39  owner->addToRepository(this);
40  }
41 }
42 
44 {
45  if(owner_)
46  {
47  owner_->removeRepository(this);
48  }
49 }
50 
52 {
53  return name_;
54 }
55 
57 {
58  return localPath_;
59 }
60 
62 {
63  if(owner_)
64  {
65  return owner_->path()/localPath();
66  }
67  else
68  {
69  return localPath();
70  }
71 }
72 
74 {
75  return owner_;
76 }
77 
79 {
80  return owner_;
81 }
82 
84 {
85  return *this;
86 }
87 
89 {
90  return *this;
91 }
92 
94 {
95  if( !repositories_.insertIf(rep->name(), rep) )
96  {
97 
99  "Failed to add repository " << rep->name() <<
100  " to repository " << this->name() <<
101  ". It is already in this repository. \n";
102  return false;
103 
104  }
105 
106  return true;
107 }
108 
110 {
111  auto name = rep->name();
112  return repositories_.erase(name) == 1;
113 }
114 
115 
116 
118 {
119  return objects_.search(nm);
120 }
121 
123  const word& nm
124  )const
125 {
126  if(auto [iter, found] = objects_.findIf(nm); found)
127  {
128  return iter->second.typeName();
129  }
130  else
131  {
133  "Object name " << nm << " is not found in repository "<< name() <<endl;
134  fatalExit;
135  return "";
136  }
137 }
138 
140 (
141  const word& nm,
142  bool downward
143 )const
144 {
145 
146 
147 
148  if(!downward)
149  {
150  // the object to start search and its owner
151  auto object = this;
152  auto owner = object->owner();
153 
154  // get to the top-most repository
155  while(!owner)
156  {
157  object = owner;
158  owner = object->owner();
159  }
160 
161  return object->globalLookupObjectName(nm, true);
162  }
163 
164  if( lookupObjectName(nm) ) return true;
165 
166  for(auto& rep:repositories_ )
167  {
168  if(rep.second)
169  {
170  if(rep.second->globalLookupObjectName(nm, true))return true;
171  }
172  }
173 
174  return false;
175 
176 }
177 
179 {
180  return repositories_.search(nm);
181 }
182 
184 {
185  if(lookupObjectName(nm)) return true;
186  if(lookupRepositoryName(nm))return true;
187  return false;
188 }
189 
191 {
192  return objects_.size();
193 }
194 
196 {
197  return repositories_.size();
198 }
199 
201 {
202 
203  if( auto [iter, success] = repositories_.findIf(name); success )
204  {
205  return *iter->second;
206  }
207  else
208  {
210  "repository with name " << name << " is not found in repository " << this->name()<<endl <<
211  "list of avaiable repositories is \n" << repositoryNames();
212  fatalExit;
213  return *iter->second;
214  }
215 }
216 
218 {
219  wordList names;
220  for(auto& ob:objects_)
221  {
222  names.push_back(ob.first);
223  }
224  return names;
225 }
226 
228 {
229  wordList names;
230  for(auto& rep:repositories_)
231  {
232  names.push_back(rep.first);
233  }
234  return names;
235 }
236 
237 
239 (
240  bool verbose
241 ) const
242 {
243 
244  for(auto& obj:objects_)
245  {
246  if(verbose)
247  {
248  REPORT(1)<< "Writing to " << obj.second.path()<<endREPORT;
249  }
250 
251  if(!obj.second.write())
252  {
253  return false;
254  }
255  }
256 
257  // - write sub-repositories
258  for(auto& rep:repositories_)
259  {
260 
261  if( rep.second )
262  {
263  if(! rep.second->write(verbose) )
264  {
266  " error in writing repository " << rep.first <<endl;
267  return false;
268  }
269  }
270  else
271  {
273  " repository " << rep.first << " is not a valid object to be referenced. \n";
274  return false;
275  }
276  }
277 
278  return true;
279 }
pFlow::List< word >
pFlow::repository::localPath
virtual fileSystem localPath() const
Definition: repository.cpp:56
endREPORT
#define endREPORT
Definition: streams.hpp:41
fatalExit
#define fatalExit
Definition: error.hpp:57
REPORT
#define REPORT(n)
Definition: streams.hpp:40
warningInFunction
#define warningInFunction
Definition: error.hpp:55
pFlow::repository::removeRepository
bool removeRepository(repository *rep)
Definition: repository.cpp:109
pFlow::repository::owner_
repository * owner_
Definition: repository.hpp:45
pFlow::repository::~repository
virtual ~repository()
Definition: repository.cpp:43
pFlow::repository::owner
const repository * owner() const
Definition: repository.cpp:73
pFlow::repository::lookupObjectName
bool lookupObjectName(const word &nm) const
Definition: repository.cpp:117
pFlow::word
std::string word
Definition: builtinTypes.hpp:63
pFlow::repository::lookupObjectTypeName
word lookupObjectTypeName(const word &nm) const
Definition: repository.cpp:122
pFlow::endl
iOstream & endl(iOstream &os)
Definition: iOstream.hpp:312
pFlow::repository::numRepositories
size_t numRepositories() const
Definition: repository.cpp:195
pFlow::repository::write
virtual bool write(bool verbose=false) const
Definition: repository.cpp:239
pFlow::repository::lookupRepositoryName
bool lookupRepositoryName(const word &nm) const
Definition: repository.cpp:178
pFlow::fileSystem
Definition: fileSystem.hpp:63
repository.hpp
pFlow::repository::name
word name() const
Definition: repository.cpp:51
pFlow::repository::globalLookupObjectName
bool globalLookupObjectName(const word &nm, bool downward=false) const
Definition: repository.cpp:140
fatalErrorInFunction
#define fatalErrorInFunction
Definition: error.hpp:42
pFlow::fileSystem::path
const pathType & path() const
Definition: fileSystem.hpp:121
pFlow::repository::repositoryNames
wordList repositoryNames() const
Definition: repository.cpp:227
pFlow::repository::thisRepository
const repository & thisRepository() const
Definition: repository.cpp:83
pFlow::repository::repository
repository(const word &name, const fileSystem &localPath, repository *owner=nullptr)
Definition: repository.cpp:26
pFlow::repository::addToRepository
bool addToRepository(repository *rep)
Definition: repository.cpp:93
pFlow::repository::path
virtual fileSystem path() const
Definition: repository.cpp:61
pFlow::repository::lookupName
bool lookupName(const word nm) const
Definition: repository.cpp:183
pFlow::repository::lookupRepository
repository & lookupRepository(const word &name)
Definition: repository.cpp:200
pFlow::repository::numObjects
size_t numObjects() const
Definition: repository.cpp:190
pFlow::repository::objectNames
wordList objectNames() const
Definition: repository.cpp:217
pFlow::repository
Definition: repository.hpp:34