Commit 92fa5a19 authored by Mark Olesen's avatar Mark Olesen
Browse files

ENH: improve objectRegistry functionality (issue #322)

- Recursive searching for objects within a registry is now optional
  (previous it was always done).

  A recursive search effectively blocks the construction of sub-sub-registries
  if their names are 'masked' by some parent level sub-registry with
  the same name! (BUG)

- Recursive search is now turned OFF by default, which makes it consistent
  with dictionary and probably causes the least number of surprises.

----
Various new convenience methods added:

lookupObjectRef()
- returns a non-const reference.
  For example,

      volScalarField& U = mesh().lookupObjectRef<volScalarField>("U");

  Instead of

      volScalarField& U = const_cast<volScalarField&>
      (
          mesh().lookupObject<volScalarField>("U")
      );

--
lookupObjectPtr()
- returns a const pointer, and nullptr on failure.
  For example,

      const volScalarField* Uptr = mesh().lookupObjectPtr<volScalarField>("U");
      if (Uptr)
      {
          const volScalarField& U...
parent 8628ddac
......@@ -76,9 +76,8 @@ void printRegistry
Foam::label indent
)
{
hashedWordList regs = obr.names<objectRegistry>();
regs.sort();
wordList names = obr.sortedNames();
hashedWordList regs = obr.sortedNames<objectRegistry>();
std::string prefix;
for (label i=indent; i; --i)
......@@ -121,7 +120,8 @@ void printRegistry
const word& name = regs[i];
const objectRegistry& next = obr.lookupObject<objectRegistry>
(
name
name,
recursive
);
os << prefix.c_str()
......@@ -158,13 +158,13 @@ int main(int argc, char *argv[])
"skip",
"skip some parts"
);
// argList::validArgs.append("recursive (true|false)");
argList::validArgs.append("recursive (true|false)");
#include "setRootCase.H"
#include "createTime.H"
#include "createPolyMesh.H"
// recursive = Switch(args[1]);
recursive = Switch(args[1]);
const bool optMesh = args.optionFound("mesh");
const bool optSkip = args.optionFound("skip");
......@@ -183,7 +183,8 @@ int main(int argc, char *argv[])
db.subRegistry
(
entryName,
true
true,
recursive
);
}
......@@ -200,7 +201,8 @@ int main(int argc, char *argv[])
const objectRegistry& subreg = db.subRegistry
(
regName,
true
true,
recursive
);
for (label j = 0; j < 3; ++j)
......@@ -210,12 +212,14 @@ int main(int argc, char *argv[])
subreg.subRegistry
(
entryName,
true
true,
recursive
);
subreg.subRegistry
(
"$" + entryName, // qualified to avoid collisions
true
true,
recursive
);
}
}
......@@ -231,7 +235,8 @@ int main(int argc, char *argv[])
db.subRegistry
(
entryName,
true
true,
recursive
);
}
......@@ -249,7 +254,8 @@ int main(int argc, char *argv[])
const objectRegistry& subreg = db.subRegistry
(
regName,
false
false,
recursive
);
if (!optSkip)
......@@ -261,7 +267,8 @@ int main(int argc, char *argv[])
subreg.subRegistry
(
entryName,
true
true,
recursive
);
}
}
......
......@@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation
\\/ M anipulation | Copyright (C) 2015 OpenCFD Ltd.
\\/ M anipulation | Copyright (C) 2015-2016 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -155,12 +155,13 @@ Foam::wordList Foam::objectRegistry::sortedNames(const word& ClassName) const
const Foam::objectRegistry& Foam::objectRegistry::subRegistry
(
const word& name,
const bool forceCreate
const bool forceCreate,
const bool recursive
) const
{
if (forceCreate && !foundObject<objectRegistry>(name))
if (forceCreate && !foundObject<objectRegistry>(name, recursive))
{
objectRegistry* fieldsCachePtr = new objectRegistry
objectRegistry* subObr = new objectRegistry
(
IOobject
(
......@@ -171,9 +172,10 @@ const Foam::objectRegistry& Foam::objectRegistry::subRegistry
IOobject::NO_WRITE
)
);
fieldsCachePtr->store();
subObr->store();
}
return lookupObject<objectRegistry>(name);
return lookupObject<objectRegistry>(name, recursive);
}
......
......@@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation
\\/ M anipulation |
\\/ M anipulation | Copyright (C) 2016 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -29,6 +29,7 @@ Description
SourceFiles
objectRegistry.C
objectRegistryTemplates.C
\*---------------------------------------------------------------------------*/
......@@ -75,10 +76,10 @@ class objectRegistry
bool parentNotTime() const;
//- Disallow Copy constructor
objectRegistry(const objectRegistry&);
objectRegistry(const objectRegistry&) = delete;
//- Disallow default bitwise copy construct and assignment
void operator=(const objectRegistry&);
void operator=(const objectRegistry&) = delete;
public:
......@@ -132,38 +133,58 @@ public:
return dbDir_;
}
//- Return the list of names of the IOobjects
//- A list of names of the objects
wordList names() const;
//- Return the sorted list of names of the IOobjects
//- A sorted list of names of the objects
wordList sortedNames() const;
//- Return the list of names of IOobjects of given class name
//- A list of names of objects that have the given class name
wordList names(const word& className) const;
//- Return the sorted list of names of IOobjects of given class name
//- A sorted list of names of objects that have the given class name
wordList sortedNames(const word& className) const;
//- Return the list of names of the IOobjects of given type
//- A list of names of objects that have the given type
template<class Type>
wordList names() const;
//- Return the list of objects whose name matches the input regExp
//- A list of names of objects that have the given type,
// and that also satisfy the input matcher
template<class Type>
wordList names(const wordRe& name) const;
wordList names(const wordRe&) const;
//- Return the list of objects whose name matches the input regExp
//- A list of names for objects that have the given type,
// and that also satisfy the input matchers
template<class Type>
wordList names(const wordReList& name) const;
wordList names(const wordReList&) const;
//- Lookup and return a const sub-objectRegistry. Optionally create
// it if it does not exist.
//- A sorted list of names of objects that have the given type
template<class Type>
wordList sortedNames() const;
//- A sorted list of names of objects that have the given type,
// and that also satisfy the input matcher
template<class Type>
wordList sortedNames(const wordRe&) const;
//- A sorted list of names of objects that have the given type,
// and that also satisfy the input matchers
template<class Type>
wordList sortedNames(const wordReList&) const;
//- Lookup and return a const sub-objectRegistry.
// Optionally create it if it does not exist.
// If recursive, search parent registries.
const objectRegistry& subRegistry
(
const word& name,
const bool forceCreate = false
const bool forceCreate = false,
const bool recursive = false
) const;
//- Lookup and return all objects of the given Type
template<class Type>
HashTable<const Type*> lookupClass(const bool strict = false) const;
......@@ -173,12 +194,56 @@ public:
HashTable<Type*> lookupClass(const bool strict = false);
//- Is the named Type found?
// If recursive, search parent registries.
template<class Type>
bool foundObject(const word& name) const;
bool foundObject
(
const word& name,
const bool recursive = false
) const;
//- Lookup and return the object of the given Type
//- Lookup and return the object of the given Type.
// If recursive, search parent registries.
template<class Type>
const Type& lookupObject(const word& name) const;
const Type& lookupObject
(
const word& name,
const bool recursive = false
) const;
//- Lookup and return the object of the given Type.
// If recursive, search parent registries.
template<class Type>
Type& lookupObjectRef
(
const word& name,
const bool recursive = false
) const;
//- Lookup and return pointer to the object of the given Type,
// otherwise nullptr if the object was not found,
// or had the incorrect type.
// If recursive, search parent registries.
template<class Type>
const Type* lookupObjectPtr
(
const word& name,
const bool recursive = false
) const;
//- Lookup and return non-const pointer to the object
// of the given Type,
// otherwise nullptr if the object was not found,
// or had the incorrect type.
// If recursive, search parent registries.
template<class Type>
Type* lookupObjectRefPtr
(
const word& name,
const bool recursive = false
) const;
//- Return new event number.
label getEvent() const;
......@@ -195,6 +260,7 @@ public:
//- Remove an regIOobject from registry
bool checkOut(regIOobject&) const;
// Reading
//- Return true if any of the object's files have been modified
......
......@@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011-2015 OpenFOAM Foundation
\\/ M anipulation |
\\/ M anipulation | Copyright (C) 2016 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -49,7 +49,7 @@ Foam::wordList Foam::objectRegistry::names() const
template<class Type>
Foam::wordList Foam::objectRegistry::names(const wordRe& name) const
Foam::wordList Foam::objectRegistry::names(const wordRe& matcher) const
{
wordList objectNames(size());
......@@ -60,7 +60,7 @@ Foam::wordList Foam::objectRegistry::names(const wordRe& name) const
{
const word& objectName = iter()->name();
if (name.match(objectName))
if (matcher.match(objectName))
{
objectNames[count++] = objectName;
}
......@@ -74,11 +74,46 @@ Foam::wordList Foam::objectRegistry::names(const wordRe& name) const
template<class Type>
Foam::wordList Foam::objectRegistry::names(const wordReList& patterns) const
Foam::wordList Foam::objectRegistry::names(const wordReList& matcher) const
{
wordList names(this->names<Type>());
return wordList(names, findStrings(patterns, names));
return wordList(names, findStrings(matcher, names));
}
template<class Type>
Foam::wordList Foam::objectRegistry::sortedNames() const
{
wordList sorted(this->names<Type>());
sort(sorted);
return sorted;
}
template<class Type>
Foam::wordList Foam::objectRegistry::sortedNames
(
const wordRe& match
) const
{
wordList sorted(this->names<Type>(match));
sort(sorted);
return sorted;
}
template<class Type>
Foam::wordList Foam::objectRegistry::sortedNames
(
const wordReList& matcher
) const
{
wordList sorted(this->names<Type>(matcher));
sort(sorted);
return sorted;
}
......@@ -139,40 +174,41 @@ Foam::HashTable<Type*> Foam::objectRegistry::lookupClass
template<class Type>
bool Foam::objectRegistry::foundObject(const word& name) const
bool Foam::objectRegistry::foundObject
(
const word& name,
const bool recursive
) const
{
const_iterator iter = find(name);
const Type* ptr = this->lookupObjectPtr<Type>(name, recursive);
if (iter != end())
if (ptr)
{
const Type* vpsiPtr_ = dynamic_cast<const Type*>(iter());
if (vpsiPtr_)
{
return true;
}
return true;
}
else if (this->parentNotTime())
else
{
return parent_.foundObject<Type>(name);
return false;
}
return false;
}
template<class Type>
const Type& Foam::objectRegistry::lookupObject(const word& name) const
const Type& Foam::objectRegistry::lookupObject
(
const word& name,
const bool recursive
) const
{
const_iterator iter = find(name);
if (iter != end())
{
const Type* vpsiPtr_ = dynamic_cast<const Type*>(iter());
const Type* ptr = dynamic_cast<const Type*>(iter());
if (vpsiPtr_)
if (ptr)
{
return *vpsiPtr_;
return *ptr;
}
FatalErrorInFunction
......@@ -183,24 +219,75 @@ const Type& Foam::objectRegistry::lookupObject(const word& name) const
<< ", it is a " << iter()->type()
<< abort(FatalError);
}
else
else if (recursive && this->parentNotTime())
{
return parent_.lookupObject<Type>(name, recursive);
}
FatalErrorInFunction
<< nl
<< " request for " << Type::typeName
<< " " << name << " from objectRegistry " << this->name()
<< " failed\n available objects of type " << Type::typeName
<< " are" << nl
<< names<Type>()
<< abort(FatalError);
return NullObjectRef<Type>();
}
template<class Type>
Type& Foam::objectRegistry::lookupObjectRef
(
const word& name,
const bool recursive
) const
{
const Type& ref = this->lookupObject<Type>(name, recursive);
// The above will already fail if things didn't work
return const_cast<Type&>(ref);
}
template<class Type>
const Type* Foam::objectRegistry::lookupObjectPtr
(
const word& name,
const bool recursive
) const
{
const_iterator iter = find(name);
if (iter != end())
{
if (this->parentNotTime())
const Type* ptr = dynamic_cast<const Type*>(iter());
if (ptr)
{
return parent_.lookupObject<Type>(name);
return ptr;
}
FatalErrorInFunction
<< nl
<< " request for " << Type::typeName
<< " " << name << " from objectRegistry " << this->name()
<< " failed\n available objects of type " << Type::typeName
<< " are" << nl
<< names<Type>()
<< abort(FatalError);
}
else if (recursive && this->parentNotTime())
{
return parent_.lookupObjectPtr<Type>(name, recursive);
}
return NullObjectRef<Type>();
return nullptr;
}
template<class Type>
Type* Foam::objectRegistry::lookupObjectRefPtr
(
const word& name,
const bool recursive
) const
{
const Type* ptr = this->lookupObjectPtr<Type>(name, recursive);
return const_cast<Type*>(ptr);
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment