/usr/include/ThePEG/Persistency/PersistentIStream.h is in libthepeg-dev 1.8.0-1.1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 | // -*- C++ -*-
//
// PersistentIStream.h is a part of ThePEG - Toolkit for HEP Event Generation
// Copyright (C) 1999-2011 Leif Lonnblad
//
// ThePEG is licenced under version 2 of the GPL, see COPYING for details.
// Please respect the MCnet academic guidelines, see GUIDELINES for details.
//
#ifndef ThePEG_PersistentIStream_H
#define ThePEG_PersistentIStream_H
// This is the declaration of the PersistentIStream class.
#include "ThePEG/Config/ThePEG.h"
#include "InputDescription.h"
#include "PersistentIStream.fh"
#include "ThePEG/Utilities/Exception.h"
#include <climits>
namespace ThePEG {
/** @ingroup Persistency
* PersistentIStream is used to read persistent objects from a stream
* where they were previously written using PersistentOStream. Basic
* types and pointers to objects derived from
* <code>PersistentBase</code> should be read in the same order they
* were written out. If <code>pedantic()</code> is true the same
* classes that were written out must be present in the current
* program. If <code>pedantic()</code> is false and if an object is
* read for which only a base class is present in the current program,
* only the parts corresponding to the base class will be read, and
* the rest will be gracefully skipped.
*
* Each base class of a given object will be asked to read its
* members from the stream starting from the least derived class going to
* the most derived one. Members may be pointers to other persistent
* objects or basic types or containers of these. The output for each
* object part should be implemented by specializing the
* ClassTraits<T>::input method, which otherwise
* will call the non-virtual <code>persistentInput</code> function of
* the class. Note that for diamond-shaped multiple inheritance
* structures, the virtual base classes will be written out several
* times for the same object.
*
* @see PersistentOStream
* @see ClassTraits
*/
class PersistentIStream {
public:
ThePEG_DECLARE_POINTERS(PersistentBase,BPtr);
/** A vector of pointers to persistent objects */
typedef vector<BPtr> ObjectVector;
/** A vector of bare pointers to InputDescription objects. */
typedef InputDescription::DescriptionVector DescriptionVector;
public:
/**
* Constuctor giving an input stream to be used as an underlying
* istream. If keepid is true, the order in which the original
* objects were created will be kept.
*/
PersistentIStream(istream & is, bool keepid = false)
: theIStream(&is), isPedantic(true),
allocStream(false), badState(false),
keepId(keepid)
{
init();
}
/**
* Constuctor giving a file name to read from. If the first
* character in the string is a '|', the corresponding program is
* run and its standard output is used instead. If the filename ends
* in ".gz" the file is uncompressed with gzip. If keepid is true,
* the order in which the original objects were created will be
* kept.
*/
PersistentIStream(string, bool keepid = false);
/**
* The destructor.
*/
~PersistentIStream();
/**
* Operator for extracting persistent objects from the stream.
* @param ptr this pointer will refer to the extracted object.
* @return a reference to the stream.
*/
template <typename T>
PersistentIStream & operator>>(RCPtr<T> & ptr) {
BPtr b = getObject();
ptr = dynamic_ptr_cast< RCPtr<T> >(b);
if ( b && !ptr ) setBadState();
return *this;
}
/**
* Operator for extracting persistent objects from the stream.
* @param ptr this pointer will refer to the extracted object.
* @return a reference to the stream.
*/
template <typename T>
PersistentIStream & operator>>(ConstRCPtr<T> & ptr) {
BPtr b = getObject();
ptr = dynamic_ptr_cast< ConstRCPtr<T> >(b);
if ( b && !ptr ) setBadState();
return *this;
}
/**
* Operator for extracting persistent objects from the stream.
* @param ptr this pointer will refer to the extracted object.
* @return a reference to the stream.
*/
template <typename T>
PersistentIStream & operator>>(TransientRCPtr<T> & ptr) {
BPtr b = getObject();
ptr = dynamic_ptr_cast< TransientRCPtr<T> >(b);
if ( b && !ptr ) setBadState();
return *this;
}
/**
* Operator for extracting persistent objects from the stream.
* @param ptr this pointer will refer to the extracted object.
* @return a reference to the stream.
*/
template <typename T>
PersistentIStream & operator>>(TransientConstRCPtr<T> & ptr) {
BPtr b = getObject();
ptr = dynamic_ptr_cast< TransientConstRCPtr<T> >(b);
if ( b && !ptr ) setBadState();
return *this;
}
/** @name Operators for extracting built-in types from the stream. */
//@{
/**
* Read a character string.
*/
PersistentIStream & operator>>(string &);
/**
* Read a character.
*/
PersistentIStream & operator>>(char &);
/**
* Read a signed character.
*/
PersistentIStream & operator>>(signed char &);
/**
* Read an unsigned character.
*/
PersistentIStream & operator>>(unsigned char &);
/**
* Read an integer.
*/
PersistentIStream & operator>>(int & i) {
is() >> i;
getSep();
return *this;
}
/**
* Read an unsigned integer.
*/
PersistentIStream & operator>>(unsigned int & i) {
is() >> i;
getSep();
return *this;
}
/**
* Read a long integer.
*/
PersistentIStream & operator>>(long & i) {
is() >> i;
getSep();
return *this;
}
/**
* Read an unsigned long integer.
*/
PersistentIStream & operator>>(unsigned long & i) {
is() >> i;
getSep();
return *this;
}
/**
* Read a short integer.
*/
PersistentIStream & operator>>(short & i) {
is() >> i;
getSep();
return *this;
}
/**
* Read an unsigned short integer.
*/
PersistentIStream & operator>>(unsigned short & i) {
is() >> i;
getSep();
return *this;
}
/**
* Read a double.
*/
PersistentIStream & operator>>(double & d) {
is() >> d;
getSep();
return *this;
}
/**
* Read a float.
*/
PersistentIStream & operator>>(float & f) {
is() >> f;
getSep();
return *this;
}
/**
* Read a bool.
*/
PersistentIStream & operator>>(bool &);
/**
* Read a Complex.
*/
PersistentIStream & operator>>(Complex &);
//@}
/**
* Intput of containers streamable objects.
* @param c the container into which objects are added.
*/
template <typename Container> void getContainer(Container & c) {
long size;
typename Container::value_type val;
c.clear();
*this >> size;
while ( size-- && good() ) {
*this >> val;
c.insert(c.end(), val);
}
}
/**
* Read in an object. Create an object and read its data from the
* stream.
* @return a pointer to the read object.
*/
BPtr getObject();
/**
* For a given object, read the member variables corresponding to a
* given InputDescription object.
* @param obj the object to be read into.
* @param pid a pointer to an InputDescription describing the
* (sub)class to be read.
*/
void getObjectPart(tBPtr obj, const InputDescription * pid);
/**
* Read a class description from the underlying stream and return a
* corresponding InputDescription object
*/
const InputDescription * getClass();
/**
* Set pedantic mode. If the stream is set to be tolerant it is
* allowed to read an object from the stream even if the
* corresponding class is not known to the running executable, under
* the condition that a public base class of the unknown class is
* known. If the stream is set to be pedantic this is not allowed.
* By default, the stream is pedantic.
*/
PersistentIStream & setPedantic() {
isPedantic = true;
return *this;
}
/**
* Set tolerant mode. If the stream is set to be tolerant it is
* allowed to read an object from the stream even if the
* corresponding class is not known to the running executable, under
* the condition that a public base class of the unknown class is
* known. If the stream is set to be pedantic this is not allowed.
* By default, the stream is pedantic.
*/
PersistentIStream & setTolerant() {
isPedantic = false;
return *this;
}
/**
* Check the state of the stream.
*/
bool good() const { return !badState && is(); }
/**
* Check the state of the stream.
*/
bool operator!() const { return !good(); }
/**
* Check the state of the stream.
*/
operator bool() const { return good(); }
/**
* Check the tolerance. Returns true if setPedantic() has been
* called or if not setTolerant() has been called.
*/
bool pedantic() const { return isPedantic; }
/**
* The global libraries loaded on initialization.
*/
const vector<string> & globalLibraries() const {
return theGlobalLibraries;
}
private:
/** @cond EXCEPTIONCLASSES */
/** @ingroup Persistency
Thrown if a class is missing */
struct MissingClass: public Exception {};
/** @ingroup Persistency Thrown if an object which should have been
read in is missing. */
struct MissingObject: public Exception {};
/** @ingroup Persistency Thrown if reading from the stream failed
for some reason. */
struct ReadFailure: public Exception {};
/** @endcond */
/**
* Internal initialization.
*/
void init();
/**
* Get the next character from the associated istream.
*/
char get() { return is().get(); }
/**
* Get the next character from the associated istream and decode it
* if it is escaped.
*/
char escaped() {
char c = get();
return c == tNoSep? tSep: c;
}
/**
* Set the stream in a bad state
*/
void setBadState() {
breakThePEG();
badState = true;
}
/**
* Read a field separator from the stream.
*/
void getSep() {
if ( !pedantic() ) skipField();
else if ( get() != tSep ) setBadState();
}
/**
* Scan the stream for the next field separator.
*/
void skipField() {
is().ignore(INT_MAX, tSep);
if ( !is() ) setBadState();
}
/**
* Check if the next char to be read is a tBegin marker.
*/
bool beginObject() { return is().peek() == tBegin; }
/**
* Scan the stream to the end of the current object. If any new object are
* found these are read prom the stream to ensure that the pointer structure
* is preserved.
*/
void endObject();
/**
* Scan stream for "end base class" marker. The \a classname is the
* name of the class currently being read and is only used for
* documenting exceptions.
*/
void endBase(string classname);
/**
* Return a reference to the associated stream.
*/
istream & is() { return *theIStream; }
/**
* Return a const reference to the associated stream.
*/
const istream & is() const { return *theIStream; }
/**
* Lists of objects that have been read.
*/
ObjectVector readObjects;
/**
* Lists of classes and corresponding version strings that have been read.
*/
DescriptionVector readClasses;
/**
* A pointer to the associated istream.
*/
istream * theIStream;
/**
* Pedantic or tolerant. See description of the setPedantic() and
* setTolerant() methods.
*/
bool isPedantic;
/**
* True if the associated istream should be deleted when the PersistentIStream
* is destroyed.
*/
bool allocStream;
/**
* False if no errors has occurred.
*/
bool badState;
/** Version number of the PersistentOStream which has written the
* file being read. */
int version;
/** Subversion number of the PersistentOStream which has written the
* file being read. */
int subVersion;
/**
* If keepId is true, the order in which the original
* objects were created will be kept.
*/
bool keepId;
/**
* Global libraries loaded in the initialization.
*/
vector<string> theGlobalLibraries;
/** @name Special marker characters */
//@{
/**
* The special marker character indicating the beginning of an object.
*/
static const char tBegin = '{';
/**
* The special marker character indicating the end of an object.
*/
static const char tEnd = '}';
/**
* The marker character indicating the beginning of the next base
* class in case of multiple inheritance.
*/
static const char tNext = '|';
/**
* The special marker character indicating an escaped marker character.
*/
static const char tNull = '\\';
/**
* The special marker character indicating the end of a value.
*/
static const char tSep = '\n';
/**
* The special marker character used to avoid confusion with escaped
* tSep markers.
*/
static const char tNoSep = 'n';
/**
* The special marker character indicating a true boolean value.
*/
static const char tYes = 'y';
/**
* The special marker character indicating a false boolean value.
*/
static const char tNo = 'n';
//@}
private:
/**
* Standard ctors and assignment are private and not implemented.
*/
PersistentIStream();
/**
* Standard ctors and assignment are private and not implemented.
*/
PersistentIStream(const PersistentIStream &);
/**
* Standard ctors and assignment are private and not implemented.
*/
PersistentIStream & operator=(const PersistentIStream &);
};
/**
* Operator for applying manipulators to the stream.
*/
inline PersistentIStream & operator>>(PersistentIStream & is,
PersistentIManip func) {
return (*func)(is);
}
/**
* The manipulator for setting pedantic mode.
*/
inline PersistentIStream & pedantic(PersistentIStream & is) {
return is.setPedantic();
}
/**
* The manipulator for setting tolerant mode.
*/
inline PersistentIStream & tolerant(PersistentIStream & is) {
return is.setTolerant();
}
/**
* @name Partial specializations of operator>> for input of
* std::containers.
*/
//@{
/** Input a pair of objects. */
template <typename T1, typename T2>
inline PersistentIStream & operator>>(PersistentIStream & is, pair<T1,T2> & p) {
return is >> p.first >> p.second;
}
/** Input a map of key/objects pairs. */
template <typename Key, typename T, typename Cmp, typename A>
inline PersistentIStream & operator>>(PersistentIStream & is, map<Key,T,Cmp,A> & m) {
m.clear();
long size;
Key k;
is >> size;
while ( size-- && is ) {
is >> k;
is >> m[k];
}
return is;
}
/** Input a multimap of key/objects pairs. */
template <typename Key, typename T, typename Cmp, typename A>
inline PersistentIStream & operator>>(PersistentIStream & is,
multimap<Key,T,Cmp,A> & m) {
m.clear();
long size;
Key k;
T t;
is >> size;
while ( size-- && is ) {
is >> k;
is >> t;
m.insert(make_pair(k, t));
}
return is;
}
/** Input a set of objects. */
template <typename Key, typename Cmp, typename A>
inline PersistentIStream & operator>>(PersistentIStream & is,
set<Key,Cmp,A> & s) {
is.getContainer(s);
return is;
}
/** Input a multoset of objects. */
template <typename Key, typename Cmp, typename A>
inline PersistentIStream & operator>>(PersistentIStream & is,
multiset<Key,Cmp,A> & s) {
is.getContainer(s);
return is;
}
/** Input a list of objects. */
template <typename T, typename A>
inline PersistentIStream & operator>>(PersistentIStream & is,
list<T,A> & l) {
is.getContainer(l);
return is;
}
/** Input a vector of objects. */
template <typename T, typename A>
inline PersistentIStream & operator>>(PersistentIStream & is,
vector<T,A> & v) {
is.getContainer(v);
return is;
}
/** Input a deque of objects. */
template <typename T, typename A>
inline PersistentIStream & operator>>(PersistentIStream & is,
deque<T,A> & d) {
is.getContainer(d);
return is;
}
}
#endif /* ThePEG_PersistentIStream_H */
|