29 #ifndef Rythmos_STATE_SERIALIZER_STRATEGY_H
30 #define Rythmos_STATE_SERIALIZER_STRATEGY_H
32 #include "Rythmos_Types.hpp"
34 #include "Teuchos_Describable.hpp"
36 #include "Thyra_VectorBase.hpp"
37 #include "Thyra_ModelEvaluatorBase.hpp"
38 #include "Teuchos_ParameterList.hpp"
39 #include "Teuchos_XMLParser.hpp"
40 #include "Teuchos_XMLParameterListWriter.hpp"
41 #include "Teuchos_XMLParameterListReader.hpp"
42 #include "Teuchos_StringInputSource.hpp"
43 #include "Thyra_SpmdMultiVectorSerializer.hpp"
52 template<
class Scalar>
54 :
virtual public Teuchos::Describable
58 virtual void serializeScalar(
const Scalar& s, std::ostream& oStream)
const = 0;
59 virtual void deSerializeScalar(
const Ptr<Scalar>& s, std::istream& iStream)
const = 0;
61 virtual void serializeInt(
const int& i, std::ostream& oStream)
const = 0;
62 virtual void deSerializeInt(
const Ptr<int>& i, std::istream& iStream)
const = 0;
64 virtual void serializeBool(
const bool& b, std::ostream& oStream)
const = 0;
65 virtual void deSerializeBool(
const Ptr<bool>& b, std::istream& iStream)
const = 0;
67 virtual void serializeVectorBase(
const VectorBase<Scalar>& vec, std::ostream& oStream)
const = 0;
68 virtual void deSerializeVectorBase(
const Ptr<VectorBase<Scalar> >& vec, std::istream& iStream)
const = 0;
70 virtual void serializeParameterList(
const Teuchos::ParameterList& pl, std::ostream& oStream)
const = 0;
71 virtual void deSerializeParameterList(
const Ptr<Teuchos::ParameterList>& pl, std::istream& iStream)
const = 0;
76 template<
class Scalar>
77 class XMLStateSerializerStrategy
82 XMLStateSerializerStrategy() {}
83 virtual ~XMLStateSerializerStrategy() {}
85 void serializeScalar(
const Scalar& s, std::ostream& oStream)
const
87 oStream.precision(std::numeric_limits<Scalar>::digits10+4);
88 oStream <<
" " << s <<
" ";
90 void deSerializeScalar(
const Ptr<Scalar>& s, std::istream& iStream)
const
92 TEUCHOS_ASSERT( !Teuchos::is_null(s) );
96 void serializeInt(
const int& i, std::ostream& oStream)
const
98 oStream.precision(std::numeric_limits<Scalar>::digits10+4);
99 oStream <<
" " << i <<
" ";
101 void deSerializeInt(
const Ptr<int>& i, std::istream& iStream)
const
103 TEUCHOS_ASSERT( !Teuchos::is_null(i) );
107 void serializeBool(
const bool& b, std::ostream& oStream)
const
109 oStream.precision(std::numeric_limits<Scalar>::digits10+4);
110 oStream <<
" " << b <<
" ";
112 void deSerializeBool(
const Ptr<bool>& b, std::istream& iStream)
const
114 TEUCHOS_ASSERT( !Teuchos::is_null(b) );
118 void serializeVectorBase(
const VectorBase<Scalar>& vec, std::ostream& oStream)
const
120 Thyra::SpmdMultiVectorSerializer<double> vectorSerializer(
false);
121 vectorSerializer.serialize(vec, oStream);
123 void deSerializeVectorBase(
const Ptr<VectorBase<Scalar> >& vec, std::istream& iStream)
const
125 TEUCHOS_ASSERT( !Teuchos::is_null(vec) );
126 Thyra::SpmdMultiVectorSerializer<double> vectorSerializer(
false);
127 vectorSerializer.deserialize( iStream, vec.get() );
130 void serializeParameterList(
const Teuchos::ParameterList& pl, std::ostream& oStream)
const
132 Teuchos::XMLParameterListWriter paramWriter;
133 Teuchos::XMLObject XMLpl = paramWriter.toXML(pl);
135 oStream <<
"\nRythmos::StateSerializerStrategy::serializeParameterList begin\n";
138 oStream <<
"\nRythmos::StateSerializerStrategy::serializeParameterList end\n";
140 void deSerializeParameterList(
const Ptr<Teuchos::ParameterList>& pl, std::istream& iStream)
const
142 TEUCHOS_ASSERT( !Teuchos::is_null(pl) );
143 Teuchos::XMLObject XMLpl;
144 std::ostringstream oStringStream;
147 std::string specialKey;
148 while (specialKey !=
"Rythmos::StateSerializerStrategy::serializeParameterList begin" ) {
149 std::getline(iStream,specialKey);
150 TEUCHOS_ASSERT( !iStream.eof() );
154 while (!iStream.eof()) {
156 std::getline(iStream,line);
158 if (line ==
"Rythmos::StateSerializerStrategy::serializeParameterList end") {
161 oStringStream << line;
163 Teuchos::StringInputSource src(oStringStream.str());
164 Teuchos::XMLParser parser(src.stream());
165 XMLpl = parser.parse();
166 Teuchos::XMLParameterListReader paramReader;
167 pl->setParameters(paramReader.toParameterList(XMLpl));
172 template<
class Scalar>
173 class BinaryStateSerializerStrategy
174 :
virtual public StateSerializerStrategy<Scalar>
178 BinaryStateSerializerStrategy() {}
179 virtual ~BinaryStateSerializerStrategy() {}
181 void serializeScalar(
const Scalar& s, std::ostream& oStream)
const
183 oStream.precision(std::numeric_limits<Scalar>::digits10+4);
184 oStream.write( reinterpret_cast<const char*>(&s),
sizeof(Scalar) );
187 void deSerializeScalar(
const Ptr<Scalar>& s, std::istream& iStream)
const
189 TEUCHOS_ASSERT( !Teuchos::is_null(s) );
190 iStream.read( reinterpret_cast<char*>(&*s),
sizeof(Scalar) );
193 void serializeInt(
const int& i, std::ostream& oStream)
const
195 oStream.precision(std::numeric_limits<Scalar>::digits10+4);
196 oStream.write( reinterpret_cast<const char*>(&i),
sizeof(
int) );
198 void deSerializeInt(
const Ptr<int>& i, std::istream& iStream)
const
200 TEUCHOS_ASSERT( !Teuchos::is_null(i) );
201 iStream.read( reinterpret_cast<char*>(&*i),
sizeof(
int) );
204 void serializeBool(
const bool& b, std::ostream& oStream)
const
206 oStream.precision(std::numeric_limits<Scalar>::digits10+4);
207 oStream.write( reinterpret_cast<const char*>(&b),
sizeof(
bool) );
209 void deSerializeBool(
const Ptr<bool>& b, std::istream& iStream)
const
211 TEUCHOS_ASSERT( !Teuchos::is_null(b) );
212 iStream.read( reinterpret_cast<char*>(&*b),
sizeof(
bool) );
215 void serializeVectorBase(
const VectorBase<Scalar>& vec, std::ostream& oStream)
const
217 Thyra::SpmdMultiVectorSerializer<double> vectorSerializer(
true);
218 vectorSerializer.serialize(vec, oStream);
220 void deSerializeVectorBase(
const Ptr<VectorBase<Scalar> >& vec, std::istream& iStream)
const
222 TEUCHOS_ASSERT( !Teuchos::is_null(vec) );
223 Thyra::SpmdMultiVectorSerializer<double> vectorSerializer(
true);
224 vectorSerializer.deserialize( iStream, vec.get() );
227 void serializeParameterList(
const Teuchos::ParameterList& pl, std::ostream& oStream)
const
229 Teuchos::XMLParameterListWriter paramWriter;
230 Teuchos::XMLObject XMLpl = paramWriter.toXML(pl);
232 oStream <<
"\nRythmos::StateSerializerStrategy::serializeParameterList begin\n";
235 oStream <<
"\nRythmos::StateSerializerStrategy::serializeParameterList end\n";
237 void deSerializeParameterList(
const Ptr<Teuchos::ParameterList>& pl, std::istream& iStream)
const
239 TEUCHOS_ASSERT( !Teuchos::is_null(pl) );
240 Teuchos::XMLObject XMLpl;
241 std::ostringstream oStringStream;
244 std::string specialKey;
245 while (specialKey !=
"Rythmos::StateSerializerStrategy::serializeParameterList begin" ) {
246 std::getline(iStream,specialKey);
247 TEUCHOS_ASSERT( !iStream.eof() );
251 while (!iStream.eof()) {
253 std::getline(iStream,line);
255 if (line ==
"Rythmos::StateSerializerStrategy::serializeParameterList end") {
258 oStringStream << line;
260 Teuchos::StringInputSource src(oStringStream.str());
261 Teuchos::XMLParser parser(src.stream());
262 XMLpl = parser.parse();
263 Teuchos::XMLParameterListReader paramReader;
264 pl->setParameters(paramReader.toParameterList(XMLpl));
270 #endif // Rythmos_STATE_SERIALIZER_STRATEGY_H
Base class for serializing Rythmos state data.