File indexing completed on 2026-08-06 09:38:30
0001
0002
0003
0004
0005
0006
0007
0008
0009 #ifndef ThePEG_PersistentIStream_H
0010 #define ThePEG_PersistentIStream_H
0011
0012
0013 #include "ThePEG/Config/ThePEG.h"
0014 #include "InputDescription.h"
0015 #include "PersistentIStream.fh"
0016 #include "ThePEG/Utilities/Exception.h"
0017 #include <climits>
0018 #include <valarray>
0019
0020 namespace ThePEG {
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048 class PersistentIStream {
0049
0050 public:
0051
0052 ThePEG_DECLARE_POINTERS(PersistentBase,BPtr);
0053
0054
0055 typedef vector<BPtr> ObjectVector;
0056
0057
0058 typedef InputDescription::DescriptionVector DescriptionVector;
0059
0060 public:
0061
0062
0063
0064
0065
0066 PersistentIStream(istream & is)
0067 : theIStream(&is), isPedantic(true),
0068 allocStream(false), badState(false)
0069 {
0070 init();
0071 }
0072
0073
0074
0075
0076
0077
0078
0079
0080
0081 PersistentIStream(string);
0082
0083
0084
0085
0086 ~PersistentIStream();
0087
0088
0089
0090
0091
0092
0093 template <typename T>
0094 PersistentIStream & operator>>(RCPtr<T> & ptr) {
0095 BPtr b = getObject();
0096 ptr = dynamic_ptr_cast< RCPtr<T> >(b);
0097 if ( b && !ptr ) setBadState();
0098 return *this;
0099 }
0100
0101
0102
0103
0104
0105
0106 template <typename T>
0107 PersistentIStream & operator>>(ConstRCPtr<T> & ptr) {
0108 BPtr b = getObject();
0109 ptr = dynamic_ptr_cast< ConstRCPtr<T> >(b);
0110 if ( b && !ptr ) setBadState();
0111 return *this;
0112 }
0113
0114
0115
0116
0117
0118
0119 template <typename T>
0120 PersistentIStream & operator>>(TransientRCPtr<T> & ptr) {
0121 BPtr b = getObject();
0122 ptr = dynamic_ptr_cast< TransientRCPtr<T> >(b);
0123 if ( b && !ptr ) setBadState();
0124 return *this;
0125 }
0126
0127
0128
0129
0130
0131
0132 template <typename T>
0133 PersistentIStream & operator>>(TransientConstRCPtr<T> & ptr) {
0134 BPtr b = getObject();
0135 ptr = dynamic_ptr_cast< TransientConstRCPtr<T> >(b);
0136 if ( b && !ptr ) setBadState();
0137 return *this;
0138 }
0139
0140
0141
0142
0143
0144
0145
0146 PersistentIStream & operator>>(string &);
0147
0148
0149
0150
0151 PersistentIStream & operator>>(char &);
0152
0153
0154
0155
0156 PersistentIStream & operator>>(signed char &);
0157
0158
0159
0160
0161 PersistentIStream & operator>>(unsigned char &);
0162
0163
0164
0165
0166 PersistentIStream & operator>>(int & i) {
0167 is() >> i;
0168 getSep();
0169 return *this;
0170 }
0171
0172
0173
0174
0175 PersistentIStream & operator>>(unsigned int & i) {
0176 is() >> i;
0177 getSep();
0178 return *this;
0179 }
0180
0181
0182
0183
0184 PersistentIStream & operator>>(long & i) {
0185 is() >> i;
0186 getSep();
0187 return *this;
0188 }
0189
0190
0191
0192
0193 PersistentIStream & operator>>(unsigned long & i) {
0194 is() >> i;
0195 getSep();
0196 return *this;
0197 }
0198
0199
0200
0201
0202 PersistentIStream & operator>>(short & i) {
0203 is() >> i;
0204 getSep();
0205 return *this;
0206 }
0207
0208
0209
0210
0211 PersistentIStream & operator>>(unsigned short & i) {
0212 is() >> i;
0213 getSep();
0214 return *this;
0215 }
0216
0217
0218
0219
0220 PersistentIStream & operator>>(double & d) {
0221 is() >> d;
0222 getSep();
0223 return *this;
0224 }
0225
0226
0227
0228
0229 PersistentIStream & operator>>(float & f) {
0230 is() >> f;
0231 getSep();
0232 return *this;
0233 }
0234
0235
0236
0237
0238 PersistentIStream & operator>>(bool &);
0239
0240
0241
0242
0243 PersistentIStream & operator>>(Complex &);
0244
0245
0246
0247
0248
0249
0250 template <typename Container> void getContainer(Container & c) {
0251 long size;
0252 typename Container::value_type val;
0253 c.clear();
0254 *this >> size;
0255 while ( size-- && good() ) {
0256 *this >> val;
0257 c.insert(c.end(), val);
0258 }
0259 }
0260
0261
0262
0263
0264
0265
0266 BPtr getObject();
0267
0268
0269
0270
0271
0272
0273
0274
0275 void getObjectPart(tBPtr obj, const InputDescription * pid);
0276
0277
0278
0279
0280
0281 const InputDescription * getClass();
0282
0283
0284
0285
0286
0287
0288
0289
0290
0291 PersistentIStream & setPedantic() {
0292 isPedantic = true;
0293 return *this;
0294 }
0295
0296
0297
0298
0299
0300
0301
0302
0303
0304 PersistentIStream & setTolerant() {
0305 isPedantic = false;
0306 return *this;
0307 }
0308
0309
0310
0311
0312 bool good() const { return !badState && is(); }
0313
0314
0315
0316
0317 bool operator!() const { return !good(); }
0318
0319
0320
0321
0322 operator bool() const { return good(); }
0323
0324
0325
0326
0327
0328 bool pedantic() const { return isPedantic; }
0329
0330
0331
0332
0333 const vector<string> & globalLibraries() const {
0334 return theGlobalLibraries;
0335 }
0336
0337 private:
0338
0339
0340
0341
0342 struct MissingClass: public Exception {};
0343
0344
0345
0346 struct MissingObject: public Exception {};
0347
0348
0349
0350 struct ReadFailure: public Exception {};
0351
0352
0353
0354
0355
0356 void init();
0357
0358
0359
0360
0361 char get() { return is().get(); }
0362
0363
0364
0365
0366
0367 char escaped() {
0368 char c = get();
0369 return c == tNoSep? tSep: c;
0370 }
0371
0372
0373
0374
0375 void setBadState() {
0376 breakThePEG();
0377 badState = true;
0378 }
0379
0380
0381
0382
0383 void getSep() {
0384 if ( !pedantic() ) skipField();
0385 else if ( get() != tSep ) setBadState();
0386 }
0387
0388
0389
0390
0391 void skipField() {
0392 is().ignore(INT_MAX, tSep);
0393 if ( !is() ) setBadState();
0394 }
0395
0396
0397
0398
0399
0400 bool beginObject() { return is().peek() == tBegin; }
0401
0402
0403
0404
0405
0406
0407 void endObject();
0408
0409
0410
0411
0412
0413
0414 void endBase(string classname);
0415
0416
0417
0418
0419 istream & is() { return *theIStream; }
0420
0421
0422
0423
0424 const istream & is() const { return *theIStream; }
0425
0426
0427
0428
0429 ObjectVector readObjects;
0430
0431
0432
0433
0434 DescriptionVector readClasses;
0435
0436
0437
0438
0439 istream * theIStream;
0440
0441
0442
0443
0444
0445 bool isPedantic;
0446
0447
0448
0449
0450
0451 bool allocStream;
0452
0453
0454
0455
0456 bool badState;
0457
0458
0459
0460 int version;
0461
0462
0463
0464 int subVersion;
0465
0466
0467
0468
0469 vector<string> theGlobalLibraries;
0470
0471
0472
0473
0474
0475
0476 static const char tBegin = '{';
0477
0478
0479
0480
0481 static const char tEnd = '}';
0482
0483
0484
0485
0486
0487 static const char tNext = '|';
0488
0489
0490
0491
0492 static const char tNull = '\\';
0493
0494
0495
0496
0497 static const char tSep = '\n';
0498
0499
0500
0501
0502
0503 static const char tNoSep = 'n';
0504
0505
0506
0507
0508 static const char tYes = 'y';
0509
0510
0511
0512
0513 static const char tNo = 'n';
0514
0515
0516 private:
0517
0518
0519
0520
0521 PersistentIStream();
0522
0523
0524
0525
0526 PersistentIStream(const PersistentIStream &);
0527
0528
0529
0530
0531 PersistentIStream & operator=(const PersistentIStream &) = delete;
0532
0533 };
0534
0535
0536
0537
0538
0539 inline PersistentIStream & operator>>(PersistentIStream & is,
0540 PersistentIManip func) {
0541 return (*func)(is);
0542 }
0543
0544
0545
0546
0547 inline PersistentIStream & pedantic(PersistentIStream & is) {
0548 return is.setPedantic();
0549 }
0550
0551
0552
0553
0554
0555 inline PersistentIStream & tolerant(PersistentIStream & is) {
0556 return is.setTolerant();
0557 }
0558
0559
0560
0561
0562
0563
0564
0565
0566 template <typename T1, typename T2>
0567 inline PersistentIStream & operator>>(PersistentIStream & is, pair<T1,T2> & p) {
0568 return is >> p.first >> p.second;
0569 }
0570
0571
0572 template <typename Key, typename T, typename Cmp, typename A>
0573 inline PersistentIStream & operator>>(PersistentIStream & is, map<Key,T,Cmp,A> & m) {
0574 m.clear();
0575 long size;
0576 Key k;
0577 is >> size;
0578 while ( size-- && is ) {
0579 is >> k;
0580 is >> m[k];
0581 }
0582 return is;
0583 }
0584
0585
0586 template <typename Key, typename T, typename Cmp, typename A>
0587 inline PersistentIStream & operator>>(PersistentIStream & is,
0588 multimap<Key,T,Cmp,A> & m) {
0589 m.clear();
0590 long size;
0591 Key k;
0592 T t;
0593 is >> size;
0594 while ( size-- && is ) {
0595 is >> k;
0596 is >> t;
0597 m.insert(make_pair(k, t));
0598 }
0599 return is;
0600 }
0601
0602
0603
0604 template <typename Key, typename Cmp, typename A>
0605 inline PersistentIStream & operator>>(PersistentIStream & is,
0606 set<Key,Cmp,A> & s) {
0607 is.getContainer(s);
0608 return is;
0609 }
0610
0611
0612 template <typename Key, typename Cmp, typename A>
0613 inline PersistentIStream & operator>>(PersistentIStream & is,
0614 multiset<Key,Cmp,A> & s) {
0615 is.getContainer(s);
0616 return is;
0617 }
0618
0619
0620
0621 template <typename T, typename A>
0622 inline PersistentIStream & operator>>(PersistentIStream & is,
0623 list<T,A> & l) {
0624 is.getContainer(l);
0625 return is;
0626 }
0627
0628
0629
0630 template <typename T, typename A>
0631 inline PersistentIStream & operator>>(PersistentIStream & is,
0632 vector<T,A> & v) {
0633 is.getContainer(v);
0634 return is;
0635 }
0636
0637
0638 template <typename T, size_t N>
0639 inline PersistentIStream & operator>>(PersistentIStream & is,
0640 array<T,N> & a) {
0641 for ( size_t i = 0; i < N && is.good(); ++i )
0642 is >> a[i];
0643 return is;
0644 }
0645
0646
0647 template <typename T, typename A>
0648 inline PersistentIStream & operator>>(PersistentIStream & is,
0649 deque<T,A> & d) {
0650 is.getContainer(d);
0651 return is;
0652 }
0653
0654
0655 template <typename T>
0656 inline PersistentIStream & operator>>(PersistentIStream & is,
0657 std::valarray<T> & v) {
0658 long size;
0659 is >> size;
0660 v = std::valarray<T>(size);
0661 for ( int i = 0; i < size && is.good(); ++i ) is >> v[i];
0662 return is;
0663 }
0664
0665 }
0666
0667 #endif