SHOGUN  4.1.0
 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 <shogun/base/SGObject.h>
18 #include <shogun/lib/DataType.h>
19 
20 namespace shogun
21 {
22 template <class T> struct SGSparseVectorEntry;
23 
26 {
27 public:
29  struct TSerializableReader :public CSGObject {
30 
31  /* ******************************************************** */
32  /* Begin of abstract write methods */
33 
34 #ifndef DOXYGEN_SHOULD_SKIP_THIS
35  virtual bool read_scalar_wrapped(
36  const TSGDataType* type, void* param) = 0;
37 
38  virtual bool read_cont_begin_wrapped(
39  const TSGDataType* type, index_t* len_read_y,
40  index_t* len_read_x) = 0;
41  virtual bool read_cont_end_wrapped(
42  const TSGDataType* type, index_t len_read_y,
43  index_t len_read_x) = 0;
44 
45  virtual bool read_string_begin_wrapped(
46  const TSGDataType* type, index_t* length) = 0;
47  virtual bool read_string_end_wrapped(
48  const TSGDataType* type, index_t length) = 0;
49 
50  virtual bool read_stringentry_begin_wrapped(
51  const TSGDataType* type, index_t y) = 0;
52  virtual bool read_stringentry_end_wrapped(
53  const TSGDataType* type, index_t y) = 0;
54 
55  virtual bool read_sparse_begin_wrapped(
56  const TSGDataType* type, index_t* length) = 0;
57  virtual bool read_sparse_end_wrapped(
58  const TSGDataType* type, index_t length) = 0;
59 
60  virtual bool read_sparseentry_begin_wrapped(
61  const TSGDataType* type, SGSparseVectorEntry<char>* first_entry,
62  index_t* feat_index, index_t y) = 0;
63  virtual bool read_sparseentry_end_wrapped(
64  const TSGDataType* type, SGSparseVectorEntry<char>* first_entry,
65  index_t* feat_index, index_t y) = 0;
66 
67  virtual bool read_item_begin_wrapped(
68  const TSGDataType* type, index_t y, index_t x) = 0;
69  virtual bool read_item_end_wrapped(
70  const TSGDataType* type, index_t y, index_t x) = 0;
71 
72  virtual bool read_sgserializable_begin_wrapped(
73  const TSGDataType* type, char* sgserializable_name,
74  EPrimitiveType* generic) = 0;
75  virtual bool read_sgserializable_end_wrapped(
76  const TSGDataType* type, const char* sgserializable_name,
77  EPrimitiveType generic) = 0;
78 
79  virtual bool read_type_begin_wrapped(
80  const TSGDataType* type, const char* name,
81  const char* prefix) = 0;
82  virtual bool read_type_end_wrapped(
83  const TSGDataType* type, const char* name,
84  const char* prefix) = 0;
85 
86 #endif
87  /* End of abstract write methods */
88  /* ******************************************************** */
89 
90  }; /* struct TSerializableReader */
91 /* public: */
92 private:
94  TSerializableReader* m_reader;
95 
96  bool is_task_warn(char rw, const char* name, const char* prefix);
97  bool false_warn(const char* prefix, const char* name);
98 
99 protected:
101  FILE* m_fstream;
103  char m_task;
105  char* m_filename;
106 
112  void init(FILE* fstream, char task, const char* filename);
113 
114  /* ************************************************************ */
115  /* Begin of abstract write methods */
116 
117 #ifndef DOXYGEN_SHOULD_SKIP_THIS
118  virtual TSerializableReader* new_reader(
119  char* dest_version, size_t n) = 0;
120 
121  virtual bool write_scalar_wrapped(
122  const TSGDataType* type, const void* param) = 0;
123 
124  virtual bool write_cont_begin_wrapped(
125  const TSGDataType* type, index_t len_real_y,
126  index_t len_real_x) = 0;
127  virtual bool write_cont_end_wrapped(
128  const TSGDataType* type, index_t len_real_y,
129  index_t len_real_x) = 0;
130 
131  virtual bool write_string_begin_wrapped(
132  const TSGDataType* type, index_t length) = 0;
133  virtual bool write_string_end_wrapped(
134  const TSGDataType* type, index_t length) = 0;
135 
136  virtual bool write_stringentry_begin_wrapped(
137  const TSGDataType* type, index_t y) = 0;
138  virtual bool write_stringentry_end_wrapped(
139  const TSGDataType* type, index_t y) = 0;
140 
141  virtual bool write_sparse_begin_wrapped(
142  const TSGDataType* type, index_t length) = 0;
143  virtual bool write_sparse_end_wrapped(
144  const TSGDataType* type, index_t length) = 0;
145 
146  virtual bool write_sparseentry_begin_wrapped(
147  const TSGDataType* type, const SGSparseVectorEntry<char>* first_entry,
148  index_t feat_index, index_t y) = 0;
149  virtual bool write_sparseentry_end_wrapped(
150  const TSGDataType* type, const SGSparseVectorEntry<char>* first_entry,
151  index_t feat_index, index_t y) = 0;
152 
153  virtual bool write_item_begin_wrapped(
154  const TSGDataType* type, index_t y, index_t x) = 0;
155  virtual bool write_item_end_wrapped(
156  const TSGDataType* type, index_t y, index_t x) = 0;
157 
158  virtual bool write_sgserializable_begin_wrapped(
159  const TSGDataType* type, const char* sgserializable_name,
160  EPrimitiveType generic) = 0;
161  virtual bool write_sgserializable_end_wrapped(
162  const TSGDataType* type, const char* sgserializable_name,
163  EPrimitiveType generic) = 0;
164 
165  virtual bool write_type_begin_wrapped(
166  const TSGDataType* type, const char* name,
167  const char* prefix) = 0;
168  virtual bool write_type_end_wrapped(
169  const TSGDataType* type, const char* name,
170  const char* prefix) = 0;
171 #endif
172 
173  /* End of abstract write methods */
174  /* ************************************************************ */
175 
176 public:
178  explicit CSerializableFile();
179 
185  explicit CSerializableFile(FILE* fstream, char rw);
186 
192  explicit CSerializableFile(const char* fname, char rw='r');
193 
195  virtual ~CSerializableFile();
196 
198  virtual void close();
199 
201  virtual bool is_opened();
202 
203  /* ************************************************************ */
204  /* Begin of public wrappers */
205 
206 #ifndef DOXYGEN_SHOULD_SKIP_THIS
207  virtual bool write_scalar(
208  const TSGDataType* type, const char* name, const char* prefix,
209  const void* param);
210  virtual bool read_scalar(
211  const TSGDataType* type, const char* name, const char* prefix,
212  void* param);
213 
214  virtual bool write_cont_begin(
215  const TSGDataType* type, const char* name, const char* prefix,
216  index_t len_real_y, index_t len_real_x);
217  virtual bool read_cont_begin(
218  const TSGDataType* type, const char* name, const char* prefix,
219  index_t* len_read_y, index_t* len_read_x);
220 
221  virtual bool write_cont_end(
222  const TSGDataType* type, const char* name, const char* prefix,
223  index_t len_real_y, index_t len_real_x);
224  virtual bool read_cont_end(
225  const TSGDataType* type, const char* name, const char* prefix,
226  index_t len_read_y, index_t len_read_x);
227 
228  virtual bool write_string_begin(
229  const TSGDataType* type, const char* name, const char* prefix,
230  index_t length);
231  virtual bool read_string_begin(
232  const TSGDataType* type, const char* name, const char* prefix,
233  index_t* length);
234 
235  virtual bool write_string_end(
236  const TSGDataType* type, const char* name, const char* prefix,
237  index_t length);
238  virtual bool read_string_end(
239  const TSGDataType* type, const char* name, const char* prefix,
240  index_t length);
241 
242  virtual bool write_stringentry_begin(
243  const TSGDataType* type, const char* name, const char* prefix,
244  index_t y);
245  virtual bool read_stringentry_begin(
246  const TSGDataType* type, const char* name, const char* prefix,
247  index_t y);
248 
249  virtual bool write_stringentry_end(
250  const TSGDataType* type, const char* name, const char* prefix,
251  index_t y);
252  virtual bool read_stringentry_end(
253  const TSGDataType* type, const char* name, const char* prefix,
254  index_t y);
255 
256  virtual bool write_sparse_begin(
257  const TSGDataType* type, const char* name, const char* prefix,
258  index_t length);
259  virtual bool read_sparse_begin(
260  const TSGDataType* type, const char* name, const char* prefix,
261  index_t* length);
262 
263  virtual bool write_sparse_end(
264  const TSGDataType* type, const char* name, const char* prefix,
265  index_t length);
266  virtual bool read_sparse_end(
267  const TSGDataType* type, const char* name, const char* prefix,
268  index_t length);
269 
270  virtual bool write_sparseentry_begin(
271  const TSGDataType* type, const char* name, const char* prefix,
272  const SGSparseVectorEntry<char>* first_entry, index_t feat_index,
273  index_t y);
274  virtual bool read_sparseentry_begin(
275  const TSGDataType* type, const char* name, const char* prefix,
276  SGSparseVectorEntry<char>* first_entry, index_t* feat_index,
277  index_t y);
278 
279  virtual bool write_sparseentry_end(
280  const TSGDataType* type, const char* name, const char* prefix,
281  const SGSparseVectorEntry<char>* first_entry, index_t feat_index,
282  index_t y);
283  virtual bool read_sparseentry_end(
284  const TSGDataType* type, const char* name, const char* prefix,
285  SGSparseVectorEntry<char>* first_entry, index_t* feat_index,
286  index_t y);
287 
288  virtual bool write_item_begin(
289  const TSGDataType* type, const char* name, const char* prefix,
290  index_t y, index_t x);
291  virtual bool read_item_begin(
292  const TSGDataType* type, const char* name, const char* prefix,
293  index_t y, index_t x);
294 
295  virtual bool write_item_end(
296  const TSGDataType* type, const char* name, const char* prefix,
297  index_t y, index_t x);
298  virtual bool read_item_end(
299  const TSGDataType* type, const char* name, const char* prefix,
300  index_t y, index_t x);
301 
302  virtual bool write_sgserializable_begin(
303  const TSGDataType* type, const char* name, const char* prefix,
304  const char* sgserializable_name, EPrimitiveType generic);
305  virtual bool read_sgserializable_begin(
306  const TSGDataType* type, const char* name, const char* prefix,
307  char* sgserializable_name, EPrimitiveType* generic);
308 
309  virtual bool write_sgserializable_end(
310  const TSGDataType* type, const char* name, const char* prefix,
311  const char* sgserializable_name, EPrimitiveType generic);
312  virtual bool read_sgserializable_end(
313  const TSGDataType* type, const char* name, const char* prefix,
314  const char* sgserializable_name, EPrimitiveType generic);
315 
316  virtual bool write_type_begin(
317  const TSGDataType* type, const char* name, const char* prefix);
318  virtual bool read_type_begin(
319  const TSGDataType* type, const char* name, const char* prefix);
320 
321  virtual bool write_type_end(
322  const TSGDataType* type, const char* name, const char* prefix);
323  virtual bool read_type_end(
324  const TSGDataType* type, const char* name, const char* prefix);
325 #endif
326  /* End of public wrappers */
327  /* ************************************************************ */
328 };
329 }
330 #endif // __SERIALIZABLE_FILE_H__

SHOGUN Machine Learning Toolbox - Documentation