IndirectListBaseIO.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) 2011-2014 OpenFOAM Foundation
9  Copyright (C) 2016-2025 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 \*---------------------------------------------------------------------------*/
28 
29 #include "IndirectListBase.H"
30 #include "Ostream.H"
31 #include "token.H"
32 
33 // * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
34 
35 template<class T, class Addr>
37 (
38  Ostream& os,
39  const label shortLen
40 ) const
41 {
42  const IndirectListBase<T, Addr>& list = *this;
43 
44  const label len = list.size();
45 
46  if (os.format() == IOstreamOption::BINARY && is_contiguous_v<T>)
47  {
48  // Binary and contiguous
49  os << nl << len << nl;
50 
51  if (len)
52  {
53  // The TOTAL number of bytes to be written.
54  // - possibly add start delimiter
55  os.beginRawWrite(len*sizeof(T));
56 
57  // Contents
58  for (label i=0; i < len; ++i)
59  {
60  os.writeRaw
61  (
62  reinterpret_cast<const char*>(&(list[i])),
63  sizeof(T)
64  );
65  }
66 
67  // End delimiter and/or cleanup.
68  os.endRawWrite();
69  }
70  }
71  else if (is_contiguous_v<T> && len > 1 && list.uniform())
72  {
73  // Two or more entries, and all entries have identical values.
74  os << len << token::BEGIN_BLOCK << list[0] << token::END_BLOCK;
75  }
76  else if
77  (
78  (len <= 1 || !shortLen)
79  ||
80  (
81  (len <= shortLen)
82  && (is_contiguous_v<T> || Foam::ListPolicy::no_linebreak<T>::value)
83  )
84  )
85  {
86  // Single-line output
87 
88  // Size and start delimiter
89  os << len << token::BEGIN_LIST;
90 
91  // Contents
92  for (label i=0; i < len; ++i)
93  {
94  if (i) os << token::SPACE;
95  os << list[i];
96  }
97 
98  // End delimiter
99  os << token::END_LIST;
100  }
101  else
102  {
103  // Multi-line output
104 
105  // Size and start delimiter
106  os << nl << len << nl << token::BEGIN_LIST << nl;
107 
108  // Contents
109  for (label i=0; i < len; ++i)
110  {
111  os << list[i] << nl;
112  }
113 
114  // End delimiter
115  os << token::END_LIST << nl;
116  }
117 
118  os.check(FUNCTION_NAME);
119  return os;
120 }
121 
122 
123 // ************************************************************************* //
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:50
Base for lists with indirect addressing, templated on the list contents type and the addressing type...
bool uniform() const
True if all entries have identical values, and list is non-empty.
label size() const noexcept
The number of elements in the list.
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
const volScalarField & T
Ostream & writeList(Ostream &os, const label shortLen=0) const
Write List, with line-breaks in ASCII when length exceeds shortLen.
Can suppress additional line breaks separate ASCII data content when the data elements are primitives...
Definition: ListPolicy.H:73