repositoryTemplates.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 template <typename Type1>
23 {
24  word err;
25  err = "The requested object " + object.name() + " with type " + Type1::TYPENAME() + ", while the type " +
26  object.typeName() + "is found in repository " + this->name();
27 
28  return err;
29 }
30 
31 template <typename Type>
33 {
34  return Type::TYPENAME() == object.typeName();
35 }
36 
37 template<typename T, typename... Args>
38 T& pFlow::repository::emplaceObject(const objectFile& objf, Args&&... args)
39 {
40 
41  if( auto [iter2, success2] = objects_.findIf(objf.name()); !success2 )
42  {
43  auto ptr = IOobject::make_object_t<T>(std::forward<Args>(args)...);
44  auto [iter, success] = objects_.emplace(std::piecewise_construct,
45  std::forward_as_tuple(objf.name()),
46  std::forward_as_tuple(objf, this, std::move(ptr) )
47  );
48 
49  return iter->second.template getObject<T>();
50  }
51  else
52  {
54  "IOobject " << objf.name() << " already exists in repository " << name() <<endl;
55  fatalExit;
56  return iter2->second.template getObject<T>();
57 
58  }
59 }
60 
61 template<typename T, typename... Args>
62 T& pFlow::repository::emplaceObjectOrGet(const objectFile& objf, Args&&... args)
63 {
64 
65  if(auto [iter, success] = objects_.findIf(objf.name()); !success )
66  {
67  return emplaceObject<T>(objf, std::forward<Args>(args)... );
68  }
69  else
70  {
71  // type check
72  if( checkForObjectType<T>( iter->second ) )
73  {
74  return iter->second.template getObject<T>();
75  }
76  else
77  {
79  " IOobject "<< objf.name() <<" already exist in the repository "<< name() <<
80  ". Trying to return the existing object but there is a type mismatch. \n"<<
81  reportTypeError<T>( iter->second );
82  fatalExit;
83  return iter->second.template getObject<T>(); // this is never executed
84  }
85  }
86 }
87 
88 template<typename T, typename... Args>
89 T& pFlow::repository::emplaceReplaceObject(const objectFile& objf, Args&&... args)
90 {
91 
92  eraseObject(objf.name());
93 
94  auto ptr = IOobject::make_object_t<T>(std::forward<Args>(args)...);
95  auto [iter, success] = objects_.emplace(std::piecewise_construct,
96  std::forward_as_tuple(objf.name()),
97  std::forward_as_tuple(objf, this, std::move(ptr) )
98  );
99 
100  return iter->second.template getObject<T>();
101 }
102 
103 template<typename T>
105 {
106  if( !ptr->owner() )
107  {
108  eraseObject(ptr->name());
109  objectFile objf( ptr() );
110 
111  auto [iter, success] = objects_.emplace
112  (
113  std::piecewise_construct,
114  std::forward_as_tuple(ptr->name()),
115  std::forward_as_tuple(objf, this, std::move(ptr))
116  );
117  return iter->second.template getObject<T>();
118  }else
119  {
120  return ptr().getObject<T>();
121  }
122 }
123 
124 template<typename T>
126 {
127  if( !ptr->owner() )
128  {
129  eraseObject(objf.name());
130 
131  auto [iter, success] = objects_.emplace
132  (
133  std::piecewise_construct,
134  std::forward_as_tuple(objf.name()),
135  std::forward_as_tuple(objf, this, std::move(ptr))
136  );
137  return iter->second.template getObject<T>();
138  }else
139  {
140  return ptr().getObject<T>();
141  }
142 }
143 
144 
145 template<typename T>
147 {
148  if( auto [iter, success] = objects_.findIf(name); success )
149  {
150 
151  if( checkType<T>(iter->second) )
152  {
153  return iter->second.template getObject<T>();
154 
155  }else
156  {
158  reportTypeError<T>(iter->second)<<endl;
159  fatalExit;
160  return iter->second.template getObject<T>();
161  }
162 
163  }
164  else
165  {
167  "Object with name " << name << " is not found in repository " << this->name()<<endl <<
168  "list of avaiable objest is \n" << objectNames();
169  fatalExit;
170  return iter->second.template getObject<T>();
171  }
172 }
pFlow::repository::emplaceReplaceObject
T & emplaceReplaceObject(const objectFile &objf, Args &&... args)
Definition: repositoryTemplates.cpp:89
fatalExit
#define fatalExit
Definition: error.hpp:57
pFlow::repository::checkForObjectType
bool checkForObjectType(IOobject &object)
Definition: repositoryTemplates.cpp:32
pFlow::repository::lookupObject
T & lookupObject(const word &name)
Definition: repositoryTemplates.cpp:146
pFlow::repository::reportTypeError
word reportTypeError(IOobject &object)
pFlow::word
std::string word
Definition: builtinTypes.hpp:63
pFlow::repository::emplaceObject
T & emplaceObject(const objectFile &objf, Args &&... args)
Definition: repositoryTemplates.cpp:38
pFlow::endl
iOstream & endl(iOstream &os)
Definition: iOstream.hpp:312
pFlow::IOobject
Definition: IOobject.hpp:35
pFlow::repository::insertReplaceObject
T & insertReplaceObject(uniquePtr< IOobject > &&ptr)
Definition: repositoryTemplates.cpp:104
pFlow::repository::name
word name() const
Definition: repository.cpp:51
fatalErrorInFunction
#define fatalErrorInFunction
Definition: error.hpp:42
pFlow::objectFile::name
virtual word name() const
Definition: objectFile.hpp:97
pFlow::objectFile
Definition: objectFile.hpp:33
pFlow::repository::emplaceObjectOrGet
T & emplaceObjectOrGet(const objectFile &objf, Args &&... args)
Definition: repositoryTemplates.cpp:62
pFlow::uniquePtr
Definition: uniquePtr.hpp:44