thepeg is hosted by Hepforge, IPPP Durham
ThePEG 2.3.0
PersistentOStream.h
1// -*- C++ -*-
2//
3// PersistentOStream.h is a part of ThePEG - Toolkit for HEP Event Generation
4// Copyright (C) 1999-2019 Leif Lonnblad
5//
6// ThePEG is licenced under version 3 of the GPL, see COPYING for details.
7// Please respect the MCnet academic guidelines, see GUIDELINES for details.
8//
9#ifndef ThePEG_PersistentOStream_H
10#define ThePEG_PersistentOStream_H
11// This is the declaration of the PersistentOStream class.
12
14#include "ThePEG/Utilities/ClassDescription.h"
15#include "ThePEG/Utilities/Exception.h"
16#include "ThePEG/Utilities/Debug.h"
17#include "PersistentOStream.fh"
18#include "PersistentOStream.xh"
19#include <valarray>
20
21namespace ThePEG {
22
52
53public:
54
56
59
62
65
66public:
67
73 PersistentOStream(ostream &, const vector<string> & libs = vector<string>());
74
83 PersistentOStream(string, const vector<string> & libs = vector<string>());
84
89
95 template <typename T>
97 return outputPointer(p);
98 }
99
105 template <typename T>
107 return outputPointer(p);
108 }
109
115 template <typename T>
117 return outputPointer(p);
118 }
119
125 template <typename T>
127 return outputPointer(p);
128 }
129
130
137 for ( string::const_iterator i = s.begin(); i < s.end(); ++i ) escape(*i);
138 put(tSep);
139 return *this;
140 }
141
146 escape(c);
147 put(tSep);
148 return *this;
149 }
150
154 PersistentOStream & operator<<(signed char c) {
155 return (*this) << static_cast<char>(c);
156 }
157
161 PersistentOStream & operator<<(unsigned char c) {
162 return (*this) << static_cast<char>(c);
163 }
164
169 os() << i;
170 put(tSep);
171 return *this;
172 }
173
177 PersistentOStream & operator<<(unsigned int i) {
178 os() << i;
179 put(tSep);
180 return *this;
181 }
182
187 os() << i;
188 put(tSep);
189 return *this;
190 }
191
195 PersistentOStream & operator<<(unsigned long i) {
196 os() << i;
197 put(tSep);
198 return *this;
199 }
200
205 os() << i;
206 put(tSep);
207 return *this;
208 }
209
213 PersistentOStream & operator<<(unsigned short i) {
214 os() << i;
215 put(tSep);
216 return *this;
217 }
218
223 if ( ! isfinite(d) )
224 throw WriteError()
225 << "Tried to write a NaN or Inf double to a persistent stream."
227 os() << setprecision(18) << d;
228 put(tSep);
229 return *this;
230 }
231
236 if ( ! isfinite(f) )
237 throw WriteError()
238 << "Tried to write a NaN or Inf float to a persistent stream."
240 os() << setprecision(9) << f;
241 put(tSep);
242 return *this;
243 }
244
249 if (t) put(tYes);
250 else put(tNo);
251 // This is a workaround for a possible bug in gcc 4.0.0
252 // which inserts tYes and tNo as global symbols although
253 // they are private
254 // put(t? tYes: tNo);
255 put(tSep);
256 return *this;
257 }
258
262 PersistentOStream & operator<<(const char * s) {
263 *this << string(s);
264 return *this;
265 }
266
271 *this << z.real() << z.imag();
272 return *this;
273 }
275
279 template <typename Container>
280 void putContainer(const Container & c) {
281 *this << c.size();
282 for ( typename Container::const_iterator it = c.begin();
283 it != c.end() && good() ; ++it )
284 *this << *it;
285 }
286
291
300
306
312 lastSavedObject.push(writtenObjects.size() - 1);
313 return *this;
314 }
315
320 lastSavedObject.pop();
321 return *this;
322 }
323
327 bool good() const { return !badState && os(); }
328
332 operator bool() const { return good(); }
333
337 bool operator!() const { return !good(); }
338
339private:
340
345 struct MissingClass: public Exception {};
349 struct WriteError: public Exception {};
355 static const int version = 0;
356
360 static const int subVersion = 3;
361
367 static const char tBegin = '{';
368
372 static const char tEnd = '}';
373
381 static const char tNext = '|';
382
386 static const char tNull = '\\';
387
391 static const char tSep = '\n';
392
397 static const char tNoSep = 'n';
398
402 static const char tYes = 'y';
403
407 static const char tNo = 'n';
409
413 bool isToken(char c) const {
414 return c == tBegin || c == tEnd || c == tNext || c == tSep || c == tNull;
415 }
416
420 void setBadState() {
421 breakThePEG();
422 badState = true;
423 }
424
428 void checkState() { if ( ! os() ) badState = true; }
429
434
439
443 void beginObject() { put(tBegin); }
444
448 void endObject() { put(tEnd); }
449
453 void endBase() { put(tNext); }
454
458 void put(char c) { os().put(c); }
459
464 void escape(char c) {
465 if ( isToken(c) ) {
466 put(tNull);
467 put( c == tSep? tNoSep: c );
468 } else
469 put(c);
470 }
471
475 ostream & os() { return *theOStream; }
476
480 const ostream & os() const { return *theOStream; }
481
485 void init(const vector<string> & libs);
486
491
495 stack<int> lastSavedObject;
496
501
505 ostream * theOStream;
506
511
516
517private:
518
523
528
533
534};
535
540operator<<(PersistentOStream & os, PersistentOManip func) {
541 return (*func)(os);
542}
543
544
548inline PersistentOStream & flush(PersistentOStream & os) { return os.flush(); }
549
553inline PersistentOStream & push(PersistentOStream & os) { return os.push(); }
554
558inline PersistentOStream & pop(PersistentOStream & os) { return os.pop(); }
559
560
567template <typename T1, typename T2>
569 const pair<T1,T2> & p) {
570 return os << p.first << p.second;
571}
572
576template <typename Key, typename T, typename Cmp, typename A>
578 const multimap<Key,T,Cmp,A> & m) {
579 os.putContainer(m);
580 return os;
581}
582
586template <typename Key, typename T, typename Cmp, typename A>
588 const map<Key,T,Cmp,A> & m) {
589 os.putContainer(m);
590 return os;
591}
592
596template <typename Key, typename Cmp, typename A>
598 const set<Key,Cmp,A> & s) {
599 os.putContainer(s);
600 return os;
601}
602
603
607template <typename Key, typename Cmp, typename A>
609 const multiset<Key,Cmp,A> & s) {
610 os.putContainer(s);
611 return os;
612}
613
614
618template <typename T, typename A>
620 const list<T,A> & l) {
621 os.putContainer(l);
622 return os;
623}
624
625
629template <typename T, typename A>
631 const vector<T,A> & v) {
632 os.putContainer(v);
633 return os;
634}
635
639template <typename T, size_t N>
641 const array<T,N> & a) {
642 for ( auto it = a.cbegin(); it != a.cend() && os.good() ; ++it )
643 os << *it;
644 return os;
645}
646
650template <typename T, typename A>
652 const deque<T,A> & d) {
653 os.putContainer(d);
654 return os;
655}
656
660template <typename T>
662 const std::valarray<T> & v) {
663 os << v.size();
664 for ( int i = 0, N = v.size(); i < N; ++i ) os << v[i];
665 return os;
666}
668
669}
670
671#endif /* ThePEG_PersistentOStream_H */
#define ThePEG_DECLARE_POINTERS(full, abbrev)
This macro helps us to declare pointers and stuff to standard classes.
Definition: Pointers.h:47
This is the main config header file for ThePEG.
ClassDescriptionBase is the base class for all class description classes.
vector< const ClassDescriptionBase * > DescriptionVector
A vector of class descriptions.
Exception is the base class for all exceptions to be used in ThePEG.
Definition: Exception.h:44
@ runerror
Severe error, (the run should be terminated).
Definition: Exception.h:61
PersistentOStream is used to write objects persistently to a stream from which they can be read in ag...
ClassDescriptionBase::DescriptionVector DescriptionVector
A vector of bare pointers to InputDescription objects.
PersistentOStream & operator<<(short i)
Write a short integer.
~PersistentOStream()
The destructor.
PersistentOStream & pop()
Instuct the stream not to save the following objects.
map< cBPtr, int, less< cBPtr > > ObjectMap
A map of objects indexed by integers.
PersistentOStream & operator<<(unsigned int i)
Write an unsigned integer.
void endObject()
Put a "end of object" marker on the associated ostream.
bool badState
True if no errors has occurred.
void init(const vector< string > &libs)
Write out initial metainfo on the stream.
PersistentOStream(string, const vector< string > &libs=vector< string >())
Constuctor giving a file name to read.
PersistentOStream & operator<<(bool t)
Write a boolean.
void escape(char c)
Put a character on the associated ostream but escape it if it is a token.
PersistentOStream & operator<<(float f)
Write a float.
PersistentOStream & flush()
Remove all objects that have been written, except those which are to be saved, from the list of writt...
PersistentOStream & operator<<(const char *s)
Write a c-style character string (to be read in as a std::string).
PersistentOStream & operator<<(const ConstRCPtr< T > &p)
Operator for writing persistent objects to the stream.
map< const ClassDescriptionBase *, int, less< const ClassDescriptionBase * > > ClassMap
A map relating class descriptions to integers.
static const char tNoSep
The special marker character used to avoid confusion with escaped tSep markers.
PersistentOStream & operator<<(signed char c)
Write a signed character.
bool operator!() const
Check the state of the stream.
const ClassDescriptionBase * writeClassId(tcBPtr)
Write out class information to the associated ostream.
PersistentOStream & operator=(const PersistentOStream &)=delete
Standard ctors and assignment are private and not implemented.
PersistentOStream(const PersistentOStream &)
Standard ctors and assignment are private and not implemented.
static const char tSep
The special marker character indicating the end of a value.
bool allocStream
True if the associated ostream should be deleted in the destructor.
ObjectMap writtenObjects
List of written objects.
static const int version
The version of this PersistentOStream implementation.
PersistentOStream & operator<<(const TransientRCPtr< T > &p)
Operator for writing persistent objects to the stream.
PersistentOStream & operator<<(string s)
Write a character string.
PersistentOStream & operator<<(Complex z)
Write a Complex.
stack< int > lastSavedObject
List of written objects that are to be saved.
void setBadState()
Set the stream in a bad state.
ostream & os()
Return a reference to the associated ostream.
void putObjectPart(tcBPtr obj, const ClassDescriptionBase *cd)
For a given object, write the member variables corresponding to a given ClassDescriptionBase object.
PersistentOStream & operator<<(unsigned char c)
Write an unsigned character.
PersistentOStream & operator<<(const RCPtr< T > &p)
Operator for writing persistent objects to the stream.
ClassMap writtenClasses
List of written classes.
PersistentOStream & push()
Instuct the stream to save the following objects (protecting them from being flushed).
void writeClassDescription(const ClassDescriptionBase *)
write out class information to the associated ostream.
static const char tYes
The special marker character indicating a true boolean value.
void endBase()
Put an "next base class" marker on the associated ostream.
static const char tNext
The marker character indicating the beginning of the next base class in case of multiple inheritance.
void checkState()
Check if the state is ok.
PersistentOStream & operator<<(double d)
Write a double.
static const char tBegin
The special marker character indicating the beginning of an object.
PersistentOStream & operator<<(char c)
Write a character.
PersistentOStream & operator<<(const TransientConstRCPtr< T > &p)
Operator for writing persistent objects to the stream.
static const int subVersion
The subversion of this PersistentOStream implementation.
void putContainer(const Container &c)
Output of containers of streamable objects.
void put(char c)
Put a character on the associated ostream.
PersistentOStream & operator<<(unsigned short i)
Write an unsigned short integer.
PersistentOStream()
Standard ctors and assignment are private and not implemented.
PersistentOStream & operator<<(long i)
Write a long integer.
static const char tEnd
The special marker character indicating the end of an object.
bool good() const
Check the state of the stream.
static const char tNull
The special marker character indicating an escaped marker character.
ostream * theOStream
A pointer to the associated ostream.
PersistentOStream & operator<<(unsigned long i)
Write an unsigned long integer.
bool isToken(char c) const
Return true if the given character is aspecial marker character.
PersistentOStream & outputPointer(tcBPtr)
Write out a persistent object given a pointer to it.
void beginObject()
Put a "begin object" marker on the associated ostream.
const ostream & os() const
Return a const reference to the associated ostream.
PersistentOStream & operator<<(int i)
Write an integer.
static const char tNo
The special marker character indicating a false boolean value.
PersistentOStream(ostream &, const vector< string > &libs=vector< string >())
Constuctor giving an output stream.
ConstRCPtr is a reference counted (smart) const pointer.
Definition: RCPtr.h:320
RCPtr is a reference counted (smart) pointer.
Definition: RCPtr.h:60
TransientConstRCPtr is a simple wrapper around a bare const pointer which can be assigned to and from...
Definition: RCPtr.h:696
TransientRCPtr is a simple wrapper around a bare pointer which can be assigned to and from an RCPtr a...
Definition: RCPtr.h:519
This is the main namespace within which all identifiers in ThePEG are declared.
Definition: FactoryBase.h:28
PersistentOStream & push(PersistentOStream &os)
The manipulator for calling PersistentOStream::push().
std::complex< double > Complex
ThePEG code should use Complex for all complex scalars.
Definition: Complex.h:23
PersistentOStream & pop(PersistentOStream &os)
The manipulator for calling PersistentOStream::pop().
vector< T > & operator<<(vector< T > &tv, const U &u)
Overload the left shift operator for vector to push_back objects to a vector.
Definition: Containers.h:179
PersistentOStream & flush(PersistentOStream &os)
The manipulator for calling PersistentOStream::flush().
#define ThePEG_DECLARE_MAP(KEYTYPE, VALTYPE, NAME)
Macro for declaring a map.
Definition: std.h:185
Define the base class from which all (polymorphic) classes in ThePEG are derived.
Definition: ThePEG.h:54