OpenFOAM logo
The Open Source CFD Toolbox
  Source Guide OpenCFD Solutions Contact OpenFOAM

KeyedI.H

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002   =========                 |
00003   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
00004    \\    /   O peration     |
00005     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
00006      \\/     M anipulation  |
00007 -------------------------------------------------------------------------------
00008 License
00009     This file is part of OpenFOAM.
00010 
00011     OpenFOAM is free software; you can redistribute it and/or modify it
00012     under the terms of the GNU General Public License as published by the
00013     Free Software Foundation; either version 2 of the License, or (at your
00014     option) any later version.
00015 
00016     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
00017     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00019     for more details.
00020 
00021     You should have received a copy of the GNU General Public License
00022     along with OpenFOAM; if not, write to the Free Software Foundation,
00023     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
00024 
00025 \*---------------------------------------------------------------------------*/
00026 
00027 #include "IOstreams.H"
00028 
00029 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00030 
00031 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00032 
00033 template<class T>
00034 inline Foam::Keyed<T>::Keyed()
00035 :
00036     key_(-1)
00037 {}
00038 
00039 
00040 template<class T>
00041 inline Foam::Keyed<T>::Keyed(const T& item, const label key)
00042 :
00043     T(item),
00044     key_(key)
00045 {}
00046 
00047 
00048 template<class T>
00049 inline Foam::Keyed<T>::Keyed(const Xfer<T>& item, const label key)
00050 :
00051     T(item),
00052     key_(key)
00053 {}
00054 
00055 
00056 template<class T>
00057 inline Foam::Keyed<T>::Keyed(Istream& is)
00058 {
00059     is >> *this;
00060 }
00061 
00062 
00063 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00064 
00065 template<class T>
00066 inline Foam::label Foam::Keyed<T>::key() const
00067 {
00068     return key_;
00069 }
00070 
00071 template<class T>
00072 inline Foam::label& Foam::Keyed<T>::key()
00073 {
00074     return key_;
00075 }
00076 
00077 
00078 template<class T>
00079 inline Foam::List<Foam::Keyed<T> >
00080 Foam::Keyed<T>::createList(const List<T>& lst, const label key)
00081 {
00082     List<Keyed<T> > newList(lst.size());
00083 
00084     forAll (lst, elemI)
00085     {
00086         newList[elemI] = Keyed(lst[elemI], key);
00087     }
00088     return newList;
00089 }
00090 
00091 
00092 template<class T>
00093 inline Foam::List<Foam::Keyed<T> >
00094 Foam::Keyed<T>::createList(const List<T>& lst, const List<label>& keys)
00095 {
00096     if (lst.size() != keys.size())
00097     {
00098         FatalErrorIn
00099         (
00100             "Foam::Keyed<T>::createList(const List<T>&, const List<label>&)"
00101         )
00102             << "size mismatch adding keys to a list:" << nl
00103             << "List has size " << lst.size()
00104             << " and keys has size " << keys.size() << nl
00105             << abort(FatalError);
00106     }
00107 
00108     List<Keyed<T> > newList(lst.size());
00109 
00110     forAll (lst, elemI)
00111     {
00112         newList[elemI] = Keyed(lst[elemI], keys[elemI]);
00113     }
00114     return newList;
00115 }
00116 
00117 
00118 // * * * * * * * * * * * * * * * Ostream Operator  * * * * * * * * * * * * * //
00119 
00120 template<class T>
00121 inline Foam::Istream& Foam::operator>>(Istream& is, Keyed<T>& item)
00122 {
00123     // Read beginning of Keyed item/key pair
00124     is.readBegin("Keyed<T>");
00125 
00126     is >> static_cast<T&>(item);
00127     is >> item.key_;
00128 
00129     // Read end of Keyed item/key pair
00130     is.readEnd("Keyed<T>");
00131 
00132     // Check state of Ostream
00133     is.check("Istream& operator>>(Istream&, Keyed&)");
00134 
00135     return is;
00136 }
00137 
00138 
00139 template<class T>
00140 inline Foam::Ostream& Foam::operator<<(Ostream& os, const Keyed<T>& item)
00141 {
00142     os  << token::BEGIN_LIST
00143         << static_cast<const T&>(item)
00144         << token::SPACE << item.key_
00145         << token::END_LIST;
00146 
00147     return os;
00148 }
00149 
00150 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00151 
00152 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd