A toolkit for working with phylogenetic data.
v0.24.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)
72  : outstream (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
void put_plain(const T v)
Write plain data to the stream, by casting it to a char array.
Serializer(std::ostream &outstream)
void file_output_stream(std::string const &filename, std::ofstream &out_stream, std::ios_base::openmode mode=std::ios_base::out)
Helper function to obtain an output stream to a file.
void put_null(const size_t n)
Write n zero bytes (\0) to the stream.
void put_raw_string(const std::string &v)
Write raw data, provided as a string, to the stream, without writing its length.
Serializer(std::string const &filename)
Container namespace for all symbols of genesis in order to keep them separate when used as a library...
Provides some valuable additions to STD.
void put_raw(char const *data, size_t n)
Write raw data, provided as a char array of length n, to the stream.
void put_float(const T v)
Write a floating point number to the stream.
void put_string(const std::string &v)
Write a string, preceded by its length, to the stream. Use get_string() to read it.
void put_int(const T v)
Write an integer number to the stream.