Chemical Data Processing Library C++ API - Version 1.2.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 
49  template <typename WriterImpl, typename DataType = typename WriterImpl::DataType>
50  class FileDataWriter : public Base::DataWriter<DataType>
51  {
52 
53  public:
54  FileDataWriter(const std::string& file_name,
55  std::ios_base::openmode mode = std::ios_base::in | std::ios_base::out | std::ios_base::trunc | std::ios_base::binary);
56 
57  FileDataWriter& write(const DataType& obj);
58 
59  void close();
60 
61  operator const void*() const;
62  bool operator!() const;
63 
64  private:
65  std::fstream stream;
66  std::string fileName;
67  WriterImpl writer;
68  };
69  } // namespace Util
70 } // namespace CDPL
71 
72 
73 // Implementation
74 
75 template <typename WriterImpl, typename DataType>
76 CDPL::Util::FileDataWriter<WriterImpl, DataType>::FileDataWriter(const std::string& file_name, std::ios_base::openmode mode):
77  stream(file_name.c_str(), mode), fileName(file_name), writer(stream)
78 {
79  writer.setParent(this);
80  writer.registerIOCallback(std::bind(&Base::DataIOBase::invokeIOCallbacks, this, std::placeholders::_2));
81 }
82 
83 template <typename WriterImpl, typename DataType>
86 {
87  try {
88  writer.write(obj);
89 
90  } catch (const std::exception& e) {
91  throw Base::IOError("FileDataWriter: while writing file '" + fileName + "': " + e.what());
92  }
93 
94  return *this;
95 }
96 
97 template <typename WriterImpl, typename DataType>
99 {
100  writer.close();
101  stream.close();
102 }
103 
104 template <typename WriterImpl, typename DataType>
106 {
107  return writer.operator const void*();
108 }
109 
110 template <typename WriterImpl, typename DataType>
112 {
113  return writer.operator!();
114 }
115 
116 #endif // CDPL_UTIL_FILEDATAWRITER_HPP
Definition of exception classes.
Definition of the class CDPL::Base::DataWriter.
void invokeIOCallbacks(double progress) const
Invokes all registered I/O callback functions with the argument *this.
An 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
FileDataWriter.
Definition: FileDataWriter.hpp:51
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)
Definition: FileDataWriter.hpp:76
void close()
Writes format dependent data (if required) to mark the end of output.
Definition: FileDataWriter.hpp:98
FileDataWriter & write(const DataType &obj)
Definition: FileDataWriter.hpp:85
bool operator!() const
Tells whether the writer is in a bad state.
Definition: FileDataWriter.hpp:111
The namespace of the Chemical Data Processing Library.