Chemical Data Processing Library C++ API - Version 1.2.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 
49  template <typename DataType>
50  class MultiFormatDataWriter : public Base::DataWriter<DataType>
51  {
52 
53  public:
54  typedef std::shared_ptr<MultiFormatDataWriter> SharedPointer;
55 
56  MultiFormatDataWriter(const std::string& file_name,
57  std::ios_base::openmode mode = std::ios_base::in | std::ios_base::out |
58  std::ios_base::trunc | std::ios_base::binary);
59 
60  MultiFormatDataWriter(const std::string& file_name, const std::string& fmt,
61  std::ios_base::openmode mode = std::ios_base::in | std::ios_base::out |
62  std::ios_base::trunc | std::ios_base::binary);
63 
64  MultiFormatDataWriter(const std::string& file_name, const Base::DataFormat& fmt,
65  std::ios_base::openmode mode = std::ios_base::in | std::ios_base::out |
66  std::ios_base::trunc | std::ios_base::binary);
67 
68  MultiFormatDataWriter(std::iostream& ios, const std::string& fmt);
69 
70  MultiFormatDataWriter(std::iostream& ios, const Base::DataFormat& fmt);
71 
73 
75 
76  const Base::DataFormat& getDataFormat() const;
77 
79 
80  void close();
81 
82  operator const void*() const;
83 
84  bool operator!() const;
85 
86  private:
87  void init();
88 
89  typedef typename Base::DataWriter<DataType>::SharedPointer WriterPtr;
90 
91  WriterPtr writerPtr;
92  Base::DataFormat dataFormat;
93  };
94  } // namespace Util
95 } // namespace CDPL
96 
97 
98 // Implementation
99 
100 template <typename DataType>
101 CDPL::Util::MultiFormatDataWriter<DataType>::MultiFormatDataWriter(const std::string& file_name, std::ios_base::openmode mode)
102 {
103 
104  const auto& handler = Base::DataIOManager<DataType>::getOutputHandlerByFileName(file_name);
105 
106  if (!handler)
107  throw Base::IOError("MultiFormatDataWriter: could not deduce data format of '" + file_name + "'");
108 
109  writerPtr = handler->createWriter(file_name, mode);
110  dataFormat = handler->getDataFormat();
111 
112  init();
113 }
114 
115 template <typename DataType>
116 CDPL::Util::MultiFormatDataWriter<DataType>::MultiFormatDataWriter(const std::string& file_name, const std::string& fmt,
117  std::ios_base::openmode mode)
118 {
120 
121  if (!handler)
122  throw Base::IOError("MultiFormatDataWriter: could not find handler for format '" + fmt + "'");
123 
124  writerPtr = handler->createWriter(file_name, mode);
125  dataFormat = handler->getDataFormat();
126 
127  init();
128 }
129 
130 template <typename DataType>
132  std::ios_base::openmode mode):
133  dataFormat(fmt)
134 {
136 
137  if (!handler)
138  throw Base::IOError("MultiFormatDataWriter: could not find handler for format '" + fmt.getName() + "'");
139 
140  writerPtr = handler->createWriter(file_name, mode);
141 
142  init();
143 }
144 
145 template <typename DataType>
147 {
149 
150  if (!handler)
151  throw Base::IOError("MultiFormatDataWriter: could not find handler for format '" + fmt + "'");
152 
153  writerPtr = handler->createWriter(ios);
154  dataFormat = handler->getDataFormat();
155 
156  init();
157 }
158 
159 template <typename DataType>
161  dataFormat(fmt)
162 {
164 
165  if (!handler)
166  throw Base::IOError("MultiFormatDataWriter: could not find handler for format '" + fmt.getName() + "'");
167 
168  writerPtr = handler->createWriter(ios);
169 
170  init();
171 }
172 
173 template <typename DataType>
175 {
176  return dataFormat;
177 }
178 
179 template <typename DataType>
182 {
183  writerPtr->write(obj);
184  return *this;
185 }
186 
187 template <typename DataType>
189 {
190  writerPtr->close();
191 }
192 
193 template <typename DataType>
195 {
196  return writerPtr->operator const void*();
197 }
198 
199 template <typename DataType>
201 {
202  return writerPtr->operator!();
203 }
204 
205 template <typename DataType>
207 {
208  writerPtr->setParent(this);
209  writerPtr->registerIOCallback(std::bind(&Base::DataIOBase::invokeIOCallbacks, this, std::placeholders::_2));
210 }
211 
212 #endif // CDPL_UTIL_MULTIFORMATDATAWRITER_HPP
Definition of exception classes.
Definition of the 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.
A singleton class that serves as a global registry for Base::DataInputHandler and Base::DataOutputHan...
Definition: DataIOManager.hpp:104
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:670
static OutputHandlerPointer getOutputHandlerByFormat(const DataFormat &fmt)
Returns a pointer to a Base::DataOutputHandler implementation instance registered for the specified d...
Definition: DataIOManager.hpp:647
An 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
MultiFormatDataWriter.
Definition: MultiFormatDataWriter.hpp:51
bool operator!() const
Tells whether the writer is in a bad state.
Definition: MultiFormatDataWriter.hpp:200
MultiFormatDataWriter & operator=(const MultiFormatDataWriter &)=delete
MultiFormatDataWriter(const MultiFormatDataWriter &)=delete
MultiFormatDataWriter & write(const DataType &obj)
Writes the data object obj.
Definition: MultiFormatDataWriter.hpp:181
std::shared_ptr< MultiFormatDataWriter > SharedPointer
Definition: MultiFormatDataWriter.hpp:54
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)
Definition: MultiFormatDataWriter.hpp:101
const Base::DataFormat & getDataFormat() const
Definition: MultiFormatDataWriter.hpp:174
void close()
Writes format dependent data (if required) to mark the end of output.
Definition: MultiFormatDataWriter.hpp:188
The namespace of the Chemical Data Processing Library.