LTI-Lib latest version v1.9 - last update 10 Apr 2010

ltiSTLIoInterface.h

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006
00003  * Lehrstuhl fuer Technische Informatik, RWTH-Aachen, Germany
00004  *
00005  * This file is part of the LTI-Computer Vision Library (LTI-Lib)
00006  *
00007  * The LTI-Lib is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public License (LGPL)
00009  * as published by the Free Software Foundation; either version 2.1 of
00010  * the License, or (at your option) any later version.
00011  *
00012  * The LTI-Lib is distributed in the hope that it will be
00013  * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
00014  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with the LTI-Lib; see the file LICENSE.  If
00019  * not, write to the Free Software Foundation, Inc., 59 Temple Place -
00020  * Suite 330, Boston, MA 02111-1307, USA.
00021  */
00022 
00023 
00024 /*----------------------------------------------------------------
00025  * project ....: LTI Digital Image/Signal Processing Library
00026  * file .......: ltiSTLIoInterface.h
00027  * authors ....: Pablo Alvarado
00028  * organization: LTI, RWTH Aachen
00029  * creation ...: 11.05.2001
00030  * revisions ..: $Id: ltiSTLIoInterface.h,v 1.8 2006/02/08 12:10:15 ltilib Exp $
00031  */
00032 
00033 #ifndef _LTI_STL_IO_INTERFACE_H_
00034 #define _LTI_STL_IO_INTERFACE_H_
00035 
00036 #include "ltiIoHandler.h"
00037 #include <list>
00038 #include <vector>
00039 #include <map>
00040 
00041 /** @file ltiSTLIoInterface.h This file contains read and write
00042  * methods for some often used STL containers. Note that for the
00043  * read() functions to work the type T must implement the the
00044  * operator=(). Further, for all functions the type T must have a
00045  * corresponding read/write function. The following functions are
00046  * implemented:
00047  *
00048  * - bool read(ioHandler& handler, std::list<T>& data, const bool complete=true)
00049  * - bool write(ioHandler& handler, const std::list<T>& data, const bool complete=true)
00050  * - bool read(ioHandler& handler, std::vector<T>& data, const bool complete=true)
00051  * - bool write(ioHandler& handler, const std::vector<T>& data, const bool complete=true)
00052  * - bool read(ioHandler& handler, std::pair<T,U>& data, const bool complete=true)
00053  * - bool write(ioHandler& handler, const std::pair<T,U>& data, const bool complete=true)
00054  * - bool read(ioHandler& handler, std::map<T,U>& data, const bool complete=true)
00055  * - bool write(ioHandler& handler, const std::map<T,U>& data, const bool complete=true)
00056  * - bool read(ioHandler& handler, std::multimap<T,U>& data, const bool complete=true)
00057  * - bool write(ioHandler& handler, const std::multimap<T,U>& data, const bool complete=true)
00058  *
00059  * The parameters are:
00060  * - \a handler: ioHandler to which the data is to be written or from which it
00061  *               is read.
00062  * - \a data: the data to be written.
00063  * - \a complete: if true the data is enclosed by the begin and end markers of
00064  *                the given ioHandler
00065  */
00066 
00067 namespace lti {
00068 
00069   // -------------------------------------------------------------------------
00070   // std::pair
00071   // -------------------------------------------------------------------------
00072 
00073   /**
00074    * read function for STL std::pair.
00075    * Works only if the types in the std::pair can be read/written with
00076    * the lti::read/lti::write functions.
00077    *
00078    * @ingroup gStorable
00079    */
00080   template<class T,class U>
00081   bool read(ioHandler& handler,std::pair<T,U>& data,const bool complete=true) {
00082     bool b = true;
00083 
00084     if (complete) {
00085       b = handler.readBegin();
00086     }
00087 
00088     b = b && lti::read(handler,data.first);
00089     b = b && handler.readDataSeparator();
00090     b = b && lti::read(handler,data.second);
00091 
00092     if (complete) {
00093       b = b && handler.readEnd();
00094     }
00095 
00096     return b;
00097   }
00098 
00099   /**
00100    * write function for STL std::pair.
00101    * Works only if the types in the std::pair can be read/written with
00102    * the lti::read/lti::write functions.
00103    *
00104    * @ingroup gStorable
00105    */
00106   template<class T,class U>
00107   bool write(ioHandler& handler,const std::pair<T,U>& data,
00108              const bool complete=true){
00109     bool b = true;
00110 
00111     if (complete) {
00112       b = handler.writeBegin();
00113     }
00114 
00115     b = b && lti::write(handler,data.first);
00116     b = b && handler.writeDataSeparator();
00117     b = b && lti::write(handler,data.second);
00118 
00119     if (complete) {
00120       b = b && handler.writeEnd();
00121     }
00122 
00123     return b;
00124   }
00125 
00126 
00127   // -------------------------------------------------------------------------
00128   // std::list
00129   // -------------------------------------------------------------------------
00130 
00131   /**
00132    * read function for STL list containers
00133    * Works only if the type T implements the operator=()
00134    *
00135    * @ingroup gStorable
00136    */
00137   template <class T>
00138   bool read(ioHandler& handler,std::list<T>& data,const bool complete=true) {
00139     int i,size;
00140     bool b = true;
00141 
00142     if (complete) {
00143       b = handler.readBegin();
00144     }
00145 
00146     b = b && handler.read("size",size);
00147     std::string str;
00148 
00149     int level = handler.getLevel();
00150 
00151     b = b && handler.readBegin();
00152     b = b && handler.readSymbol(str);
00153 
00154     if (str == "data") {
00155       b = b && handler.readKeyValueSeparator();
00156       b = b && handler.readBegin();
00157       data.clear();
00158       if (size > 0) {
00159         size--;
00160         T tmp;
00161         for (i=0;i<size;++i) {
00162           b = b && read(handler,tmp);
00163           data.push_back(tmp);
00164           b = b && handler.readDataSeparator();
00165         }
00166         b = b && read(handler,tmp);
00167         data.push_back(tmp);
00168       }
00169     }
00170 
00171     // read all the next end tokens... and ensure consistency
00172     while (handler.readEnd() && (handler.getLevel() > level));
00173 
00174     if (complete) {
00175       b = b && handler.readEnd();
00176     }
00177     return b;
00178   }
00179 
00180   /**
00181    * write function for STL list containers
00182    *
00183    * @ingroup gStorable
00184    */
00185   template <class T>
00186   bool write(ioHandler& handler,
00187              const std::list<T>& data,
00188              const bool complete=true) {
00189     int i;
00190     const int theSize = static_cast<int>(data.size());
00191     bool b = true;
00192 
00193     if (complete) {
00194       b = handler.writeBegin();
00195     }
00196 
00197     b = b && handler.write("size",theSize);
00198 
00199     b = b && handler.writeBegin();
00200     b = b && handler.writeSymbol(std::string("data"));
00201     b = b && handler.writeKeyValueSeparator();
00202     b = b && handler.writeBegin();
00203     if (theSize > 0) {
00204       typename std::list<T>::const_iterator it;
00205       // begin from one to ensure that the last element of the container
00206       // will NOT be written yet!
00207       for (i=1,it=data.begin();i<theSize;++it,++i) {
00208         b = b && write(handler,(*it));
00209         b = b && handler.writeDataSeparator();
00210       }
00211       b = b && write(handler,(*it));
00212     }
00213     b = b && handler.writeEnd();
00214     b = b && handler.writeEnd();
00215 
00216     if (complete) {
00217       b = b && handler.writeEnd();
00218     }
00219 
00220     return b;
00221   }
00222 
00223   // -------------------------------------------------------------------------
00224   // std::vector
00225   // -------------------------------------------------------------------------
00226 
00227   /**
00228    * read function for STL vector containers
00229    * Works only if the type T implements the operator=()
00230    *
00231    * @ingroup gStorable
00232    */
00233   template <class T>
00234   bool read(ioHandler& handler,
00235             std::vector<T>& data,
00236             const bool complete=true) {
00237 
00238     int i,size;
00239     bool b = true;
00240 
00241     if (complete) {
00242       b = handler.readBegin();
00243     }
00244 
00245     b = b && handler.read("size",size);
00246     std::string str;
00247 
00248     int level = handler.getLevel();
00249 
00250     b = b && handler.readBegin();
00251     b = b && handler.readSymbol(str);
00252 
00253     if (str == "data") {
00254       b = b && handler.readKeyValueSeparator();
00255       b = b && handler.readBegin();
00256       data.resize(size);
00257       if (size > 0) {
00258         size--;
00259         for (i=0;i<size;++i) {
00260           b = b && read(handler,data[i]);
00261           b = b && handler.readDataSeparator();
00262         }
00263         b = b && read(handler,data[i]);
00264       }
00265     }
00266 
00267     // read all the next end tokens... and ensure consistency
00268     while (handler.readEnd() && (handler.getLevel() > level));
00269 
00270     if (complete) {
00271       b = b && handler.readEnd();
00272     }
00273 
00274     return b;
00275   }
00276 
00277   /**
00278    * write function for STL vector containers
00279    *
00280    * @ingroup gStorable
00281    */
00282   template <class T>
00283   bool write(ioHandler& handler,
00284              const std::vector<T>& data,
00285              const bool complete=true) {
00286     int i;
00287     const int theSize = static_cast<int>(data.size());
00288     bool b = true;
00289 
00290     if (complete) {
00291       b = handler.writeBegin();
00292     }
00293 
00294     b = b && handler.write("size",theSize);
00295 
00296     b = b && handler.writeBegin();
00297     b = b && handler.writeSymbol(std::string("data"));
00298     b = b && handler.writeKeyValueSeparator();
00299     b = b && handler.writeBegin();
00300     if (theSize > 0) {
00301       typename std::vector<T>::const_iterator it;
00302       // begin from one to ensure that the last element of the container
00303       // will NOT be written yet!
00304       for (i=1,it=data.begin();i<theSize;++it,++i) {
00305         b = b && write(handler,(*it));
00306         b = b && handler.writeDataSeparator();
00307       }
00308       b = b && write(handler,(*it));
00309     }
00310     b = b && handler.writeEnd();
00311     b = b && handler.writeEnd();
00312 
00313     if (complete) {
00314       b = b && handler.writeEnd();
00315     }
00316 
00317     return b;
00318   }
00319 
00320 
00321   // -------------------------------------------------------------------------
00322   // std::map
00323   // -------------------------------------------------------------------------
00324 
00325   /**
00326    * read function for STL map containers
00327    * Works only if the type U implements the operator=()
00328    *
00329    * @ingroup gStorable
00330    */
00331   template <class T,class U>
00332   bool read(ioHandler& handler,std::map<T,U>& data,const bool complete=true) {
00333     int i,size;
00334     bool b = true;
00335 
00336     if (complete) {
00337       b = handler.readBegin();
00338     }
00339 
00340     b = b && handler.read("size",size);
00341     std::string str;
00342 
00343     int level = handler.getLevel();
00344 
00345     b = b && handler.readBegin();
00346     b = b && handler.readSymbol(str);
00347 
00348     if (str == "data") {
00349       b = b && handler.readKeyValueSeparator();
00350       b = b && handler.readBegin();
00351       data.clear();
00352       if (size > 0) {
00353         size--;
00354         std::pair<T,U> tmp;
00355         for (i=0;i<size;++i) {
00356           b = b && read(handler,tmp);         
00357           data.insert(tmp);
00358           b = b && handler.readDataSeparator();
00359         }
00360         b = b && read(handler,tmp);
00361         data.insert(tmp);
00362       }
00363     }
00364 
00365     // read all the next end tokens... and ensure consistency
00366     while (handler.readEnd() && (handler.getLevel() > level));
00367 
00368     if (complete) {
00369       b = b && handler.readEnd();
00370     }
00371     return b;
00372   }
00373 
00374   /**
00375    * write function for STL map containers
00376    *
00377    * @ingroup gStorable
00378    */
00379   template <class T,class U>
00380   bool write(ioHandler& handler,
00381              const std::map<T,U>& data,
00382              const bool complete=true) {
00383     int i;
00384     const int theSize = static_cast<int>(data.size());
00385     bool b = true;
00386 
00387     if (complete) {
00388       b = handler.writeBegin();
00389     }
00390 
00391     b = b && handler.write("size",theSize);
00392 
00393     b = b && handler.writeBegin();
00394     b = b && handler.writeSymbol(std::string("data"));
00395     b = b && handler.writeKeyValueSeparator();
00396     b = b && handler.writeBegin();
00397     if (theSize > 0) {
00398       typename std::map<T,U>::const_iterator it;
00399       // begin from one to ensure that the last element of the container
00400       // will NOT be written yet!
00401       for (i=1,it=data.begin();i<theSize;++it,++i) {
00402         b = b && write(handler,(*it));
00403         b = b && handler.writeDataSeparator();
00404       }
00405       b = b && write(handler,(*it));
00406     }
00407     b = b && handler.writeEnd();
00408     b = b && handler.writeEnd();
00409 
00410     if (complete) {
00411       b = b && handler.writeEnd();
00412     }
00413 
00414     return b;
00415   }
00416 
00417   // -------------------------------------------------------------------------
00418   // std::multimap
00419   // -------------------------------------------------------------------------
00420 
00421 
00422   /**
00423    * read function for STL multimap containers
00424    * Works only if the type U implements the operator=()
00425    *
00426    * @ingroup gStorable
00427    */
00428   template <class T,class U>
00429   bool read(ioHandler& handler,std::multimap<T,U>& data,
00430             const bool complete=true) {
00431     int i,size;
00432     bool b = true;
00433     
00434     if (complete) {
00435       b = handler.readBegin();
00436     }
00437 
00438     b = b && handler.read("size",size);
00439     std::string str;
00440 
00441     int level = handler.getLevel();
00442 
00443     b = b && handler.readBegin();
00444     b = b && handler.readSymbol(str);
00445 
00446     if (str == "data") {
00447       b = b && handler.readKeyValueSeparator();
00448       b = b && handler.readBegin();
00449       data.clear();
00450       if (size > 0) {
00451         size--;
00452         std::pair<T,U> tmp;
00453         for (i=0;i<size;++i) {
00454           b = b && read(handler,tmp);
00455           data.insert(tmp);
00456           b = b && handler.readDataSeparator();
00457         }
00458         b = b && read(handler,tmp);
00459         data.insert(tmp);
00460       }
00461     }
00462 
00463     // read all the next end tokens... and ensure consistency
00464     while (handler.readEnd() && (handler.getLevel() > level));
00465 
00466     if (complete) {
00467       b = b && handler.readEnd();
00468     }
00469     return b;
00470   }
00471 
00472   /**
00473    * write function for STL multimap containers
00474    *
00475    * @ingroup gStorable
00476    */
00477   template <class T,class U>
00478   bool write(ioHandler& handler,const std::multimap<T,U>& data,
00479              const bool complete=true) {
00480     int i;
00481     const int theSize = data.size();
00482     bool b = true;
00483 
00484     if (complete) {
00485       b = handler.writeBegin();
00486     }
00487 
00488     b = b && handler.write("size",theSize);
00489 
00490     b = b && handler.writeBegin();
00491     b = b && handler.writeSymbol(std::string("data"));
00492     b = b && handler.writeKeyValueSeparator();
00493     b = b && handler.writeBegin();
00494     if (theSize > 0) {
00495       typename std::map<T,U>::const_iterator it;
00496       // begin from one to ensure that the last element of the container
00497       // will NOT be written yet!
00498       for (i=1,it=data.begin();i<theSize;++it,++i) {
00499         b = b && write(handler,(*it));
00500         b = b && handler.writeDataSeparator();
00501       }
00502       b = b && write(handler,(*it));
00503     }
00504     b = b && handler.writeEnd();
00505     b = b && handler.writeEnd();
00506 
00507     if (complete) {
00508       b = b && handler.writeEnd();
00509     }
00510 
00511     return b;
00512   }
00513 
00514 
00515 }
00516 
00517 #endif

Generated on Sat Apr 10 15:26:15 2010 for LTI-Lib by Doxygen 1.6.1