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-2021 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 #include "contiguous.H"
33 
34 // * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
35 
36 template<class T, class Addr>
38 (
39  Ostream& os,
40  const label shortLen
41 ) const
42 {
43  const IndirectListBase<T, Addr>& list = *this;
44 
45  const label len = list.size();
46 
47  if (os.format() == IOstream::BINARY && is_contiguous<T>::value)
48  {
49  // Binary and contiguous
50  os << nl << len << nl;
51 
52  if (len)
53  {
54  // The TOTAL number of bytes to be written.
55  // - possibly add start delimiter
56  os.beginRawWrite(len*sizeof(T));
57 
58  // Contents
59  for (label i=0; i < len; ++i)
60  {
61  os.writeRaw
62  (
63  reinterpret_cast<const char*>(&(list[i])),
64  sizeof(T)
65  );
66  }
67 
68  // End delimiter and/or cleanup.
69  os.endRawWrite();
70  }
71  }
72  else if (len > 1 && is_contiguous<T>::value && list.uniform())
73  {
74  // Two or more entries, and all entries have identical values.
75  os << len << token::BEGIN_BLOCK << list[0] << token::END_BLOCK;
76  }
77  else if
78  (
79  (len <= 1 || !shortLen)
80  ||
81  (
82  (len <= shortLen)
83  &&
84  (
87  )
88  )
89  )
90  {
91  // Single-line output
92 
93  // Size and start delimiter
94  os << len << token::BEGIN_LIST;
95 
96  // Contents
97  for (label i=0; i < len; ++i)
98  {
99  if (i) os << token::SPACE;
100  os << list[i];
101  }
102 
103  // End delimiter
104  os << token::END_LIST;
105  }
106  else
107  {
108  // Multi-line output
109 
110  // Size and start delimiter
111  os << nl << len << nl << token::BEGIN_LIST << nl;
112 
113  // Contents
114  for (label i=0; i < len; ++i)
115  {
116  os << list[i] << nl;
117  }
118 
119  // End delimiter
120  os << token::END_LIST << nl;
121  }
122 
123  os.check(FUNCTION_NAME);
124  return os;
125 }
126 
127 
128 // ************************************************************************* //
token.H
IndirectListBase.H
os
OBJstream os(runTime.globalPath()/outputName)
Foam::IndirectListBase::size
label size() const noexcept
The number of elements in the list.
Definition: IndirectListBase.H:125
T
const volScalarField & T
Definition: createFieldRefs.H:2
Ostream.H
Foam::nl
constexpr char nl
Definition: Ostream.H:404
Foam::Detail::ListPolicy::no_linebreak
Definition: ListPolicy.H:76
Foam::IndirectListBase::uniform
bool uniform() const
True if all entries have identical values, and list is non-empty.
Definition: IndirectListBaseI.H:78
contiguous.H
FUNCTION_NAME
#define FUNCTION_NAME
Definition: messageStream.H:295
Foam::IndirectListBase
Base for lists with indirect addressing, templated on the list contents type and the addressing type....
Definition: IndirectListBase.H:56
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::IndirectListBase::writeList
Ostream & writeList(Ostream &os, const label shortLen=0) const
Write List, with line-breaks in ASCII when length exceeds shortLen.
Definition: IndirectListBaseIO.C:38
Foam::is_contiguous
A template class to specify that a data type can be considered as being contiguous in memory.
Definition: contiguous.H:75