LOOS  v2.3.2
StreamWrapper.hpp
1 /*
2  This file is part of LOOS.
3 
4  LOOS (Lightweight Object-Oriented Structure library)
5  Copyright (c) 2008, Tod D. Romo, Alan Grossfield
6  Department of Biochemistry and Biophysics
7  School of Medicine & Dentistry, University of Rochester
8 
9  This package (LOOS) is free software: you can redistribute it and/or modify
10  it under the terms of the GNU General Public License as published by
11  the Free Software Foundation under version 3 of the License.
12 
13  This package is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21 
22 
23 
24 
25 #if !defined(LOOS_STREAMWRAPPER_HPP)
26 #define LOOS_STREAMWRAPPER_HPP
27 
28 #include <istream>
29 #include <fstream>
30 #include <string>
31 #include <stdexcept>
32 
33 #include <boost/utility.hpp>
34 #include <exceptions.hpp>
35 
36 
37 namespace loos {
38 
40 
53  class StreamWrapper : public boost::noncopyable {
54  public:
55  StreamWrapper() : new_stream(false), stream(0) { }
56 
58  explicit StreamWrapper(std::istream& fs) : new_stream(false), stream(&fs) { }
59 
61  StreamWrapper(const std::string& s,
62  const std::ios_base::openmode mode = std::ios_base::in | std::ios_base::binary)
63  throw(FileOpenError)
64  : new_stream(true)
65  {
66  stream = new std::fstream(s.c_str(), mode);
67  if (!stream->good())
68  throw(FileOpenError(s));
69  }
70 
72  void setStream(const std::string& s,
73  const std::ios_base::openmode mode = std::ios_base::in | std::ios_base::binary)
74  throw(FileOpenError)
75  {
76  if (new_stream)
77  delete stream;
78 
79  new_stream = true;
80  stream = new std::fstream(s.c_str(), mode);
81  if (!stream->good())
82  throw(FileOpenError(s));
83  }
84 
86  void setStream(std::istream& fs) {
87  if (new_stream)
88  delete stream;
89  new_stream = false;
90  stream = &fs;
91  }
92 
94  std::istream* operator()(void) throw(LOOSError) {
95  if (stream == 0)
96  throw(LOOSError("Attempting to access an unset stream"));
97  return(stream);
98  }
99 
101  bool isUnset(void) const { return(stream == 0); }
102 
103  ~StreamWrapper() { if (new_stream) delete stream; }
104 
105 
106  private:
107  bool new_stream;
108  std::istream* stream;
109  };
110 
111 
112 }
113 
114 #endif
StreamWrapper(const std::string &s, const std::ios_base::openmode mode=std::ios_base::in|std::ios_base::binary)
Opens a new stream with file named 's'.
Error while opening a file.
Definition: exceptions.hpp:170
Simple wrapper class for caching stream pointers.
StreamWrapper(std::istream &fs)
Sets the internal stream pointer to fs.
void setStream(std::istream &fs)
Sets the internal stream to the passed fstream.
Generic LOOS exception.
Definition: exceptions.hpp:40
bool isUnset(void) const
Returns true if the internal stream pointer is unset.
Namespace for most things not already encapsulated within a class.
void setStream(const std::string &s, const std::ios_base::openmode mode=std::ios_base::in|std::ios_base::binary)
Sets the internal stream to point to a newly opened filed...
std::istream * operator()(void)
Returns the internal istream pointer.