thepeg is hosted by Hepforge, IPPP Durham
ThePEG  2.2.1
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 
13 #include "ThePEG/Config/ThePEG.h"
14 #include "InputDescription.h"
15 #include "PersistentIStream.fh"
16 #include "ThePEG/Utilities/Exception.h"
17 #include <climits>
18 #include <valarray>
19 
20 namespace ThePEG {
21 
49 
50 public:
51 
53 
55  typedef vector<BPtr> ObjectVector;
56 
59 
60 public:
61 
66  PersistentIStream(istream & is)
67  : theIStream(&is), isPedantic(true),
68  allocStream(false), badState(false)
69  {
70  init();
71  }
72 
73 
74 
81  PersistentIStream(string);
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();
123  if ( b && !ptr ) setBadState();
124  return *this;
125  }
126 
132  template <typename T>
134  BPtr b = getObject();
136  if ( b && !ptr ) setBadState();
137  return *this;
138  }
139 
140 
146  PersistentIStream & operator>>(string &);
147 
151  PersistentIStream & operator>>(char &);
152 
156  PersistentIStream & operator>>(signed char &);
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 
238  PersistentIStream & operator>>(bool &);
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 
266  BPtr getObject();
267 
275  void getObjectPart(tBPtr obj, const InputDescription * pid);
276 
281  const InputDescription * getClass();
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 
337 private:
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 
429  ObjectVector readObjects;
430 
434  DescriptionVector readClasses;
435 
439  istream * theIStream;
440 
446 
452 
456  bool badState;
457 
460  int version;
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 
516 private:
517 
522 
527 
531  PersistentIStream & operator=(const PersistentIStream &) = delete;
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 
566 template <typename T1, typename T2>
567 inline PersistentIStream & operator>>(PersistentIStream & is, pair<T1,T2> & p) {
568  return is >> p.first >> p.second;
569 }
570 
572 template <typename Key, typename T, typename Cmp, typename A>
573 inline 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 
586 template <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 
604 template <typename Key, typename Cmp, typename A>
606  set<Key,Cmp,A> & s) {
607  is.getContainer(s);
608  return is;
609 }
610 
612 template <typename Key, typename Cmp, typename A>
614  multiset<Key,Cmp,A> & s) {
615  is.getContainer(s);
616  return is;
617 }
618 
619 
621 template <typename T, typename A>
623  list<T,A> & l) {
624  is.getContainer(l);
625  return is;
626 }
627 
628 
630 template <typename T, typename A>
632  vector<T,A> & v) {
633  is.getContainer(v);
634  return is;
635 }
636 
638 template <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 
647 template <typename T, typename A>
649  deque<T,A> & d) {
650  is.getContainer(d);
651  return is;
652 }
653 
655 template <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 */
const vector< string > & globalLibraries() const
The global libraries loaded on initialization.
PersistentIStream is used to read persistent objects from a stream where they were previously written...
static const char tNext
The marker character indicating the beginning of the next base class in case of multiple inheritance...
std::complex< double > Complex
ThePEG code should use Complex for all complex scalars.
Definition: Complex.h:23
InputDescription::DescriptionVector DescriptionVector
A vector of bare pointers to InputDescription objects.
BPtr getObject()
Read in an object.
Define the base class from which all (polymorphic) classes in ThePEG are derived. ...
Definition: ThePEG.h:54
int subVersion
Subversion number of the PersistentOStream which has written the file being read. ...
void setBadState()
Set the stream in a bad state.
void skipField()
Scan the stream for the next field separator.
PersistentIStream & setPedantic()
Set pedantic mode.
PersistentIStream & operator>>(TransientConstRCPtr< T > &ptr)
Operator for extracting persistent objects from the stream.
bool good() const
Check the state of the stream.
static const char tNull
The special marker character indicating an escaped marker character.
ObjectVector readObjects
Lists of objects that have been read.
PersistentIStream & operator>>(ConstRCPtr< T > &ptr)
Operator for extracting persistent objects from the stream.
PersistentIStream & operator>>(long &i)
Read a long integer.
PersistentIStream & operator>>(short &i)
Read a short integer.
static const char tEnd
The special marker character indicating the end of an object.
TransientConstRCPtr is a simple wrapper around a bare const pointer which can be assigned to and from...
Definition: RCPtr.h:696
static const char tNoSep
The special marker character used to avoid confusion with escaped tSep markers.
PersistentIStream & operator>>(RCPtr< T > &ptr)
Operator for extracting persistent objects from the stream.
TransientRCPtr is a simple wrapper around a bare pointer which can be assigned to and from an RCPtr a...
Definition: RCPtr.h:519
void getObjectPart(tBPtr obj, const InputDescription *pid)
For a given object, read the member variables corresponding to a given InputDescription object...
ConstRCPtr is a reference counted (smart) const pointer.
Definition: RCPtr.h:320
T1 dynamic_ptr_cast(const T2 &t2)
Replacement for the standard dynamic_cast.
Definition: PtrTraits.h:168
static const char tSep
The special marker character indicating the end of a value.
This is the main namespace within which all identifiers in ThePEG are declared.
Definition: FactoryBase.h:28
PersistentIStream & operator>>(int &i)
Read an integer.
void getContainer(Container &c)
Intput of containers streamable objects.
This is the main config header file for ThePEG.
const InputDescription * getClass()
Read a class description from the underlying stream and return a corresponding InputDescription objec...
bool pedantic() const
Check the tolerance.
istream * theIStream
A pointer to the associated istream.
bool badState
False if no errors has occurred.
bool beginObject()
Check if the next char to be read is a tBegin marker.
~PersistentIStream()
The destructor.
void init()
Internal initialization.
PersistentIStream(istream &is)
Constuctor giving an input stream to be used as an underlying istream.
PersistentIStream()
Standard ctors and assignment are private and not implemented.
vector< const InputDescription * > DescriptionVector
A vector of pointers to InputDescription objects.
static const char tBegin
The special marker character indicating the beginning of an object.
vector< BPtr > ObjectVector
A vector of pointers to persistent objects.
bool isPedantic
Pedantic or tolerant.
const istream & is() const
Return a const reference to the associated stream.
bool allocStream
True if the associated istream should be deleted when the PersistentIStream is destroyed.
RCPtr is a reference counted (smart) pointer.
Definition: RCPtr.h:60
InputDescription objects are used by the PersistentIStream class to keep track of all classes it has ...
#define ThePEG_DECLARE_POINTERS(full, abbrev)
This macro helps us to declare pointers and stuff to standard classes.
Definition: Pointers.h:47
PersistentIStream & operator>>(unsigned short &i)
Read an unsigned short integer.
vector< string > theGlobalLibraries
Global libraries loaded in the initialization.
void endBase(string classname)
Scan stream for "end base class" marker.
PersistentIStream & tolerant(PersistentIStream &is)
The manipulator for setting tolerant mode.
void getSep()
Read a field separator from the stream.
Exception is the base class for all exceptions to be used in ThePEG.
Definition: Exception.h:44
DescriptionVector readClasses
Lists of classes and corresponding version strings that have been read.
PersistentIStream & operator>>(double &d)
Read a double.
istream & is()
Return a reference to the associated stream.
static const char tYes
The special marker character indicating a true boolean value.
char escaped()
Get the next character from the associated istream and decode it if it is escaped.
bool operator!() const
Check the state of the stream.
PersistentIStream & operator=(const PersistentIStream &)=delete
Standard ctors and assignment are private and not implemented.
void endObject()
Scan the stream to the end of the current object.
PersistentIStream & setTolerant()
Set tolerant mode.
PersistentIStream & operator>>(unsigned int &i)
Read an unsigned integer.
int version
Version number of the PersistentOStream which has written the file being read.
PersistentIStream & operator>>(TransientRCPtr< T > &ptr)
Operator for extracting persistent objects from the stream.
static const char tNo
The special marker character indicating a false boolean value.
PersistentIStream & operator>>(float &f)
Read a float.
PersistentIStream & operator>>(unsigned long &i)
Read an unsigned long integer.