SHOGUN
v1.1.0
|
00001 /* 00002 * This program is free software; you can redistribute it and/or modify 00003 * it under the terms of the GNU General Public License as published by 00004 * the Free Software Foundation; either version 3 of the License, or 00005 * (at your option) any later version. 00006 * 00007 * Written (W) 1999-2009 Soeren Sonnenburg 00008 * Copyright (C) 1999-2009 Fraunhofer Institute FIRST and Max-Planck-Society 00009 * Copyright (C) 2010 Berlin Institute of Technology 00010 */ 00011 00012 #ifndef __SERIALIZABLE_FILE_H__ 00013 #define __SERIALIZABLE_FILE_H__ 00014 00015 #include <stdio.h> 00016 #include <shogun/base/SGObject.h> 00017 #include <shogun/lib/DataType.h> 00018 00019 namespace shogun 00020 { 00021 class CSGObject; 00022 00024 class CSerializableFile :public CSGObject 00025 { 00026 public: 00028 struct TSerializableReader :public CSGObject { 00029 00030 /* ******************************************************** */ 00031 /* Begin of abstract write methods */ 00032 00033 #ifndef DOXYGEN_SHOULD_SKIP_THIS 00034 virtual bool read_scalar_wrapped( 00035 const TSGDataType* type, void* param) = 0; 00036 00037 virtual bool read_cont_begin_wrapped( 00038 const TSGDataType* type, index_t* len_read_y, 00039 index_t* len_read_x) = 0; 00040 virtual bool read_cont_end_wrapped( 00041 const TSGDataType* type, index_t len_read_y, 00042 index_t len_read_x) = 0; 00043 00044 virtual bool read_string_begin_wrapped( 00045 const TSGDataType* type, index_t* length) = 0; 00046 virtual bool read_string_end_wrapped( 00047 const TSGDataType* type, index_t length) = 0; 00048 00049 virtual bool read_stringentry_begin_wrapped( 00050 const TSGDataType* type, index_t y) = 0; 00051 virtual bool read_stringentry_end_wrapped( 00052 const TSGDataType* type, index_t y) = 0; 00053 00054 virtual bool read_sparse_begin_wrapped( 00055 const TSGDataType* type, index_t* vec_index, 00056 index_t* length) = 0; 00057 virtual bool read_sparse_end_wrapped( 00058 const TSGDataType* type, index_t* vec_index, 00059 index_t length) = 0; 00060 00061 virtual bool read_sparseentry_begin_wrapped( 00062 const TSGDataType* type, SGSparseVectorEntry<char>* first_entry, 00063 index_t* feat_index, index_t y) = 0; 00064 virtual bool read_sparseentry_end_wrapped( 00065 const TSGDataType* type, SGSparseVectorEntry<char>* first_entry, 00066 index_t* feat_index, index_t y) = 0; 00067 00068 virtual bool read_item_begin_wrapped( 00069 const TSGDataType* type, index_t y, index_t x) = 0; 00070 virtual bool read_item_end_wrapped( 00071 const TSGDataType* type, index_t y, index_t x) = 0; 00072 00073 virtual bool read_sgserializable_begin_wrapped( 00074 const TSGDataType* type, char* sgserializable_name, 00075 EPrimitiveType* generic) = 0; 00076 virtual bool read_sgserializable_end_wrapped( 00077 const TSGDataType* type, const char* sgserializable_name, 00078 EPrimitiveType generic) = 0; 00079 00080 virtual bool read_type_begin_wrapped( 00081 const TSGDataType* type, const char* name, 00082 const char* prefix) = 0; 00083 virtual bool read_type_end_wrapped( 00084 const TSGDataType* type, const char* name, 00085 const char* prefix) = 0; 00086 00087 #endif 00088 /* End of abstract write methods */ 00089 /* ******************************************************** */ 00090 00091 }; /* struct TSerializableReader */ 00092 /* public: */ 00093 private: 00095 TSerializableReader* m_reader; 00096 00097 bool is_task_warn(char rw, const char* name, const char* prefix); 00098 bool false_warn(const char* prefix, const char* name); 00099 00100 protected: 00102 FILE* m_fstream; 00104 char m_task; 00106 char* m_filename; 00107 00113 virtual void init(FILE* fstream, char task, const char* filename); 00114 00115 /* ************************************************************ */ 00116 /* Begin of abstract write methods */ 00117 00118 #ifndef DOXYGEN_SHOULD_SKIP_THIS 00119 virtual TSerializableReader* new_reader( 00120 char* dest_version, size_t n) = 0; 00121 00122 virtual bool write_scalar_wrapped( 00123 const TSGDataType* type, const void* param) = 0; 00124 00125 virtual bool write_cont_begin_wrapped( 00126 const TSGDataType* type, index_t len_real_y, 00127 index_t len_real_x) = 0; 00128 virtual bool write_cont_end_wrapped( 00129 const TSGDataType* type, index_t len_real_y, 00130 index_t len_real_x) = 0; 00131 00132 virtual bool write_string_begin_wrapped( 00133 const TSGDataType* type, index_t length) = 0; 00134 virtual bool write_string_end_wrapped( 00135 const TSGDataType* type, index_t length) = 0; 00136 00137 virtual bool write_stringentry_begin_wrapped( 00138 const TSGDataType* type, index_t y) = 0; 00139 virtual bool write_stringentry_end_wrapped( 00140 const TSGDataType* type, index_t y) = 0; 00141 00142 virtual bool write_sparse_begin_wrapped( 00143 const TSGDataType* type, index_t vec_index, 00144 index_t length) = 0; 00145 virtual bool write_sparse_end_wrapped( 00146 const TSGDataType* type, index_t vec_index, 00147 index_t length) = 0; 00148 00149 virtual bool write_sparseentry_begin_wrapped( 00150 const TSGDataType* type, const SGSparseVectorEntry<char>* first_entry, 00151 index_t feat_index, index_t y) = 0; 00152 virtual bool write_sparseentry_end_wrapped( 00153 const TSGDataType* type, const SGSparseVectorEntry<char>* first_entry, 00154 index_t feat_index, index_t y) = 0; 00155 00156 virtual bool write_item_begin_wrapped( 00157 const TSGDataType* type, index_t y, index_t x) = 0; 00158 virtual bool write_item_end_wrapped( 00159 const TSGDataType* type, index_t y, index_t x) = 0; 00160 00161 virtual bool write_sgserializable_begin_wrapped( 00162 const TSGDataType* type, const char* sgserializable_name, 00163 EPrimitiveType generic) = 0; 00164 virtual bool write_sgserializable_end_wrapped( 00165 const TSGDataType* type, const char* sgserializable_name, 00166 EPrimitiveType generic) = 0; 00167 00168 virtual bool write_type_begin_wrapped( 00169 const TSGDataType* type, const char* name, 00170 const char* prefix) = 0; 00171 virtual bool write_type_end_wrapped( 00172 const TSGDataType* type, const char* name, 00173 const char* prefix) = 0; 00174 #endif 00175 00176 /* End of abstract write methods */ 00177 /* ************************************************************ */ 00178 00179 public: 00181 explicit CSerializableFile(); 00182 00188 explicit CSerializableFile(FILE* fstream, char rw); 00189 00195 explicit CSerializableFile(const char* fname, char rw='r'); 00196 00198 virtual ~CSerializableFile(); 00199 00201 virtual void close(); 00202 00204 virtual bool is_opened(); 00205 00206 /* ************************************************************ */ 00207 /* Begin of public wrappers */ 00208 00209 #ifndef DOXYGEN_SHOULD_SKIP_THIS 00210 virtual bool write_scalar( 00211 const TSGDataType* type, const char* name, const char* prefix, 00212 const void* param); 00213 virtual bool read_scalar( 00214 const TSGDataType* type, const char* name, const char* prefix, 00215 void* param); 00216 00217 virtual bool write_cont_begin( 00218 const TSGDataType* type, const char* name, const char* prefix, 00219 index_t len_real_y, index_t len_real_x); 00220 virtual bool read_cont_begin( 00221 const TSGDataType* type, const char* name, const char* prefix, 00222 index_t* len_read_y, index_t* len_read_x); 00223 00224 virtual bool write_cont_end( 00225 const TSGDataType* type, const char* name, const char* prefix, 00226 index_t len_real_y, index_t len_real_x); 00227 virtual bool read_cont_end( 00228 const TSGDataType* type, const char* name, const char* prefix, 00229 index_t len_read_y, index_t len_read_x); 00230 00231 virtual bool write_string_begin( 00232 const TSGDataType* type, const char* name, const char* prefix, 00233 index_t length); 00234 virtual bool read_string_begin( 00235 const TSGDataType* type, const char* name, const char* prefix, 00236 index_t* length); 00237 00238 virtual bool write_string_end( 00239 const TSGDataType* type, const char* name, const char* prefix, 00240 index_t length); 00241 virtual bool read_string_end( 00242 const TSGDataType* type, const char* name, const char* prefix, 00243 index_t length); 00244 00245 virtual bool write_stringentry_begin( 00246 const TSGDataType* type, const char* name, const char* prefix, 00247 index_t y); 00248 virtual bool read_stringentry_begin( 00249 const TSGDataType* type, const char* name, const char* prefix, 00250 index_t y); 00251 00252 virtual bool write_stringentry_end( 00253 const TSGDataType* type, const char* name, const char* prefix, 00254 index_t y); 00255 virtual bool read_stringentry_end( 00256 const TSGDataType* type, const char* name, const char* prefix, 00257 index_t y); 00258 00259 virtual bool write_sparse_begin( 00260 const TSGDataType* type, const char* name, const char* prefix, 00261 index_t vec_index, index_t length); 00262 virtual bool read_sparse_begin( 00263 const TSGDataType* type, const char* name, const char* prefix, 00264 index_t* vec_index, index_t* length); 00265 00266 virtual bool write_sparse_end( 00267 const TSGDataType* type, const char* name, const char* prefix, 00268 index_t vec_index, index_t length); 00269 virtual bool read_sparse_end( 00270 const TSGDataType* type, const char* name, const char* prefix, 00271 index_t* vec_index, index_t length); 00272 00273 virtual bool write_sparseentry_begin( 00274 const TSGDataType* type, const char* name, const char* prefix, 00275 const SGSparseVectorEntry<char>* first_entry, index_t feat_index, 00276 index_t y); 00277 virtual bool read_sparseentry_begin( 00278 const TSGDataType* type, const char* name, const char* prefix, 00279 SGSparseVectorEntry<char>* first_entry, index_t* feat_index, 00280 index_t y); 00281 00282 virtual bool write_sparseentry_end( 00283 const TSGDataType* type, const char* name, const char* prefix, 00284 const SGSparseVectorEntry<char>* first_entry, index_t feat_index, 00285 index_t y); 00286 virtual bool read_sparseentry_end( 00287 const TSGDataType* type, const char* name, const char* prefix, 00288 SGSparseVectorEntry<char>* first_entry, index_t* feat_index, 00289 index_t y); 00290 00291 virtual bool write_item_begin( 00292 const TSGDataType* type, const char* name, const char* prefix, 00293 index_t y, index_t x); 00294 virtual bool read_item_begin( 00295 const TSGDataType* type, const char* name, const char* prefix, 00296 index_t y, index_t x); 00297 00298 virtual bool write_item_end( 00299 const TSGDataType* type, const char* name, const char* prefix, 00300 index_t y, index_t x); 00301 virtual bool read_item_end( 00302 const TSGDataType* type, const char* name, const char* prefix, 00303 index_t y, index_t x); 00304 00305 virtual bool write_sgserializable_begin( 00306 const TSGDataType* type, const char* name, const char* prefix, 00307 const char* sgserializable_name, EPrimitiveType generic); 00308 virtual bool read_sgserializable_begin( 00309 const TSGDataType* type, const char* name, const char* prefix, 00310 char* sgserializable_name, EPrimitiveType* generic); 00311 00312 virtual bool write_sgserializable_end( 00313 const TSGDataType* type, const char* name, const char* prefix, 00314 const char* sgserializable_name, EPrimitiveType generic); 00315 virtual bool read_sgserializable_end( 00316 const TSGDataType* type, const char* name, const char* prefix, 00317 const char* sgserializable_name, EPrimitiveType generic); 00318 00319 virtual bool write_type_begin( 00320 const TSGDataType* type, const char* name, const char* prefix); 00321 virtual bool read_type_begin( 00322 const TSGDataType* type, const char* name, const char* prefix); 00323 00324 virtual bool write_type_end( 00325 const TSGDataType* type, const char* name, const char* prefix); 00326 virtual bool read_type_end( 00327 const TSGDataType* type, const char* name, const char* prefix); 00328 #endif 00329 /* End of public wrappers */ 00330 /* ************************************************************ */ 00331 }; 00332 } 00333 #endif // __SERIALIZABLE_FILE_H__