PtrListDetailIO.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2018-2023 OpenCFD Ltd.
9 -------------------------------------------------------------------------------
10 License
11  This file is part of OpenFOAM.
12 
13  OpenFOAM is free software: you can redistribute it and/or modify it
14  under the terms of the GNU General Public License as published by
15  the Free Software Foundation, either version 3 of the License, or
16  (at your option) any later version.
17 
18  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21  for more details.
22 
23  You should have received a copy of the GNU General Public License
24  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25 
26 \*---------------------------------------------------------------------------*/
27 
28 #include "PtrListDetail.H"
29 #include "error.H"
30 #include "Ostream.H"
31 
32 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
33 
34 template<class T>
36 (
37  Ostream& os,
38  const bool trimNull
39 ) const
40 {
41  const label len = this->size();
42 
43  // The net length (after trimming any nullptr)
44  const label netLen = (trimNull ? this->count() : len);
45 
46  if (!netLen)
47  {
48  // 0-sized : can write with less vertical space
49  os << nl << indent << netLen
50  << token::BEGIN_LIST << token::END_LIST << nl;
51  return os;
52  }
53 
54  // The (output) size and start delimiter
55  os << nl << indent << netLen << nl
56  << indent << token::BEGIN_LIST << incrIndent << nl;
57 
58  // Contents
59  for (label i=0; i < len; ++i)
60  {
61  const T* ptr = (*this)[i];
62  if (ptr)
63  {
64  os << *ptr << nl;
65  }
66  else if (!trimNull)
67  {
69  << "cannot dereference nullptr at index " << i
70  << " in range [0," << len << ")"
71  << abort(FatalError);
72  }
73  }
74 
75  // End delimiter
76  os << decrIndent << indent << token::END_LIST << nl;
77 
78  os.check(FUNCTION_NAME);
79  return os;
80 }
81 
82 
83 // ************************************************************************* //
Ostream & indent(Ostream &os)
Indent stream.
Definition: Ostream.H:493
Ostream & write(Ostream &os, const bool trimNull=false) const
Write output, optionally silently trimming nullptrs.
error FatalError
Error stream (stdout output on all processes), with additional &#39;FOAM FATAL ERROR&#39; header text and sta...
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:598
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:50
unsigned int count(const UList< bool > &bools, const bool val=true)
Count number of &#39;true&#39; entries.
Definition: BitOps.H:73
errorManip< error > abort(error &err)
Definition: errorManip.H:139
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:56
OBJstream os(runTime.globalPath()/outputName)
#define FUNCTION_NAME
Ostream & decrIndent(Ostream &os)
Decrement the indent level.
Definition: Ostream.H:511
const volScalarField & T
Ostream & incrIndent(Ostream &os)
Increment the indent level.
Definition: Ostream.H:502