mapgd  0.4
A program for the Maximum-likelihood analysis of population genomic data.
 All Data Structures Functions Variables Friends Groups Pages
data.h
1 #ifndef _DATA_H_
2 #define _DATA_H_
3 
4 #include <vector>
5 #include <string>
6 #include <map>
7 #include <libintl.h>
8 #include <iostream>
9 
10 #include "typedef.h"
11 #include "error_codes.h"
12 
14 
34 class Data {
35 private:
39  static const bool binary;
40 protected:
41 
43  virtual void read(std::istream& str) = 0;
44 
46  virtual void write(std::ostream& str) const = 0;
47 
48 // Data
49 /* //! T can be constructed form this, but this can not be constructed from T.
50  template <class T, typename... Args>
51  void
52  injection_ (T&) const {
53  injection_{}
54  }
56  template <class T>
57  void
58  surjection_ (const T&) { }
59 
60  template<typename T, typename... Args>
61  T& convert(const Args &args ...)
62  {
63 
64  args.injection_(T);
65  }*/
66 
67 public:
69  static const std::string file_name;
71  static const std::string table_name;
72 
73  virtual std::string header(void) const = 0;
74 
75  void read_binary(std::istream& str) {};
76  void write_binary(std::ostream& str) const {};
77 
78  Data(){};
79  Data(std::vector <std::string> &){};
80 
81  virtual const std::string get_file_name() const {return this->file_name;};
82  virtual const std::string get_table_name() const {return this->table_name;};
83 
84  virtual const bool indexed() const {return false;};
85 
86  virtual const bool get_binary() const {return this->binary;};
87 
93  /* E.g. Column_name_1 integer, Column_name_2 integer, ...
95  */
96  virtual const std::string sql_header(void) const {return "";};
98  /* E.g. Column_name_1, Column_name_2, ...
99  */
100  virtual const std::string sql_column_names(void) const {return "";};
102  /* E.g. 1, 0, ...
103  */
104  virtual const std::string sql_values(void) const {return "";};
106  /* E.g. 1, 0, ...
107  */
108  virtual void sql_read(std::istream &);
109 
110  /*
111  *@}
112  */
113 
118  friend std::ostream& operator << (std::ostream&, const Data &);
121  friend std::istream& operator >> (std::istream&, Data &);
122 
124  /* If size() is undefined in the child class, then we don't want to
125  * read or write any at all, so we return 0.
126  */
127  virtual size_t size(void) const {return 0;};
128  /*
129  *@}
130  */
131 
133  static Data * new_from_str (const std::string &, const std::vector<std::string> &);
134 // static Data * new_empty (const std::string &);
135 
137 // virtual const enum tags(void) const {return "";};
138 
139 };
140 
142 /* Indexed_data can be stored in indexed files.
143  */
144 class Indexed_data : public virtual Data {
145 protected:
146  id1_t abs_pos_;
147 public:
148  using Data::write_binary;
149  using Data::read_binary;
150  void write_pos(std::ostream& str) const
151  {
152  str.write((char *)(&abs_pos_), sizeof(id1_t) );
153  }
154 
155  void read_pos(std::istream& str)
156  {
157  str.read((char *)(&abs_pos_), sizeof(id1_t) );
158  }
159 
160  Indexed_data(){};
161  Indexed_data(std::vector <std::string> &){};
162  id1_t get_abs_pos (void) const;
163 
164  void set_abs_pos (const id1_t &);
165  const bool indexed() const {return true;};
166 };
167 
168 
170 /* Indexed_data can be stored in indexed files.
171  */
172 
173 #define get_abs_pos1 get_abs_pos
174 #define set_abs_pos1 set_abs_pos
175 
176 class Double_indexed_data : public virtual Data
177 {
178 protected:
179  id1_t abs_pos1_, abs_pos2_;
180 public:
181  using Data::write_binary;
182  using Data::read_binary;
183 
184  void write_pos(std::ostream& str) const
185  {
186  str.write((char *)(&abs_pos1_), sizeof(id1_t) );
187  str.write((char *)(&abs_pos2_), sizeof(id1_t) );
188  }
189 
190  void read_pos(std::istream& str)
191  {
192  str.read((char *)(&abs_pos1_), sizeof(id1_t) );
193  str.read((char *)(&abs_pos2_), sizeof(id1_t) );
194  }
195 
197  Double_indexed_data(std::vector <std::string> &){};
198 
199  id1_t get_abs_pos (void) const;
200  id1_t get_abs_pos2 (void) const;
201 
202  void set_abs_pos (const id1_t &);
203  void set_abs_pos2 (const id1_t &);
204 
205  const bool indexed() const {return true;};
206 };
207 
209 /* This initializes m_data_ctor map which should not be visible
210  * outside of data.cc
211  */
212 static struct Registry_initalizer {
213  Registry_initalizer ();
214  ~Registry_initalizer ();
215 } Registry_initalizer;
216 
218 
223 {
224 public:
226  Registration (const std::string &str, Data*(*fn)(const std::vector <std::string> &) );
228  ~Registration (void);
229 private:
230  std::string name_;
231 };
232 
233 std::vector <std::string> registry_list(void);
234 
235 #endif
Registration(const std::string &str, Data *(*fn)(const std::vector< std::string > &))
< The constructor of Registration, which stores a constructor for a Data class to new_data_ ...
Definition: data.cc:72
virtual void write(std::ostream &str) const =0
The write function must be defined in the child class.
static Data * new_from_str(const std::string &, const std::vector< std::string > &)
Constructs an instance of the class Registered w/ string.
Definition: data.cc:37
static const bool binary
A flag to indicate that binary reading/writing is not supported by default.
Definition: data.h:39
Data which has an absolute position.
Definition: data.h:144
friend std::istream & operator>>(std::istream &, Data &)
Use the >> operator to read Data in text mode.
Definition: data.cc:60
virtual const std::string sql_values(void) const
Return the values to be placed in columns.
Definition: data.h:104
void set_abs_pos(const id1_t &)
Indexed data needs to associate each datum with a position in the genome.
Definition: data.cc:143
friend std::ostream & operator<<(std::ostream &, const Data &)
Use the << operator to write Data in text mode.
Definition: data.cc:66
Definition: data.h:176
virtual const std::string sql_header(void) const
Return the names of the columns, along with variable type.
Definition: data.h:96
void set_abs_pos(const id1_t &)
Indexed data needs to associate each datum with a position in the genome.
Definition: data.cc:138
id1_t get_abs_pos(void) const
Indexed data needs to associate each datum with a position in the genome.
Definition: data.cc:128
static const std::string file_name
The read function must be defined in the child class.
Definition: data.h:69
virtual void sql_read(std::istream &)
Reads the values...
Definition: data.cc:53
virtual void read(std::istream &str)=0
The read function must be defined in the child class.
void set_abs_pos2(const id1_t &)
Indexed data needs to associate each datum with a position in the genome.
Definition: data.cc:148
id1_t get_abs_pos(void) const
Indexed data needs to associate each datum with a position in the genome.
Definition: data.cc:123
A class which registers a child of Data in Data::new_data_.
Definition: data.h:222
A class which can be written as flat text file or into an SQL database.
Definition: data.h:34
virtual const std::string sql_column_names(void) const
Return the names of the columns.
Definition: data.h:100
id1_t get_abs_pos2(void) const
Indexed data needs to associate each datum with a position in the genome.
Definition: data.cc:133
static const std::string table_name
The read function must be defined in the child class.
Definition: data.h:71
virtual size_t size(void) const
The size of the class in bytes.
Definition: data.h:127