A library for working with phylogenetic and population genetic data.
v0.27.0
utils/io/serializer.hpp
Go to the documentation of this file.
1 #ifndef GENESIS_UTILS_IO_SERIALIZER_H_
2 #define GENESIS_UTILS_IO_SERIALIZER_H_
3 
4 /*
5  Genesis - A toolkit for working with phylogenetic data.
6  Copyright (C) 2014-2019 Lucas Czech and HITS gGmbH
7 
8  This program is free software: you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation, either version 3 of the License, or
11  (at your option) any later version.
12 
13  This program 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
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with this program. If not, see <http://www.gnu.org/licenses/>.
20 
21  Contact:
22  Lucas Czech <lucas.czech@h-its.org>
23  Exelixis Lab, Heidelberg Institute for Theoretical Studies
24  Schloss-Wolfsbrunnenweg 35, D-69118 Heidelberg, Germany
25 */
26 
39 
40 #include <algorithm>
41 #include <cstring>
42 #include <fstream>
43 #include <iostream>
44 #include <stdexcept>
45 #include <string>
46 
47 namespace genesis {
48 namespace utils {
49 
50 // =================================================================================================
51 // Serializer
52 // =================================================================================================
53 
58 {
59 public:
60 
61  // -------------------------------------------------------------------------
62  // Constructor and Destructor
63  // -------------------------------------------------------------------------
64 
65  explicit Serializer( std::string const& filename )
66  : outstream (outfile)
67  {
68  utils::file_output_stream( filename, outfile, std::ofstream::out | std::ofstream::binary );
69  }
70 
71  explicit Serializer (std::ostream& outstream)
73  {}
74 
76  {
77  outfile.close();
78  }
79 
80  // -------------------------------------------------------------------------
81  // Stream Status
82  // -------------------------------------------------------------------------
83 
84  inline operator bool() const
85  {
86  return !outstream.fail();
87  }
88 
89  inline bool good() const
90  {
91  return outstream.good();
92  }
93 
94  inline bool eof() const
95  {
96  return outstream.eof();
97  }
98 
99  inline bool fail() const
100  {
101  return outstream.fail();
102  }
103 
104  inline bool bad() const
105  {
106  return outstream.bad();
107  }
108 
109  // -------------------------------------------------------------------------
110  // File Status
111  // -------------------------------------------------------------------------
112 
113  inline bool is_open() const
114  {
115  return outfile.is_open();
116  }
117 
118  inline void flush()
119  {
120  outfile.flush();
121  }
122 
123  inline void close()
124  {
125  outfile.close();
126  }
127 
128  // -------------------------------------------------------------------------
129  // Serialization
130  // -------------------------------------------------------------------------
131 
135  void put_null (const size_t n)
136  {
137  char* buffer = new char[n];
138  std::fill(buffer, buffer+n, '\0');
139  outstream.write (buffer, n);
140  delete[] buffer;
141  }
142 
146  void put_raw( char const* data, size_t n )
147  {
148  outstream.write (data, n);
149  }
150 
154  void put_raw_string (const std::string& v)
155  {
156  outstream.write (v.c_str(), v.length());
157  }
158 
162  void put_string (const std::string& v)
163  {
164  size_t len = v.length();
165  put_int(len);
166  put_raw_string(v);
167  }
168 
172  template<typename T>
173  void put_plain (const T v)
174  {
175  outstream.write( reinterpret_cast<char const*>( &v ), sizeof(v));
176  }
177 
184  template<typename T>
185  void put_int (const T v)
186  {
187  put_plain(v);
188  }
189 
196  template<typename T>
197  void put_float (const T v)
198  {
199  put_plain(v);
200  }
201 
202  // -------------------------------------------------------------------------
203  // Data Members
204  // -------------------------------------------------------------------------
205 
206 protected:
207  std::ofstream outfile;
208  std::ostream& outstream;
209 };
210 
211 } // namespace utils
212 } // namespace genesis
213 
214 #endif // include guard
input_reader.hpp
genesis::utils::Serializer::Serializer
Serializer(std::ostream &outstream)
Definition: utils/io/serializer.hpp:71
genesis::utils::Serializer::flush
void flush()
Definition: utils/io/serializer.hpp:118
output_stream.hpp
genesis::utils::Serializer::good
bool good() const
Definition: utils/io/serializer.hpp:89
genesis::utils::Serializer::close
void close()
Definition: utils/io/serializer.hpp:123
std.hpp
Provides some valuable additions to STD.
input_source.hpp
genesis::utils::Serializer::outstream
std::ostream & outstream
Definition: utils/io/serializer.hpp:208
genesis::utils::Serializer::put_plain
void put_plain(const T v)
Write plain data to the stream, by casting it to a char array.
Definition: utils/io/serializer.hpp:173
genesis::utils::Serializer::~Serializer
~Serializer()
Definition: utils/io/serializer.hpp:75
genesis::utils::Serializer::Serializer
Serializer(std::string const &filename)
Definition: utils/io/serializer.hpp:65
genesis::utils::Serializer::put_raw
void put_raw(char const *data, size_t n)
Write raw data, provided as a char array of length n, to the stream.
Definition: utils/io/serializer.hpp:146
genesis
Container namespace for all symbols of genesis in order to keep them separate when used as a library.
Definition: placement/formats/edge_color.cpp:42
genesis::utils::Serializer::put_raw_string
void put_raw_string(const std::string &v)
Write raw data, provided as a string, to the stream, without writing its length.
Definition: utils/io/serializer.hpp:154
genesis::utils::Serializer::bad
bool bad() const
Definition: utils/io/serializer.hpp:104
genesis::utils::Serializer
Definition: utils/io/serializer.hpp:57
genesis::utils::Serializer::fail
bool fail() const
Definition: utils/io/serializer.hpp:99
genesis::utils::Serializer::put_string
void put_string(const std::string &v)
Write a string, preceded by its length, to the stream. Use get_string() to read it.
Definition: utils/io/serializer.hpp:162
genesis::utils::Serializer::is_open
bool is_open() const
Definition: utils/io/serializer.hpp:113
genesis::utils::Serializer::eof
bool eof() const
Definition: utils/io/serializer.hpp:94
genesis::utils::Serializer::outfile
std::ofstream outfile
Definition: utils/io/serializer.hpp:207
genesis::utils::Serializer::put_null
void put_null(const size_t n)
Write n zero bytes (\0) to the stream.
Definition: utils/io/serializer.hpp:135
genesis::utils::Serializer::put_int
void put_int(const T v)
Write an integer number to the stream.
Definition: utils/io/serializer.hpp:185
genesis::utils::Serializer::put_float
void put_float(const T v)
Write a floating point number to the stream.
Definition: utils/io/serializer.hpp:197
input_buffer.hpp
genesis::utils::file_output_stream
void file_output_stream(std::string const &filename, std::ofstream &out_stream, std::ios_base::openmode mode=std::ios_base::out, bool create_dirs=true)
Helper function to obtain an output stream to a file.
Definition: output_stream.hpp:69