IMP logo
IMP Reference Guide  2.12.0
The Integrative Modeling Platform
Avro2PBReader.h
Go to the documentation of this file.
1 /**
2  * \file Avro2PBReader.h
3  * \brief an iterator that reads npctransport protobuf output files one by one
4  * from a list of avro files (used mainly as a wrapper for python usage)
5  *
6  * Copyright 2007-2019 IMP Inventors. All rights reserved.
7  */
8 
9 #ifndef IMPNPCTRANSPORT_AVRO2PB_READER_H
10 #define IMPNPCTRANSPORT_AVRO2PB_READER_H
11 
12 #include <IMP/npctransport/avro.h>
13 #include <IMP/npctransport/AvroDataFileData.h>
14 #include <IMP/value_macros.h>
15 #include <IMP/types.h>
16 #include <IMP/showable_macros.h>
17 
18 #ifndef SWIG
19 IMP_GCC_PUSH_POP(diagnostic push)
20 IMP_GCC_PRAGMA(diagnostic ignored "-Wextra")
21 #include <DataFile.hh>
22 IMP_GCC_PUSH_POP(diagnostic pop)
23 #endif
24 
25 #include <fstream>
26 #include <iomanip>
27 
28 IMPNPCTRANSPORT_BEGIN_NAMESPACE
29 
30 class IMPNPCTRANSPORTEXPORT Avro2PBReader {
31  private:
32  typedef IMP_NPCTRANSPORT_AVRO_NAMESPACE::DataFileReader<IMP_npctransport::wrapper> t_avro_reader;
33 
34  public:
35  /** Initiates a reader that goes over all output
36  entries in all files specified in avro_filenames
37  */
38  Avro2PBReader(const Strings& avro_filenames);
39 
40  /** Initiates a reader that goes over all output
41  entries in all files specified in avro_filenames
42  */
43  Avro2PBReader(std::string avro_filename);
44 
45  /** closes any open files */
46  ~Avro2PBReader();
47 
48 #if defined(SWIG) || defined(IMP_SWIG_WRAPPER)
49  typedef std::string ByteBuffer;
50  ByteBuffer read_next();
51 #else
52  /**
53  Read the next output entry into output and returns it
54  as string. If no input is left, returns "" and invalidates
55  this object.
56  */
57  std::string read_next();
58 #endif
59 
60  //! returns true if there are still files to go over
61  //! (though possibly no entries left in neither of them)
62  bool get_is_valid();
63 
64  //! returns the name of the file that is currently being read
65  //! or "" if reader is at invalid state
66  std::string get_cur_file_name() {
67  if (!get_is_valid()) return "";
68  return avro_filenames_[cur_file_];
69  }
70 
71  private:
72  //! close any open file if one exists and move cursor to next file index
73  void advance_current_reader();
74 
75  // called from ctr, this pain is needed since constructor delegation
76  // is only supported from g++ 4.7, so we use init() for backward compatibility
77  void init(const Strings& avro_filenames);
78 
79  private:
80  Strings avro_filenames_; // list of files to go over
81  t_avro_reader* avro_reader_;
82  unsigned int cur_file_; // file index we're reading now
83 
84  public:
85  IMP_SHOWABLE_INLINE(Avro2PBReader,
86  out << "Avro2PBReader with " << avro_filenames_.size()
87  << " input avro files");
88 };
89 
90 IMP_VALUES(Avro2PBReader, Avro2PBReaders);
91 
92 IMPNPCTRANSPORT_END_NAMESPACE
93 
94 #endif /* IMPNPCTRANSPORT_AVRO2PB_READER_H */
Basic types used by IMP.
#define IMP_SHOWABLE_INLINE(Name, how_to_show)
Declare the methods needed by an object that can be printed.
IMP::Vector< String > Strings
Standard way to pass a bunch of String values.
Definition: types.h:51
A more IMP-like version of the std::vector.
Definition: Vector.h:39
#define IMP_VALUES(Name, PluralName)
Define the type for storing sets of values.
Definition: value_macros.h:23
Various general useful macros for IMP.
Various general useful macros for IMP.