Chemical Data Processing Library C++ API - Version 1.4.0
FileDataWriter.hpp
Go to the documentation of this file.
1 /*
2  * FileDataWriter.hpp
3  *
4  * This file is part of the Chemical Data Processing Toolkit
5  *
6  * Copyright (C) 2003 Thomas Seidel <thomas.seidel@univie.ac.at>
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library 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 GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with this library; see the file COPYING. If not, write to
20  * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  */
23 
29 #ifndef CDPL_UTIL_FILEDATAWRITER_HPP
30 #define CDPL_UTIL_FILEDATAWRITER_HPP
31 
32 #include <fstream>
33 #include <string>
34 #include <functional>
35 
36 #include "CDPL/Base/DataWriter.hpp"
37 #include "CDPL/Base/Exceptions.hpp"
38 
39 
40 namespace CDPL
41 {
42 
43  namespace Util
44  {
45 
53  template <typename WriterImpl, typename DataType = typename WriterImpl::DataType>
54  class FileDataWriter : public Base::DataWriter<DataType>
55  {
56 
57  public:
65  FileDataWriter(const std::string& file_name,
66  std::ios_base::openmode mode = std::ios_base::in | std::ios_base::out | std::ios_base::trunc | std::ios_base::binary);
67 
74  FileDataWriter& write(const DataType& obj);
75 
79  void close();
80 
85  operator const void*() const;
86 
91  bool operator!() const;
92 
93  private:
94  std::fstream stream;
95  std::string fileName;
96  WriterImpl writer;
97  };
98  } // namespace Util
99 } // namespace CDPL
100 
101 
102 // Implementation
103 
104 template <typename WriterImpl, typename DataType>
105 CDPL::Util::FileDataWriter<WriterImpl, DataType>::FileDataWriter(const std::string& file_name, std::ios_base::openmode mode):
106  stream(file_name.c_str(), mode), fileName(file_name), writer(stream)
107 {
108  if (!stream.good())
109  throw Base::IOError("FileDataWriter: could not open file");
110 
111  writer.setParent(this);
112  writer.registerIOCallback(std::bind(&Base::DataIOBase::invokeIOCallbacks, this, std::placeholders::_2));
113 }
114 
115 template <typename WriterImpl, typename DataType>
118 {
119  try {
120  writer.write(obj);
121 
122  } catch (const std::exception& e) {
123  throw Base::IOError("FileDataWriter: while writing file '" + fileName + "': " + e.what());
124  }
125 
126  return *this;
127 }
128 
129 template <typename WriterImpl, typename DataType>
131 {
132  writer.close();
133  stream.close();
134 }
135 
136 template <typename WriterImpl, typename DataType>
138 {
139  return writer.operator const void*();
140 }
141 
142 template <typename WriterImpl, typename DataType>
144 {
145  return writer.operator!();
146 }
147 
148 #endif // CDPL_UTIL_FILEDATAWRITER_HPP
Definition of exception classes.
Definition of class CDPL::Base::DataWriter.
void invokeIOCallbacks(double progress) const
Invokes all registered I/O callback functions with the argument *this.
Interface for writing data objects of a given type to an arbitrary data sink.
Definition: DataWriter.hpp:63
typename WriterImpl::DataType DataType
The type of the written data objects.
Definition: DataWriter.hpp:74
Thrown to indicate that an I/O operation has failed because of physical (e.g. broken pipe) or logical...
Definition: Base/Exceptions.hpp:250
Convenience wrapper that adapts a stream-based writer implementation WriterImpl into a file-based Bas...
Definition: FileDataWriter.hpp:55
FileDataWriter(const std::string &file_name, std::ios_base::openmode mode=std::ios_base::in|std::ios_base::out|std::ios_base::trunc|std::ios_base::binary)
Constructs a FileDataWriter instance that opens the file file_name in the given mode and forwards all...
Definition: FileDataWriter.hpp:105
void close()
Closes the wrapped writer and the underlying file stream.
Definition: FileDataWriter.hpp:130
FileDataWriter & write(const DataType &obj)
Writes obj as the next record via the wrapped writer.
Definition: FileDataWriter.hpp:117
bool operator!() const
Tells whether the writer is in a bad (non-writable) state.
Definition: FileDataWriter.hpp:143
The namespace of the Chemical Data Processing Library.