Chemical Data Processing Library C++ API - Version 1.4.0
MultiFormatDataWriter.hpp
Go to the documentation of this file.
1 /*
2  * MultiFormatDataWriter.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_MULTIFORMATDATAWRITER_HPP
30 #define CDPL_UTIL_MULTIFORMATDATAWRITER_HPP
31 
32 #include <string>
33 #include <functional>
34 #include <memory>
35 
37 #include "CDPL/Base/Exceptions.hpp"
38 
39 
40 namespace CDPL
41 {
42 
43  namespace Util
44  {
45 
52  template <typename DataType>
53  class MultiFormatDataWriter : public Base::DataWriter<DataType>
54  {
55 
56  public:
60  typedef std::shared_ptr<MultiFormatDataWriter> SharedPointer;
61 
68  MultiFormatDataWriter(const std::string& file_name,
69  std::ios_base::openmode mode = std::ios_base::in | std::ios_base::out |
70  std::ios_base::trunc | std::ios_base::binary);
71 
79  MultiFormatDataWriter(const std::string& file_name, const std::string& fmt,
80  std::ios_base::openmode mode = std::ios_base::in | std::ios_base::out |
81  std::ios_base::trunc | std::ios_base::binary);
82 
90  MultiFormatDataWriter(const std::string& file_name, const Base::DataFormat& fmt,
91  std::ios_base::openmode mode = std::ios_base::in | std::ios_base::out |
92  std::ios_base::trunc | std::ios_base::binary);
93 
100  MultiFormatDataWriter(std::iostream& ios, const std::string& fmt);
101 
108  MultiFormatDataWriter(std::iostream& ios, const Base::DataFormat& fmt);
109 
111 
113 
118  const Base::DataFormat& getDataFormat() const;
119 
125  MultiFormatDataWriter& write(const DataType& obj);
126 
130  void close();
131 
136  operator const void*() const;
137 
142  bool operator!() const;
143 
144  private:
145  void init();
146 
147  typedef typename Base::DataWriter<DataType>::SharedPointer WriterPtr;
148 
149  WriterPtr writerPtr;
150  Base::DataFormat dataFormat;
151  };
152  } // namespace Util
153 } // namespace CDPL
154 
155 
156 // Implementation
157 
158 template <typename DataType>
159 CDPL::Util::MultiFormatDataWriter<DataType>::MultiFormatDataWriter(const std::string& file_name, std::ios_base::openmode mode)
160 {
161 
162  const auto& handler = Base::DataIOManager<DataType>::getOutputHandlerByFileName(file_name);
163 
164  if (!handler)
165  throw Base::IOError("MultiFormatDataWriter: could not deduce data format of '" + file_name + "'");
166 
167  writerPtr = handler->createWriter(file_name, mode);
168  dataFormat = handler->getDataFormat();
169 
170  init();
171 }
172 
173 template <typename DataType>
174 CDPL::Util::MultiFormatDataWriter<DataType>::MultiFormatDataWriter(const std::string& file_name, const std::string& fmt,
175  std::ios_base::openmode mode)
176 {
178 
179  if (!handler)
180  throw Base::IOError("MultiFormatDataWriter: could not find handler for format '" + fmt + "'");
181 
182  writerPtr = handler->createWriter(file_name, mode);
183  dataFormat = handler->getDataFormat();
184 
185  init();
186 }
187 
188 template <typename DataType>
190  std::ios_base::openmode mode):
191  dataFormat(fmt)
192 {
194 
195  if (!handler)
196  throw Base::IOError("MultiFormatDataWriter: could not find handler for format '" + fmt.getName() + "'");
197 
198  writerPtr = handler->createWriter(file_name, mode);
199 
200  init();
201 }
202 
203 template <typename DataType>
205 {
207 
208  if (!handler)
209  throw Base::IOError("MultiFormatDataWriter: could not find handler for format '" + fmt + "'");
210 
211  writerPtr = handler->createWriter(ios);
212  dataFormat = handler->getDataFormat();
213 
214  init();
215 }
216 
217 template <typename DataType>
219  dataFormat(fmt)
220 {
222 
223  if (!handler)
224  throw Base::IOError("MultiFormatDataWriter: could not find handler for format '" + fmt.getName() + "'");
225 
226  writerPtr = handler->createWriter(ios);
227 
228  init();
229 }
230 
231 template <typename DataType>
233 {
234  return dataFormat;
235 }
236 
237 template <typename DataType>
240 {
241  writerPtr->write(obj);
242  return *this;
243 }
244 
245 template <typename DataType>
247 {
248  writerPtr->close();
249 }
250 
251 template <typename DataType>
253 {
254  return writerPtr->operator const void*();
255 }
256 
257 template <typename DataType>
259 {
260  return writerPtr->operator!();
261 }
262 
263 template <typename DataType>
265 {
266  writerPtr->setParent(this);
267  writerPtr->registerIOCallback(std::bind(&Base::DataIOBase::invokeIOCallbacks, this, std::placeholders::_2));
268 }
269 
270 #endif // CDPL_UTIL_MULTIFORMATDATAWRITER_HPP
Definition of exception classes.
Definition of class CDPL::Base::DataIOManager.
void setParent(const ControlParameterContainer *cntnr)
Sets or removes the parent control-parameter container used to resolve requests for missing entries.
Provides meta-information about a particular data storage format.
Definition: Base/DataFormat.hpp:49
const std::string & getName() const
Returns the short-name of the data format.
void invokeIOCallbacks(double progress) const
Invokes all registered I/O callback functions with the argument *this.
Singleton class that serves as a global registry for Base::DataInputHandler and Base::DataOutputHandl...
Definition: DataIOManager.hpp:110
static OutputHandlerPointer getOutputHandlerByFileExtension(const std::string &file_ext)
Returns a pointer to a Base::DataOutputHandler implementation instance registered for the data format...
Definition: DataIOManager.hpp:692
static OutputHandlerPointer getOutputHandlerByFormat(const DataFormat &fmt)
Returns a pointer to a Base::DataOutputHandler implementation instance registered for the specified d...
Definition: DataIOManager.hpp:669
Interface for writing data objects of a given type to an arbitrary data sink.
Definition: DataWriter.hpp:63
DataType DataType
The type of the written data objects.
Definition: DataWriter.hpp:74
std::shared_ptr< DataWriter > SharedPointer
A reference-counted smart pointer [SHPTR] for dynamically allocated DataWriter instances.
Definition: DataWriter.hpp:69
Thrown to indicate that an I/O operation has failed because of physical (e.g. broken pipe) or logical...
Definition: Base/Exceptions.hpp:250
Base::DataWriter front-end that automatically selects a concrete handler from Base::DataIOManager bas...
Definition: MultiFormatDataWriter.hpp:54
bool operator!() const
Tells whether the writer is in a bad (non-writable) state.
Definition: MultiFormatDataWriter.hpp:258
MultiFormatDataWriter & operator=(const MultiFormatDataWriter &)=delete
MultiFormatDataWriter(const MultiFormatDataWriter &)=delete
MultiFormatDataWriter & write(const DataType &obj)
Writes obj via the wrapped writer.
Definition: MultiFormatDataWriter.hpp:239
std::shared_ptr< MultiFormatDataWriter > SharedPointer
A reference-counted smart pointer [SHPTR] for dynamically allocated MultiFormatDataWriter instances.
Definition: MultiFormatDataWriter.hpp:60
MultiFormatDataWriter(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 MultiFormatDataWriter that opens file_name and deduces the output format from its name.
Definition: MultiFormatDataWriter.hpp:159
const Base::DataFormat & getDataFormat() const
Returns the data format actually used by the wrapped output handler.
Definition: MultiFormatDataWriter.hpp:232
void close()
Closes the wrapped writer (and the underlying file stream if owned).
Definition: MultiFormatDataWriter.hpp:246
The namespace of the Chemical Data Processing Library.