Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members  

streamable_map.hh

00001 /*
00002 *  Name:      streamable_map.hh
00003 *  Author:    Rafael Jesus Alcantara Perez
00004 *  Summary:   Map class extended with stream capabilities
00005 *  Date:      $Date: 2003/04/14 00:18:31 $
00006 *  Revision:  $Revision: 1.1 $
00007 *
00008 *  Copyright (C) 1999-2002  Rafael Jesus Alcantara Perez <rafa@dedalo-ing.com>
00009 *
00010 *  This program is free software; you can redistribute it and/or modify
00011 *  it under the terms of the GNU General Public License as published by
00012 *  the Free Software Foundation; either version 2 of the License, or
00013 *  (at your option) any later version.
00014 *
00015 *  This program is distributed in the hope that it will be useful,
00016 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 *  GNU General Public License for more details.
00019 *
00020 *  You should have received a copy of the GNU General Public License
00021 *  along with this program; if not, write to the Free Software
00022 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston,
00023 *  MA 02111-1307, USA.
00024 */
00025 
00026 #ifndef _MPCL_IO_STREAMABLE_MAP__
00027 #define _MPCL_IO_STREAMABLE_MAP__
00028 
00029 #include "../util/collection/map.hh"
00030 #include "streamable.hh"
00031 
00032 
00034 namespace mpcl
00035 {
00036 
00038   namespace io
00039   {
00040 
00041     using util::collection::TMap;
00042 
00048     template < typename TKey                                   ,
00049                typename TItem                                  ,
00050                typename TCompare       = std::less<TKey>       ,
00051                typename TItemAllocator = std::allocator<TItem> >
00052     class TStreamableMap                                 :
00053       public TMap<TKey, TItem, TCompare, TItemAllocator> ,
00054       public virtual IStreamable<>
00055     {
00056 
00057       public:
00058 
00059         typedef
00060           typename std::map<TKey, TItem, TCompare, TItemAllocator>::allocator_type
00061           allocator_type;
00062 
00063         typedef
00064           typename std::map<TKey, TItem, TCompare, TItemAllocator>::const_iterator
00065           const_iterator;
00066 
00067         typedef
00068           typename std::map<TKey, TItem, TCompare, TItemAllocator>::const_pointer
00069           const_pointer;
00070 
00071         typedef
00072           typename std::map<TKey, TItem, TCompare, TItemAllocator>::const_reference
00073           const_reference;
00074 
00075         typedef
00076           typename std::map<TKey, TItem, TCompare, TItemAllocator>::const_reverse_iterator
00077           const_reverse_iterator;
00078 
00079         typedef
00080           typename std::map<TKey, TItem, TCompare, TItemAllocator>::difference_type
00081           difference_type;
00082 
00083         typedef
00084           typename std::map<TKey, TItem, TCompare, TItemAllocator>::iterator
00085           iterator;
00086 
00087         typedef
00088           typename std::map<TKey, TItem, TCompare, TItemAllocator>::key_compare
00089           key_compare;
00090 
00091         typedef
00092           typename std::map<TKey, TItem, TCompare, TItemAllocator>::key_type
00093           key_type;
00094 
00095         typedef
00096           typename std::map<TKey, TItem, TCompare, TItemAllocator>::mapped_type
00097           mapped_type;
00098 
00099         typedef
00100           typename std::map<TKey, TItem, TCompare, TItemAllocator>::pointer
00101           pointer;
00102 
00103         typedef
00104           typename std::map<TKey, TItem, TCompare, TItemAllocator>::reference
00105           reference;
00106 
00107         typedef
00108           typename std::map<TKey, TItem, TCompare, TItemAllocator>::reverse_iterator
00109           reverse_iterator;
00110 
00111         typedef
00112           typename std::map<TKey, TItem, TCompare, TItemAllocator>::size_type
00113           size_type;
00114 
00115         typedef
00116           typename std::map<TKey, TItem, TCompare, TItemAllocator>::value_type
00117           value_type;
00118 
00119 
00120       public:
00121 
00122         //
00123         //  C O N S T R U C T O R S
00124         //
00125 
00127         TStreamableMap (void)                            :
00128           TMap<TKey, TItem, TCompare, TItemAllocator> () ,
00129           IStreamable<>                               () {}
00130 
00132         explicit TStreamableMap (const TCompare& rktCOMPARE)       :
00133           TMap<TKey, TItem, TCompare, TItemAllocator> (rktCOMPARE) ,
00134           IStreamable<>                               ()           {}
00135 
00137         template <typename InputIterator>
00138         TStreamableMap (InputIterator tBEGIN, InputIterator tEND)    :
00139           TMap<TKey, TItem, TCompare, TItemAllocator> (tBEGIN, tEND) ,
00140           IStreamable<>                               ()             {}
00141 
00143         template <typename InputIterator>
00144         TStreamableMap ( InputIterator   tBEGIN     ,
00145                          InputIterator   tEND       ,
00146                          const TCompare& rktCOMPARE )                            :
00147           TMap<TKey, TItem, TCompare, TItemAllocator> (tBEGIN, tEND, rktCOMPARE) ,
00148           IStreamable<>                               ()                         {}
00149 
00151         TStreamableMap (const std::map<TKey, TItem, TCompare, TItemAllocator>& rktSOURCE_MAP) :
00152           TMap<TKey, TItem, TCompare, TItemAllocator> (rktSOURCE_MAP)                         ,
00153           IStreamable<>                               ()                                      {}
00154 
00160         TStreamableMap<TKey, TItem, TCompare, TItemAllocator>&
00161         operator = (const TStreamableMap<TKey, TItem, TCompare, TItemAllocator>& rktSOURCE_MAP)
00162         {
00163           TMap<TKey, TItem, TCompare, TItemAllocator>::operator = (rktSOURCE_MAP);
00164           return *this;
00165         }
00166 
00167         void write (std::basic_ostream<char_type, traits_type>& rtTARGET_OSTREAM) const
00168         {
00169           const_iterator   I     = begin();
00170           const_iterator   ktEnd = end();
00171 
00172           for (; ( I != ktEnd ) ;++I)
00173           {
00174             rtTARGET_OSTREAM << I->first;
00175             rtTARGET_OSTREAM << I->second;
00176           }
00177         }
00178 
00179 
00180       public:
00181 
00182         //
00183         //  S E L E C T O R S
00184         //
00185 
00186         void read (std::basic_istream<char_type, traits_type>& rtSOURCE_ISTREAM)
00187         {
00188           TKey    tInputKey;
00189           TItem   tInputItem;
00190 
00191           clear();
00192           while ( rtSOURCE_ISTREAM >> tInputKey )
00193           {
00194             if ( rtSOURCE_ISTREAM >> tInputItem )
00195             {
00196               insert (std::make_pair (tInputKey, tInputItem));
00197             }
00198           }
00199           if ( empty() )
00200           {
00201             rtSOURCE_ISTREAM.setstate (std::ios::failbit);
00202           }
00203           else
00204           {
00205             rtSOURCE_ISTREAM.clear();
00206           }
00207         }
00208 
00209     };  // class TStreamableMap
00210 
00211   }  // namespace io
00212 
00213 }  // namespace mpcl
00214 
00215 
00216 #endif  // not _MPCL_IO_STREAMABLE_MAP__

Generated on Mon Oct 13 02:35:24 2003 for MPCL by doxygen1.2.18