Loading...
Searching...
No Matches
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-------------------------------------------------------------------------------
10License
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
29#include "error.H"
30#include "Ostream.H"
31
32// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
33
34template<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
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;
68 os.check(FUNCTION_NAME);
69 return os;
70}
71
72
73template<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
90 return os;
91 }
92
93 // The (output) size and start delimiter
94 os << nl << indent << netLen << 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 & write(Ostream &os, const bool trimNull=false) const
Write output, optionally silently trimming nullptrs.
Ostream & printAddresses(Ostream &os, label maxLen=-1) const
Write pointer values to Ostream (debugging only).
label count_nonnull() const noexcept
The number of non-nullptr entries in the list.
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition Ostream.H:59
const T * cdata() const noexcept
Return pointer to the underlying array serving as data storage.
Definition UListI.H:267
void size(const label n)
Older name for setAddressableSize.
Definition UList.H:118
@ BEGIN_LIST
Begin list [isseparator].
Definition token.H:174
@ END_LIST
End list [isseparator].
Definition token.H:175
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition error.H:600
OBJstream os(runTime.globalPath()/outputName)
#define FUNCTION_NAME
Ostream & incrIndent(Ostream &os)
Increment the indent level.
Definition Ostream.H:490
Ostream & indent(Ostream &os)
Indent stream.
Definition Ostream.H:481
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
error FatalError
Error stream (stdout output on all processes), with additional 'FOAM FATAL ERROR' header text and sta...
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for expressions::valueTypeCode::INVALID.
Definition exprTraits.C:127
Ostream & decrIndent(Ostream &os)
Decrement the indent level.
Definition Ostream.H:499
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
constexpr char nl
The newline '\n' character (0x0a).
Definition Ostream.H:50