thepeg is hosted by Hepforge, IPPP Durham
ThePEG  2.2.1
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 
52 
53 public:
54 
56 
59 
62 
65 
66 public:
67 
73  PersistentOStream(ostream &, const vector<string> & libs = vector<string>());
74 
83  PersistentOStream(string, const vector<string> & libs = vector<string>());
84 
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 
137  for ( string::const_iterator i = s.begin(); i < s.end(); ++i ) escape(*i);
138  put(tSep);
139  return *this;
140  }
141 
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 
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 
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 
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 
223  if ( ! isfinite(d) )
224  throw WriteError()
225  << "Tried to write a NaN or Inf double to a persistent stream."
227  os() << setprecision(18) << d;
228  put(tSep);
229  return *this;
230  }
231 
236  if ( ! isfinite(f) )
237  throw WriteError()
238  << "Tried to write a NaN or Inf float to a persistent stream."
240  os() << setprecision(9) << f;
241  put(tSep);
242  return *this;
243  }
244 
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 
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 
291 
299  void putObjectPart(tcBPtr obj, const ClassDescriptionBase * cd);
300 
306 
312  lastSavedObject.push(writtenObjects.size() - 1);
313  return *this;
314  }
315 
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 
434 
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 
491 
495  stack<int> lastSavedObject;
496 
501 
505  ostream * theOStream;
506 
510  bool badState;
511 
516 
517 private:
518 
523 
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>
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>
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>
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>
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>
609  const multiset<Key,Cmp,A> & s) {
610  os.putContainer(s);
611  return os;
612 }
613 
614 
618 template <typename T, typename A>
620  const list<T,A> & l) {
621  os.putContainer(l);
622  return os;
623 }
624 
625 
629 template <typename T, typename A>
631  const vector<T,A> & v) {
632  os.putContainer(v);
633  return os;
634 }
635 
639 template <typename T, size_t N>
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>
652  const deque<T,A> & d) {
653  os.putContainer(d);
654  return os;
655 }
656 
660 template <typename T>
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 */
PersistentOStream & operator<<(bool t)
Write a boolean.
PersistentOStream & operator<<(unsigned long i)
Write an unsigned long integer.
void writeClassDescription(const ClassDescriptionBase *)
write out class information to the associated ostream.
PersistentOStream & operator<<(int i)
Write an integer.
PersistentOStream & operator<<(float f)
Write a float.
ClassDescriptionBase is the base class for all class description classes.
void checkState()
Check if the state is ok.
void endBase()
Put an "next base class" marker on the associated ostream.
std::complex< double > Complex
ThePEG code should use Complex for all complex scalars.
Definition: Complex.h:23
static const int subVersion
The subversion of this PersistentOStream implementation.
Define the base class from which all (polymorphic) classes in ThePEG are derived. ...
Definition: ThePEG.h:54
PersistentOStream & operator<<(const RCPtr< T > &p)
Operator for writing persistent objects to the stream.
PersistentOStream is used to write objects persistently to a stream from which they can be read in ag...
PersistentOStream & operator<<(unsigned int i)
Write an unsigned integer.
TransientConstRCPtr is a simple wrapper around a bare const pointer which can be assigned to and from...
Definition: RCPtr.h:696
PersistentOStream & operator<<(char c)
Write a character.
ConstRCPtr is a reference counted (smart) const pointer.
Definition: RCPtr.h:320
const ostream & os() const
Return a const reference to the associated ostream.
void setBadState()
Set the stream in a bad state.
PersistentOStream & flush()
Remove all objects that have been written, except those which are to be saved, from the list of writt...
ostream * theOStream
A pointer to the associated ostream.
This is the main namespace within which all identifiers in ThePEG are declared.
Definition: FactoryBase.h:28
void beginObject()
Put a "begin object" marker on the associated ostream.
PersistentOStream & operator<<(signed char c)
Write a signed character.
This is the main config header file for ThePEG.
static const char tNull
The special marker character indicating an escaped marker character.
map< cBPtr, int, less< cBPtr > > ObjectMap
A map of objects indexed by integers.
static const char tNoSep
The special marker character used to avoid confusion with escaped tSep markers.
PersistentOStream & operator<<(unsigned char c)
Write an unsigned character.
PersistentOStream & operator<<(Complex z)
Write a Complex.
ClassMap writtenClasses
List of written classes.
PersistentOStream & operator<<(const char *s)
Write a c-style character string (to be read in as a std::string).
ThePEG_DECLARE_MAP(long, PDPtr, ParticleMap)
A map relating integers to ParticleData objects.
void put(char c)
Put a character on the associated ostream.
void putContainer(const Container &c)
Output of containers of streamable objects.
~PersistentOStream()
The destructor.
void escape(char c)
Put a character on the associated ostream but escape it if it is a token.
bool badState
True if no errors has occurred.
Severe error, (the run should be terminated).
Definition: Exception.h:61
ostream & os()
Return a reference to the associated ostream.
static const char tNext
The marker character indicating the beginning of the next base class in case of multiple inheritance...
void init(const vector< string > &libs)
Write out initial metainfo on the stream.
bool good() const
Check the state of the stream.
static const char tYes
The special marker character indicating a true boolean value.
PersistentOStream & outputPointer(tcBPtr)
Write out a persistent object given a pointer to it.
bool allocStream
True if the associated ostream should be deleted in the destructor.
RCPtr is a reference counted (smart) pointer.
Definition: RCPtr.h:60
PersistentOStream & operator=(const PersistentOStream &)=delete
Standard ctors and assignment are private and not implemented.
static const char tNo
The special marker character indicating a false boolean value.
static const char tEnd
The special marker character indicating the end of an object.
#define ThePEG_DECLARE_POINTERS(full, abbrev)
This macro helps us to declare pointers and stuff to standard classes.
Definition: Pointers.h:47
static const char tBegin
The special marker character indicating the beginning of an object.
const ClassDescriptionBase * writeClassId(tcBPtr)
Write out class information to the associated ostream.
ClassDescriptionBase::DescriptionVector DescriptionVector
A vector of bare pointers to InputDescription objects.
Exception is the base class for all exceptions to be used in ThePEG.
Definition: Exception.h:44
static const int version
The version of this PersistentOStream implementation.
PersistentOStream & push()
Instuct the stream to save the following objects (protecting them from being flushed).
void endObject()
Put a "end of object" marker on the associated ostream.
PersistentOStream & operator<<(short i)
Write a short integer.
bool operator!() const
Check the state of the stream.
PersistentOStream & operator<<(unsigned short i)
Write an unsigned short integer.
bool isToken(char c) const
Return true if the given character is aspecial marker character.
vector< const ClassDescriptionBase * > DescriptionVector
A vector of class descriptions.
PersistentOStream & operator<<(long i)
Write a long integer.
PersistentOStream()
Standard ctors and assignment are private and not implemented.
map< const ClassDescriptionBase *, int, less< const ClassDescriptionBase *> > ClassMap
A map relating class descriptions to integers.
void putObjectPart(tcBPtr obj, const ClassDescriptionBase *cd)
For a given object, write the member variables corresponding to a given ClassDescriptionBase object...
stack< int > lastSavedObject
List of written objects that are to be saved.
static const char tSep
The special marker character indicating the end of a value.
PersistentOStream & pop()
Instuct the stream not to save the following objects.
PersistentOStream & operator<<(double d)
Write a double.
ObjectMap writtenObjects
List of written objects.
PersistentOStream & operator<<(string s)
Write a character string.