thepeg
is hosted by
Hepforge
,
IPPP Durham
ThePEG
2.3.0
Persistency
PersistentOStream.h
1
// -*- C++ -*-
2
//
3
// PersistentOStream.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_PersistentOStream_H
10
#define ThePEG_PersistentOStream_H
11
// This is the declaration of the PersistentOStream class.
12
13
#include "
ThePEG/Config/ThePEG.h
"
14
#include "ThePEG/Utilities/ClassDescription.h"
15
#include "ThePEG/Utilities/Exception.h"
16
#include "ThePEG/Utilities/Debug.h"
17
#include "PersistentOStream.fh"
18
#include "PersistentOStream.xh"
19
#include <valarray>
20
21
namespace
ThePEG
{
22
51
class
PersistentOStream
{
52
53
public
:
54
55
ThePEG_DECLARE_POINTERS
(
PersistentBase
,
BPtr
);
56
58
ThePEG_DECLARE_MAP
(
cBPtr
,
int
,
ObjectMap
);
59
61
ThePEG_DECLARE_MAP
(
const
ClassDescriptionBase
*,
int
,
ClassMap
);
62
64
typedef
ClassDescriptionBase::DescriptionVector
DescriptionVector
;
65
66
public
:
67
73
PersistentOStream
(ostream &,
const
vector<string> & libs = vector<string>());
74
83
PersistentOStream
(
string
,
const
vector<string> & libs = vector<string>());
84
88
~PersistentOStream
();
89
95
template
<
typename
T>
96
PersistentOStream
&
operator<<
(
const
RCPtr<T>
& p) {
97
return
outputPointer
(p);
98
}
99
105
template
<
typename
T>
106
PersistentOStream
&
operator<<
(
const
ConstRCPtr<T>
& p) {
107
return
outputPointer
(p);
108
}
109
115
template
<
typename
T>
116
PersistentOStream
&
operator<<
(
const
TransientRCPtr<T>
& p) {
117
return
outputPointer
(p);
118
}
119
125
template
<
typename
T>
126
PersistentOStream
&
operator<<
(
const
TransientConstRCPtr<T>
& p) {
127
return
outputPointer
(p);
128
}
129
130
136
PersistentOStream
&
operator<<
(
string
s) {
137
for
( string::const_iterator i = s.begin(); i < s.end(); ++i )
escape
(*i);
138
put
(
tSep
);
139
return
*
this
;
140
}
141
145
PersistentOStream
&
operator<<
(
char
c) {
146
escape
(c);
147
put
(
tSep
);
148
return
*
this
;
149
}
150
154
PersistentOStream
&
operator<<
(
signed
char
c) {
155
return
(*
this
) <<
static_cast<
char
>
(c);
156
}
157
161
PersistentOStream
&
operator<<
(
unsigned
char
c) {
162
return
(*
this
) <<
static_cast<
char
>
(c);
163
}
164
168
PersistentOStream
&
operator<<
(
int
i) {
169
os
() << i;
170
put
(
tSep
);
171
return
*
this
;
172
}
173
177
PersistentOStream
&
operator<<
(
unsigned
int
i) {
178
os
() << i;
179
put
(
tSep
);
180
return
*
this
;
181
}
182
186
PersistentOStream
&
operator<<
(
long
i) {
187
os
() << i;
188
put
(
tSep
);
189
return
*
this
;
190
}
191
195
PersistentOStream
&
operator<<
(
unsigned
long
i) {
196
os
() << i;
197
put
(
tSep
);
198
return
*
this
;
199
}
200
204
PersistentOStream
&
operator<<
(
short
i) {
205
os
() << i;
206
put
(
tSep
);
207
return
*
this
;
208
}
209
213
PersistentOStream
&
operator<<
(
unsigned
short
i) {
214
os
() << i;
215
put
(
tSep
);
216
return
*
this
;
217
}
218
222
PersistentOStream
&
operator<<
(
double
d) {
223
if
( ! isfinite(d) )
224
throw
WriteError()
225
<<
"Tried to write a NaN or Inf double to a persistent stream."
226
<<
Exception::runerror
;
227
os
() << setprecision(18) << d;
228
put
(
tSep
);
229
return
*
this
;
230
}
231
235
PersistentOStream
&
operator<<
(
float
f) {
236
if
( ! isfinite(f) )
237
throw
WriteError()
238
<<
"Tried to write a NaN or Inf float to a persistent stream."
239
<<
Exception::runerror
;
240
os
() << setprecision(9) << f;
241
put
(
tSep
);
242
return
*
this
;
243
}
244
248
PersistentOStream
&
operator<<
(
bool
t) {
249
if
(t)
put
(
tYes
);
250
else
put
(
tNo
);
251
// This is a workaround for a possible bug in gcc 4.0.0
252
// which inserts tYes and tNo as global symbols although
253
// they are private
254
// put(t? tYes: tNo);
255
put
(
tSep
);
256
return
*
this
;
257
}
258
262
PersistentOStream
&
operator<<
(
const
char
* s) {
263
*
this
<< string(s);
264
return
*
this
;
265
}
266
270
PersistentOStream
&
operator<<
(
Complex
z) {
271
*
this
<< z.real() << z.imag();
272
return
*
this
;
273
}
275
279
template
<
typename
Container>
280
void
putContainer
(
const
Container & c) {
281
*
this
<< c.size();
282
for
(
typename
Container::const_iterator it = c.begin();
283
it != c.end() &&
good
() ; ++it )
284
*
this
<< *it;
285
}
286
290
PersistentOStream
&
outputPointer
(
tcBPtr
);
291
299
void
putObjectPart
(
tcBPtr
obj,
const
ClassDescriptionBase
* cd);
300
305
PersistentOStream
&
flush
();
306
311
PersistentOStream
&
push
() {
312
lastSavedObject
.push(
writtenObjects
.size() - 1);
313
return
*
this
;
314
}
315
319
PersistentOStream
&
pop
() {
320
lastSavedObject
.pop();
321
return
*
this
;
322
}
323
327
bool
good
()
const
{
return
!
badState
&&
os
(); }
328
332
operator
bool()
const
{
return
good
(); }
333
337
bool
operator!
()
const
{
return
!
good
(); }
338
339
private
:
340
345
struct
MissingClass:
public
Exception
{};
349
struct
WriteError:
public
Exception {};
355
static
const
int
version
= 0;
356
360
static
const
int
subVersion
= 3;
361
367
static
const
char
tBegin
=
'{'
;
368
372
static
const
char
tEnd
=
'}'
;
373
381
static
const
char
tNext
=
'|'
;
382
386
static
const
char
tNull
=
'\\'
;
387
391
static
const
char
tSep
=
'\n'
;
392
397
static
const
char
tNoSep
=
'n'
;
398
402
static
const
char
tYes
=
'y'
;
403
407
static
const
char
tNo
=
'n'
;
409
413
bool
isToken
(
char
c)
const
{
414
return
c ==
tBegin
|| c ==
tEnd
|| c ==
tNext
|| c ==
tSep
|| c ==
tNull
;
415
}
416
420
void
setBadState
() {
421
breakThePEG();
422
badState
=
true
;
423
}
424
428
void
checkState
() {
if
( !
os
() )
badState
=
true
; }
429
433
const
ClassDescriptionBase
*
writeClassId
(
tcBPtr
);
434
438
void
writeClassDescription
(
const
ClassDescriptionBase
*);
439
443
void
beginObject
() {
put
(
tBegin
); }
444
448
void
endObject
() {
put
(
tEnd
); }
449
453
void
endBase
() {
put
(
tNext
); }
454
458
void
put
(
char
c) {
os
().put(c); }
459
464
void
escape
(
char
c) {
465
if
(
isToken
(c) ) {
466
put
(
tNull
);
467
put
( c ==
tSep
?
tNoSep
: c );
468
}
else
469
put
(c);
470
}
471
475
ostream &
os
() {
return
*
theOStream
; }
476
480
const
ostream &
os
()
const
{
return
*
theOStream
; }
481
485
void
init
(
const
vector<string> & libs);
486
490
ObjectMap
writtenObjects
;
491
495
stack<int>
lastSavedObject
;
496
500
ClassMap
writtenClasses
;
501
505
ostream *
theOStream
;
506
510
bool
badState
;
511
515
bool
allocStream
;
516
517
private
:
518
522
PersistentOStream
();
523
527
PersistentOStream
(
const
PersistentOStream
&);
528
532
PersistentOStream
&
operator=
(
const
PersistentOStream
&) =
delete
;
533
534
};
535
539
inline
PersistentOStream
&
540
operator<<
(
PersistentOStream
& os, PersistentOManip func) {
541
return
(*func)(os);
542
}
543
544
548
inline
PersistentOStream
&
flush
(
PersistentOStream
& os) {
return
os.
flush
(); }
549
553
inline
PersistentOStream
&
push
(
PersistentOStream
& os) {
return
os.
push
(); }
554
558
inline
PersistentOStream
&
pop
(
PersistentOStream
& os) {
return
os.
pop
(); }
559
560
567
template
<
typename
T1,
typename
T2>
568
inline
PersistentOStream
&
operator<<
(
PersistentOStream
& os,
569
const
pair<T1,T2> & p) {
570
return
os << p.first << p.second;
571
}
572
576
template
<
typename
Key,
typename
T,
typename
Cmp,
typename
A>
577
inline
PersistentOStream
&
operator<<
(
PersistentOStream
& os,
578
const
multimap<Key,T,Cmp,A> & m) {
579
os.
putContainer
(m);
580
return
os;
581
}
582
586
template
<
typename
Key,
typename
T,
typename
Cmp,
typename
A>
587
inline
PersistentOStream
&
operator<<
(
PersistentOStream
& os,
588
const
map<Key,T,Cmp,A> & m) {
589
os.
putContainer
(m);
590
return
os;
591
}
592
596
template
<
typename
Key,
typename
Cmp,
typename
A>
597
inline
PersistentOStream
&
operator<<
(
PersistentOStream
& os,
598
const
set<Key,Cmp,A> & s) {
599
os.
putContainer
(s);
600
return
os;
601
}
602
603
607
template
<
typename
Key,
typename
Cmp,
typename
A>
608
inline
PersistentOStream
&
operator<<
(
PersistentOStream
& os,
609
const
multiset<Key,Cmp,A> & s) {
610
os.
putContainer
(s);
611
return
os;
612
}
613
614
618
template
<
typename
T,
typename
A>
619
inline
PersistentOStream
&
operator<<
(
PersistentOStream
& os,
620
const
list<T,A> & l) {
621
os.
putContainer
(l);
622
return
os;
623
}
624
625
629
template
<
typename
T,
typename
A>
630
inline
PersistentOStream
&
operator<<
(
PersistentOStream
& os,
631
const
vector<T,A> & v) {
632
os.
putContainer
(v);
633
return
os;
634
}
635
639
template
<
typename
T,
size_t
N>
640
inline
PersistentOStream
&
operator<<
(
PersistentOStream
& os,
641
const
array<T,N> & a) {
642
for
(
auto
it = a.cbegin(); it != a.cend() && os.
good
() ; ++it )
643
os << *it;
644
return
os;
645
}
646
650
template
<
typename
T,
typename
A>
651
inline
PersistentOStream
&
operator<<
(
PersistentOStream
& os,
652
const
deque<T,A> & d) {
653
os.
putContainer
(d);
654
return
os;
655
}
656
660
template
<
typename
T>
661
inline
PersistentOStream
&
operator<<
(
PersistentOStream
& os,
662
const
std::valarray<T> & v) {
663
os << v.size();
664
for
(
int
i = 0, N = v.size(); i < N; ++i ) os << v[i];
665
return
os;
666
}
668
669
}
670
671
#endif
/* ThePEG_PersistentOStream_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::ClassDescriptionBase
ClassDescriptionBase is the base class for all class description classes.
Definition:
ClassDescription.h:63
ThePEG::ClassDescriptionBase::DescriptionVector
vector< const ClassDescriptionBase * > DescriptionVector
A vector of class descriptions.
Definition:
ClassDescription.h:68
ThePEG::Exception
Exception is the base class for all exceptions to be used in ThePEG.
Definition:
Exception.h:44
ThePEG::Exception::runerror
@ runerror
Severe error, (the run should be terminated).
Definition:
Exception.h:61
ThePEG::PersistentOStream
PersistentOStream is used to write objects persistently to a stream from which they can be read in ag...
Definition:
PersistentOStream.h:51
ThePEG::PersistentOStream::DescriptionVector
ClassDescriptionBase::DescriptionVector DescriptionVector
A vector of bare pointers to InputDescription objects.
Definition:
PersistentOStream.h:64
ThePEG::PersistentOStream::operator<<
PersistentOStream & operator<<(short i)
Write a short integer.
Definition:
PersistentOStream.h:204
ThePEG::PersistentOStream::~PersistentOStream
~PersistentOStream()
The destructor.
ThePEG::PersistentOStream::pop
PersistentOStream & pop()
Instuct the stream not to save the following objects.
Definition:
PersistentOStream.h:319
ThePEG::PersistentOStream::ObjectMap
map< cBPtr, int, less< cBPtr > > ObjectMap
A map of objects indexed by integers.
Definition:
PersistentOStream.h:58
ThePEG::PersistentOStream::operator<<
PersistentOStream & operator<<(unsigned int i)
Write an unsigned integer.
Definition:
PersistentOStream.h:177
ThePEG::PersistentOStream::endObject
void endObject()
Put a "end of object" marker on the associated ostream.
Definition:
PersistentOStream.h:448
ThePEG::PersistentOStream::badState
bool badState
True if no errors has occurred.
Definition:
PersistentOStream.h:510
ThePEG::PersistentOStream::init
void init(const vector< string > &libs)
Write out initial metainfo on the stream.
ThePEG::PersistentOStream::PersistentOStream
PersistentOStream(string, const vector< string > &libs=vector< string >())
Constuctor giving a file name to read.
ThePEG::PersistentOStream::operator<<
PersistentOStream & operator<<(bool t)
Write a boolean.
Definition:
PersistentOStream.h:248
ThePEG::PersistentOStream::escape
void escape(char c)
Put a character on the associated ostream but escape it if it is a token.
Definition:
PersistentOStream.h:464
ThePEG::PersistentOStream::operator<<
PersistentOStream & operator<<(float f)
Write a float.
Definition:
PersistentOStream.h:235
ThePEG::PersistentOStream::flush
PersistentOStream & flush()
Remove all objects that have been written, except those which are to be saved, from the list of writt...
ThePEG::PersistentOStream::operator<<
PersistentOStream & operator<<(const char *s)
Write a c-style character string (to be read in as a std::string).
Definition:
PersistentOStream.h:262
ThePEG::PersistentOStream::operator<<
PersistentOStream & operator<<(const ConstRCPtr< T > &p)
Operator for writing persistent objects to the stream.
Definition:
PersistentOStream.h:106
ThePEG::PersistentOStream::ClassMap
map< const ClassDescriptionBase *, int, less< const ClassDescriptionBase * > > ClassMap
A map relating class descriptions to integers.
Definition:
PersistentOStream.h:61
ThePEG::PersistentOStream::tNoSep
static const char tNoSep
The special marker character used to avoid confusion with escaped tSep markers.
Definition:
PersistentOStream.h:397
ThePEG::PersistentOStream::operator<<
PersistentOStream & operator<<(signed char c)
Write a signed character.
Definition:
PersistentOStream.h:154
ThePEG::PersistentOStream::operator!
bool operator!() const
Check the state of the stream.
Definition:
PersistentOStream.h:337
ThePEG::PersistentOStream::writeClassId
const ClassDescriptionBase * writeClassId(tcBPtr)
Write out class information to the associated ostream.
ThePEG::PersistentOStream::operator=
PersistentOStream & operator=(const PersistentOStream &)=delete
Standard ctors and assignment are private and not implemented.
ThePEG::PersistentOStream::PersistentOStream
PersistentOStream(const PersistentOStream &)
Standard ctors and assignment are private and not implemented.
ThePEG::PersistentOStream::tSep
static const char tSep
The special marker character indicating the end of a value.
Definition:
PersistentOStream.h:391
ThePEG::PersistentOStream::allocStream
bool allocStream
True if the associated ostream should be deleted in the destructor.
Definition:
PersistentOStream.h:515
ThePEG::PersistentOStream::writtenObjects
ObjectMap writtenObjects
List of written objects.
Definition:
PersistentOStream.h:490
ThePEG::PersistentOStream::version
static const int version
The version of this PersistentOStream implementation.
Definition:
PersistentOStream.h:355
ThePEG::PersistentOStream::operator<<
PersistentOStream & operator<<(const TransientRCPtr< T > &p)
Operator for writing persistent objects to the stream.
Definition:
PersistentOStream.h:116
ThePEG::PersistentOStream::operator<<
PersistentOStream & operator<<(string s)
Write a character string.
Definition:
PersistentOStream.h:136
ThePEG::PersistentOStream::operator<<
PersistentOStream & operator<<(Complex z)
Write a Complex.
Definition:
PersistentOStream.h:270
ThePEG::PersistentOStream::lastSavedObject
stack< int > lastSavedObject
List of written objects that are to be saved.
Definition:
PersistentOStream.h:495
ThePEG::PersistentOStream::setBadState
void setBadState()
Set the stream in a bad state.
Definition:
PersistentOStream.h:420
ThePEG::PersistentOStream::os
ostream & os()
Return a reference to the associated ostream.
Definition:
PersistentOStream.h:475
ThePEG::PersistentOStream::putObjectPart
void putObjectPart(tcBPtr obj, const ClassDescriptionBase *cd)
For a given object, write the member variables corresponding to a given ClassDescriptionBase object.
ThePEG::PersistentOStream::operator<<
PersistentOStream & operator<<(unsigned char c)
Write an unsigned character.
Definition:
PersistentOStream.h:161
ThePEG::PersistentOStream::operator<<
PersistentOStream & operator<<(const RCPtr< T > &p)
Operator for writing persistent objects to the stream.
Definition:
PersistentOStream.h:96
ThePEG::PersistentOStream::writtenClasses
ClassMap writtenClasses
List of written classes.
Definition:
PersistentOStream.h:500
ThePEG::PersistentOStream::push
PersistentOStream & push()
Instuct the stream to save the following objects (protecting them from being flushed).
Definition:
PersistentOStream.h:311
ThePEG::PersistentOStream::writeClassDescription
void writeClassDescription(const ClassDescriptionBase *)
write out class information to the associated ostream.
ThePEG::PersistentOStream::tYes
static const char tYes
The special marker character indicating a true boolean value.
Definition:
PersistentOStream.h:402
ThePEG::PersistentOStream::endBase
void endBase()
Put an "next base class" marker on the associated ostream.
Definition:
PersistentOStream.h:453
ThePEG::PersistentOStream::tNext
static const char tNext
The marker character indicating the beginning of the next base class in case of multiple inheritance.
Definition:
PersistentOStream.h:381
ThePEG::PersistentOStream::checkState
void checkState()
Check if the state is ok.
Definition:
PersistentOStream.h:428
ThePEG::PersistentOStream::operator<<
PersistentOStream & operator<<(double d)
Write a double.
Definition:
PersistentOStream.h:222
ThePEG::PersistentOStream::tBegin
static const char tBegin
The special marker character indicating the beginning of an object.
Definition:
PersistentOStream.h:367
ThePEG::PersistentOStream::operator<<
PersistentOStream & operator<<(char c)
Write a character.
Definition:
PersistentOStream.h:145
ThePEG::PersistentOStream::operator<<
PersistentOStream & operator<<(const TransientConstRCPtr< T > &p)
Operator for writing persistent objects to the stream.
Definition:
PersistentOStream.h:126
ThePEG::PersistentOStream::subVersion
static const int subVersion
The subversion of this PersistentOStream implementation.
Definition:
PersistentOStream.h:360
ThePEG::PersistentOStream::putContainer
void putContainer(const Container &c)
Output of containers of streamable objects.
Definition:
PersistentOStream.h:280
ThePEG::PersistentOStream::put
void put(char c)
Put a character on the associated ostream.
Definition:
PersistentOStream.h:458
ThePEG::PersistentOStream::operator<<
PersistentOStream & operator<<(unsigned short i)
Write an unsigned short integer.
Definition:
PersistentOStream.h:213
ThePEG::PersistentOStream::PersistentOStream
PersistentOStream()
Standard ctors and assignment are private and not implemented.
ThePEG::PersistentOStream::operator<<
PersistentOStream & operator<<(long i)
Write a long integer.
Definition:
PersistentOStream.h:186
ThePEG::PersistentOStream::tEnd
static const char tEnd
The special marker character indicating the end of an object.
Definition:
PersistentOStream.h:372
ThePEG::PersistentOStream::good
bool good() const
Check the state of the stream.
Definition:
PersistentOStream.h:327
ThePEG::PersistentOStream::tNull
static const char tNull
The special marker character indicating an escaped marker character.
Definition:
PersistentOStream.h:386
ThePEG::PersistentOStream::theOStream
ostream * theOStream
A pointer to the associated ostream.
Definition:
PersistentOStream.h:505
ThePEG::PersistentOStream::operator<<
PersistentOStream & operator<<(unsigned long i)
Write an unsigned long integer.
Definition:
PersistentOStream.h:195
ThePEG::PersistentOStream::isToken
bool isToken(char c) const
Return true if the given character is aspecial marker character.
Definition:
PersistentOStream.h:413
ThePEG::PersistentOStream::outputPointer
PersistentOStream & outputPointer(tcBPtr)
Write out a persistent object given a pointer to it.
ThePEG::PersistentOStream::beginObject
void beginObject()
Put a "begin object" marker on the associated ostream.
Definition:
PersistentOStream.h:443
ThePEG::PersistentOStream::os
const ostream & os() const
Return a const reference to the associated ostream.
Definition:
PersistentOStream.h:480
ThePEG::PersistentOStream::operator<<
PersistentOStream & operator<<(int i)
Write an integer.
Definition:
PersistentOStream.h:168
ThePEG::PersistentOStream::tNo
static const char tNo
The special marker character indicating a false boolean value.
Definition:
PersistentOStream.h:407
ThePEG::PersistentOStream::PersistentOStream
PersistentOStream(ostream &, const vector< string > &libs=vector< string >())
Constuctor giving an output stream.
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::push
PersistentOStream & push(PersistentOStream &os)
The manipulator for calling PersistentOStream::push().
Definition:
PersistentOStream.h:553
ThePEG::Complex
std::complex< double > Complex
ThePEG code should use Complex for all complex scalars.
Definition:
Complex.h:23
ThePEG::pop
PersistentOStream & pop(PersistentOStream &os)
The manipulator for calling PersistentOStream::pop().
Definition:
PersistentOStream.h:558
ThePEG::operator<<
vector< T > & operator<<(vector< T > &tv, const U &u)
Overload the left shift operator for vector to push_back objects to a vector.
Definition:
Containers.h:179
ThePEG::flush
PersistentOStream & flush(PersistentOStream &os)
The manipulator for calling PersistentOStream::flush().
Definition:
PersistentOStream.h:548
ThePEG_DECLARE_MAP
#define ThePEG_DECLARE_MAP(KEYTYPE, VALTYPE, NAME)
Macro for declaring a map.
Definition:
std.h:185
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