00001
00002
00003
00004
00005
00006
00007
00008
00009 #ifndef ThePEG_PersistentIStream_H
00010 #define ThePEG_PersistentIStream_H
00011
00012
00013 #include "ThePEG/Config/ThePEG.h"
00014 #include "InputDescription.h"
00015 #include "PersistentIStream.fh"
00016 #include "ThePEG/Utilities/Exception.h"
00017 #include <climits>
00018
00019 namespace ThePEG {
00020
00047 class PersistentIStream {
00048
00049 public:
00050
00051 ThePEG_DECLARE_POINTERS(PersistentBase,BPtr);
00052
00054 typedef vector<BPtr> ObjectVector;
00055
00057 typedef InputDescription::DescriptionVector DescriptionVector;
00058
00059 public:
00060
00066 PersistentIStream(istream & is, bool keepid = false)
00067 : theIStream(&is), isPedantic(true),
00068 allocStream(false), badState(false),
00069 keepId(keepid)
00070 {
00071 init();
00072 }
00073
00074
00075
00084 PersistentIStream(string, bool keepid = false);
00085
00089 ~PersistentIStream();
00090
00096 template <typename T>
00097 PersistentIStream & operator>>(RCPtr<T> & ptr) {
00098 BPtr b = getObject();
00099 ptr = dynamic_ptr_cast< RCPtr<T> >(b);
00100 if ( b && !ptr ) setBadState();
00101 return *this;
00102 }
00103
00109 template <typename T>
00110 PersistentIStream & operator>>(ConstRCPtr<T> & ptr) {
00111 BPtr b = getObject();
00112 ptr = dynamic_ptr_cast< ConstRCPtr<T> >(b);
00113 if ( b && !ptr ) setBadState();
00114 return *this;
00115 }
00116
00122 template <typename T>
00123 PersistentIStream & operator>>(TransientRCPtr<T> & ptr) {
00124 BPtr b = getObject();
00125 ptr = dynamic_ptr_cast< TransientRCPtr<T> >(b);
00126 if ( b && !ptr ) setBadState();
00127 return *this;
00128 }
00129
00135 template <typename T>
00136 PersistentIStream & operator>>(TransientConstRCPtr<T> & ptr) {
00137 BPtr b = getObject();
00138 ptr = dynamic_ptr_cast< TransientConstRCPtr<T> >(b);
00139 if ( b && !ptr ) setBadState();
00140 return *this;
00141 }
00142
00143
00149 PersistentIStream & operator>>(string &);
00150
00154 PersistentIStream & operator>>(char &);
00155
00159 PersistentIStream & operator>>(signed char &);
00160
00164 PersistentIStream & operator>>(unsigned char &);
00165
00169 PersistentIStream & operator>>(int & i) {
00170 is() >> i;
00171 getSep();
00172 return *this;
00173 }
00174
00178 PersistentIStream & operator>>(unsigned int & i) {
00179 is() >> i;
00180 getSep();
00181 return *this;
00182 }
00183
00187 PersistentIStream & operator>>(long & i) {
00188 is() >> i;
00189 getSep();
00190 return *this;
00191 }
00192
00196 PersistentIStream & operator>>(unsigned long & i) {
00197 is() >> i;
00198 getSep();
00199 return *this;
00200 }
00201
00205 PersistentIStream & operator>>(short & i) {
00206 is() >> i;
00207 getSep();
00208 return *this;
00209 }
00210
00214 PersistentIStream & operator>>(unsigned short & i) {
00215 is() >> i;
00216 getSep();
00217 return *this;
00218 }
00219
00223 PersistentIStream & operator>>(double & d) {
00224 is() >> d;
00225 getSep();
00226 return *this;
00227 }
00228
00232 PersistentIStream & operator>>(float & f) {
00233 is() >> f;
00234 getSep();
00235 return *this;
00236 }
00237
00241 PersistentIStream & operator>>(bool &);
00242
00246 PersistentIStream & operator>>(Complex &);
00248
00253 template <typename Container> void getContainer(Container & c) {
00254 long size;
00255 typename Container::value_type val;
00256 c.clear();
00257 *this >> size;
00258 while ( size-- && good() ) {
00259 *this >> val;
00260 c.insert(c.end(), val);
00261 }
00262 }
00263
00269 BPtr getObject();
00270
00278 void getObjectPart(tBPtr obj, const InputDescription * pid);
00279
00284 const InputDescription * getClass();
00285
00294 PersistentIStream & setPedantic() {
00295 isPedantic = true;
00296 return *this;
00297 }
00298
00307 PersistentIStream & setTolerant() {
00308 isPedantic = false;
00309 return *this;
00310 }
00311
00315 bool good() const { return !badState && is(); }
00316
00320 bool operator!() const { return !good(); }
00321
00325 operator bool() const { return good(); }
00326
00331 bool pedantic() const { return isPedantic; }
00332
00333 private:
00334
00338 struct MissingClass: public Exception {};
00339
00342 struct MissingObject: public Exception {};
00343
00346 struct ReadFailure: public Exception {};
00352 void init();
00353
00357 char get() { return is().get(); }
00358
00363 char escaped() {
00364 char c = get();
00365 return c == tNoSep? tSep: c;
00366 }
00367
00371 void setBadState() {
00372 breakThePEG();
00373 badState = true;
00374 }
00375
00379 void getSep() {
00380 if ( !pedantic() ) skipField();
00381 else if ( get() != tSep ) setBadState();
00382 }
00383
00387 void skipField() {
00388 is().ignore(INT_MAX, tSep);
00389 if ( !is() ) setBadState();
00390 }
00391
00392
00396 bool beginObject() { return is().peek() == tBegin; }
00397
00403 void endObject();
00404
00410 void endBase(string classname);
00411
00415 istream & is() { return *theIStream; }
00416
00420 const istream & is() const { return *theIStream; }
00421
00425 ObjectVector readObjects;
00426
00430 DescriptionVector readClasses;
00431
00435 istream * theIStream;
00436
00441 bool isPedantic;
00442
00447 bool allocStream;
00448
00452 bool badState;
00453
00456 int version;
00457
00460 int subVersion;
00461
00466 bool keepId;
00467
00473 static const char tBegin = '{';
00474
00478 static const char tEnd = '}';
00479
00484 static const char tNext = '|';
00485
00489 static const char tNull = '\\';
00490
00494 static const char tSep = '\n';
00495
00500 static const char tNoSep = 'n';
00501
00505 static const char tYes = 'y';
00506
00510 static const char tNo = 'n';
00512
00513 private:
00514
00518 PersistentIStream();
00519
00523 PersistentIStream(const PersistentIStream &);
00524
00528 PersistentIStream & operator=(const PersistentIStream &);
00529
00530 };
00531
00532
00536 inline PersistentIStream & operator>>(PersistentIStream & is,
00537 PersistentIManip func) {
00538 return (*func)(is);
00539 }
00540
00544 inline PersistentIStream & pedantic(PersistentIStream & is) {
00545 return is.setPedantic();
00546 }
00547
00548
00552 inline PersistentIStream & tolerant(PersistentIStream & is) {
00553 return is.setTolerant();
00554 }
00555
00556
00563 template <typename T1, typename T2>
00564 inline PersistentIStream & operator>>(PersistentIStream & is, pair<T1,T2> & p) {
00565 return is >> p.first >> p.second;
00566 }
00567
00569 template <typename Key, typename T, typename Cmp, typename A>
00570 inline PersistentIStream & operator>>(PersistentIStream & is, map<Key,T,Cmp,A> & m) {
00571 m.clear();
00572 long size;
00573 Key k;
00574 is >> size;
00575 while ( size-- && is ) {
00576 is >> k;
00577 is >> m[k];
00578 }
00579 return is;
00580 }
00581
00583 template <typename Key, typename T, typename Cmp, typename A>
00584 inline PersistentIStream & operator>>(PersistentIStream & is,
00585 multimap<Key,T,Cmp,A> & m) {
00586 m.clear();
00587 long size;
00588 Key k;
00589 T t;
00590 is >> size;
00591 while ( size-- && is ) {
00592 is >> k;
00593 is >> t;
00594 m.insert(make_pair(k, t));
00595 }
00596 return is;
00597 }
00598
00599
00601 template <typename Key, typename Cmp, typename A>
00602 inline PersistentIStream & operator>>(PersistentIStream & is,
00603 set<Key,Cmp,A> & s) {
00604 is.getContainer(s);
00605 return is;
00606 }
00607
00609 template <typename Key, typename Cmp, typename A>
00610 inline PersistentIStream & operator>>(PersistentIStream & is,
00611 multiset<Key,Cmp,A> & s) {
00612 is.getContainer(s);
00613 return is;
00614 }
00615
00616
00618 template <typename T, typename A>
00619 inline PersistentIStream & operator>>(PersistentIStream & is,
00620 list<T,A> & l) {
00621 is.getContainer(l);
00622 return is;
00623 }
00624
00625
00627 template <typename T, typename A>
00628 inline PersistentIStream & operator>>(PersistentIStream & is,
00629 vector<T,A> & v) {
00630 is.getContainer(v);
00631 return is;
00632 }
00633
00634
00636 template <typename T, typename A>
00637 inline PersistentIStream & operator>>(PersistentIStream & is,
00638 deque<T,A> & d) {
00639 is.getContainer(d);
00640 return is;
00641 }
00642
00643 }
00644
00645 #endif