ProteoWizard
Reader.hpp
Go to the documentation of this file.
1 //
2 // $Id: Reader.hpp 1623 2009-12-17 20:58:35Z chambm $
3 //
4 //
5 // Original author: Matt Chambers <matt.chambers .@. vanderbilt.edu>
6 //
7 // Copyright 2009 Vanderbilt University - Nashville, TN 37232
8 //
9 // Licensed under the Apache License, Version 2.0 (the "License");
10 // you may not use this file except in compliance with the License.
11 // You may obtain a copy of the License at
12 //
13 // http://www.apache.org/licenses/LICENSE-2.0
14 //
15 // Unless required by applicable law or agreed to in writing, software
16 // distributed under the License is distributed on an "AS IS" BASIS,
17 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 // See the License for the specific language governing permissions and
19 // limitations under the License.
20 //
21 
22 
23 #ifndef _PROTEOME_READER_HPP_
24 #define _PROTEOME_READER_HPP_
25 
27 #include "ProteomeData.hpp"
28 #include <string>
29 #include <stdexcept>
30 
31 
32 namespace pwiz {
33 namespace proteome {
34 
35 /// interface for file readers
37 {
38  public:
39 
40 
41  /// return true iff Reader recognizes the file as one it should handle
42  /// that's not to say one it CAN handle, necessarily, as in Thermo on linux,
43  /// see comment for identify() below
44  bool accept(const std::string& uri,
45  boost::shared_ptr<std::istream> uriStreamPtr) const
46  {
47  return (identify(uri, uriStreamPtr).length() != 0);
48  }
49 
50  /// return file type iff Reader recognizes the file, else empty;
51  /// note: for formats requiring a 3rd party DLL identify() should
52  /// return non-empty if it recognized the format, even though reading
53  /// may fail if the 3rd party DLL isn't actually present
54  /// Reader may filter based on URI and/or contents of the file
55  virtual std::string identify(const std::string& uri,
56  boost::shared_ptr<std::istream> uriStreamPtr) const = 0;
57 
58  /// fill in the ProteomeData structure from a new URI stream
59  virtual void read(const std::string& uri,
60  ProteomeData& result) const;
61 
62  /// fill in the ProteomeData structure from a shared URI stream
63  virtual void read(const std::string& uri,
64  boost::shared_ptr<std::istream> uriStreamPtr,
65  ProteomeData& result) const = 0;
66 
67 
68  virtual const char *getType() const = 0; // what kind of reader are you?
69 
70  virtual ~Reader(){}
71 };
72 
73 class PWIZ_API_DECL ReaderFail : public std::runtime_error // reader failure exception
74 {
75  public:
76 
77  ReaderFail(const std::string& error)
78  : std::runtime_error(("[ReaderFail] " + error).c_str()),
79  error_(error)
80  {}
81 
82  virtual const std::string& error() const {return error_;}
83  virtual ~ReaderFail() throw() {}
84 
85  private:
86  std::string error_;
87 };
88 
89 typedef boost::shared_ptr<Reader> ReaderPtr;
90 
91 
92 ///
93 /// Reader container (composite pattern).
94 ///
95 /// The template get<reader_type>() gives access to child Readers by type, to facilitate
96 /// Reader-specific configuration at runtime.
97 ///
99  public std::vector<ReaderPtr>
100 {
101  public:
102 
103  /// returns child name iff some child identifies, else empty string
104  virtual std::string identify(const std::string& uri) const;
105 
106  /// returns child name iff some child identifies, else empty string
107  virtual std::string identify(const std::string& uri,
108  boost::shared_ptr<std::istream> uriStreamPtr) const;
109 
110  /// delegates to first child that identifies
111  virtual void read(const std::string& uri,
112  ProteomeData& result) const;
113 
114  /// delegates to first child that identifies
115  virtual void read(const std::string& uri,
116  boost::shared_ptr<std::istream> uriStreamPtr,
117  ProteomeData& result) const;
118 
119 
120  /// appends all of the rhs operand's Readers to the list
121  ReaderList& operator +=(const ReaderList& rhs);
122 
123  /// appends the rhs Reader to the list
124  ReaderList& operator +=(const ReaderPtr& rhs);
125 
126  /// returns a concatenated list of all the Readers from the lhs and rhs operands
127  ReaderList operator +(const ReaderList& rhs) const;
128 
129  /// returns a concatenated list of all the Readers from the lhs and rhs operands
130  ReaderList operator +(const ReaderPtr& rhs) const;
131 
132  /// returns pointer to Reader of the specified type
133  template <typename reader_type>
134  reader_type* get()
135  {
136  for (iterator it=begin(); it!=end(); ++it)
137  {
138  reader_type* p = dynamic_cast<reader_type*>(it->get());
139  if (p) return p;
140  }
141 
142  return 0;
143  }
144 
145  /// returns const pointer to Reader of the specified type
146  template <typename reader_type>
147  const reader_type* get() const
148  {
149  return const_cast<ReaderList*>(this)->get<reader_type>();
150  }
151 
152  virtual const char *getType() const {return "ReaderList";} // satisfy inheritance
153 
154 };
155 
156 
157 /// returns a list containing the lhs and rhs as readers
158 PWIZ_API_DECL ReaderList operator +(const ReaderPtr& lhs, const ReaderPtr& rhs);
159 
160 
161 } // namespace proteome
162 } // namespace pwiz
163 
164 
165 #endif // _PROTEOME_READER_HPP_