# PhasicFlow: src/phasicFlow/repository/repository/repositoryTemplates.cpp Source File

repositoryTemplates.cpp
Go to the documentation of this file.
/*------------------------------- phasicFlow ---------------------------------
O C enter of
O O E ngineering and
O O M ultiscale modeling of
OOOOOOO F luid flow
------------------------------------------------------------------------------
Copyright (C): www.cemf.ir
email: hamid.r.norouzi AT gmail.com
------------------------------------------------------------------------------
Licence:
This file is part of phasicFlow code. It is a free software for simulating
granular and multiphase flows. You can redistribute it and/or modify it under
the terms of GNU General Public License v3 or any other later versions.
phasicFlow is distributed to help others in their research in the field of
granular and multiphase flows, but WITHOUT ANY WARRANTY; without even the
implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
-----------------------------------------------------------------------------*/
template <typename Type1>
pFlow::word pFlow::repository::reportTypeError (IOobject& object)
{
word err;
err = "The requested object " + object.name() + " with type " + Type1::TYPENAME() + ", while the type " +
object.typeName() + "is found in repository " + this->name();
return err;
}
template <typename Type>
bool pFlow::repository::checkForObjectType(IOobject& object)
{
return Type::TYPENAME() == object.typeName();
}
template<typename T, typename... Args>
T& pFlow::repository::emplaceObject(const objectFile& objf, Args&&... args)
{
if( auto [iter2, success2] = objects_.findIf(objf.name()); !success2 )
{
auto ptr = IOobject::make_object_t<T>(std::forward<Args>(args)...);
auto [iter, success] = objects_.emplace(std::piecewise_construct,
std::forward_as_tuple(objf.name()),
std::forward_as_tuple(objf, this, std::move(ptr) )
);
return iter->second.template getObject<T>();
}
else
{
fatalErrorInFunction<<
"IOobject " << objf.name() << " already exists in repository " << name() <<endl;
fatalExit;
return iter2->second.template getObject<T>();
}
}
template<typename T, typename... Args>
T& pFlow::repository::emplaceObjectOrGet(const objectFile& objf, Args&&... args)
{
if(auto [iter, success] = objects_.findIf(objf.name()); !success )
{
return emplaceObject<T>(objf, std::forward<Args>(args)... );
}
else
{
// type check
if( checkForObjectType<T>( iter->second ) )
{
return iter->second.template getObject<T>();
}
else
{
fatalErrorInFunction<<
" IOobject "<< objf.name() <<" already exist in the repository "<< name() <<
". Trying to return the existing object but there is a type mismatch. \n"<<
reportTypeError<T>( iter->second );
fatalExit;
return iter->second.template getObject<T>(); // this is never executed
}
}
}
template<typename T, typename... Args>
T& pFlow::repository::emplaceReplaceObject(const objectFile& objf, Args&&... args)
{
eraseObject(objf.name());
auto ptr = IOobject::make_object_t<T>(std::forward<Args>(args)...);
auto [iter, success] = objects_.emplace(std::piecewise_construct,
std::forward_as_tuple(objf.name()),
std::forward_as_tuple(objf, this, std::move(ptr) )
);
return iter->second.template getObject<T>();
}
template<typename T>
T& pFlow::repository::insertReplaceObject(uniquePtr<IOobject>&& ptr )
{
if( !ptr->owner() )
{
eraseObject(ptr->name());
objectFile objf( ptr() );
auto [iter, success] = objects_.emplace
(
std::piecewise_construct,
std::forward_as_tuple(ptr->name()),
std::forward_as_tuple(objf, this, std::move(ptr))
);
return iter->second.template getObject<T>();
}else
{
return ptr().getObject<T>();
}
}
template<typename T>
T& pFlow::repository::insertReplaceObject(const objectFile& objf, uniquePtr<IOobject>&& ptr )
{
if( !ptr->owner() )
{
eraseObject(objf.name());
auto [iter, success] = objects_.emplace
(
std::piecewise_construct,
std::forward_as_tuple(objf.name()),
std::forward_as_tuple(objf, this, std::move(ptr))
);
return iter->second.template getObject<T>();
}else
{
return ptr().getObject<T>();
}
}
template<typename T>
T& pFlow::repository::lookupObject(const word& name)
{
if( auto [iter, success] = objects_.findIf(name); success )
{
if( checkType<T>(iter->second) )
{
return iter->second.template getObject<T>();
}else
{
fatalErrorInFunction <<
reportTypeError<T>(iter->second)<<endl;
fatalExit;
return iter->second.template getObject<T>();
}
}
else
{
fatalErrorInFunction <<
"Object with name " << name << " is not found in repository " << this->name()<<endl <<
"list of avaiable objest is \n" << objectNames();
fatalExit;
return iter->second.template getObject<T>();
}
}
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