CVR-Lib last update 20 Sep 2009

cvrIoHandler.h

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 1998-2004
00003  * Lehrstuhl fuer Technische Informatik, RWTH-Aachen, Germany
00004  *
00005  *
00006  * This file is part of the Computer Vision and Robotics Library (CVR-Lib)
00007  *
00008  * The CVR-Lib is free software; you can redistribute it and/or
00009  * modify it under the terms of the BSD License.
00010  *
00011  * All rights reserved.
00012  *
00013  * Redistribution and use in source and binary forms, with or without
00014  * modification, are permitted provided that the following conditions are met:
00015  *
00016  * 1. Redistributions of source code must retain the above copyright notice,
00017  *    this list of conditions and the following disclaimer.
00018  *
00019  * 2. Redistributions in binary form must reproduce the above copyright notice,
00020  *    this list of conditions and the following disclaimer in the documentation
00021  *    and/or other materials provided with the distribution.
00022  *
00023  * 3. Neither the name of the authors nor the names of its contributors may be
00024  *    used to endorse or promote products derived from this software without
00025  *    specific prior written permission.
00026  *
00027  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00028  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00029  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00030  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00031  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00032  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00033  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00034  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00035  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00036  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00037  * POSSIBILITY OF SUCH DAMAGE.
00038  */
00039 
00040 
00041 
00042 /**
00043  * \file   cvrIoHandler.h
00044  *         Contains the class cvr::ioHandler, which is the base for all
00045  *         serialization concepts in the CVR-Lib
00046  * \author Pablo Alvarado
00047  * \date   07.12.2000
00048  *
00049  * $Id: cvrIoHandler.h,v 1.12 2006/09/29 13:35:19 doerfler Exp $
00050  */
00051 
00052 #ifndef _CVR_IO_HANDLER_H_
00053 #define _CVR_IO_HANDLER_H_
00054 
00055 
00056 #include "cvrObject.h"
00057 #include "cvrTypes.h"
00058 #include "cvrStatus.h"
00059 #include <string>
00060 #include <iosfwd> // required for method use()
00061 
00062 namespace cvr {
00063 
00064   /**
00065    * Input/output handlers.
00066    *
00067    * The ioHandler classes offer an abstract interface for the functor
00068    * parameters and other cvr::ioObject classes to read() and write() them in
00069    * different formats.
00070    *
00071    * The CVR-Libstandard ioHandler is the cvr::lispStreamHandler, which uses
00072    * a LISP-similar syntax to write and read data from streams.  To save space
00073    * and time while serializing objects, you can use the
00074    * cvr::binaryStreamHandler.
00075    *
00076    * All ioHandlers are used as wrappers for std::istreams or std::ostreams
00077    * objects.  Therefore, the abstract interface provides the virtual methods
00078    * use() to indicate which stream has to be used.
00079    *
00080    * @ingroup gStorable
00081    */
00082   class ioHandler : public object, public status {
00083   public:
00084 
00085     /**
00086      * Default constructor
00087      */
00088     ioHandler();
00089 
00090     /**
00091      * Copy constructor
00092      */
00093     ioHandler(const ioHandler& other);
00094 
00095     /**
00096      * Destructor
00097      */
00098     virtual ~ioHandler();
00099 
00100     /**
00101      * Copy data of "other" functor.
00102      * @param other the functor to be copied
00103      * @return a reference to this functor object
00104      */
00105     ioHandler& copy(const ioHandler& other);
00106 
00107     /**
00108      * Returns the name of this class.
00109      */
00110     virtual const std::string& name() const = 0;
00111 
00112     /**
00113      * Returns a pointer to a clone of this functor.
00114      */
00115     virtual ioHandler* clone() const = 0;
00116 
00117     /**
00118      * Returns a pointer to a new instance of the dynamic class type.
00119      */
00120     virtual ioHandler* newInstance() const = 0;
00121 
00122     /**
00123      * Indicate the output stream to be used
00124      *
00125      * Calling this method you will reinitialize the state of the
00126      * parser (see also clear()).
00127      */
00128     virtual void use(std::ostream& aStream) = 0;
00129 
00130     /**
00131      * Indicate the input stream to be used
00132      *
00133      * Calling this method you will reinitialize the state of the
00134      * parser (see also clear()).
00135      */
00136     virtual void use(std::istream& aStream) = 0;
00137 
00138     /**
00139      * Write the begin token or tokens
00140      */
00141     virtual bool writeBegin();
00142 
00143     /**
00144      * Write the end token or tokens
00145      */
00146     virtual bool writeEnd();
00147 
00148     /**
00149      * Read the begin token or tokens
00150      */
00151     virtual bool readBegin();
00152 
00153     /**
00154      * Read the end token or tokens
00155      */
00156     virtual bool readEnd();
00157 
00158     /**
00159      * \name Write methods for standard types
00160      */
00161     //@{
00162     /**
00163      * Write standard types
00164      */
00165     virtual bool write(const std::string& data) = 0;
00166     virtual bool write(const char* data) = 0;
00167     virtual bool write(const double& data) = 0;
00168     virtual bool write(const float& data) = 0;
00169     virtual bool write(const int data) = 0;
00170     virtual bool write(const unsigned int& data) = 0;
00171     virtual bool write(const char& data) = 0;
00172     virtual bool write(const byte& data) = 0;
00173     virtual bool write(const ubyte& data) = 0;
00174     virtual bool write(const bool data) = 0;
00175     virtual bool write(const long& data) = 0;
00176     virtual bool write(const unsigned long& data) = 0;
00177     virtual bool write(const short& data) = 0;
00178     virtual bool write(const unsigned short& data) = 0;
00179     //@}
00180 
00181 
00182     /**
00183      * \name Write methods for symbol-data pairs
00184      */
00185     //@{
00186     /**
00187      * Write symbol-data pair
00188      */
00189     virtual bool write(const std::string& name,const std::string& data);
00190     virtual bool write(const std::string& name,const char* data);
00191     virtual bool write(const std::string& name,const double& data);
00192     virtual bool write(const std::string& name,const float& data);
00193     virtual bool write(const std::string& name,const int data);
00194     virtual bool write(const std::string& name,const unsigned int& data);
00195     virtual bool write(const std::string& name,const char& data);
00196     virtual bool write(const std::string& name,const byte& data);
00197     virtual bool write(const std::string& name,const ubyte& data);
00198     virtual bool write(const std::string& name,const bool data);
00199     virtual bool write(const std::string& name,const long& data);
00200     virtual bool write(const std::string& name,const unsigned long& data);
00201     virtual bool write(const std::string& name,const short& data);
00202     virtual bool write(const std::string& name,const unsigned short& data);
00203     //@}
00204 
00205     /**
00206      * \name Read methods for standard types
00207      */
00208     //@{
00209     /**
00210      * Read standard type
00211      */
00212     virtual bool read(std::string& data) = 0;
00213     virtual bool read(double& data) = 0;
00214     virtual bool read(float& data) = 0;
00215     virtual bool read(int& data) = 0;
00216     virtual bool read(unsigned int& data) = 0;
00217     virtual bool read(char& data) = 0;
00218     virtual bool read(byte& data) = 0;
00219     virtual bool read(ubyte& data) = 0;
00220     virtual bool read(bool& data) = 0;
00221     virtual bool read(long& data) = 0;
00222     virtual bool read(unsigned long& data) = 0;
00223     virtual bool read(short& data) = 0;
00224     virtual bool read(unsigned short& data) = 0;
00225     //@}
00226 
00227     /**
00228      * \name Read methods for symbol-data pairs
00229      */
00230     //@{
00231     /**
00232      * Read symbol-data pair
00233      */
00234     virtual bool read(const std::string& name, std::string& data);
00235     virtual bool read(const std::string& name, double& data);
00236     virtual bool read(const std::string& name, float& data);
00237     virtual bool read(const std::string& name, int& data);
00238     virtual bool read(const std::string& name, unsigned int& data);
00239     virtual bool read(const std::string& name, char& data);
00240     virtual bool read(const std::string& name, byte& data);
00241     virtual bool read(const std::string& name, ubyte& data);
00242     virtual bool read(const std::string& name, bool& data);
00243     virtual bool read(const std::string& name, long& data);
00244     virtual bool read(const std::string& name, unsigned long& data);
00245     virtual bool read(const std::string& name, short& data);
00246     virtual bool read(const std::string& name, unsigned short& data);
00247     //@}
00248 
00249     /**
00250      * Write a std::string as a symbol token.
00251      *
00252      * A symbol must not contain special characters or spaces.  Only letters
00253      * and numbers are allowed, and the first character must be a letter.
00254      *
00255      * @param data The symbol to be written
00256      */
00257     virtual bool writeSymbol(const std::string& data) = 0;
00258 
00259     /**
00260      * Write comment writes the input data without any preprocessing,
00261      * just ensuring that the comment format is given
00262      */
00263     virtual bool writeComment(const std::string& data) = 0;
00264 
00265     /**
00266      * Write comment writes the input data without any preprocessing,
00267      * just ensuring that the comment format is given
00268      */
00269     virtual bool writeComment(const char* data) = 0;
00270 
00271     /**
00272      * Read a std::string as a symbol token
00273      *
00274      * A symbol must not contain special characters or spaces.  Only letters
00275      * and numbers are allowed, and the first character must be a letter.
00276      *
00277      * @param data the symbol readed will be left here
00278      */
00279     virtual bool readSymbol(std::string& data) = 0;
00280 
00281     /**
00282      * Try to read the given symbol from the handler.
00283      * If present, returns true and the token is removed from the
00284      * handler, if not present returns false and leaves the handle as
00285      * it was before calling this member...
00286      *
00287      * A symbol must not contain special characters or spaces.  Only letters
00288      * and numbers are allowed, and the first character must be a letter.
00289      *
00290      * @param data the symbol to be readed
00291      */
00292     virtual bool trySymbol(const std::string& data) = 0;
00293 
00294     /**
00295      * Try to read the begin token from the handler.
00296      * If present, returns true and the token is removed from the
00297      * handler, if not present returns false and leaves the handle as
00298      * it was before calling this member...
00299      */
00300     virtual bool tryBegin() = 0;
00301 
00302     /**
00303      * Try to read the end token from the handler.
00304      * If present, returns true and the token is removed from the
00305      * handler, if not present returns false and leaves the handle as
00306      * it was before calling this member...
00307      */
00308     virtual bool tryEnd() = 0;
00309 
00310     /**
00311      * Write spaces (default value 1)
00312      * The spaces are tokes that will be ignored by reading
00313      * @param s the number of space-tokens to be written
00314      */
00315     virtual bool writeSpaces(const int s=1) = 0;
00316 
00317     /**
00318      * Write end-of-line token
00319      * The EOL are tokes that will be ignored by reading
00320      */
00321     virtual bool writeEOL() = 0;
00322 
00323     /**
00324      * Write key/value separator.
00325      * The key/value separator is the token which will separate a symbol from
00326      * its value in a symbol-data-pair.
00327      */
00328     virtual bool writeKeyValueSeparator() = 0;
00329 
00330     /**
00331      * Write inter-data separator.
00332      * The data-separator token separates data elements in a data list or array
00333      */
00334     virtual bool writeDataSeparator() = 0;
00335 
00336     /**
00337      * Read key/value separator token.
00338      */
00339     virtual bool readKeyValueSeparator() = 0;
00340 
00341     /**
00342      * Read inter-data separator token.
00343      */
00344     virtual bool readDataSeparator() = 0;
00345 
00346     /**
00347      * Return the actual nesting level for begins and ends.
00348      *
00349      * Example
00350      * \code
00351      * ioHandler_childClass h; // h is an ioHandler of a childClass
00352      *                         // this could be for example a lispStreamHandler
00353      * int i;
00354      * i = h.getLevel(); // returns 0;
00355      * h.writeBegin();
00356      * i = h.getLevel(); // returns 1;
00357      * h.writeBegin();
00358      * i = h.getLevel(); // returns 2;
00359      * h.writeEnd();
00360      * i = h.getLevel(); // returns 1;
00361      * h.writeEnd();
00362      * i = h.getLevel(); // returns 0;
00363      * \endcode
00364      */
00365     virtual const int& getLevel() const;
00366 
00367     /**
00368      * Restore all the information in the handler taken in the actual
00369      * level.
00370      */
00371     virtual bool restoreLevel() = 0;
00372 
00373     /**
00374      * Usually this member is needed by reinitialization routines to set
00375      * the begin/end nesting level to zero.
00376      */
00377     virtual void resetLevel(const int theLevel = 0);
00378 
00379     /**
00380      * Append contextual information
00381      *
00382      * This function should help the users to find errors in their files.
00383      * It just inserts some contextual information into the status string
00384      * to help localizing wrong data.
00385      *
00386      * It is useful for streams that can be edited by hand, because the
00387      * users will make errors!
00388      *
00389      * The default behaviour does nothing.
00390      */
00391     virtual void appendContextStatus() const;
00392 
00393 
00394   protected:
00395     /**
00396      * begin() nesting level
00397      */
00398     int level_;
00399 
00400   };
00401 
00402   /**
00403    * \name Read functions for standard types
00404    */
00405   //@{
00406   bool read(ioHandler& handler,std::string& data);
00407   bool read(ioHandler& handler,double& data);
00408   bool read(ioHandler& handler,float& data);
00409   bool read(ioHandler& handler,int& data);
00410   bool read(ioHandler& handler,unsigned int& data);
00411   bool read(ioHandler& handler,char& data);
00412   bool read(ioHandler& handler,byte& data);
00413   bool read(ioHandler& handler,ubyte& data);
00414   bool read(ioHandler& handler,bool& data);
00415   bool read(ioHandler& handler,long& data);
00416   bool read(ioHandler& handler,unsigned long& data);
00417   bool read(ioHandler& handler,short& data);
00418   bool read(ioHandler& handler,unsigned short& data);
00419   //@}
00420 
00421   /**
00422    * \name Write functions for standard types
00423    */
00424   //@{
00425   bool write(ioHandler& handler,const std::string& data);
00426   bool write(ioHandler& handler,const char* data);
00427   bool write(ioHandler& handler,const double& data);
00428   bool write(ioHandler& handler,const float& data);
00429   bool write(ioHandler& handler, const int data);
00430   bool write(ioHandler& handler,const unsigned int& data);
00431   bool write(ioHandler& handler,const char& data);
00432   bool write(ioHandler& handler,const byte& data);
00433   bool write(ioHandler& handler,const ubyte& data);
00434   bool write(ioHandler& handler,const bool data);
00435   bool write(ioHandler& handler,const long& data);
00436   bool write(ioHandler& handler,const unsigned long& data);
00437   bool write(ioHandler& handler,const short& data);
00438   bool write(ioHandler& handler,const unsigned short& data);
00439   //@}
00440 
00441 
00442 
00443   /**
00444    * @name Read functions with standard storable interface
00445    */
00446   //@{
00447   // Implementation note: The non-template overloads are needed since
00448   // otherwise these templates get instantiated a lot in the parameters IO
00449   // functions
00450   bool read(ioHandler& handler, const std::string& name,
00451             std::string& data, const bool complete=true);
00452   bool read(ioHandler& handler, const std::string& name,
00453             double& data, const bool complete=true);
00454   bool read(ioHandler& handler, const std::string& name,
00455             float& data, const bool complete=true);
00456   bool read(ioHandler& handler, const std::string& name,
00457             int& data, const bool complete=true);
00458   bool read(ioHandler& handler, const std::string& name,
00459             unsigned int& data, const bool complete=true);
00460   bool read(ioHandler& handler, const std::string& name,
00461             char& data, const bool complete=true);
00462   bool read(ioHandler& handler, const std::string& name,
00463             byte& data, const bool complete=true);
00464   bool read(ioHandler& handler, const std::string& name,
00465             ubyte& data, const bool complete=true);
00466   bool read(ioHandler& handler, const std::string& name,
00467             bool& data, const bool complete=true);
00468   bool read(ioHandler& handler, const std::string& name,
00469             long& data, const bool complete=true);
00470   bool read(ioHandler& handler, const std::string& name,
00471             unsigned long& data, const bool complete=true);
00472   bool read(ioHandler& handler, const std::string& name,
00473             short& data, const bool complete=true);
00474   bool read(ioHandler& handler, const std::string& name,
00475             unsigned short& data, const bool complete=true);
00476 
00477   template <class T>
00478   bool read(ioHandler& handler,const std::string& name,
00479             T& data,const bool complete=true);
00480   //@}
00481 
00482   /**
00483    * @ name Write functions with standard storable interface
00484    */
00485   //@{
00486   // Implementation note: The non-template overloads are needed since
00487   // otherwise these templates get instantiated a lot in the parameters IO
00488   // functions
00489   bool write(ioHandler& handler, const std::string& name,
00490              const std::string& data, const bool complete=true);
00491   bool write(ioHandler& handler, const std::string& name,
00492              const char* data, const bool complete=true);
00493   bool write(ioHandler& handler, const std::string& name,
00494              const double& data, const bool complete=true);
00495   bool write(ioHandler& handler, const std::string& name,
00496              const float& data, const bool complete=true);
00497   bool write(ioHandler& handler, const std::string& name,
00498              const int data, const bool complete=true);
00499   bool write(ioHandler& handler, const std::string& name,
00500              const unsigned int& data, const bool complete=true);
00501   bool write(ioHandler& handler, const std::string& name,
00502              const char& data, const bool complete=true);
00503   bool write(ioHandler& handler, const std::string& name,
00504              const byte& data, const bool complete=true);
00505   bool write(ioHandler& handler, const std::string& name,
00506              const ubyte& data, const bool complete=true);
00507   bool write(ioHandler& handler, const std::string& name,
00508              const bool data, const bool complete=true);
00509   bool write(ioHandler& handler, const std::string& name,
00510              const long& data, const bool complete=true);
00511   bool write(ioHandler& handler, const std::string& name,
00512              const unsigned long& data, const bool complete=true);
00513   bool write(ioHandler& handler, const std::string& name,
00514              const short& data, const bool complete=true);
00515   bool write(ioHandler& handler, const std::string& name,
00516              const unsigned short& data, const bool complete=true);
00517 
00518   template <class T>
00519   bool write(ioHandler& handler,const std::string& name,
00520              const T& data,const bool complete=true);
00521   //@}
00522 
00523 }
00524 
00525 #include "cvrIoHandler_template.h"
00526 
00527 #endif
00528 

Generated on Sun Sep 20 22:07:59 2009 for CVR-Lib by Doxygen 1.5.8