thepeg is hosted by Hepforge, IPPP Durham
ThePEG 2.3.0
PersistentIStream.h
1// -*- C++ -*-
2//
3// PersistentIStream.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_PersistentIStream_H
10#define ThePEG_PersistentIStream_H
11// This is the declaration of the PersistentIStream class.
12
14#include "InputDescription.h"
15#include "PersistentIStream.fh"
16#include "ThePEG/Utilities/Exception.h"
17#include <climits>
18#include <valarray>
19
20namespace ThePEG {
21
49
50public:
51
53
55 typedef vector<BPtr> ObjectVector;
56
59
60public:
61
67 : theIStream(&is), isPedantic(true),
68 allocStream(false), badState(false)
69 {
70 init();
71 }
72
73
74
82
87
93 template <typename T>
95 BPtr b = getObject();
96 ptr = dynamic_ptr_cast< RCPtr<T> >(b);
97 if ( b && !ptr ) setBadState();
98 return *this;
99 }
100
106 template <typename T>
108 BPtr b = getObject();
109 ptr = dynamic_ptr_cast< ConstRCPtr<T> >(b);
110 if ( b && !ptr ) setBadState();
111 return *this;
112 }
113
119 template <typename T>
121 BPtr b = getObject();
122 ptr = dynamic_ptr_cast< TransientRCPtr<T> >(b);
123 if ( b && !ptr ) setBadState();
124 return *this;
125 }
126
132 template <typename T>
134 BPtr b = getObject();
135 ptr = dynamic_ptr_cast< TransientConstRCPtr<T> >(b);
136 if ( b && !ptr ) setBadState();
137 return *this;
138 }
139
140
147
152
157
161 PersistentIStream & operator>>(unsigned char &);
162
167 is() >> i;
168 getSep();
169 return *this;
170 }
171
175 PersistentIStream & operator>>(unsigned int & i) {
176 is() >> i;
177 getSep();
178 return *this;
179 }
180
185 is() >> i;
186 getSep();
187 return *this;
188 }
189
193 PersistentIStream & operator>>(unsigned long & i) {
194 is() >> i;
195 getSep();
196 return *this;
197 }
198
203 is() >> i;
204 getSep();
205 return *this;
206 }
207
211 PersistentIStream & operator>>(unsigned short & i) {
212 is() >> i;
213 getSep();
214 return *this;
215 }
216
221 is() >> d;
222 getSep();
223 return *this;
224 }
225
230 is() >> f;
231 getSep();
232 return *this;
233 }
234
239
245
250 template <typename Container> void getContainer(Container & c) {
251 long size;
252 typename Container::value_type val;
253 c.clear();
254 *this >> size;
255 while ( size-- && good() ) {
256 *this >> val;
257 c.insert(c.end(), val);
258 }
259 }
260
267
275 void getObjectPart(tBPtr obj, const InputDescription * pid);
276
282
292 isPedantic = true;
293 return *this;
294 }
295
305 isPedantic = false;
306 return *this;
307 }
308
312 bool good() const { return !badState && is(); }
313
317 bool operator!() const { return !good(); }
318
322 operator bool() const { return good(); }
323
328 bool pedantic() const { return isPedantic; }
329
333 const vector<string> & globalLibraries() const {
334 return theGlobalLibraries;
335 }
336
337private:
338
342 struct MissingClass: public Exception {};
343
346 struct MissingObject: public Exception {};
347
350 struct ReadFailure: public Exception {};
356 void init();
357
361 char get() { return is().get(); }
362
367 char escaped() {
368 char c = get();
369 return c == tNoSep? tSep: c;
370 }
371
375 void setBadState() {
376 breakThePEG();
377 badState = true;
378 }
379
383 void getSep() {
384 if ( !pedantic() ) skipField();
385 else if ( get() != tSep ) setBadState();
386 }
387
391 void skipField() {
392 is().ignore(INT_MAX, tSep);
393 if ( !is() ) setBadState();
394 }
395
396
400 bool beginObject() { return is().peek() == tBegin; }
401
407 void endObject();
408
414 void endBase(string classname);
415
419 istream & is() { return *theIStream; }
420
424 const istream & is() const { return *theIStream; }
425
430
435
439 istream * theIStream;
440
446
452
457
461
465
469 vector<string> theGlobalLibraries;
470
476 static const char tBegin = '{';
477
481 static const char tEnd = '}';
482
487 static const char tNext = '|';
488
492 static const char tNull = '\\';
493
497 static const char tSep = '\n';
498
503 static const char tNoSep = 'n';
504
508 static const char tYes = 'y';
509
513 static const char tNo = 'n';
515
516private:
517
522
527
532
533};
534
535
540 PersistentIManip func) {
541 return (*func)(is);
542}
543
548 return is.setPedantic();
549}
550
551
556 return is.setTolerant();
557}
558
559
566template <typename T1, typename T2>
567inline PersistentIStream & operator>>(PersistentIStream & is, pair<T1,T2> & p) {
568 return is >> p.first >> p.second;
569}
570
572template <typename Key, typename T, typename Cmp, typename A>
573inline PersistentIStream & operator>>(PersistentIStream & is, map<Key,T,Cmp,A> & m) {
574 m.clear();
575 long size;
576 Key k;
577 is >> size;
578 while ( size-- && is ) {
579 is >> k;
580 is >> m[k];
581 }
582 return is;
583}
584
586template <typename Key, typename T, typename Cmp, typename A>
588 multimap<Key,T,Cmp,A> & m) {
589 m.clear();
590 long size;
591 Key k;
592 T t;
593 is >> size;
594 while ( size-- && is ) {
595 is >> k;
596 is >> t;
597 m.insert(make_pair(k, t));
598 }
599 return is;
600}
601
602
604template <typename Key, typename Cmp, typename A>
606 set<Key,Cmp,A> & s) {
607 is.getContainer(s);
608 return is;
609}
610
612template <typename Key, typename Cmp, typename A>
614 multiset<Key,Cmp,A> & s) {
615 is.getContainer(s);
616 return is;
617}
618
619
621template <typename T, typename A>
623 list<T,A> & l) {
624 is.getContainer(l);
625 return is;
626}
627
628
630template <typename T, typename A>
632 vector<T,A> & v) {
633 is.getContainer(v);
634 return is;
635}
636
638template <typename T, size_t N>
640 array<T,N> & a) {
641 for ( size_t i = 0; i < N && is.good(); ++i )
642 is >> a[i];
643 return is;
644}
645
647template <typename T, typename A>
649 deque<T,A> & d) {
650 is.getContainer(d);
651 return is;
652}
653
655template <typename T>
657 std::valarray<T> & v) {
658 long size;
659 is >> size;
660 v = std::valarray<T>(size);
661 for ( int i = 0; i < size && is.good(); ++i ) is >> v[i];
662 return is;
663}
664
665}
666
667#endif /* ThePEG_PersistentIStream_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.
Exception is the base class for all exceptions to be used in ThePEG.
Definition: Exception.h:44
InputDescription objects are used by the PersistentIStream class to keep track of all classes it has ...
vector< const InputDescription * > DescriptionVector
A vector of pointers to InputDescription objects.
PersistentIStream is used to read persistent objects from a stream where they were previously written...
static const char tSep
The special marker character indicating the end of a value.
bool good() const
Check the state of the stream.
PersistentIStream & operator>>(long &i)
Read a long integer.
PersistentIStream & operator>>(unsigned int &i)
Read an unsigned integer.
void endObject()
Scan the stream to the end of the current object.
char escaped()
Get the next character from the associated istream and decode it if it is escaped.
int version
Version number of the PersistentOStream which has written the file being read.
PersistentIStream & operator>>(unsigned char &)
Read an unsigned character.
PersistentIStream & operator>>(double &d)
Read a double.
static const char tNoSep
The special marker character used to avoid confusion with escaped tSep markers.
void getContainer(Container &c)
Intput of containers streamable objects.
istream & is()
Return a reference to the associated stream.
~PersistentIStream()
The destructor.
bool isPedantic
Pedantic or tolerant.
PersistentIStream & operator>>(Complex &)
Read a Complex.
vector< BPtr > ObjectVector
A vector of pointers to persistent objects.
vector< string > theGlobalLibraries
Global libraries loaded in the initialization.
char get()
Get the next character from the associated istream.
PersistentIStream & operator>>(ConstRCPtr< T > &ptr)
Operator for extracting persistent objects from the stream.
bool allocStream
True if the associated istream should be deleted when the PersistentIStream is destroyed.
BPtr getObject()
Read in an object.
PersistentIStream & operator>>(RCPtr< T > &ptr)
Operator for extracting persistent objects from the stream.
bool beginObject()
Check if the next char to be read is a tBegin marker.
PersistentIStream & operator>>(float &f)
Read a float.
PersistentIStream & setPedantic()
Set pedantic mode.
bool pedantic() const
Check the tolerance.
static const char tNo
The special marker character indicating a false boolean value.
PersistentIStream & operator>>(int &i)
Read an integer.
PersistentIStream & operator=(const PersistentIStream &)=delete
Standard ctors and assignment are private and not implemented.
int subVersion
Subversion number of the PersistentOStream which has written the file being read.
void getObjectPart(tBPtr obj, const InputDescription *pid)
For a given object, read the member variables corresponding to a given InputDescription object.
PersistentIStream()
Standard ctors and assignment are private and not implemented.
static const char tNext
The marker character indicating the beginning of the next base class in case of multiple inheritance.
static const char tNull
The special marker character indicating an escaped marker character.
InputDescription::DescriptionVector DescriptionVector
A vector of bare pointers to InputDescription objects.
const vector< string > & globalLibraries() const
The global libraries loaded on initialization.
PersistentIStream & setTolerant()
Set tolerant mode.
PersistentIStream & operator>>(string &)
Read a character string.
const istream & is() const
Return a const reference to the associated stream.
void skipField()
Scan the stream for the next field separator.
istream * theIStream
A pointer to the associated istream.
void init()
Internal initialization.
PersistentIStream & operator>>(unsigned short &i)
Read an unsigned short integer.
PersistentIStream(istream &is)
Constuctor giving an input stream to be used as an underlying istream.
void getSep()
Read a field separator from the stream.
PersistentIStream & operator>>(unsigned long &i)
Read an unsigned long integer.
PersistentIStream & operator>>(TransientRCPtr< T > &ptr)
Operator for extracting persistent objects from the stream.
static const char tYes
The special marker character indicating a true boolean value.
PersistentIStream & operator>>(bool &)
Read a bool.
PersistentIStream & operator>>(TransientConstRCPtr< T > &ptr)
Operator for extracting persistent objects from the stream.
static const char tEnd
The special marker character indicating the end of an object.
ObjectVector readObjects
Lists of objects that have been read.
const InputDescription * getClass()
Read a class description from the underlying stream and return a corresponding InputDescription objec...
void setBadState()
Set the stream in a bad state.
PersistentIStream & operator>>(char &)
Read a character.
bool badState
False if no errors has occurred.
PersistentIStream & operator>>(signed char &)
Read a signed character.
bool operator!() const
Check the state of the stream.
PersistentIStream & operator>>(short &i)
Read a short integer.
DescriptionVector readClasses
Lists of classes and corresponding version strings that have been read.
PersistentIStream(const PersistentIStream &)
Standard ctors and assignment are private and not implemented.
static const char tBegin
The special marker character indicating the beginning of an object.
void endBase(string classname)
Scan stream for "end base class" marker.
PersistentIStream(string)
Constuctor giving a file name to read from.
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
vector< T > & operator>>(vector< T > &tv, U &u)
Overload the right shift operator for vector to pop objects from a vector.
Definition: Containers.h:192
std::complex< double > Complex
ThePEG code should use Complex for all complex scalars.
Definition: Complex.h:23
PersistentIStream & tolerant(PersistentIStream &is)
The manipulator for setting tolerant mode.
PersistentIStream & pedantic(PersistentIStream &is)
The manipulator for setting pedantic mode.
Define the base class from which all (polymorphic) classes in ThePEG are derived.
Definition: ThePEG.h:54