From fbeec6286b8c66a077a8059e1e406172d6409bc4 Mon Sep 17 00:00:00 2001
From: Mark Olesen <Mark.Olesen@esi-group.com>
Date: Tue, 16 Apr 2019 18:00:20 +0200
Subject: [PATCH] ENH: add Ostream output for std::vector

- convenient when using data structures from other codes
---
 applications/test/string/Test-string.C        |  6 +-
 src/OpenFOAM/containers/Lists/UList/UList.H   |  6 ++
 .../containers/Lists/UList/stdVectorIO.C      | 60 +++++++++++++++++++
 3 files changed, 70 insertions(+), 2 deletions(-)
 create mode 100644 src/OpenFOAM/containers/Lists/UList/stdVectorIO.C

diff --git a/applications/test/string/Test-string.C b/applications/test/string/Test-string.C
index d2584be13fc..37f49b9a7ac 100644
--- a/applications/test/string/Test-string.C
+++ b/applications/test/string/Test-string.C
@@ -2,7 +2,7 @@
   =========                 |
   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
    \\    /   O peration     |
-    \\  /    A nd           | Copyright (C) 2004-2011, 2016 OpenCFD Ltd.
+    \\  /    A nd           | Copyright (C) 2004-2011, 2016-2019 OpenCFD Ltd.
      \\/     M anipulation  |
 -------------------------------------------------------------------------------
                             | Copyright (C) 2011-2013 OpenFOAM Foundation
@@ -40,6 +40,7 @@ Description
 #include "Switch.H"
 #include "fileName.H"
 #include "stringList.H"
+#include "stringOps.H"
 
 using namespace Foam;
 
@@ -132,7 +133,8 @@ int main(int argc, char *argv[])
             string output(stringOps::expand(input));
 
             Info<< "input:  " << input  << nl
-                << "expand: " << output << nl << nl;
+                << "expand: " << output << nl
+                << "split: " << stringOps::split(output, "/") << nl << nl;
         }
     }
 
diff --git a/src/OpenFOAM/containers/Lists/UList/UList.H b/src/OpenFOAM/containers/Lists/UList/UList.H
index 3a31cfcbc58..e195c5b3a9e 100644
--- a/src/OpenFOAM/containers/Lists/UList/UList.H
+++ b/src/OpenFOAM/containers/Lists/UList/UList.H
@@ -58,6 +58,7 @@ SourceFiles
 #include <initializer_list>
 #include <iterator>
 #include <type_traits>
+#include <vector>
 
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
 
@@ -567,6 +568,10 @@ Ostream& operator<<(Ostream& os, const UList<T>& list)
     return list.writeList(os, Detail::ListPolicy::short_length<T>::value);
 }
 
+//- Write std::vector to Ostream
+template<class T>
+Ostream& operator<<(Ostream& os, const std::vector<T>& list);
+
 
 // * * * * * * * * * * * * * * Global Functions  * * * * * * * * * * * * * * //
 
@@ -666,6 +671,7 @@ struct sizeOp
 
 #ifdef NoRepository
     #include "UList.C"
+    #include "stdVectorIO.C"
 #endif
 
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
diff --git a/src/OpenFOAM/containers/Lists/UList/stdVectorIO.C b/src/OpenFOAM/containers/Lists/UList/stdVectorIO.C
new file mode 100644
index 00000000000..f1cfb363f24
--- /dev/null
+++ b/src/OpenFOAM/containers/Lists/UList/stdVectorIO.C
@@ -0,0 +1,60 @@
+/*---------------------------------------------------------------------------*\
+  =========                 |
+  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
+   \\    /   O peration     |
+    \\  /    A nd           | Copyright (C) 2019 OpenCFD Ltd.
+     \\/     M anipulation  |
+-------------------------------------------------------------------------------
+License
+    This file is part of OpenFOAM.
+
+    OpenFOAM is free software: you can redistribute it and/or modify it
+    under the terms of the GNU General Public License as published by
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+
+    OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
+    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+    for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with OpenFOAM.  If not, see <http://www.gnu.org/licenses/>.
+
+\*---------------------------------------------------------------------------*/
+
+#include "UList.H"
+#include "Ostream.H"
+#include "token.H"
+#include <vector>
+
+// * * * * * * * * * * * * * * * IOstream Operators  * * * * * * * * * * * * //
+
+template<class T>
+Foam::Ostream& Foam::operator<<(Ostream& os, const std::vector<T>& list)
+{
+    auto iter = list.cbegin();
+    const auto last = list.cend();
+
+    // Write ascii list contents, no breaks
+
+    os << label(list.size()) << token::BEGIN_LIST;
+
+    if (iter != last)
+    {
+        os << *iter;
+
+        for (++iter; iter != last; ++iter)
+        {
+            os << token::SPACE << *iter;
+        }
+    }
+
+    os << token::END_LIST;
+
+    os.check(FUNCTION_NAME);
+    return os;
+}
+
+
+// ************************************************************************* //
-- 
GitLab