1
0
mirror of https://github.com/paboyle/Grid.git synced 2025-04-05 03:35:55 +01:00

Hadrons: environment access to derived objects

This commit is contained in:
Antonin Portelli 2018-03-13 16:10:16 +00:00
parent b85f987b0b
commit 78f8d47528
2 changed files with 43 additions and 8 deletions

View File

@ -116,6 +116,10 @@ public:
Ts && ... args);
void setObjectModule(const unsigned int objAddress,
const int modAddress);
template <typename B, typename T>
T * getDerivedObject(const unsigned int address) const;
template <typename B, typename T>
T * getDerivedObject(const std::string name) const;
template <typename T>
T * getObject(const unsigned int address) const;
template <typename T>
@ -186,7 +190,7 @@ Holder<T>::Holder(T *pt)
template <typename T>
T & Holder<T>::get(void) const
{
return &objPt_.get();
return *objPt_.get();
}
template <typename T>
@ -231,7 +235,7 @@ void Environment::createDerivedObject(const std::string name,
object_[address].Ls = Ls;
object_[address].data.reset(new Holder<B>(new T(std::forward<Ts>(args)...)));
object_[address].size = MemoryProfiler::stats->maxAllocated - initMem;
object_[address].type = &typeid(T);
object_[address].type = &typeid(B);
if (MemoryProfiler::stats == &memStats)
{
MemoryProfiler::stats = nullptr;
@ -241,7 +245,7 @@ void Environment::createDerivedObject(const std::string name,
else if ((object_[address].storage != Storage::cache) or
(object_[address].storage != storage) or
(object_[address].name != name) or
(object_[address].type != &typeid(T)))
(object_[address].type != &typeid(B)))
{
HADRON_ERROR(Definition, "object '" + name + "' already allocated");
}
@ -256,21 +260,37 @@ void Environment::createObject(const std::string name,
createDerivedObject<T, T>(name, storage, Ls, std::forward<Ts>(args)...);
}
template <typename T>
T * Environment::getObject(const unsigned int address) const
template <typename B, typename T>
T * Environment::getDerivedObject(const unsigned int address) const
{
if (hasObject(address))
{
if (hasCreatedObject(address))
{
if (auto h = dynamic_cast<Holder<T> *>(object_[address].data.get()))
if (auto h = dynamic_cast<Holder<B> *>(object_[address].data.get()))
{
return h->getPt();
if (&typeid(T) == &typeid(B))
{
return dynamic_cast<T *>(h->getPt());
}
else
{
if (auto hder = dynamic_cast<T *>(h->getPt()))
{
return hder;
}
else
{
HADRON_ERROR(Definition, "object with address " + std::to_string(address) +
" cannot be casted to '" + typeName(&typeid(T)) +
"' (has type '" + typeName(&typeid(h->get())) + "')");
}
}
}
else
{
HADRON_ERROR(Definition, "object with address " + std::to_string(address) +
" does not have type '" + typeName(&typeid(T)) +
" does not have type '" + typeName(&typeid(B)) +
"' (has type '" + getObjectType(address) + "')");
}
}
@ -286,6 +306,18 @@ T * Environment::getObject(const unsigned int address) const
}
}
template <typename B, typename T>
T * Environment::getDerivedObject(const std::string name) const
{
return getDerivedObject<B, T>(getObjectAddress(name));
}
template <typename T>
T * Environment::getObject(const unsigned int address) const
{
return getDerivedObject<T, T>(address);
}
template <typename T>
T * Environment::getObject(const std::string name) const
{

View File

@ -91,6 +91,9 @@ static ns##mod##ModuleRegistrar ns##mod##ModuleRegistrarInstance;
#define envGet(type, name)\
*env().template getObject<type>(name)
#define envGetDerived(base, type, name)\
*env().template getDerivedObject<base, type>(name)
#define envGetTmp(type, var)\
type &var = *env().template getObject<type>(getName() + "_tmp_" + #var)