Libosmium  2.1.0
Fast and flexible C++ library for working with OpenStreetMap data
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
writer.hpp
Go to the documentation of this file.
1 #ifndef OSMIUM_IO_WRITER_HPP
2 #define OSMIUM_IO_WRITER_HPP
3 
4 /*
5 
6 This file is part of Osmium (http://osmcode.org/libosmium).
7 
8 Copyright 2013-2015 Jochen Topf <jochen@topf.org> and others (see README).
9 
10 Boost Software License - Version 1.0 - August 17th, 2003
11 
12 Permission is hereby granted, free of charge, to any person or organization
13 obtaining a copy of the software and accompanying documentation covered by
14 this license (the "Software") to use, reproduce, display, distribute,
15 execute, and transmit the Software, and to prepare derivative works of the
16 Software, and to permit third-parties to whom the Software is furnished to
17 do so, all subject to the following:
18 
19 The copyright notices in the Software and this entire statement, including
20 the above license grant, this restriction and the following disclaimer,
21 must be included in all copies of the Software, in whole or in part, and
22 all derivative works of the Software, unless such copies or derivative
23 works are solely in the form of machine-executable object code generated by
24 a source language processor.
25 
26 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
29 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
30 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
31 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
32 DEALINGS IN THE SOFTWARE.
33 
34 */
35 
36 #include <memory>
37 #include <string>
38 #include <utility>
39 
41 #include <osmium/io/detail/output_format.hpp>
42 #include <osmium/io/detail/read_write.hpp>
43 #include <osmium/io/detail/write_thread.hpp>
44 #include <osmium/io/file.hpp>
45 #include <osmium/io/header.hpp>
46 #include <osmium/io/overwrite.hpp>
47 #include <osmium/memory/buffer.hpp>
48 #include <osmium/thread/util.hpp>
49 
50 namespace osmium {
51 
52  namespace io {
53 
60  class Writer {
61 
63 
64  osmium::io::detail::data_queue_type m_output_queue;
65 
66  std::unique_ptr<osmium::io::detail::OutputFormat> m_output;
67 
68  std::unique_ptr<osmium::io::Compressor> m_compressor;
69 
70  std::future<bool> m_write_future;
71 
72  public:
73 
89  explicit Writer(const osmium::io::File& file, const osmium::io::Header& header = osmium::io::Header(), overwrite allow_overwrite = overwrite::no) :
90  m_file(file),
91  m_output_queue(20, "raw_output"), // XXX
92  m_output(osmium::io::detail::OutputFormatFactory::instance().create_output(m_file, m_output_queue)),
93  m_compressor(osmium::io::CompressionFactory::instance().create_compressor(file.compression(), osmium::io::detail::open_for_writing(m_file.filename(), allow_overwrite))),
94  m_write_future(std::async(std::launch::async, detail::WriteThread(m_output_queue, m_compressor.get()))) {
95  assert(!m_file.buffer());
96  m_output->write_header(header);
97  }
98 
99  explicit Writer(const std::string& filename, const osmium::io::Header& header = osmium::io::Header(), overwrite allow_overwrite = overwrite::no) :
100  Writer(osmium::io::File(filename), header, allow_overwrite) {
101  }
102 
103  explicit Writer(const char* filename, const osmium::io::Header& header = osmium::io::Header(), overwrite allow_overwrite = overwrite::no) :
104  Writer(osmium::io::File(filename), header, allow_overwrite) {
105  }
106 
107  Writer(const Writer&) = delete;
108  Writer& operator=(const Writer&) = delete;
109 
111  close();
112  }
113 
120  osmium::thread::check_for_exception(m_write_future);
121  if (buffer.committed() > 0) {
122  m_output->write_buffer(std::move(buffer));
123  }
124  }
125 
134  void close() {
135  m_output->close();
136  osmium::thread::wait_until_done(m_write_future);
137  }
138 
139  }; // class Writer
140 
141  } // namespace io
142 
143 } // namespace osmium
144 
145 #endif // OSMIUM_IO_WRITER_HPP
Writer(const char *filename, const osmium::io::Header &header=osmium::io::Header(), overwrite allow_overwrite=overwrite::no)
Definition: writer.hpp:103
Definition: reader_iterator.hpp:39
osmium::io::detail::data_queue_type m_output_queue
Definition: writer.hpp:64
std::unique_ptr< osmium::io::detail::OutputFormat > m_output
Definition: writer.hpp:66
~Writer()
Definition: writer.hpp:110
std::unique_ptr< osmium::io::Compressor > m_compressor
Definition: writer.hpp:68
Definition: file.hpp:73
void operator()(osmium::memory::Buffer &&buffer)
Definition: writer.hpp:119
Namespace for everything in the Osmium library.
Definition: assembler.hpp:55
Writer(const osmium::io::File &file, const osmium::io::Header &header=osmium::io::Header(), overwrite allow_overwrite=overwrite::no)
Definition: writer.hpp:89
void close()
Definition: writer.hpp:134
osmium::io::File m_file
Definition: writer.hpp:62
Definition: buffer.hpp:95
const char * buffer() const noexcept
Definition: file.hpp:159
Definition: writer.hpp:60
std::future< bool > m_write_future
Definition: writer.hpp:70
void check_for_exception(std::future< T > &future)
Definition: util.hpp:53
Definition: compression.hpp:105
Definition: header.hpp:49
void wait_until_done(std::future< T > &future)
Definition: util.hpp:64
Writer(const std::string &filename, const osmium::io::Header &header=osmium::io::Header(), overwrite allow_overwrite=overwrite::no)
Definition: writer.hpp:99
overwrite
Definition: overwrite.hpp:43
Writer & operator=(const Writer &)=delete