From 8526e44884f778e17baedb1ebe19e29de453c39a Mon Sep 17 00:00:00 2001
From: Mark Olesen <Mark.Olesen@esi-group.com>
Date: Mon, 22 Jun 2020 09:53:51 +0200
Subject: [PATCH] ENH: dlLibraryTable InfoProxy output (#1735)

---
 .../foamHasLibrary/foamHasLibrary.C           | 21 +++++---
 .../dlLibraryTable/dlLibraryTable.C           | 39 +++++++++++++--
 .../dlLibraryTable/dlLibraryTable.H           | 50 ++++++++++++++-----
 3 files changed, 87 insertions(+), 23 deletions(-)

diff --git a/applications/utilities/miscellaneous/foamHasLibrary/foamHasLibrary.C b/applications/utilities/miscellaneous/foamHasLibrary/foamHasLibrary.C
index 931fc3677e0..07243cac400 100644
--- a/applications/utilities/miscellaneous/foamHasLibrary/foamHasLibrary.C
+++ b/applications/utilities/miscellaneous/foamHasLibrary/foamHasLibrary.C
@@ -40,6 +40,9 @@ Usage
         Success if any of the libraries can be loaded.
         Does not short-circuit.
 
+      - \par -detail
+        Additional detail (meaning may change).
+
       - \par -verbose
         Additional verbosity
 
@@ -71,6 +74,11 @@ int main(int argc, char *argv[])
         "(does not short-circuit)"
     );
     argList::addBoolOption
+    (
+        "detail",
+        "Additional detail"
+    );
+    argList::addBoolOption
     (
         "verbose",
         "Additional verbosity"
@@ -85,6 +93,7 @@ int main(int argc, char *argv[])
     #include "foamDlOpenLibs.H"
 
     const bool testOr = args.found("or");
+    const bool detail = args.found("detail");
     const bool verbose = args.found("verbose");
 
     label ngood = 0;
@@ -134,14 +143,12 @@ int main(int argc, char *argv[])
         }
     }
 
-    const bool okay
-    (
-        testOr
-      ? (ngood > 0 || nbad == 0)
-      : nbad == 0
-    );
+    if (detail)
+    {
+        InfoErr << libs.info();
+    }
 
-    return okay ? 0 : 1;
+    return (nbad == 0 || (testOr && ngood > 0)) ? 0 : 1;
 }
 
 
diff --git a/src/OpenFOAM/db/dynamicLibrary/dlLibraryTable/dlLibraryTable.C b/src/OpenFOAM/db/dynamicLibrary/dlLibraryTable/dlLibraryTable.C
index da0979a6f64..fc58b077c9f 100644
--- a/src/OpenFOAM/db/dynamicLibrary/dlLibraryTable/dlLibraryTable.C
+++ b/src/OpenFOAM/db/dynamicLibrary/dlLibraryTable/dlLibraryTable.C
@@ -6,7 +6,7 @@
      \\/     M anipulation  |
 -------------------------------------------------------------------------------
     Copyright (C) 2011-2017 OpenFOAM Foundation
-    Copyright (C) 2018-2019 OpenCFD Ltd.
+    Copyright (C) 2018-2020 OpenCFD Ltd.
 -------------------------------------------------------------------------------
 License
     This file is part of OpenFOAM.
@@ -312,11 +312,13 @@ bool Foam::dlLibraryTable::close
         return false;
     }
 
+    void* ptr = libPtrs_[index];
+
     DebugInFunction
         << "Closing " << libName
-        << " with handle " << Foam::name(libPtrs_[index]) << nl;
+        << " with handle " << Foam::name(ptr) << nl;
 
-    const bool ok = Foam::dlClose(libPtrs_[index]);
+    const bool ok = Foam::dlClose(ptr);
 
     libPtrs_[index] = nullptr;
     libNames_[index].clear();
@@ -367,4 +369,35 @@ bool Foam::dlLibraryTable::open
 }
 
 
+// * * * * * * * * * * * * * * * IOstream Operators  * * * * * * * * * * * * //
+
+template<>
+Foam::Ostream& Foam::operator<<
+(
+    Ostream& os,
+    const InfoProxy<dlLibraryTable>& ip
+)
+{
+    const dlLibraryTable& tbl = ip.t_;
+
+    os << token::BEGIN_LIST << nl;
+
+    // Lengths of pointers/names are guaranteed interally to be identical
+    forAll(tbl.pointers(), i)
+    {
+        const void* ptr = tbl.pointers()[i];
+        const fileName& libName = tbl.names()[i];
+
+        // Also write out empty filenames
+        // (specified with '-lib' but did not load)
+
+        os  << Foam::name(ptr) << token::SPACE << libName << nl;
+    }
+
+    os << token::END_LIST << nl;
+
+    return os;
+}
+
+
 // ************************************************************************* //
diff --git a/src/OpenFOAM/db/dynamicLibrary/dlLibraryTable/dlLibraryTable.H b/src/OpenFOAM/db/dynamicLibrary/dlLibraryTable/dlLibraryTable.H
index 26b5c57e857..fb988f478a1 100644
--- a/src/OpenFOAM/db/dynamicLibrary/dlLibraryTable/dlLibraryTable.H
+++ b/src/OpenFOAM/db/dynamicLibrary/dlLibraryTable/dlLibraryTable.H
@@ -6,7 +6,7 @@
      \\/     M anipulation  |
 -------------------------------------------------------------------------------
     Copyright (C) 2011-2016 OpenFOAM Foundation
-    Copyright (C) 2018-2019 OpenCFD Ltd.
+    Copyright (C) 2018-2020 OpenCFD Ltd.
 -------------------------------------------------------------------------------
 License
     This file is part of OpenFOAM.
@@ -32,6 +32,7 @@ Description
 
 SourceFiles
     dlLibraryTable.C
+    dlLibraryTableTemplates.C
 
 \*---------------------------------------------------------------------------*/
 
@@ -39,6 +40,7 @@ SourceFiles
 #define dlLibraryTable_H
 
 #include "DynamicList.H"
+#include "InfoProxy.H"
 
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
 
@@ -64,26 +66,32 @@ class dlLibraryTable
         //  Warning messages, but no additional side-effects.
         void* openLibrary(const fileName& libName, bool verbose);
 
-        //- No copy construct
-        dlLibraryTable(const dlLibraryTable&) = delete;
-
-        //- No copy assignment
-        void operator=(const dlLibraryTable&) = delete;
-
 
 public:
 
     // Declare name of the class and its debug switch
     ClassName("dlLibraryTable");
 
-    // Constructors
+    // Generated Methods
 
-        //- Construct null
+        //- Default construct
         dlLibraryTable() = default;
 
+        //- No copy construct
+        dlLibraryTable(const dlLibraryTable&) = delete;
+
         //- Move construct
         dlLibraryTable(dlLibraryTable&&) = default;
 
+        //- No copy assignment
+        void operator=(const dlLibraryTable&) = delete;
+
+        //- Move assignment
+        dlLibraryTable& operator=(dlLibraryTable&&) = default;
+
+
+    // Constructors
+
         //- Open specified libraries. Ignores duplicate names.
         explicit dlLibraryTable
         (
@@ -102,12 +110,24 @@ public:
 
     // Member Functions
 
-        //- True if no there are no libraries loaded by the table
+        //- True if there are no libraries loaded by the table
         bool empty() const;
 
         //- The number of libraries loaded by the table
         label size() const;
 
+        //- Names of the libraries in use, or requested
+        const UList<fileName>& names() const
+        {
+            return libNames_;
+        }
+
+        //- Pointers to the libraries in use. Access with caution.
+        const UList<void*>& pointers() const
+        {
+            return libPtrs_;
+        }
+
         //- Clearing closes all libraries loaded by the table.
         void clear(bool verbose = true);
 
@@ -152,10 +172,14 @@ public:
         );
 
 
-    // Member Operators
+    // Info
 
-        //- Move assignment
-        dlLibraryTable& operator=(dlLibraryTable&&) = default;
+        //- Return info proxy.
+        //  Used to print library table information to a stream
+        InfoProxy<dlLibraryTable> info() const
+        {
+            return *this;
+        }
 };
 
 
-- 
GitLab