UIndirectListIO.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2026 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "UIndirectList.H"
27 #include "Ostream.H"
28 #include "token.H"
29 #include "contiguous.H"
30 
31 // * * * * * * * * * * * * * * * IOstream Functions * * * * * * * * * * * * //
32 
33 template<class T>
35 {
36  writeListEntry(os, l);
37 }
38 
39 
40 // * * * * * * * * * * * * * * * Ostream Operator * * * * * * * * * * * * * //
41 
42 template<class T>
43 Foam::Ostream& Foam::operator<<
44 (
45  Foam::Ostream& os,
46  const Foam::UIndirectList<T>& L
47 )
48 {
49  // Write list contents depending on data format
50  if (os.format() == IOstream::ASCII || !contiguous<T>())
51  {
52  bool uniform = false;
53 
54  if (L.size() > 1 && contiguous<T>())
55  {
56  uniform = true;
57 
58  forAll(L, i)
59  {
60  if (L[i] != L[0])
61  {
62  uniform = false;
63  break;
64  }
65  }
66  }
67 
68  if (uniform)
69  {
70  // Write size and start delimiter
71  os << L.size() << token::BEGIN_BLOCK;
72 
73  // Write contents
74  os << L[0];
75 
76  // Write end delimiter
77  os << token::END_BLOCK;
78  }
79  else if (L.size() <= 1 || (L.size() < 11 && contiguous<T>()))
80  {
81  // Write size and start delimiter
82  os << L.size() << token::BEGIN_LIST;
83 
84  // Write contents
85  forAll(L, i)
86  {
87  if (i) os << token::SPACE;
88  os << L[i];
89  }
90 
91  // Write end delimiter
92  os << token::END_LIST;
93  }
94  else
95  {
96  // Write size and start delimiter
97  os << nl << L.size() << nl << token::BEGIN_LIST;
98 
99  // Write contents
100  forAll(L, i)
101  {
102  os << nl << L[i];
103  }
104 
105  // Write end delimiter
106  os << nl << token::END_LIST << nl;
107  }
108  }
109  else
110  {
111  // this is annoying, and wasteful, but there's currently no alternative
112 
113  os << nl << L.size() << nl;
114 
115  if (L.size())
116  {
117  List<T> lst = L();
118 
119  os.write
120  (
121  reinterpret_cast<const char*>(lst.cdata()),
122  lst.byteSize()
123  );
124  }
125  }
126 
127  // Check state of IOstream
128  os.check("Ostream& operator<<(Ostream&, const UIndirectList&)");
129 
130  return os;
131 }
132 
133 
134 // ************************************************************************* //
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:449
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:57
A List with indirect addressing.
Definition: UIndirectList.H:61
Motion of the mesh specified as a list of pointMeshMovers.
Template function to specify if the data of a type are contiguous.
void writeListEntry(Ostream &os, const ListType &l)
Definition: UListIO.C:35
void writeEntry(Ostream &os, const word &key, const DimensionedFieldFunction< DimensionedFieldType > &f)
static const char nl
Definition: Ostream.H:297