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-2019 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  // Write list contents depending on data format
48  if (os.format() == IOstream::ASCII || !is_contiguous<T>::value)
49  {
50  if (len > 1 && is_contiguous<T>::value && list.uniform())
51  {
52  // Two or more entries, and all entries have identical values.
53  os << len << token::BEGIN_BLOCK << list[0] << token::END_BLOCK;
54  }
55  else if
56  (
57  (len <= 1 || !shortLen)
58  ||
59  (
60  (len <= shortLen)
61  &&
62  (
65  )
66  )
67  )
68  {
69  // Size and start delimiter
70  os << len << token::BEGIN_LIST;
71 
72  // Contents
73  for (label i=0; i < len; ++i)
74  {
75  if (i) os << token::SPACE;
76  os << list[i];
77  }
78 
79  // End delimiter
80  os << token::END_LIST;
81  }
82  else
83  {
84  // Size and start delimiter
85  os << nl << len << nl << token::BEGIN_LIST << nl;
86 
87  // Contents
88  for (label i=0; i < len; ++i)
89  {
90  os << list[i] << nl;
91  }
92 
93  // End delimiter
94  os << token::END_LIST << nl;
95  }
96  }
97  else
98  {
99  // Contents are binary and contiguous
100  os << nl << len << nl;
101 
102  if (len)
103  {
104  // The TOTAL number of bytes to be written.
105  // - possibly add start delimiter
106  os.beginRawWrite(len*sizeof(T));
107 
108  // Contents
109  for (label i=0; i < len; ++i)
110  {
111  os.writeRaw
112  (
113  reinterpret_cast<const char*>(&(list[i])),
114  sizeof(T)
115  );
116  }
117 
118  // End delimiter and/or cleanup.
119  os.endRawWrite();
120  }
121  }
122 
123  os.check(FUNCTION_NAME);
124  return os;
125 }
126 
127 
128 // ************************************************************************* //
token.H
Foam::IOstreamOption::format
streamFormat format() const noexcept
Get the current stream format.
Definition: IOstreamOption.H:289
Foam::Ostream::beginRawWrite
virtual bool beginRawWrite(std::streamsize count)=0
Emit begin marker for low-level raw binary output.
IndirectListBase.H
Foam::IOstream::check
virtual bool check(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:51
T
const volScalarField & T
Definition: createFieldRefs.H:2
Foam::Ostream::endRawWrite
virtual bool endRawWrite()=0
Emit end marker for low-level raw binary output.
Ostream.H
Foam::nl
constexpr char nl
Definition: Ostream.H:385
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:270
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::Ostream::writeRaw
virtual Ostream & writeRaw(const char *data, std::streamsize count)=0
Low-level raw binary output.
Foam::IndirectListBase::size
label size() const
The number of elements in the list.
Definition: IndirectListBase.H:125
Foam::is_contiguous
A template class to specify that a data type can be considered as being contiguous in memory.
Definition: contiguous.H:75