iostream_ext.h

Go to the documentation of this file.
00001 /* ========================================================================
00002  * Copyright (c) 2006,
00003  * Institute for Computer Graphics and Vision
00004  * Graz University of Technology
00005  * All rights reserved.
00006  *
00007  * Redistribution and use in source and binary forms, with or without
00008  * modification, are permitted provided that the following conditions are
00009  * met:
00010  *
00011  * Redistributions of source code must retain the above copyright notice,
00012  * this list of conditions and the following disclaimer.
00013  *
00014  * Redistributions in binary form must reproduce the above copyright
00015  * notice, this list of conditions and the following disclaimer in the
00016  * documentation and/or other materials provided with the distribution.
00017  *
00018  * Neither the name of the Graz University of Technology nor the names of
00019  * its contributors may be used to endorse or promote products derived from
00020  * this software without specific prior written permission.
00021  *
00022  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
00023  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
00024  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
00025  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
00026  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00027  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00028  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00029  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00030  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00031  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00032  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00033  * ========================================================================
00034  * PROJECT: OpenTracker
00035  * ======================================================================== */
00042 /* ======================================================================= */
00043 
00044 #ifndef IOSTREAM_EXT_HEADER
00045 #define IOSTREAM_EXT_HEADER
00046 
00047 #include <iostream>
00048 #include <sstream>
00049 #include <vector>
00050 #include <list>
00051 
00052 namespace ot
00053 {
00060     template <typename T>
00061     std::ostream& operator<<(std::ostream& os, const std::vector<T>& object)
00062     {
00063         typename std::vector<T>::const_iterator it;
00064 
00065         os << "[" << object.size() << ":";
00066         for (it = object.begin(); it != object.end(); it++)
00067         {
00068             if (it != object.begin())
00069                 os << ";";
00070             os << *it;
00071         }
00072         os << "]";
00073         return os;
00074     };
00081     template <typename T>
00082     std::ostream& operator<<(std::ostream& os, const std::list<T>& object)
00083     {
00084         typename std::list<T>::const_iterator it;
00085 
00086         os << object.size() << ":";
00087         for (it = object.begin(); it != object.end(); it++)
00088         {
00089             if (it != object.begin())
00090                 os << ";";
00091             os << *it;
00092         }
00093         os << ".";
00094         return os;
00095     };
00102     template <typename T>
00103     std::istream& operator>>(std::istream& is, std::vector<T>& object)
00104     {
00105         typename std::vector<T>::size_type n;
00106         typename std::vector<T>::size_type i;
00107         char c;
00108 
00109         object.clear();
00110 
00111         // read "["
00112         if (!(is >> c))
00113             return is;
00114         if (c != '[')
00115         {
00116             is.setstate(std::ios_base::failbit);
00117             return is;
00118         }
00119 
00120         // read size
00121         if (!(is >> n))
00122             return is;
00123         object.reserve(n);
00124 
00125         // read ":"
00126         if (!(is >> c))
00127             return is;
00128         if (c != ':')
00129         {
00130             is.setstate(std::ios_base::failbit);
00131             return is;
00132         }
00133 
00134         // read data
00135         for (i = 0; i < n; i++)
00136         {
00137             T t;
00138             is >> t;
00139             object.push_back(t);
00140             // read ";" or "]"
00141             if (!(is >> c))
00142                 return is;
00143             if (c != ';' && c != ']')
00144             {
00145                 is.setstate(std::ios_base::failbit);
00146                 return is;
00147             }
00148         }
00149 
00150         return is;
00151     };
00158     template <typename T>
00159     std::istream& operator>>(std::istream& is, std::list<T>& object)
00160     {
00161         typename std::list<T>::size_type n;
00162         typename std::list<T>::size_type i;
00163         char c;
00164 
00165         object.clear();
00166 
00167         // read size
00168         if (!(is >> n))
00169             return is;
00170 
00171         // read ":"
00172         if (!(is >> c))
00173             return is;
00174         if (c != ':')
00175         {
00176             is.setstate(std::ios_base::failbit);
00177             return is;
00178         }
00179 
00180         // read data
00181         for (i = 0; i < n; i++)
00182         {
00183             T t;
00184             is >> t;
00185             object.push_back(t);
00186             // read ";"
00187             if (!(is >> c))
00188                 return is;
00189             if (c != ';' && c!= '.')
00190             {
00191                 is.setstate(std::ios_base::failbit);
00192                 return is;
00193             }
00194         }
00195 
00196         return is;
00197     };
00198 
00199 } // namespace ot
00200 
00201 #endif
00202 
00203 /* 
00204  * ------------------------------------------------------------
00205  *   End of iostream_ext.h
00206  * ------------------------------------------------------------
00207  *   Automatic Emacs configuration follows.
00208  *   Local Variables:
00209  *   mode:c++
00210  *   c-basic-offset: 4
00211  *   eval: (c-set-offset 'substatement-open 0)
00212  *   eval: (c-set-offset 'case-label '+)
00213  *   eval: (c-set-offset 'statement 'c-lineup-runin-statements)
00214  *   eval: (setq indent-tabs-mode nil)
00215  *   End:
00216  * ------------------------------------------------------------ 
00217  */

Generated on Wed Feb 28 15:18:48 2007 for NaviTrack by  doxygen 1.4.6