Chemical Data Processing Library C++ API - Version 1.1.1
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  for (std::string::size_type i = file_name.find_first_of('.', 0); i != std::string::npos; i = file_name.find_first_of('.', i)) {
104  const auto& handler = Base::DataIOManager<DataType>::getOutputHandlerByFileExtension(file_name.substr(++i));
105 
106  if (!handler)
107  continue;
108 
109  writerPtr = handler->createWriter(file_name, mode);
110  dataFormat = handler->getDataFormat();
111  break;
112  }
113 
114  if (!writerPtr)
115  throw Base::IOError("MultiFormatDataWriter: could not deduce data format of '" + file_name + "'");
116 
117  init();
118 }
119 
120 template <typename DataType>
121 CDPL::Util::MultiFormatDataWriter<DataType>::MultiFormatDataWriter(const std::string& file_name, const std::string& fmt,
122  std::ios_base::openmode mode)
123 {
125 
126  if (!handler)
127  throw Base::IOError("MultiFormatDataWriter: could not find handler for format '" + fmt + "'");
128 
129  writerPtr = handler->createWriter(file_name, mode);
130  dataFormat = handler->getDataFormat();
131 
132  init();
133 }
134 
135 template <typename DataType>
137  std::ios_base::openmode mode):
138  dataFormat(fmt)
139 {
141 
142  if (!handler)
143  throw Base::IOError("MultiFormatDataWriter: could not find handler for format '" + fmt.getName() + "'");
144 
145  writerPtr = handler->createWriter(file_name, mode);
146 
147  init();
148 }
149 
150 template <typename DataType>
152 {
154 
155  if (!handler)
156  throw Base::IOError("MultiFormatDataWriter: could not find handler for format '" + fmt + "'");
157 
158  writerPtr = handler->createWriter(ios);
159  dataFormat = handler->getDataFormat();
160 
161  init();
162 }
163 
164 template <typename DataType>
166  dataFormat(fmt)
167 {
169 
170  if (!handler)
171  throw Base::IOError("MultiFormatDataWriter: could not find handler for format '" + fmt.getName() + "'");
172 
173  writerPtr = handler->createWriter(ios);
174 
175  init();
176 }
177 
178 template <typename DataType>
180 {
181  return dataFormat;
182 }
183 
184 template <typename DataType>
187 {
188  writerPtr->write(obj);
189  return *this;
190 }
191 
192 template <typename DataType>
194 {
195  writerPtr->close();
196 }
197 
198 template <typename DataType>
200 {
201  return writerPtr->operator const void*();
202 }
203 
204 template <typename DataType>
206 {
207  return writerPtr->operator!();
208 }
209 
210 template <typename DataType>
212 {
213  writerPtr->setParent(this);
214  writerPtr->registerIOCallback(std::bind(&Base::DataIOBase::invokeIOCallbacks, this, std::placeholders::_2));
215 }
216 
217 #endif // CDPL_UTIL_MULTIFORMATDATAWRITER_HPP
CDPL::Util::MultiFormatDataWriter::operator=
MultiFormatDataWriter & operator=(const MultiFormatDataWriter &)=delete
CDPL::Base::DataFormat
Provides meta-information about a particular data storage format.
Definition: Base/DataFormat.hpp:49
CDPL::Util::MultiFormatDataWriter::getDataFormat
const Base::DataFormat & getDataFormat() const
Definition: MultiFormatDataWriter.hpp:179
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::MultiFormatDataWriter
MultiFormatDataWriter.
Definition: MultiFormatDataWriter.hpp:51
CDPL::Util::MultiFormatDataWriter::write
MultiFormatDataWriter & write(const DataType &obj)
Writes the data object obj.
Definition: MultiFormatDataWriter.hpp:186
CDPL::Base::DataIOBase::invokeIOCallbacks
void invokeIOCallbacks(double progress) const
Invokes all registered I/O callback functions with the argument *this.
CDPL::Util::MultiFormatDataWriter::operator!
bool operator!() const
Definition: MultiFormatDataWriter.hpp:205
CDPL::Util::MultiFormatDataWriter::close
void close()
Writes format dependent data (if required) to mark the end of output.
Definition: MultiFormatDataWriter.hpp:193
CDPL::Base::DataWriter< DataType >::DataType
DataType DataType
The type of the written data objects.
Definition: DataWriter.hpp:74
CDPL::Base::DataIOManager
A singleton class that serves as a global registry for Base::DataInputHandler and Base::DataOutputHan...
Definition: DataIOManager.hpp:104
CDPL::Base::DataIOManager::getOutputHandlerByFileExtension
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:632
Exceptions.hpp
Definition of exception classes.
CDPL::Base::DataWriter::SharedPointer
std::shared_ptr< DataWriter > SharedPointer
A reference-counted smart pointer [SHPTR] for dynamically allocated DataWriter instances.
Definition: DataWriter.hpp:69
CDPL::Base::DataFormat::getName
const std::string & getName() const
Returns the short-name of the data format.
CDPL::Base::ControlParameterContainer::setParent
void setParent(const ControlParameterContainer *cntnr)
Sets or removes the parent control-parameter container used to resolve requests for missing entries.
CDPL
The namespace of the Chemical Data Processing Library.
DataIOManager.hpp
Definition of the class CDPL::Base::DataIOManager.
CDPL::Util::MultiFormatDataWriter::SharedPointer
std::shared_ptr< MultiFormatDataWriter > SharedPointer
Definition: MultiFormatDataWriter.hpp:54
CDPL::Base::DataWriter
An interface for writing data objects of a given type to an arbitrary data sink.
Definition: DataWriter.hpp:63
CDPL::Util::MultiFormatDataWriter::MultiFormatDataWriter
MultiFormatDataWriter(const MultiFormatDataWriter &)=delete
CDPL::Base::DataIOManager::getOutputHandlerByFormat
static OutputHandlerPointer getOutputHandlerByFormat(const DataFormat &fmt)
Returns a pointer to a Base::DataOutputHandler implementation instance registered for the specified d...
Definition: DataIOManager.hpp:609
CDPL::Util::MultiFormatDataWriter::MultiFormatDataWriter
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