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-2025 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  label maxLen
39 ) const
40 {
41  if (maxLen <= 0)
42  {
43  maxLen = this->size();
44  }
45 
46  const label len = Foam::min(this->size(), maxLen);
47 
48  // The (output) size and start delimiter
49  os << nl << indent << maxLen << nl
50  << indent << token::BEGIN_LIST << nl;
51 
52  // const T* const * iter = this->cdata();
53  const auto* iter = this->cdata();
54 
55  // Contents
56  for (label i = 0; i < len; ++i)
57  {
58  os << indent << " " << Foam::name(iter[i]) << nl;
59  }
60  for (label i = len; i < maxLen; ++i)
61  {
62  os << indent << " [" << Foam::name(iter[i]) << ']' << nl;
63  }
64 
65  // End delimiter
66  os << indent << token::END_LIST << nl;
67 
69  return os;
70 }
71 
72 
73 template<class T>
75 (
76  Ostream& os,
77  const bool trimNull
78 ) const
79 {
80  const label len = this->size();
81 
82  // The net length, optionally after trimming any nullptr
83  const label netLen = (trimNull ? this->count_nonnull() : len);
84 
85  if (!netLen)
86  {
87  // 0-sized : can write with less vertical space
88  os << nl << indent << netLen
89  << token::BEGIN_LIST << token::END_LIST << nl;
90  return os;
91  }
92 
93  // The (output) size and start delimiter
94  os << nl << indent << netLen << nl
95  << indent << token::BEGIN_LIST << incrIndent << nl;
96 
97  // Contents
98  for (label i=0; i < len; ++i)
99  {
100  const T* ptr = (*this)[i];
101  if (ptr)
102  {
103  os << *ptr << nl;
104  }
105  else if (!trimNull)
106  {
108  << "cannot dereference nullptr at index " << i
109  << " in range [0," << len << ")"
110  << abort(FatalError);
111  }
112  }
113 
114  // End delimiter
115  os << decrIndent << indent << token::END_LIST << nl;
116 
117  os.check(FUNCTION_NAME);
118  return os;
119 }
120 
121 
122 // ************************************************************************* //
Ostream & indent(Ostream &os)
Indent stream.
Definition: Ostream.H:480
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:652
virtual bool check(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:45
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:50
Ostream & printAddresses(Ostream &os, label maxLen=-1) const
Write pointer values to Ostream (debugging only).
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for expressions::valueTypeCode::INVALID.
Definition: exprTraits.C:127
label min(const labelHashSet &set, label minValue=labelMax)
Find the min value in labelHashSet, optionally limited by second argument.
Definition: hashSets.C:26
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
#define FUNCTION_NAME
Ostream & decrIndent(Ostream &os)
Decrement the indent level.
Definition: Ostream.H:498
const volScalarField & T
Ostream & incrIndent(Ostream &os)
Increment the indent level.
Definition: Ostream.H:489