# PhasicFlow: src/phasicFlow/containers/List/anyList/anyList.hpp Source File
PhasicFlow is an open-source parallel DEM (discrete element method) package for simulating granular flow. It is developed in C++ and can be exectued on both GPU (like CUDA) and CPU.

anyList.hpp
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.
-----------------------------------------------------------------------------*/
#ifndef __anyList_hpp__
#define __anyList_hpp__
#include <any>
#include "typeInfo.hpp"
#include "types.hpp"
#include "List.hpp"
namespace pFlow
{
class anyList
{
public:
using anyListType = List<std::any>;
using reference = typename anyListType::reference;
using iterator = typename anyListType::iterator;
using const_iterator= typename anyListType::const_iterator;
private:
anyListType anyList_;
wordList names_;
wordList types_;
public:
// - Type info
TypeInfoNV("anyList");
// All five constructors are created by compiler
size_t size()const;
bool empty()const;
bool contains(const word& name)const;
const wordList& names()const;
template<typename T, typename... Args>
reference emplaceBack(const word& name, Args&&... args)
{
if( contains(name))
{
fatalErrorInFunction<<
"variable name "<< name << " already exists in the anyList."<<endl<<
"list of variables is "<<names_<<endl;
fatalExit;
}
names_.push_back(name);
types_.push_back(getTypeName<T>());
return anyList_.emplace_back(
std::in_place_type<T>,
std::forward<Args>(args)...);
}
template<typename T>
reference emplaceBack(const word& name, const T & other)
{
if( contains(name) )
{
fatalErrorInFunction<<
"variable name "<< name << " already exists in the anyList."<<endl<<
"list of variables is "<<names_<<endl;
fatalExit;
}
names_.push_back(name);
types_.push_back(getTypeName(other));
return anyList_.emplace_back(std::in_place_type<T>, other);
}
template<typename T>
reference emplaceBack(const word& name, T&& other)
{
if( contains(name) )
{
fatalErrorInFunction<<
"variable name "<< name << " already exists in the anyList."<<endl<<
"list of variables is "<<names_<<endl;
fatalExit;
}
names_.push_back(name);
types_.push_back(getTypeName(other));
return anyList_.emplace_back(std::in_place_type<T>, std::forward<T>(other));
}
template<typename T>
T& getObject(size_t i)
{
if(getTypeName<T>() != types_[i])
{
fatalErrorInFunction<<
"requested object type is "<<getTypeName<T>()<<
" while strored type is "<<types_[i]<<
" for object "<< names_[i]<<endl;
fatalExit;
}
std::any *a = &(*anyList_.pos(i));
if(!a->has_value())
{
fatalErrorInFunction<<
"any does not have a value for dereferencing. "<<
"index in anyList is "<<i<<"."<<endl;
fatalExit;
}
return *std::any_cast<T>(a);
}
template<typename T>
T& getObject(const word& name)
{
int32 i = names_.findi(name);
if(i == -1 )
{
fatalErrorInFunction<<
"variable name "<< name << " does not exist in the anyList."<<endl<<
"list of variables is "<<names_<<endl;
fatalExit;
}
return getObject<T>(static_cast<size_t>(i));
}
template<typename T>
const T& getObject(const word& name)const
{
int32 i = names_.findi(name);
if(i == -1 )
{
fatalErrorInFunction<<
"variable name "<< name << " does not exist in the anyList."<<endl<<
"list of variables is "<<names_<<endl;
fatalExit;
}
return getObject<T>(static_cast<size_t>(i));
}
template<typename T>
const T& getObject(size_t i)const
{
if(getTypeName<T>() != types_[i])
{
fatalErrorInFunction<<
"requested object type is "<<getTypeName<T>()<<
" while strored type is "<<types_[i]<<
" for object "<< names_[i]<<endl;
fatalExit;
}
const std::any *a = &(*anyList_.pos(i));
if(!a->has_value())
{
fatalErrorInFunction<<
"any does not have a value for dereferencing. "<<
"index in anyList is "<<i<<"."<<endl;
fatalExit;
}
return *std::any_cast<const T>(a);
}
template<typename T>
T* getObjectPtr(size_t i)
{
if(i>= size())return nullptr;
if(getTypeName<T>() != types_[i])
{
warningInFunction<<
"requested object type is "<<getTypeName<T>()<<
" while strored type is "<<types_[i]<<
" for object "<< names_[i]<<endl;
return nullptr;
}
std::any *a = &(*anyList_.pos(i));
if( a->has_value())
{
return std::any_cast<T>(a);
}
else
{
return nullptr;
}
}
template<typename T>
const T* getObjectPtr(size_t i)const
{
if(i>=size())return nullptr;
if(getTypeName<T>() != types_[i])
{
warningInFunction<<
"requested object type is "<<getTypeName<T>()<<
" while strored type is "<<types_[i]<<
" for object "<< names_[i]<<endl;
return nullptr;
}
const std::any *a = &(*anyList_.pos(i));
if( a->has_value())
{
return std::any_cast<const T>(a);
}
else
{
return nullptr;
}
}
};
} // pFlow
#endif //__anyList_hpp__
pFlow::List< std::any >
pFlow::anyList::getObject
const T & getObject(const word &name) const
Get the const reference to variable by name.
Definition: anyList.hpp:173
pFlow::anyList::names_
wordList names_
List of variable names in anyList_.
Definition: anyList.hpp:54
fatalExit
#define fatalExit
Fatal exit.
Definition: error.hpp:98
pFlow::anyList::getObject
T & getObject(size_t i)
Get the reference to variable by index.
Definition: anyList.hpp:134
pFlow::anyList::types_
wordList types_
Definition: anyList.hpp:56
pFlow::anyList::getObjectPtr
T * getObjectPtr(size_t i)
Get the pointer to variable by index.
Definition: anyList.hpp:212
types.hpp
warningInFunction
#define warningInFunction
Report a warning.
Definition: error.hpp:95
pFlow::List< std::any >::const_iterator
typename listType::const_iterator const_iterator
Definition: List.hpp:52
List.hpp
pFlow::word
std::string word
Definition: builtinTypes.hpp:64
pFlow::anyList::contains
bool contains(const word &name) const
Does container contain this variable name?
Definition: anyList.cpp:35
pFlow::anyList::empty
bool empty() const
Is container empty.
Definition: anyList.cpp:30
pFlow::endl
iOstream & endl(iOstream &os)
Add newline and flush stream.
Definition: iOstream.hpp:341
pFlow::anyList::emplaceBack
reference emplaceBack(const word &name, T &&other)
Create variable using move constructor.
Definition: anyList.hpp:118
pFlow
Definition: demGeometry.hpp:27
pFlow::anyList::reference
typename anyListType::reference reference
Definition: anyList.hpp:42
pFlow::List::findi
int32 findi(const T &val) const
Definition: ListI.hpp:125
pFlow::anyList
Definition: anyList.hpp:35
pFlow::anyList::TypeInfoNV
TypeInfoNV("anyList")
fatalErrorInFunction
#define fatalErrorInFunction
Report a fatal error and function name and exit the application.
Definition: error.hpp:77
pFlow::int32
int int32
Definition: builtinTypes.hpp:50
pFlow::List< std::any >::iterator
typename listType::iterator iterator
Definition: List.hpp:50
pFlow::anyList::const_iterator
typename anyListType::const_iterator const_iterator
Definition: anyList.hpp:46
pFlow::List< std::any >::reference
typename listType::reference reference
Definition: List.hpp:54
pFlow::anyList::iterator
typename anyListType::iterator iterator
Definition: anyList.hpp:44
pFlow::anyList::getObjectPtr
const T * getObjectPtr(size_t i) const
Get the const pointer to variable by index.
Definition: anyList.hpp:238
pFlow::getTypeName
word getTypeName()
Definition: typeInfo.hpp:122
pFlow::List::pos
auto pos(size_t i, bool noError=false)
Definition: ListI.hpp:23
typeInfo.hpp
pFlow::anyList::getObject
const T & getObject(size_t i) const
Get the const reference to object by index.
Definition: anyList.hpp:188
pFlow::anyList::emplaceBack
reference emplaceBack(const word &name, const T &other)
Create variable using copy constructor.
Definition: anyList.hpp:102
pFlow::anyList::size
size_t size() const
Size of container.
Definition: anyList.cpp:25
pFlow::anyList::emplaceBack
reference emplaceBack(const word &name, Args &&... args)
Create variable using constructor in-place.
Definition: anyList.hpp:84
pFlow::anyList::anyList_
anyListType anyList_
Contains a list of variables with any type.
Definition: anyList.hpp:51
pFlow::anyList::getObject
T & getObject(const word &name)
Get the reference to variable by name.
Definition: anyList.hpp:158
pFlow::anyList::names
const wordList & names() const
List of varibales names.
Definition: anyList.cpp:40