Commit 801828a0 authored by mattijs's avatar mattijs
Browse files

ENH:UList: read into UList

parent 8036ed63
......@@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011-2012 OpenFOAM Foundation
\\ / A nd | Copyright (C) 2011-2013 OpenFOAM Foundation
\\/ M anipulation |
-------------------------------------------------------------------------------
License
......@@ -58,6 +58,7 @@ template<class T> class SubList;
// Forward declaration of friend functions and operators
template<class T> class UList;
template<class T> Ostream& operator<<(Ostream&, const UList<T>&);
template<class T> Istream& operator>>(Istream&, UList<T>&);
typedef UList<label> labelUList;
......@@ -331,6 +332,14 @@ public:
Ostream&,
const UList<T>&
);
//- Read UList contents from Istream. Requires size to have been set
// before.
friend Istream& operator>> <T>
(
Istream&,
UList<T>&
);
};
template<class T>
......
......@@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011-2012 OpenFOAM Foundation
\\ / A nd | Copyright (C) 2011-2013 OpenFOAM Foundation
\\/ M anipulation |
-------------------------------------------------------------------------------
License
......@@ -26,6 +26,7 @@ License
#include "UList.H"
#include "Ostream.H"
#include "token.H"
#include "SLList.H"
#include "contiguous.H"
// * * * * * * * * * * * * * * * Ostream Operator * * * * * * * * * * * * * //
......@@ -137,4 +138,155 @@ Foam::Ostream& Foam::operator<<(Foam::Ostream& os, const Foam::UList<T>& L)
}
template<class T>
Foam::Istream& Foam::operator>>(Istream& is, UList<T>& L)
{
is.fatalCheck("operator>>(Istream&, UList<T>&)");
token firstToken(is);
is.fatalCheck("operator>>(Istream&, UList<T>&) : reading first token");
if (firstToken.isCompound())
{
List<T> elems;
elems.transfer
(
dynamicCast<token::Compound<List<T> > >
(
firstToken.transferCompoundToken(is)
)
);
// Check list length
label s = elems.size();
if (s != L.size())
{
FatalIOErrorIn("operator>>(Istream&, UList<T>&)", is)
<< "incorrect length for UList. Read " << s
<< " expected " << L.size()
<< exit(FatalIOError);
}
for (register label i=0; i<s; i++)
{
L[i] = elems[i];
}
}
else if (firstToken.isLabel())
{
label s = firstToken.labelToken();
// Set list length to that read
if (s != L.size())
{
FatalIOErrorIn("operator>>(Istream&, UList<T>&)", is)
<< "incorrect length for UList. Read " << s
<< " expected " << L.size()
<< exit(FatalIOError);
}
// Read list contents depending on data format
if (is.format() == IOstream::ASCII || !contiguous<T>())
{
// Read beginning of contents
char delimiter = is.readBeginList("List");
if (s)
{
if (delimiter == token::BEGIN_LIST)
{
for (register label i=0; i<s; i++)
{
is >> L[i];
is.fatalCheck
(
"operator>>(Istream&, UList<T>&) : reading entry"
);
}
}
else
{
T element;
is >> element;
is.fatalCheck
(
"operator>>(Istream&, UList<T>&) : "
"reading the single entry"
);
for (register label i=0; i<s; i++)
{
L[i] = element;
}
}
}
// Read end of contents
is.readEndList("List");
}
else
{
if (s)
{
is.read(reinterpret_cast<char*>(L.data()), s*sizeof(T));
is.fatalCheck
(
"operator>>(Istream&, UList<T>&) : reading the binary block"
);
}
}
}
else if (firstToken.isPunctuation())
{
if (firstToken.pToken() != token::BEGIN_LIST)
{
FatalIOErrorIn("operator>>(Istream&, UList<T>&)", is)
<< "incorrect first token, expected '(', found "
<< firstToken.info()
<< exit(FatalIOError);
}
// Putback the opening bracket
is.putBack(firstToken);
// Now read as a singly-linked list
SLList<T> sll(is);
if (sll.size() != L.size())
{
FatalIOErrorIn("operator>>(Istream&, UList<T>&)", is)
<< "incorrect length for UList. Read " << sll.size()
<< " expected " << L.size()
<< exit(FatalIOError);
}
// Convert the singly-linked list to this list
label i = 0;
for
(
typename SLList<T>::const_iterator iter = sll.begin();
iter != sll.end();
++iter
)
{
L[i] = iter();
}
}
else
{
FatalIOErrorIn("operator>>(Istream&, UList<T>&)", is)
<< "incorrect first token, expected <int> or '(', found "
<< firstToken.info()
<< exit(FatalIOError);
}
return is;
}
// ************************************************************************* //
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment