SHOGUN  3.2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SerializableFile.h
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 3 of the License, or
5  * (at your option) any later version.
6  *
7  * Written (W) 1999-2009 Soeren Sonnenburg
8  * Copyright (C) 1999-2009 Fraunhofer Institute FIRST and Max-Planck-Society
9  * Copyright (C) 2010 Berlin Institute of Technology
10  */
11 
12 #ifndef __SERIALIZABLE_FILE_H__
13 #define __SERIALIZABLE_FILE_H__
14 
15 #include <shogun/lib/config.h>
16 
17 #include <stdio.h>
18 #include <shogun/base/SGObject.h>
19 #include <shogun/lib/DataType.h>
20 
21 namespace shogun
22 {
23 template <class T> struct SGSparseVectorEntry;
24 
27 {
28 public:
30  struct TSerializableReader :public CSGObject {
31 
32  /* ******************************************************** */
33  /* Begin of abstract write methods */
34 
35 #ifndef DOXYGEN_SHOULD_SKIP_THIS
36  virtual bool read_scalar_wrapped(
37  const TSGDataType* type, void* param) = 0;
38 
39  virtual bool read_cont_begin_wrapped(
40  const TSGDataType* type, index_t* len_read_y,
41  index_t* len_read_x) = 0;
42  virtual bool read_cont_end_wrapped(
43  const TSGDataType* type, index_t len_read_y,
44  index_t len_read_x) = 0;
45 
46  virtual bool read_string_begin_wrapped(
47  const TSGDataType* type, index_t* length) = 0;
48  virtual bool read_string_end_wrapped(
49  const TSGDataType* type, index_t length) = 0;
50 
51  virtual bool read_stringentry_begin_wrapped(
52  const TSGDataType* type, index_t y) = 0;
53  virtual bool read_stringentry_end_wrapped(
54  const TSGDataType* type, index_t y) = 0;
55 
56  virtual bool read_sparse_begin_wrapped(
57  const TSGDataType* type, index_t* length) = 0;
58  virtual bool read_sparse_end_wrapped(
59  const TSGDataType* type, index_t length) = 0;
60 
61  virtual bool read_sparseentry_begin_wrapped(
62  const TSGDataType* type, SGSparseVectorEntry<char>* first_entry,
63  index_t* feat_index, index_t y) = 0;
64  virtual bool read_sparseentry_end_wrapped(
65  const TSGDataType* type, SGSparseVectorEntry<char>* first_entry,
66  index_t* feat_index, index_t y) = 0;
67 
68  virtual bool read_item_begin_wrapped(
69  const TSGDataType* type, index_t y, index_t x) = 0;
70  virtual bool read_item_end_wrapped(
71  const TSGDataType* type, index_t y, index_t x) = 0;
72 
73  virtual bool read_sgserializable_begin_wrapped(
74  const TSGDataType* type, char* sgserializable_name,
75  EPrimitiveType* generic) = 0;
76  virtual bool read_sgserializable_end_wrapped(
77  const TSGDataType* type, const char* sgserializable_name,
78  EPrimitiveType generic) = 0;
79 
80  virtual bool read_type_begin_wrapped(
81  const TSGDataType* type, const char* name,
82  const char* prefix) = 0;
83  virtual bool read_type_end_wrapped(
84  const TSGDataType* type, const char* name,
85  const char* prefix) = 0;
86 
87 #endif
88  /* End of abstract write methods */
89  /* ******************************************************** */
90 
91  }; /* struct TSerializableReader */
92 /* public: */
93 private:
95  TSerializableReader* m_reader;
96 
97  bool is_task_warn(char rw, const char* name, const char* prefix);
98  bool false_warn(const char* prefix, const char* name);
99 
100 protected:
102  FILE* m_fstream;
104  char m_task;
106  char* m_filename;
107 
113  void init(FILE* fstream, char task, const char* filename);
114 
115  /* ************************************************************ */
116  /* Begin of abstract write methods */
117 
118 #ifndef DOXYGEN_SHOULD_SKIP_THIS
119  virtual TSerializableReader* new_reader(
120  char* dest_version, size_t n) = 0;
121 
122  virtual bool write_scalar_wrapped(
123  const TSGDataType* type, const void* param) = 0;
124 
125  virtual bool write_cont_begin_wrapped(
126  const TSGDataType* type, index_t len_real_y,
127  index_t len_real_x) = 0;
128  virtual bool write_cont_end_wrapped(
129  const TSGDataType* type, index_t len_real_y,
130  index_t len_real_x) = 0;
131 
132  virtual bool write_string_begin_wrapped(
133  const TSGDataType* type, index_t length) = 0;
134  virtual bool write_string_end_wrapped(
135  const TSGDataType* type, index_t length) = 0;
136 
137  virtual bool write_stringentry_begin_wrapped(
138  const TSGDataType* type, index_t y) = 0;
139  virtual bool write_stringentry_end_wrapped(
140  const TSGDataType* type, index_t y) = 0;
141 
142  virtual bool write_sparse_begin_wrapped(
143  const TSGDataType* type, index_t length) = 0;
144  virtual bool write_sparse_end_wrapped(
145  const TSGDataType* type, index_t length) = 0;
146 
147  virtual bool write_sparseentry_begin_wrapped(
148  const TSGDataType* type, const SGSparseVectorEntry<char>* first_entry,
149  index_t feat_index, index_t y) = 0;
150  virtual bool write_sparseentry_end_wrapped(
151  const TSGDataType* type, const SGSparseVectorEntry<char>* first_entry,
152  index_t feat_index, index_t y) = 0;
153 
154  virtual bool write_item_begin_wrapped(
155  const TSGDataType* type, index_t y, index_t x) = 0;
156  virtual bool write_item_end_wrapped(
157  const TSGDataType* type, index_t y, index_t x) = 0;
158 
159  virtual bool write_sgserializable_begin_wrapped(
160  const TSGDataType* type, const char* sgserializable_name,
161  EPrimitiveType generic) = 0;
162  virtual bool write_sgserializable_end_wrapped(
163  const TSGDataType* type, const char* sgserializable_name,
164  EPrimitiveType generic) = 0;
165 
166  virtual bool write_type_begin_wrapped(
167  const TSGDataType* type, const char* name,
168  const char* prefix) = 0;
169  virtual bool write_type_end_wrapped(
170  const TSGDataType* type, const char* name,
171  const char* prefix) = 0;
172 #endif
173 
174  /* End of abstract write methods */
175  /* ************************************************************ */
176 
177 public:
179  explicit CSerializableFile();
180 
186  explicit CSerializableFile(FILE* fstream, char rw);
187 
193  explicit CSerializableFile(const char* fname, char rw='r');
194 
196  virtual ~CSerializableFile();
197 
199  virtual void close();
200 
202  virtual bool is_opened();
203 
204  /* ************************************************************ */
205  /* Begin of public wrappers */
206 
207 #ifndef DOXYGEN_SHOULD_SKIP_THIS
208  virtual bool write_scalar(
209  const TSGDataType* type, const char* name, const char* prefix,
210  const void* param);
211  virtual bool read_scalar(
212  const TSGDataType* type, const char* name, const char* prefix,
213  void* param);
214 
215  virtual bool write_cont_begin(
216  const TSGDataType* type, const char* name, const char* prefix,
217  index_t len_real_y, index_t len_real_x);
218  virtual bool read_cont_begin(
219  const TSGDataType* type, const char* name, const char* prefix,
220  index_t* len_read_y, index_t* len_read_x);
221 
222  virtual bool write_cont_end(
223  const TSGDataType* type, const char* name, const char* prefix,
224  index_t len_real_y, index_t len_real_x);
225  virtual bool read_cont_end(
226  const TSGDataType* type, const char* name, const char* prefix,
227  index_t len_read_y, index_t len_read_x);
228 
229  virtual bool write_string_begin(
230  const TSGDataType* type, const char* name, const char* prefix,
231  index_t length);
232  virtual bool read_string_begin(
233  const TSGDataType* type, const char* name, const char* prefix,
234  index_t* length);
235 
236  virtual bool write_string_end(
237  const TSGDataType* type, const char* name, const char* prefix,
238  index_t length);
239  virtual bool read_string_end(
240  const TSGDataType* type, const char* name, const char* prefix,
241  index_t length);
242 
243  virtual bool write_stringentry_begin(
244  const TSGDataType* type, const char* name, const char* prefix,
245  index_t y);
246  virtual bool read_stringentry_begin(
247  const TSGDataType* type, const char* name, const char* prefix,
248  index_t y);
249 
250  virtual bool write_stringentry_end(
251  const TSGDataType* type, const char* name, const char* prefix,
252  index_t y);
253  virtual bool read_stringentry_end(
254  const TSGDataType* type, const char* name, const char* prefix,
255  index_t y);
256 
257  virtual bool write_sparse_begin(
258  const TSGDataType* type, const char* name, const char* prefix,
259  index_t length);
260  virtual bool read_sparse_begin(
261  const TSGDataType* type, const char* name, const char* prefix,
262  index_t* length);
263 
264  virtual bool write_sparse_end(
265  const TSGDataType* type, const char* name, const char* prefix,
266  index_t length);
267  virtual bool read_sparse_end(
268  const TSGDataType* type, const char* name, const char* prefix,
269  index_t length);
270 
271  virtual bool write_sparseentry_begin(
272  const TSGDataType* type, const char* name, const char* prefix,
273  const SGSparseVectorEntry<char>* first_entry, index_t feat_index,
274  index_t y);
275  virtual bool read_sparseentry_begin(
276  const TSGDataType* type, const char* name, const char* prefix,
277  SGSparseVectorEntry<char>* first_entry, index_t* feat_index,
278  index_t y);
279 
280  virtual bool write_sparseentry_end(
281  const TSGDataType* type, const char* name, const char* prefix,
282  const SGSparseVectorEntry<char>* first_entry, index_t feat_index,
283  index_t y);
284  virtual bool read_sparseentry_end(
285  const TSGDataType* type, const char* name, const char* prefix,
286  SGSparseVectorEntry<char>* first_entry, index_t* feat_index,
287  index_t y);
288 
289  virtual bool write_item_begin(
290  const TSGDataType* type, const char* name, const char* prefix,
291  index_t y, index_t x);
292  virtual bool read_item_begin(
293  const TSGDataType* type, const char* name, const char* prefix,
294  index_t y, index_t x);
295 
296  virtual bool write_item_end(
297  const TSGDataType* type, const char* name, const char* prefix,
298  index_t y, index_t x);
299  virtual bool read_item_end(
300  const TSGDataType* type, const char* name, const char* prefix,
301  index_t y, index_t x);
302 
303  virtual bool write_sgserializable_begin(
304  const TSGDataType* type, const char* name, const char* prefix,
305  const char* sgserializable_name, EPrimitiveType generic);
306  virtual bool read_sgserializable_begin(
307  const TSGDataType* type, const char* name, const char* prefix,
308  char* sgserializable_name, EPrimitiveType* generic);
309 
310  virtual bool write_sgserializable_end(
311  const TSGDataType* type, const char* name, const char* prefix,
312  const char* sgserializable_name, EPrimitiveType generic);
313  virtual bool read_sgserializable_end(
314  const TSGDataType* type, const char* name, const char* prefix,
315  const char* sgserializable_name, EPrimitiveType generic);
316 
317  virtual bool write_type_begin(
318  const TSGDataType* type, const char* name, const char* prefix);
319  virtual bool read_type_begin(
320  const TSGDataType* type, const char* name, const char* prefix);
321 
322  virtual bool write_type_end(
323  const TSGDataType* type, const char* name, const char* prefix);
324  virtual bool read_type_end(
325  const TSGDataType* type, const char* name, const char* prefix);
326 #endif
327  /* End of public wrappers */
328  /* ************************************************************ */
329 };
330 }
331 #endif // __SERIALIZABLE_FILE_H__

SHOGUN Machine Learning Toolbox - Documentation