SHOGUN  v1.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros 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 <stdio.h>
16 #include <shogun/base/SGObject.h>
17 #include <shogun/lib/DataType.h>
18 
19 namespace shogun
20 {
21  class CSGObject;
22 
25 {
26 public:
28  struct TSerializableReader :public CSGObject {
29 
30  /* ******************************************************** */
31  /* Begin of abstract write methods */
32 
33 #ifndef DOXYGEN_SHOULD_SKIP_THIS
34  virtual bool read_scalar_wrapped(
35  const TSGDataType* type, void* param) = 0;
36 
37  virtual bool read_cont_begin_wrapped(
38  const TSGDataType* type, index_t* len_read_y,
39  index_t* len_read_x) = 0;
40  virtual bool read_cont_end_wrapped(
41  const TSGDataType* type, index_t len_read_y,
42  index_t len_read_x) = 0;
43 
44  virtual bool read_string_begin_wrapped(
45  const TSGDataType* type, index_t* length) = 0;
46  virtual bool read_string_end_wrapped(
47  const TSGDataType* type, index_t length) = 0;
48 
49  virtual bool read_stringentry_begin_wrapped(
50  const TSGDataType* type, index_t y) = 0;
51  virtual bool read_stringentry_end_wrapped(
52  const TSGDataType* type, index_t y) = 0;
53 
54  virtual bool read_sparse_begin_wrapped(
55  const TSGDataType* type, index_t* vec_index,
56  index_t* length) = 0;
57  virtual bool read_sparse_end_wrapped(
58  const TSGDataType* type, index_t* vec_index,
59  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  virtual 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 vec_index,
144  index_t length) = 0;
145  virtual bool write_sparse_end_wrapped(
146  const TSGDataType* type, index_t vec_index,
147  index_t length) = 0;
148 
149  virtual bool write_sparseentry_begin_wrapped(
150  const TSGDataType* type, const SGSparseVectorEntry<char>* first_entry,
151  index_t feat_index, index_t y) = 0;
152  virtual bool write_sparseentry_end_wrapped(
153  const TSGDataType* type, const SGSparseVectorEntry<char>* first_entry,
154  index_t feat_index, index_t y) = 0;
155 
156  virtual bool write_item_begin_wrapped(
157  const TSGDataType* type, index_t y, index_t x) = 0;
158  virtual bool write_item_end_wrapped(
159  const TSGDataType* type, index_t y, index_t x) = 0;
160 
161  virtual bool write_sgserializable_begin_wrapped(
162  const TSGDataType* type, const char* sgserializable_name,
163  EPrimitiveType generic) = 0;
164  virtual bool write_sgserializable_end_wrapped(
165  const TSGDataType* type, const char* sgserializable_name,
166  EPrimitiveType generic) = 0;
167 
168  virtual bool write_type_begin_wrapped(
169  const TSGDataType* type, const char* name,
170  const char* prefix) = 0;
171  virtual bool write_type_end_wrapped(
172  const TSGDataType* type, const char* name,
173  const char* prefix) = 0;
174 #endif
175 
176  /* End of abstract write methods */
177  /* ************************************************************ */
178 
179 public:
181  explicit CSerializableFile();
182 
188  explicit CSerializableFile(FILE* fstream, char rw);
189 
195  explicit CSerializableFile(const char* fname, char rw='r');
196 
198  virtual ~CSerializableFile();
199 
201  virtual void close();
202 
204  virtual bool is_opened();
205 
206  /* ************************************************************ */
207  /* Begin of public wrappers */
208 
209 #ifndef DOXYGEN_SHOULD_SKIP_THIS
210  virtual bool write_scalar(
211  const TSGDataType* type, const char* name, const char* prefix,
212  const void* param);
213  virtual bool read_scalar(
214  const TSGDataType* type, const char* name, const char* prefix,
215  void* param);
216 
217  virtual bool write_cont_begin(
218  const TSGDataType* type, const char* name, const char* prefix,
219  index_t len_real_y, index_t len_real_x);
220  virtual bool read_cont_begin(
221  const TSGDataType* type, const char* name, const char* prefix,
222  index_t* len_read_y, index_t* len_read_x);
223 
224  virtual bool write_cont_end(
225  const TSGDataType* type, const char* name, const char* prefix,
226  index_t len_real_y, index_t len_real_x);
227  virtual bool read_cont_end(
228  const TSGDataType* type, const char* name, const char* prefix,
229  index_t len_read_y, index_t len_read_x);
230 
231  virtual bool write_string_begin(
232  const TSGDataType* type, const char* name, const char* prefix,
233  index_t length);
234  virtual bool read_string_begin(
235  const TSGDataType* type, const char* name, const char* prefix,
236  index_t* length);
237 
238  virtual bool write_string_end(
239  const TSGDataType* type, const char* name, const char* prefix,
240  index_t length);
241  virtual bool read_string_end(
242  const TSGDataType* type, const char* name, const char* prefix,
243  index_t length);
244 
245  virtual bool write_stringentry_begin(
246  const TSGDataType* type, const char* name, const char* prefix,
247  index_t y);
248  virtual bool read_stringentry_begin(
249  const TSGDataType* type, const char* name, const char* prefix,
250  index_t y);
251 
252  virtual bool write_stringentry_end(
253  const TSGDataType* type, const char* name, const char* prefix,
254  index_t y);
255  virtual bool read_stringentry_end(
256  const TSGDataType* type, const char* name, const char* prefix,
257  index_t y);
258 
259  virtual bool write_sparse_begin(
260  const TSGDataType* type, const char* name, const char* prefix,
261  index_t vec_index, index_t length);
262  virtual bool read_sparse_begin(
263  const TSGDataType* type, const char* name, const char* prefix,
264  index_t* vec_index, index_t* length);
265 
266  virtual bool write_sparse_end(
267  const TSGDataType* type, const char* name, const char* prefix,
268  index_t vec_index, index_t length);
269  virtual bool read_sparse_end(
270  const TSGDataType* type, const char* name, const char* prefix,
271  index_t* vec_index, index_t length);
272 
273  virtual bool write_sparseentry_begin(
274  const TSGDataType* type, const char* name, const char* prefix,
275  const SGSparseVectorEntry<char>* first_entry, index_t feat_index,
276  index_t y);
277  virtual bool read_sparseentry_begin(
278  const TSGDataType* type, const char* name, const char* prefix,
279  SGSparseVectorEntry<char>* first_entry, index_t* feat_index,
280  index_t y);
281 
282  virtual bool write_sparseentry_end(
283  const TSGDataType* type, const char* name, const char* prefix,
284  const SGSparseVectorEntry<char>* first_entry, index_t feat_index,
285  index_t y);
286  virtual bool read_sparseentry_end(
287  const TSGDataType* type, const char* name, const char* prefix,
288  SGSparseVectorEntry<char>* first_entry, index_t* feat_index,
289  index_t y);
290 
291  virtual bool write_item_begin(
292  const TSGDataType* type, const char* name, const char* prefix,
293  index_t y, index_t x);
294  virtual bool read_item_begin(
295  const TSGDataType* type, const char* name, const char* prefix,
296  index_t y, index_t x);
297 
298  virtual bool write_item_end(
299  const TSGDataType* type, const char* name, const char* prefix,
300  index_t y, index_t x);
301  virtual bool read_item_end(
302  const TSGDataType* type, const char* name, const char* prefix,
303  index_t y, index_t x);
304 
305  virtual bool write_sgserializable_begin(
306  const TSGDataType* type, const char* name, const char* prefix,
307  const char* sgserializable_name, EPrimitiveType generic);
308  virtual bool read_sgserializable_begin(
309  const TSGDataType* type, const char* name, const char* prefix,
310  char* sgserializable_name, EPrimitiveType* generic);
311 
312  virtual bool write_sgserializable_end(
313  const TSGDataType* type, const char* name, const char* prefix,
314  const char* sgserializable_name, EPrimitiveType generic);
315  virtual bool read_sgserializable_end(
316  const TSGDataType* type, const char* name, const char* prefix,
317  const char* sgserializable_name, EPrimitiveType generic);
318 
319  virtual bool write_type_begin(
320  const TSGDataType* type, const char* name, const char* prefix);
321  virtual bool read_type_begin(
322  const TSGDataType* type, const char* name, const char* prefix);
323 
324  virtual bool write_type_end(
325  const TSGDataType* type, const char* name, const char* prefix);
326  virtual bool read_type_end(
327  const TSGDataType* type, const char* name, const char* prefix);
328 #endif
329  /* End of public wrappers */
330  /* ************************************************************ */
331 };
332 }
333 #endif // __SERIALIZABLE_FILE_H__

SHOGUN Machine Learning Toolbox - Documentation