ThePEG  1.8.0
PersistentIStream.h
1 // -*- C++ -*-
2 //
3 // PersistentIStream.h is a part of ThePEG - Toolkit for HEP Event Generation
4 // Copyright (C) 1999-2011 Leif Lonnblad
5 //
6 // ThePEG is licenced under version 2 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 
19 namespace ThePEG {
20 
48 
49 public:
50 
52 
54  typedef vector<BPtr> ObjectVector;
55 
58 
59 public:
60 
66  PersistentIStream(istream & is, bool keepid = false)
67  : theIStream(&is), isPedantic(true),
68  allocStream(false), badState(false),
69  keepId(keepid)
70  {
71  init();
72  }
73 
74 
75 
84  PersistentIStream(string, bool keepid = false);
85 
90 
96  template <typename T>
98  BPtr b = getObject();
99  ptr = dynamic_ptr_cast< RCPtr<T> >(b);
100  if ( b && !ptr ) setBadState();
101  return *this;
102  }
103 
109  template <typename T>
111  BPtr b = getObject();
112  ptr = dynamic_ptr_cast< ConstRCPtr<T> >(b);
113  if ( b && !ptr ) setBadState();
114  return *this;
115  }
116 
122  template <typename T>
124  BPtr b = getObject();
126  if ( b && !ptr ) setBadState();
127  return *this;
128  }
129 
135  template <typename T>
137  BPtr b = getObject();
139  if ( b && !ptr ) setBadState();
140  return *this;
141  }
142 
143 
149  PersistentIStream & operator>>(string &);
150 
154  PersistentIStream & operator>>(char &);
155 
159  PersistentIStream & operator>>(signed char &);
160 
164  PersistentIStream & operator>>(unsigned char &);
165 
170  is() >> i;
171  getSep();
172  return *this;
173  }
174 
178  PersistentIStream & operator>>(unsigned int & i) {
179  is() >> i;
180  getSep();
181  return *this;
182  }
183 
188  is() >> i;
189  getSep();
190  return *this;
191  }
192 
196  PersistentIStream & operator>>(unsigned long & i) {
197  is() >> i;
198  getSep();
199  return *this;
200  }
201 
206  is() >> i;
207  getSep();
208  return *this;
209  }
210 
214  PersistentIStream & operator>>(unsigned short & i) {
215  is() >> i;
216  getSep();
217  return *this;
218  }
219 
224  is() >> d;
225  getSep();
226  return *this;
227  }
228 
233  is() >> f;
234  getSep();
235  return *this;
236  }
237 
241  PersistentIStream & operator>>(bool &);
242 
248 
253  template <typename Container> void getContainer(Container & c) {
254  long size;
255  typename Container::value_type val;
256  c.clear();
257  *this >> size;
258  while ( size-- && good() ) {
259  *this >> val;
260  c.insert(c.end(), val);
261  }
262  }
263 
269  BPtr getObject();
270 
278  void getObjectPart(tBPtr obj, const InputDescription * pid);
279 
284  const InputDescription * getClass();
285 
295  isPedantic = true;
296  return *this;
297  }
298 
308  isPedantic = false;
309  return *this;
310  }
311 
315  bool good() const { return !badState && is(); }
316 
320  bool operator!() const { return !good(); }
321 
325  operator bool() const { return good(); }
326 
331  bool pedantic() const { return isPedantic; }
332 
336  const vector<string> & globalLibraries() const {
337  return theGlobalLibraries;
338  }
339 
340 private:
341 
345  struct MissingClass: public Exception {};
346 
349  struct MissingObject: public Exception {};
350 
353  struct ReadFailure: public Exception {};
359  void init();
360 
364  char get() { return is().get(); }
365 
370  char escaped() {
371  char c = get();
372  return c == tNoSep? tSep: c;
373  }
374 
378  void setBadState() {
379  breakThePEG();
380  badState = true;
381  }
382 
386  void getSep() {
387  if ( !pedantic() ) skipField();
388  else if ( get() != tSep ) setBadState();
389  }
390 
394  void skipField() {
395  is().ignore(INT_MAX, tSep);
396  if ( !is() ) setBadState();
397  }
398 
399 
403  bool beginObject() { return is().peek() == tBegin; }
404 
410  void endObject();
411 
417  void endBase(string classname);
418 
422  istream & is() { return *theIStream; }
423 
427  const istream & is() const { return *theIStream; }
428 
432  ObjectVector readObjects;
433 
437  DescriptionVector readClasses;
438 
442  istream * theIStream;
443 
449 
455 
459  bool badState;
460 
463  int version;
464 
468 
473  bool keepId;
474 
478  vector<string> theGlobalLibraries;
479 
485  static const char tBegin = '{';
486 
490  static const char tEnd = '}';
491 
496  static const char tNext = '|';
497 
501  static const char tNull = '\\';
502 
506  static const char tSep = '\n';
507 
512  static const char tNoSep = 'n';
513 
517  static const char tYes = 'y';
518 
522  static const char tNo = 'n';
524 
525 private:
526 
531 
536 
541 
542 };
543 
544 
549  PersistentIManip func) {
550  return (*func)(is);
551 }
552 
557  return is.setPedantic();
558 }
559 
560 
565  return is.setTolerant();
566 }
567 
568 
575 template <typename T1, typename T2>
576 inline PersistentIStream & operator>>(PersistentIStream & is, pair<T1,T2> & p) {
577  return is >> p.first >> p.second;
578 }
579 
581 template <typename Key, typename T, typename Cmp, typename A>
582 inline PersistentIStream & operator>>(PersistentIStream & is, map<Key,T,Cmp,A> & m) {
583  m.clear();
584  long size;
585  Key k;
586  is >> size;
587  while ( size-- && is ) {
588  is >> k;
589  is >> m[k];
590  }
591  return is;
592 }
593 
595 template <typename Key, typename T, typename Cmp, typename A>
597  multimap<Key,T,Cmp,A> & m) {
598  m.clear();
599  long size;
600  Key k;
601  T t;
602  is >> size;
603  while ( size-- && is ) {
604  is >> k;
605  is >> t;
606  m.insert(make_pair(k, t));
607  }
608  return is;
609 }
610 
611 
613 template <typename Key, typename Cmp, typename A>
615  set<Key,Cmp,A> & s) {
616  is.getContainer(s);
617  return is;
618 }
619 
621 template <typename Key, typename Cmp, typename A>
623  multiset<Key,Cmp,A> & s) {
624  is.getContainer(s);
625  return is;
626 }
627 
628 
630 template <typename T, typename A>
632  list<T,A> & l) {
633  is.getContainer(l);
634  return is;
635 }
636 
637 
639 template <typename T, typename A>
641  vector<T,A> & v) {
642  is.getContainer(v);
643  return is;
644 }
645 
646 
648 template <typename T, typename A>
650  deque<T,A> & d) {
651  is.getContainer(d);
652  return is;
653 }
654 
655 }
656 
657 #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...
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.
std::complex< double > Complex
ThePEG code should use Complex for all complex scalars.
Definition: Complex.h:23
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:681
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:509
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:315
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.
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()
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.
void endObject()
Scan the stream to the end of the current object.
PersistentIStream & operator=(const PersistentIStream &)
Standard ctors and assignment are private and not implemented.
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.
bool keepId
If keepId is true, the order in which the original objects were created will be kept.
static const char tNo
The special marker character indicating a false boolean value.
PersistentIStream & operator>>(float &f)
Read a float.
PersistentIStream(istream &is, bool keepid=false)
Constuctor giving an input stream to be used as an underlying istream.
PersistentIStream & operator>>(unsigned long &i)
Read an unsigned long integer.