Chemical Data Processing Library C++ API - Version 1.1.1
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 
63  bool operator!() const;
64 
65  private:
66  std::fstream stream;
67  std::string fileName;
68  WriterImpl writer;
69  };
70  } // namespace Util
71 } // namespace CDPL
72 
73 
74 // Implementation
75 
76 template <typename WriterImpl, typename DataType>
77 CDPL::Util::FileDataWriter<WriterImpl, DataType>::FileDataWriter(const std::string& file_name, std::ios_base::openmode mode):
78  stream(file_name.c_str(), mode), fileName(file_name), writer(stream)
79 {
80  writer.setParent(this);
81  writer.registerIOCallback(std::bind(&Base::DataIOBase::invokeIOCallbacks, this, std::placeholders::_2));
82 }
83 
84 template <typename WriterImpl, typename DataType>
87 {
88  try {
89  writer.write(obj);
90 
91  } catch (const std::exception& e) {
92  throw Base::IOError("FileDataWriter: while writing file '" + fileName + "': " + e.what());
93  }
94 
95  return *this;
96 }
97 
98 template <typename WriterImpl, typename DataType>
100 {
101  writer.close();
102  stream.close();
103 }
104 
105 template <typename WriterImpl, typename DataType>
107 {
108  return writer.operator const void*();
109 }
110 
111 template <typename WriterImpl, typename DataType>
113 {
114  return writer.operator!();
115 }
116 
117 #endif // CDPL_UTIL_FILEDATAWRITER_HPP
CDPL::Util::FileDataWriter::close
void close()
Writes format dependent data (if required) to mark the end of output.
Definition: FileDataWriter.hpp:99
DataWriter.hpp
Definition of the class CDPL::Base::DataWriter.
CDPL::Base::IOError
Thrown to indicate that an I/O operation has failed because of physical (e.g. broken pipe) or logical...
Definition: Base/Exceptions.hpp:250
CDPL::Util::FileDataWriter
FileDataWriter.
Definition: FileDataWriter.hpp:51
CDPL::Base::DataIOBase::invokeIOCallbacks
void invokeIOCallbacks(double progress) const
Invokes all registered I/O callback functions with the argument *this.
CDPL::Util::FileDataWriter::FileDataWriter
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:77
CDPL::Util::FileDataWriter::operator!
bool operator!() const
Definition: FileDataWriter.hpp:112
CDPL::Base::DataWriter< typename WriterImpl::DataType >::DataType
typename WriterImpl::DataType DataType
The type of the written data objects.
Definition: DataWriter.hpp:74
Exceptions.hpp
Definition of exception classes.
CDPL
The namespace of the Chemical Data Processing Library.
CDPL::Base::DataWriter
An interface for writing data objects of a given type to an arbitrary data sink.
Definition: DataWriter.hpp:63
CDPL::Util::FileDataWriter::write
FileDataWriter & write(const DataType &obj)
Definition: FileDataWriter.hpp:86