ProteoWizard
Reader.hpp
Go to the documentation of this file.
1//
2// $Id$
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 _TRADATA_READER_HPP_
24#define _TRADATA_READER_HPP_
25
27#include "TraData.hpp"
28#include <string>
29#include <stdexcept>
30
31
32namespace pwiz {
33namespace tradata {
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& filename,
45 const std::string& head) const
46 {
47 return (identify(filename,head).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 true 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 filename and/or head of the file
55 virtual std::string identify(const std::string& filename,
56 const std::string& head) const = 0;
57
58 /// fill in the TraData structure from the first (or only) sample
59 virtual void read(const std::string& filename,
60 const std::string& head,
61 TraData& result,
62 int runIndex = 0) const = 0;
63
64 /// fill in a vector of TraData structures; provides support for multi-run input files
65 virtual void read(const std::string& filename,
66 const std::string& head,
67 std::vector<TraDataPtr>& results) const = 0;
68
69 /// fill in a vector of MSData.Id values; provides support for multi-run input files
70 /*virtual void readIds(const std::string& filename,
71 const std::string& head,
72 std::vector<std::string>& dataIds) const;*/
73
74 virtual const char *getType() const = 0; // what kind of reader are you?
75
76 virtual ~Reader(){}
77};
78
79class PWIZ_API_DECL ReaderFail : public std::runtime_error // reader failure exception
80{
81 public:
82
83 ReaderFail(const std::string& error)
84 : std::runtime_error(("[ReaderFail] " + error).c_str()),
85 error_(error)
86 {}
87
88 virtual const std::string& error() const {return error_;}
89 virtual ~ReaderFail() throw() {}
90
91 private:
92 std::string error_;
93};
94
95typedef boost::shared_ptr<Reader> ReaderPtr;
96
97
98///
99/// Reader container (composite pattern).
100///
101/// The template get<reader_type>() gives access to child Readers by type, to facilitate
102/// Reader-specific configuration at runtime.
103///
105 public std::vector<ReaderPtr>
106{
107 public:
108
109 /// returns child name iff some child identifies, else empty string
110 virtual std::string identify(const std::string& filename) const;
111
112 /// returns child name iff some child identifies, else empty string
113 virtual std::string identify(const std::string& filename,
114 const std::string& head) const;
115
116 /// delegates to first child that identifies
117 virtual void read(const std::string& filename,
118 TraData& result,
119 int runIndex = 0) const;
120
121 /// delegates to first child that identifies
122 virtual void read(const std::string& filename,
123 const std::string& head,
124 TraData& result,
125 int runIndex = 0) const;
126
127 /// delegates to first child that identifies;
128 /// provides support for multi-run input files
129 virtual void read(const std::string& filename,
130 std::vector<TraDataPtr>& results) const;
131
132 /// delegates to first child that identifies;
133 /// provides support for multi-run input files
134 virtual void read(const std::string& filename,
135 const std::string& head,
136 std::vector<TraDataPtr>& results) const;
137
138 /// delegates to first child that identifies;
139 /// provides support for multi-run input files
140 /*virtual void readIds(const std::string& filename,
141 std::vector<std::string>& results) const;*/
142
143 /// delegates to first child that identifies;
144 /// provides support for multi-run input files
145 /*virtual void readIds(const std::string& filename,
146 const std::string& head,
147 std::vector<std::string>& results) const;*/
148
149 /// appends all of the rhs operand's Readers to the list
150 ReaderList& operator +=(const ReaderList& rhs);
151
152 /// appends the rhs Reader to the list
153 ReaderList& operator +=(const ReaderPtr& rhs);
154
155 /// returns a concatenated list of all the Readers from the lhs and rhs operands
156 ReaderList operator +(const ReaderList& rhs) const;
157
158 /// returns a concatenated list of all the Readers from the lhs and rhs operands
159 ReaderList operator +(const ReaderPtr& rhs) const;
160
161 /// returns pointer to Reader of the specified type
162 template <typename reader_type>
163 reader_type* get()
164 {
165 for (iterator it=begin(); it!=end(); ++it)
166 {
167 reader_type* p = dynamic_cast<reader_type*>(it->get());
168 if (p) return p;
169 }
170
171 return 0;
172 }
173
174 /// returns const pointer to Reader of the specified type
175 template <typename reader_type>
176 const reader_type* get() const
177 {
178 return const_cast<ReaderList*>(this)->get<reader_type>();
179 }
180
181 virtual const char *getType() const {return "ReaderList";} // satisfy inheritance
182
183};
184
185
186/// returns a list containing the lhs and rhs as readers
188
189
190} // namespace tradata
191} // namespace pwiz
192
193
194#endif // _TRADATA_READER_HPP_
#define PWIZ_API_DECL
Definition Export.hpp:32
interface for file readers
Definition Reader.hpp:37
Reader container (composite pattern).
Definition Reader.hpp:100
ReaderFail(const std::string &error)
Definition Reader.hpp:83
virtual const std::string & error() const
Definition Reader.hpp:88
virtual void read(const std::string &filename, const std::string &head, TraData &result, int runIndex=0) const =0
fill in the TraData structure from the first (or only) sample
virtual const char * getType() const =0
fill in a vector of MSData.Id values; provides support for multi-run input files
virtual std::string identify(const std::string &filename, const std::string &head) const =0
return file type iff Reader recognizes the file, else empty;
virtual void read(const std::string &filename, const std::string &head, std::vector< TraDataPtr > &results) const =0
fill in a vector of TraData structures; provides support for multi-run input files
bool accept(const std::string &filename, const std::string &head) const
return true iff Reader recognizes the file as one it should handle
Definition Reader.hpp:44
virtual void read(const std::string &filename, const std::string &head, TraData &result, int runIndex=0) const
delegates to first child that identifies
reader_type * get()
returns pointer to Reader of the specified type
Definition Reader.hpp:163
const reader_type * get() const
returns const pointer to Reader of the specified type
Definition Reader.hpp:176
virtual void read(const std::string &filename, std::vector< TraDataPtr > &results) const
delegates to first child that identifies; provides support for multi-run input files
virtual void read(const std::string &filename, const std::string &head, std::vector< TraDataPtr > &results) const
delegates to first child that identifies; provides support for multi-run input files
virtual const char * getType() const
Definition Reader.hpp:181
virtual std::string identify(const std::string &filename, const std::string &head) const
returns child name iff some child identifies, else empty string
virtual void read(const std::string &filename, TraData &result, int runIndex=0) const
delegates to first child that identifies
virtual std::string identify(const std::string &filename) const
returns child name iff some child identifies, else empty string
boost::shared_ptr< Reader > ReaderPtr
Definition Reader.hpp:89
PWIZ_API_DECL ReaderList operator+(const ReaderPtr &lhs, const ReaderPtr &rhs)
returns a list containing the lhs and rhs as readers
STL namespace.