thepeg
is hosted by
Hepforge
,
IPPP Durham
ThePEG
2.3.0
Persistency
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
48
class
PersistentIStream
{
49
50
public
:
51
52
ThePEG_DECLARE_POINTERS
(
PersistentBase
,
BPtr
);
53
55
typedef
vector<BPtr>
ObjectVector
;
56
58
typedef
InputDescription::DescriptionVector
DescriptionVector
;
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
86
~PersistentIStream
();
87
93
template
<
typename
T>
94
PersistentIStream
&
operator>>
(
RCPtr<T>
& ptr) {
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>
107
PersistentIStream
&
operator>>
(
ConstRCPtr<T>
& ptr) {
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>
120
PersistentIStream
&
operator>>
(
TransientRCPtr<T>
& ptr) {
121
BPtr
b =
getObject
();
122
ptr = dynamic_ptr_cast< TransientRCPtr<T> >(b);
123
if
( b && !ptr )
setBadState
();
124
return
*
this
;
125
}
126
132
template
<
typename
T>
133
PersistentIStream
&
operator>>
(
TransientConstRCPtr<T>
& ptr) {
134
BPtr
b =
getObject
();
135
ptr = dynamic_ptr_cast< TransientConstRCPtr<T> >(b);
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
166
PersistentIStream
&
operator>>
(
int
& i) {
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
184
PersistentIStream
&
operator>>
(
long
& i) {
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
202
PersistentIStream
&
operator>>
(
short
& i) {
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
220
PersistentIStream
&
operator>>
(
double
& d) {
221
is
() >> d;
222
getSep
();
223
return
*
this
;
224
}
225
229
PersistentIStream
&
operator>>
(
float
& f) {
230
is
() >> f;
231
getSep
();
232
return
*
this
;
233
}
234
238
PersistentIStream
&
operator>>
(
bool
&);
239
243
PersistentIStream
&
operator>>
(
Complex
&);
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
291
PersistentIStream
&
setPedantic
() {
292
isPedantic
=
true
;
293
return
*
this
;
294
}
295
304
PersistentIStream
&
setTolerant
() {
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
445
bool
isPedantic
;
446
451
bool
allocStream
;
452
456
bool
badState
;
457
460
int
version
;
461
464
int
subVersion
;
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
521
PersistentIStream
();
522
526
PersistentIStream
(
const
PersistentIStream
&);
527
531
PersistentIStream
&
operator=
(
const
PersistentIStream
&) =
delete
;
532
533
};
534
535
539
inline
PersistentIStream
&
operator>>
(
PersistentIStream
& is,
540
PersistentIManip func) {
541
return
(*func)(is);
542
}
543
547
inline
PersistentIStream
&
pedantic
(
PersistentIStream
& is) {
548
return
is.
setPedantic
();
549
}
550
551
555
inline
PersistentIStream
&
tolerant
(
PersistentIStream
& is) {
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>
587
inline
PersistentIStream
&
operator>>
(
PersistentIStream
& is,
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>
605
inline
PersistentIStream
&
operator>>
(
PersistentIStream
& is,
606
set<Key,Cmp,A> & s) {
607
is.
getContainer
(s);
608
return
is;
609
}
610
612
template
<
typename
Key,
typename
Cmp,
typename
A>
613
inline
PersistentIStream
&
operator>>
(
PersistentIStream
& is,
614
multiset<Key,Cmp,A> & s) {
615
is.
getContainer
(s);
616
return
is;
617
}
618
619
621
template
<
typename
T,
typename
A>
622
inline
PersistentIStream
&
operator>>
(
PersistentIStream
& is,
623
list<T,A> & l) {
624
is.
getContainer
(l);
625
return
is;
626
}
627
628
630
template
<
typename
T,
typename
A>
631
inline
PersistentIStream
&
operator>>
(
PersistentIStream
& is,
632
vector<T,A> & v) {
633
is.
getContainer
(v);
634
return
is;
635
}
636
638
template
<
typename
T,
size_t
N>
639
inline
PersistentIStream
&
operator>>
(
PersistentIStream
& is,
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>
648
inline
PersistentIStream
&
operator>>
(
PersistentIStream
& is,
649
deque<T,A> & d) {
650
is.
getContainer
(d);
651
return
is;
652
}
653
655
template
<
typename
T>
656
inline
PersistentIStream
&
operator>>
(
PersistentIStream
& is,
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 */
ThePEG_DECLARE_POINTERS
#define ThePEG_DECLARE_POINTERS(full, abbrev)
This macro helps us to declare pointers and stuff to standard classes.
Definition:
Pointers.h:47
ThePEG.h
This is the main config header file for ThePEG.
ThePEG::Exception
Exception is the base class for all exceptions to be used in ThePEG.
Definition:
Exception.h:44
ThePEG::InputDescription
InputDescription objects are used by the PersistentIStream class to keep track of all classes it has ...
Definition:
InputDescription.h:31
ThePEG::InputDescription::DescriptionVector
vector< const InputDescription * > DescriptionVector
A vector of pointers to InputDescription objects.
Definition:
InputDescription.h:36
ThePEG::PersistentIStream
PersistentIStream is used to read persistent objects from a stream where they were previously written...
Definition:
PersistentIStream.h:48
ThePEG::PersistentIStream::tSep
static const char tSep
The special marker character indicating the end of a value.
Definition:
PersistentIStream.h:497
ThePEG::PersistentIStream::good
bool good() const
Check the state of the stream.
Definition:
PersistentIStream.h:312
ThePEG::PersistentIStream::operator>>
PersistentIStream & operator>>(long &i)
Read a long integer.
Definition:
PersistentIStream.h:184
ThePEG::PersistentIStream::operator>>
PersistentIStream & operator>>(unsigned int &i)
Read an unsigned integer.
Definition:
PersistentIStream.h:175
ThePEG::PersistentIStream::endObject
void endObject()
Scan the stream to the end of the current object.
ThePEG::PersistentIStream::escaped
char escaped()
Get the next character from the associated istream and decode it if it is escaped.
Definition:
PersistentIStream.h:367
ThePEG::PersistentIStream::version
int version
Version number of the PersistentOStream which has written the file being read.
Definition:
PersistentIStream.h:460
ThePEG::PersistentIStream::operator>>
PersistentIStream & operator>>(unsigned char &)
Read an unsigned character.
ThePEG::PersistentIStream::operator>>
PersistentIStream & operator>>(double &d)
Read a double.
Definition:
PersistentIStream.h:220
ThePEG::PersistentIStream::tNoSep
static const char tNoSep
The special marker character used to avoid confusion with escaped tSep markers.
Definition:
PersistentIStream.h:503
ThePEG::PersistentIStream::getContainer
void getContainer(Container &c)
Intput of containers streamable objects.
Definition:
PersistentIStream.h:250
ThePEG::PersistentIStream::is
istream & is()
Return a reference to the associated stream.
Definition:
PersistentIStream.h:419
ThePEG::PersistentIStream::~PersistentIStream
~PersistentIStream()
The destructor.
ThePEG::PersistentIStream::isPedantic
bool isPedantic
Pedantic or tolerant.
Definition:
PersistentIStream.h:445
ThePEG::PersistentIStream::operator>>
PersistentIStream & operator>>(Complex &)
Read a Complex.
ThePEG::PersistentIStream::ObjectVector
vector< BPtr > ObjectVector
A vector of pointers to persistent objects.
Definition:
PersistentIStream.h:55
ThePEG::PersistentIStream::theGlobalLibraries
vector< string > theGlobalLibraries
Global libraries loaded in the initialization.
Definition:
PersistentIStream.h:469
ThePEG::PersistentIStream::get
char get()
Get the next character from the associated istream.
Definition:
PersistentIStream.h:361
ThePEG::PersistentIStream::operator>>
PersistentIStream & operator>>(ConstRCPtr< T > &ptr)
Operator for extracting persistent objects from the stream.
Definition:
PersistentIStream.h:107
ThePEG::PersistentIStream::allocStream
bool allocStream
True if the associated istream should be deleted when the PersistentIStream is destroyed.
Definition:
PersistentIStream.h:451
ThePEG::PersistentIStream::getObject
BPtr getObject()
Read in an object.
ThePEG::PersistentIStream::operator>>
PersistentIStream & operator>>(RCPtr< T > &ptr)
Operator for extracting persistent objects from the stream.
Definition:
PersistentIStream.h:94
ThePEG::PersistentIStream::beginObject
bool beginObject()
Check if the next char to be read is a tBegin marker.
Definition:
PersistentIStream.h:400
ThePEG::PersistentIStream::operator>>
PersistentIStream & operator>>(float &f)
Read a float.
Definition:
PersistentIStream.h:229
ThePEG::PersistentIStream::setPedantic
PersistentIStream & setPedantic()
Set pedantic mode.
Definition:
PersistentIStream.h:291
ThePEG::PersistentIStream::pedantic
bool pedantic() const
Check the tolerance.
Definition:
PersistentIStream.h:328
ThePEG::PersistentIStream::tNo
static const char tNo
The special marker character indicating a false boolean value.
Definition:
PersistentIStream.h:513
ThePEG::PersistentIStream::operator>>
PersistentIStream & operator>>(int &i)
Read an integer.
Definition:
PersistentIStream.h:166
ThePEG::PersistentIStream::operator=
PersistentIStream & operator=(const PersistentIStream &)=delete
Standard ctors and assignment are private and not implemented.
ThePEG::PersistentIStream::subVersion
int subVersion
Subversion number of the PersistentOStream which has written the file being read.
Definition:
PersistentIStream.h:464
ThePEG::PersistentIStream::getObjectPart
void getObjectPart(tBPtr obj, const InputDescription *pid)
For a given object, read the member variables corresponding to a given InputDescription object.
ThePEG::PersistentIStream::PersistentIStream
PersistentIStream()
Standard ctors and assignment are private and not implemented.
ThePEG::PersistentIStream::tNext
static const char tNext
The marker character indicating the beginning of the next base class in case of multiple inheritance.
Definition:
PersistentIStream.h:487
ThePEG::PersistentIStream::tNull
static const char tNull
The special marker character indicating an escaped marker character.
Definition:
PersistentIStream.h:492
ThePEG::PersistentIStream::DescriptionVector
InputDescription::DescriptionVector DescriptionVector
A vector of bare pointers to InputDescription objects.
Definition:
PersistentIStream.h:58
ThePEG::PersistentIStream::globalLibraries
const vector< string > & globalLibraries() const
The global libraries loaded on initialization.
Definition:
PersistentIStream.h:333
ThePEG::PersistentIStream::setTolerant
PersistentIStream & setTolerant()
Set tolerant mode.
Definition:
PersistentIStream.h:304
ThePEG::PersistentIStream::operator>>
PersistentIStream & operator>>(string &)
Read a character string.
ThePEG::PersistentIStream::is
const istream & is() const
Return a const reference to the associated stream.
Definition:
PersistentIStream.h:424
ThePEG::PersistentIStream::skipField
void skipField()
Scan the stream for the next field separator.
Definition:
PersistentIStream.h:391
ThePEG::PersistentIStream::theIStream
istream * theIStream
A pointer to the associated istream.
Definition:
PersistentIStream.h:439
ThePEG::PersistentIStream::init
void init()
Internal initialization.
ThePEG::PersistentIStream::operator>>
PersistentIStream & operator>>(unsigned short &i)
Read an unsigned short integer.
Definition:
PersistentIStream.h:211
ThePEG::PersistentIStream::PersistentIStream
PersistentIStream(istream &is)
Constuctor giving an input stream to be used as an underlying istream.
Definition:
PersistentIStream.h:66
ThePEG::PersistentIStream::getSep
void getSep()
Read a field separator from the stream.
Definition:
PersistentIStream.h:383
ThePEG::PersistentIStream::operator>>
PersistentIStream & operator>>(unsigned long &i)
Read an unsigned long integer.
Definition:
PersistentIStream.h:193
ThePEG::PersistentIStream::operator>>
PersistentIStream & operator>>(TransientRCPtr< T > &ptr)
Operator for extracting persistent objects from the stream.
Definition:
PersistentIStream.h:120
ThePEG::PersistentIStream::tYes
static const char tYes
The special marker character indicating a true boolean value.
Definition:
PersistentIStream.h:508
ThePEG::PersistentIStream::operator>>
PersistentIStream & operator>>(bool &)
Read a bool.
ThePEG::PersistentIStream::operator>>
PersistentIStream & operator>>(TransientConstRCPtr< T > &ptr)
Operator for extracting persistent objects from the stream.
Definition:
PersistentIStream.h:133
ThePEG::PersistentIStream::tEnd
static const char tEnd
The special marker character indicating the end of an object.
Definition:
PersistentIStream.h:481
ThePEG::PersistentIStream::readObjects
ObjectVector readObjects
Lists of objects that have been read.
Definition:
PersistentIStream.h:429
ThePEG::PersistentIStream::getClass
const InputDescription * getClass()
Read a class description from the underlying stream and return a corresponding InputDescription objec...
ThePEG::PersistentIStream::setBadState
void setBadState()
Set the stream in a bad state.
Definition:
PersistentIStream.h:375
ThePEG::PersistentIStream::operator>>
PersistentIStream & operator>>(char &)
Read a character.
ThePEG::PersistentIStream::badState
bool badState
False if no errors has occurred.
Definition:
PersistentIStream.h:456
ThePEG::PersistentIStream::operator>>
PersistentIStream & operator>>(signed char &)
Read a signed character.
ThePEG::PersistentIStream::operator!
bool operator!() const
Check the state of the stream.
Definition:
PersistentIStream.h:317
ThePEG::PersistentIStream::operator>>
PersistentIStream & operator>>(short &i)
Read a short integer.
Definition:
PersistentIStream.h:202
ThePEG::PersistentIStream::readClasses
DescriptionVector readClasses
Lists of classes and corresponding version strings that have been read.
Definition:
PersistentIStream.h:434
ThePEG::PersistentIStream::PersistentIStream
PersistentIStream(const PersistentIStream &)
Standard ctors and assignment are private and not implemented.
ThePEG::PersistentIStream::tBegin
static const char tBegin
The special marker character indicating the beginning of an object.
Definition:
PersistentIStream.h:476
ThePEG::PersistentIStream::endBase
void endBase(string classname)
Scan stream for "end base class" marker.
ThePEG::PersistentIStream::PersistentIStream
PersistentIStream(string)
Constuctor giving a file name to read from.
ThePEG::Pointer::ConstRCPtr
ConstRCPtr is a reference counted (smart) const pointer.
Definition:
RCPtr.h:320
ThePEG::Pointer::RCPtr
RCPtr is a reference counted (smart) pointer.
Definition:
RCPtr.h:60
ThePEG::Pointer::TransientConstRCPtr
TransientConstRCPtr is a simple wrapper around a bare const pointer which can be assigned to and from...
Definition:
RCPtr.h:696
ThePEG::Pointer::TransientRCPtr
TransientRCPtr is a simple wrapper around a bare pointer which can be assigned to and from an RCPtr a...
Definition:
RCPtr.h:519
ThePEG
This is the main namespace within which all identifiers in ThePEG are declared.
Definition:
FactoryBase.h:28
ThePEG::operator>>
vector< T > & operator>>(vector< T > &tv, U &u)
Overload the right shift operator for vector to pop objects from a vector.
Definition:
Containers.h:192
ThePEG::Complex
std::complex< double > Complex
ThePEG code should use Complex for all complex scalars.
Definition:
Complex.h:23
ThePEG::tolerant
PersistentIStream & tolerant(PersistentIStream &is)
The manipulator for setting tolerant mode.
Definition:
PersistentIStream.h:555
ThePEG::pedantic
PersistentIStream & pedantic(PersistentIStream &is)
The manipulator for setting pedantic mode.
Definition:
PersistentIStream.h:547
ThePEG::Base
Define the base class from which all (polymorphic) classes in ThePEG are derived.
Definition:
ThePEG.h:54
Generated on Thu Jun 20 2024 14:47:00 for ThePEG by
1.9.6