$treeview $search $mathjax
Palabos  Version 1.1
$projectbrief
$projectbrief
$searchbox

serializerIO.hh

Go to the documentation of this file.
00001 /* This file is part of the Palabos library.
00002  *
00003  * Copyright (C) 2011 FlowKit Sarl
00004  * Avenue de Chailly 23
00005  * 1012 Lausanne, Switzerland
00006  * E-mail contact: contact@flowkit.com
00007  *
00008  * The most recent release of Palabos can be downloaded at 
00009  * <http://www.palabos.org/>
00010  *
00011  * The library Palabos is free software: you can redistribute it and/or
00012  * modify it under the terms of the GNU Affero General Public License as
00013  * published by the Free Software Foundation, either version 3 of the
00014  * License, or (at your option) any later version.
00015  *
00016  * The library is distributed in the hope that it will be useful,
00017  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  * GNU Affero General Public License for more details.
00020  *
00021  * You should have received a copy of the GNU Affero General Public License
00022  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00023 */
00024 
00025 #ifndef SERIALIZER_IO_HH
00026 #define SERIALIZER_IO_HH
00027 
00028 #include "io/serializerIO.h"
00029 #include "core/serializer.h"
00030 #include <iosfwd>
00031 #include <iomanip>
00032 
00033 namespace plb {
00034 
00035 
00036 /* *************** Class AsciiWriter ******************************** */
00037 
00038 template<typename T>
00039 AsciiWriter<T>::AsciiWriter(std::ostream* ostr_, plint numDigits_)
00040     : ostr(ostr_),
00041       numDigits(numDigits_)
00042 { }
00043 
00044 template<typename T>
00045 AsciiWriter<T>* AsciiWriter<T>::clone() const {
00046     return new AsciiWriter<T>(*this);
00047 }
00048 
00049 template<typename T>
00050 void AsciiWriter<T>::writeHeader(pluint dataSize)
00051 { }
00052 
00053 template<typename T>
00054 void AsciiWriter<T>::writeData(char const* dataBuffer, pluint bufferSize) {
00055     T tmp;
00056     for (pluint iData=0; iData<bufferSize; iData+=sizeof(T))
00057     {
00058         for (pluint iVal=0; iVal<sizeof(T); ++iVal) {
00059             *((char*)&tmp+iVal) = dataBuffer[iData+iVal];
00060         }
00061         if (numDigits==0) {
00062             (*ostr) << tmp << " ";
00063         }
00064         else {
00065             (*ostr) << std::setprecision(numDigits) << tmp << " ";
00066         }
00067     }
00068 }
00069 
00070 /* *************** Class AsciiReader ******************************** */
00071 
00072 
00073 template<typename T>
00074 AsciiReader<T>::AsciiReader(std::istream* istr_)
00075     : istr(istr_)
00076 { }
00077 
00078 template<typename T>
00079 AsciiReader<T>* AsciiReader<T>::clone() const {
00080     return new AsciiReader<T>(*this);
00081 }
00082 
00083 template<typename T>
00084 void AsciiReader<T>::readHeader(pluint dataSize) const
00085 { }
00086 
00087 template<typename T>
00088 void AsciiReader<T>::readData(char* dataBuffer, pluint bufferSize) const
00089 {
00090     T tmp;
00091     // Read the input data.
00092     for (pluint iData=0; iData<bufferSize; iData+=sizeof(T))
00093     {
00094         (*istr) >> tmp;
00095         for (pluint iVal=0; iVal<sizeof(T); ++iVal) {
00096             dataBuffer[iData+iVal] = *( (char*)&tmp+iVal );
00097         }
00098     }
00099 }
00100 
00101 
00102 
00103 template<typename T>
00104 void serializerToAsciiStream(DataSerializer const* serializer, std::ostream* ostr, plint numDigits)
00105 {
00106     serializerToSink(serializer, new AsciiWriter<T>(ostr, numDigits));
00107 }
00108 
00109 template<typename T>
00110 void asciiStreamToUnSerializer(std::istream* istr, DataUnSerializer* unSerializer)
00111 {
00112     sourceToUnSerializer(new AsciiReader<T>(istr), unSerializer);
00113 }
00114 
00115 } // namespace plb
00116 
00117 #endif  // SERIALIZER_IO_HH