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:
58  typedef std::shared_ptr<MultiFormatDataWriter> SharedPointer;
59 
66  MultiFormatDataWriter(const std::string& file_name,
67  std::ios_base::openmode mode = std::ios_base::in | std::ios_base::out |
68  std::ios_base::trunc | std::ios_base::binary);
69 
77  MultiFormatDataWriter(const std::string& file_name, const std::string& fmt,
78  std::ios_base::openmode mode = std::ios_base::in | std::ios_base::out |
79  std::ios_base::trunc | std::ios_base::binary);
80 
88  MultiFormatDataWriter(const std::string& file_name, const Base::DataFormat& fmt,
89  std::ios_base::openmode mode = std::ios_base::in | std::ios_base::out |
90  std::ios_base::trunc | std::ios_base::binary);
91 
98  MultiFormatDataWriter(std::iostream& ios, const std::string& fmt);
99 
106  MultiFormatDataWriter(std::iostream& ios, const Base::DataFormat& fmt);
107 
109 
111 
116  const Base::DataFormat& getDataFormat() const;
117 
123  MultiFormatDataWriter& write(const DataType& obj);
124 
128  void close();
129 
134  operator const void*() const;
135 
140  bool operator!() const;
141 
142  private:
143  void init();
144 
145  typedef typename Base::DataWriter<DataType>::SharedPointer WriterPtr;
146 
147  WriterPtr writerPtr;
148  Base::DataFormat dataFormat;
149  };
150  } // namespace Util
151 } // namespace CDPL
152 
153 
154 // Implementation
155 
156 template <typename DataType>
157 CDPL::Util::MultiFormatDataWriter<DataType>::MultiFormatDataWriter(const std::string& file_name, std::ios_base::openmode mode)
158 {
159 
160  const auto& handler = Base::DataIOManager<DataType>::getOutputHandlerByFileName(file_name);
161 
162  if (!handler)
163  throw Base::IOError("MultiFormatDataWriter: could not deduce data format of '" + file_name + "'");
164 
165  writerPtr = handler->createWriter(file_name, mode);
166  dataFormat = handler->getDataFormat();
167 
168  init();
169 }
170 
171 template <typename DataType>
172 CDPL::Util::MultiFormatDataWriter<DataType>::MultiFormatDataWriter(const std::string& file_name, const std::string& fmt,
173  std::ios_base::openmode mode)
174 {
176 
177  if (!handler)
178  throw Base::IOError("MultiFormatDataWriter: could not find handler for format '" + fmt + "'");
179 
180  writerPtr = handler->createWriter(file_name, mode);
181  dataFormat = handler->getDataFormat();
182 
183  init();
184 }
185 
186 template <typename DataType>
188  std::ios_base::openmode mode):
189  dataFormat(fmt)
190 {
192 
193  if (!handler)
194  throw Base::IOError("MultiFormatDataWriter: could not find handler for format '" + fmt.getName() + "'");
195 
196  writerPtr = handler->createWriter(file_name, mode);
197 
198  init();
199 }
200 
201 template <typename DataType>
203 {
205 
206  if (!handler)
207  throw Base::IOError("MultiFormatDataWriter: could not find handler for format '" + fmt + "'");
208 
209  writerPtr = handler->createWriter(ios);
210  dataFormat = handler->getDataFormat();
211 
212  init();
213 }
214 
215 template <typename DataType>
217  dataFormat(fmt)
218 {
220 
221  if (!handler)
222  throw Base::IOError("MultiFormatDataWriter: could not find handler for format '" + fmt.getName() + "'");
223 
224  writerPtr = handler->createWriter(ios);
225 
226  init();
227 }
228 
229 template <typename DataType>
231 {
232  return dataFormat;
233 }
234 
235 template <typename DataType>
238 {
239  writerPtr->write(obj);
240  return *this;
241 }
242 
243 template <typename DataType>
245 {
246  writerPtr->close();
247 }
248 
249 template <typename DataType>
251 {
252  return writerPtr->operator const void*();
253 }
254 
255 template <typename DataType>
257 {
258  return writerPtr->operator!();
259 }
260 
261 template <typename DataType>
263 {
264  writerPtr->setParent(this);
265  writerPtr->registerIOCallback(std::bind(&Base::DataIOBase::invokeIOCallbacks, this, std::placeholders::_2));
266 }
267 
268 #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:678
static OutputHandlerPointer getOutputHandlerByFormat(const DataFormat &fmt)
Returns a pointer to a Base::DataOutputHandler implementation instance registered for the specified d...
Definition: DataIOManager.hpp:655
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:256
MultiFormatDataWriter & operator=(const MultiFormatDataWriter &)=delete
MultiFormatDataWriter(const MultiFormatDataWriter &)=delete
MultiFormatDataWriter & write(const DataType &obj)
Writes obj via the wrapped writer.
Definition: MultiFormatDataWriter.hpp:237
std::shared_ptr< MultiFormatDataWriter > SharedPointer
A reference-counted smart pointer [SHPTR] for dynamically allocated MultiFormatDataWriter instances.
Definition: MultiFormatDataWriter.hpp:58
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:157
const Base::DataFormat & getDataFormat() const
Returns the data format actually used by the wrapped output handler.
Definition: MultiFormatDataWriter.hpp:230
void close()
Closes the wrapped writer (and the underlying file stream if owned).
Definition: MultiFormatDataWriter.hpp:244
The namespace of the Chemical Data Processing Library.