diff --git a/applications/test/objectRegistry/Test-objectRegistry.C b/applications/test/objectRegistry/Test-objectRegistry.C index c807ba9c047d254f42aa023977e5f61e3fd08c65..5ccdb3fe91b924171add0905f5550ae4d044bcdc 100644 --- a/applications/test/objectRegistry/Test-objectRegistry.C +++ b/applications/test/objectRegistry/Test-objectRegistry.C @@ -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 ); } } diff --git a/src/OpenFOAM/db/IOobjectList/IOobjectList.C b/src/OpenFOAM/db/IOobjectList/IOobjectList.C index 007b56dc0bfbde9fbcb24598d4cf1e3d5cd8f6fd..f58bab42140d96864c9a0eae8b0e1b2648e547a8 100644 --- a/src/OpenFOAM/db/IOobjectList/IOobjectList.C +++ b/src/OpenFOAM/db/IOobjectList/IOobjectList.C @@ -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; +} + + // ************************************************************************* // diff --git a/src/OpenFOAM/db/IOobjectList/IOobjectList.H b/src/OpenFOAM/db/IOobjectList/IOobjectList.H index 5a0177d197d5c2e2bcb1ab15c08c9b6bbff9b0e6..926135222fd8686869c881496c1537da01b13592 100644 --- a/src/OpenFOAM/db/IOobjectList/IOobjectList.H +++ b/src/OpenFOAM/db/IOobjectList/IOobjectList.H @@ -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; }; diff --git a/src/OpenFOAM/db/objectRegistry/objectRegistry.C b/src/OpenFOAM/db/objectRegistry/objectRegistry.C index 84df1a647023255b17fb86b0d6a5f819390cc19a..d72efda11cab30f7eecb11dbc88e7b807150af8a 100644 --- a/src/OpenFOAM/db/objectRegistry/objectRegistry.C +++ b/src/OpenFOAM/db/objectRegistry/objectRegistry.C @@ -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); } diff --git a/src/OpenFOAM/db/objectRegistry/objectRegistry.H b/src/OpenFOAM/db/objectRegistry/objectRegistry.H index bae145b04196885c76a128cdffa3e4a4cd77fe10..16dc0e17780da30f9381c0e8035b73d0e99125b5 100644 --- a/src/OpenFOAM/db/objectRegistry/objectRegistry.H +++ b/src/OpenFOAM/db/objectRegistry/objectRegistry.H @@ -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 diff --git a/src/OpenFOAM/db/objectRegistry/objectRegistryTemplates.C b/src/OpenFOAM/db/objectRegistry/objectRegistryTemplates.C index 0c26009482ef8adad9d3bd3eb5e7f0f3fc29cdf2..c5c583ad2666e24f3b6f37f5415c79d65e3923a8 100644 --- a/src/OpenFOAM/db/objectRegistry/objectRegistryTemplates.C +++ b/src/OpenFOAM/db/objectRegistry/objectRegistryTemplates.C @@ -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_; + return *ptr; } FatalErrorInFunction @@ -183,24 +211,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); }