Commit eed44631 authored by Mark OLESEN's avatar Mark OLESEN
Browse files

Merge branch 'feature-objectRegistry' into 'develop'

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 = *Uptr;
          ...
      }

  Instead of

      if (mesh().foundObject<volScalarField>("U"))
      {
          const volScalarField& U = mesh().lookupObject<volScalarField>("U");
          ...
      }

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

      volScalarField* Uptr = mesh().lookupObjectRefPtr<volScalarField>("U");
      if (Uptr)
      {
          volScalarField& U = *Uptr;  // use as reference
          (*Uptr) = ...;              // or use directly
      }

  Instead of

      if (mesh().foundObject<volScalarField>("U"))
      {
          volScalarField& U = const_cast<volScalarField&>
          (
              mesh().lookupObject<volScalarField>("U")
          );
      }

--
sortedNames()
- now works with template parameters and with regular expression
  matching as well.
  For example,

      wordList names  = mesh().sortedNames();
      wordList fields = mesh().sortedName<volScalarField>();

  Instead of

      wordList names  = mesh().sortedNames();
      wordList fields = mesh().names<volScalarField>();
      Foam::sort(fields);

--

See merge request !83
parents a54e9f8d b4c998a2
......@@ -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 |
\\/ M anipulation | Copyright (C) 2016 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -27,6 +27,7 @@ License
#include "Time.H"
#include "OSspecific.H"
#include "IOList.H"
#include "stringListOps.H"
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
......@@ -228,7 +229,10 @@ Foam::wordList Foam::IOobjectList::sortedNames() const
}
Foam::wordList Foam::IOobjectList::names(const word& ClassName) const
Foam::wordList Foam::IOobjectList::names
(
const word& ClassName
) const
{
wordList objectNames(size());
......@@ -247,7 +251,34 @@ Foam::wordList Foam::IOobjectList::names(const word& ClassName) const
}
Foam::wordList Foam::IOobjectList::sortedNames(const word& ClassName) const
Foam::wordList Foam::IOobjectList::names
(
const word& ClassName,
const wordRe& matcher
) const
{
wordList objNames = names(ClassName);
return wordList(objNames, findStrings(matcher, objNames));
}
Foam::wordList Foam::IOobjectList::names
(
const word& ClassName,
const wordReList& matcher
) const
{
wordList objNames = names(ClassName);
return wordList(objNames, findStrings(matcher, objNames));
}
Foam::wordList Foam::IOobjectList::sortedNames
(
const word& ClassName
) const
{
wordList sortedLst = names(ClassName);
sort(sortedLst);
......@@ -256,4 +287,30 @@ Foam::wordList Foam::IOobjectList::sortedNames(const word& ClassName) const
}
Foam::wordList Foam::IOobjectList::sortedNames
(
const word& ClassName,
const wordRe& matcher
) const
{
wordList sortedLst = names(ClassName, matcher);
sort(sortedLst);
return sortedLst;
}
Foam::wordList Foam::IOobjectList::sortedNames
(
const word& ClassName,
const wordReList& matcher
) const
{
wordList sortedLst = names(ClassName, matcher);
sort(sortedLst);
return sortedLst;
}
// ************************************************************************* //
......@@ -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.
......@@ -55,7 +55,7 @@ class IOobjectList
// Private Member Functions
//- Disallow default bitwise assignment
void operator=(const IOobjectList&);
void operator=(const IOobjectList&) = delete;
public:
......@@ -104,17 +104,35 @@ public:
//- Return the list for all IOobjects of a given class
IOobjectList lookupClass(const word& className) const;
//- Return the list of names of the IOobjects
wordList names() const;
//- Return the sorted list of names of the IOobjects
wordList sortedNames() const;
//- A list of names of the IOobjects
wordList names() const;
//- Return the list of names of the IOobjects of given class
//- A list of names of IOobjects of the given class
wordList names(const word& className) const;
//- Return the sorted list of names of the IOobjects of given class
//- A list of names of IOobjects of the given class,
// and that also satisfy the input matcher
wordList names(const word& className, const wordRe&) const;
//- A list of names of IOobjects of the given class,
// and that also satisfy the input matchers
wordList names(const word& className, const wordReList&) const;
//- A sorted list of names of the IOobjects
wordList sortedNames() const;
//- A sorted list of names of IOobjects of given class
wordList sortedNames(const word& className) const;
//- A sorted list of names of IOobjects of the given class,
// and that also satisfy the input matcher
wordList sortedNames(const word& className, const wordRe&) const;
//- A sorted list of names of IOobjects of the given class,
// and that also satisfy the input matchers
wordList sortedNames(const word& className, const wordReList&) const;
};
......
......@@ -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;
}
......@@ -92,11 +127,7 @@ Foam::HashTable<const Type*> Foam::objectRegistry::lookupClass
forAllConstIter(HashTable<regIOobject*>, *this, iter)
{
if
(
(strict && isType<Type>(*iter()))
|| (!strict && isA<Type>(*iter()))
)
if (strict ? isType<Type>(*iter()) : isA<Type>(*iter()))
{
objectsOfClass.insert
(
......@@ -120,11 +151,7 @@ Foam::HashTable<Type*> Foam::objectRegistry::lookupClass
forAllIter(HashTable<regIOobject*>, *this, iter)
{
if
(
(strict && isType<Type>(*iter()))
|| (!strict && isA<Type>(*iter()))
)
if (strict ? isType<Type>(*iter()) : isA<Type>(*iter()))
{
objectsOfClass.insert
(
......@@ -139,40 +166,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_;