Skip to content
Snippets Groups Projects
Commit 501cd353 authored by Mark OLESEN's avatar Mark OLESEN
Browse files

ENH: consistent naming for ZoneMesh lookup method

- findZone(), cfindZone() to return pointer to existing or nullptr if
  not found. This fits with methods such as findObject() etc for other
  classes and can simplify code without checks for '-1' as not found.

- use simpler constructors for empty cell/face/point zones
parent be7a3f21
Branches
Tags
Loading
......@@ -487,36 +487,65 @@ Foam::label Foam::ZoneMesh<ZoneType, MeshType>::findZoneID
{
label zoneId = findIndexImpl(*this, zoneName);
if (zoneId >= 0)
if (zoneId < 0)
{
return zoneId;
DebugInFunction
<< "Zone named " << zoneName << " not found. "
<< "List of available zone names: " << names() << endl;
// Used for -dry-run, for example
if (disallowGenericZones != 0)
{
auto& zm = const_cast<ZoneMesh<ZoneType, MeshType>&>(*this);
zoneId = zm.size();
Info<< "Creating dummy zone " << zoneName << endl;
zm.append(new ZoneType(zoneName, zoneId, zm));
}
}
// Zone not found
DebugInFunction
<< "Zone named " << zoneName << " not found. "
<< "List of available zone names: " << names() << endl;
return zoneId;
}
if (disallowGenericZones != 0)
{
// Create a new ...
Info<< "Creating dummy zone " << zoneName << endl;
dictionary dict;
dict.set("type", ZoneType::typeName);
dict.set(ZoneType::labelsName, labelList());
template<class ZoneType, class MeshType>
const ZoneType* Foam::ZoneMesh<ZoneType, MeshType>::cfindZone
(
const word& zoneName
) const
{
const PtrList<ZoneType>& zones = *this;
// flipMap only really applicable for face zones, but should not get
// in the way for cell- and point-zones...
dict.set("flipMap", boolList());
for (auto iter = zones.begin(); iter != zones.end(); ++iter)
{
const ZoneType* ptr = iter.get();
if (ptr && zoneName == ptr->name())
{
return ptr;
}
}
// Used for -dry-run, for example
if (disallowGenericZones != 0)
{
auto& zm = const_cast<ZoneMesh<ZoneType, MeshType>&>(*this);
zoneId = zm.size();
zm.append(new ZoneType(zoneName, dict, zoneId, zm));
Info<< "Creating dummy zone " << zoneName << endl;
zm.append(new ZoneType(zoneName, zm.size(), zm));
}
return zoneId;
return nullptr;
}
template<class ZoneType, class MeshType>
ZoneType* Foam::ZoneMesh<ZoneType, MeshType>::findZone
(
const word& zoneName
)
{
return const_cast<ZoneType*>(this->cfindZone(zoneName));
}
......@@ -570,50 +599,6 @@ Foam::bitSet Foam::ZoneMesh<ZoneType, MeshType>::selection
}
template<class ZoneType, class MeshType>
const ZoneType* Foam::ZoneMesh<ZoneType, MeshType>::zonePtr
(
const word& zoneName
) const
{
const PtrList<ZoneType>& zones = *this;
for (auto iter = zones.begin(); iter != zones.end(); ++iter)
{
const ZoneType* ptr = iter.get();
if (ptr && zoneName == ptr->name())
{
return ptr;
}
}
return nullptr;
}
template<class ZoneType, class MeshType>
ZoneType* Foam::ZoneMesh<ZoneType, MeshType>::zonePtr
(
const word& zoneName
)
{
PtrList<ZoneType>& zones = *this;
for (auto iter = zones.begin(); iter != zones.end(); ++iter)
{
ZoneType* ptr = iter.get();
if (ptr && zoneName == ptr->name())
{
return ptr;
}
}
return nullptr;
}
template<class ZoneType, class MeshType>
void Foam::ZoneMesh<ZoneType, MeshType>::clearAddressing()
{
......@@ -801,34 +786,25 @@ ZoneType& Foam::ZoneMesh<ZoneType, MeshType>::operator()
const bool verbose
)
{
PtrList<ZoneType>& zones = *this;
ZoneType* ptr = findZone(zoneName);
label zoneId = findZoneID(zoneName);
const bool existing = bool(ptr);
if (zoneId < 0)
if (!ptr)
{
zoneId = zones.size();
zones.resize(zoneId+1);
zones.set(zoneId, new ZoneType(zoneName, zoneId, *this));
if (verbose)
{
Info<< ZoneType::typeName << " " << zoneName
<< " (new at index " << zoneId << ")"
<< endl;
}
ptr = new ZoneType(zoneName, this->size(), *this);
this->append(ptr);
}
else
if (verbose)
{
if (verbose)
{
Info<< ZoneType::typeName << " " << zoneName
<< " (existing at index " << zoneId << ")"
<< endl;
}
Info<< ZoneType::typeName << ' ' << zoneName
<< " (" << (existing ? "existing" : "new")
<< " at index " << ptr->index() << ')'
<< endl;
}
return zones[zoneId];
return *ptr;
}
......
......@@ -198,17 +198,21 @@ public:
//- Return zone indices for all matches
labelList indices(const wordRes& matcher) const;
//- Return zone index for the first match, return -1 if not found
//- Zone index for the first match, return -1 if not found
label findIndex(const keyType& key) const;
//- Return zone index for the first match, return -1 if not found
//- Zone index for the first match, return -1 if not found
label findIndex(const wordRes& matcher) const;
//- Find zone index given a name, return -1 if not found
//- Find zone index by name, return -1 if not found
label findZoneID(const word& zoneName) const;
//- Find zone by name and return const pointer, nullptr on error
const ZoneType* cfindZone(const word& zoneName) const;
//- Find zone by name and return pointer, nullptr on error
ZoneType* findZone(const word& zoneName);
//- Return all elements (cells, faces, points) contained in the
//- listed zones.
......@@ -229,13 +233,6 @@ public:
bitSet selection(const wordRes& matcher) const;
//- Lookup zone by name and return const pointer, nullptr on error.
const ZoneType* zonePtr(const word& zoneName) const;
//- Lookup zone by name and return pointer, nullptr on error.
ZoneType* zonePtr(const word& zoneName);
//- Clear addressing
void clearAddressing();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment