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 
433 
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 */