Main Page   Class Hierarchy   Alphabetical List   Data Structures   File List   Data Fields  

stltools.Map.h

00001 /*----------------------------------------------------------------------------
00002   SWORD 2000 - Software With Objects for Rapid Development
00003   Copyright (C) 2003 Eric NICOLAS
00004   ----------------------------------------------------------------------------
00005   SWORD is free software; you can redistribute it and/or modify
00006   it under the terms of the GNU Lesser General Public License as published by
00007   the Free Software Foundation; either version 2 of the License, or
00008   (at your option) any later version.
00009 
00010   SWORD is distributed in the hope that it will be useful,
00011   but WITHOUT ANY WARRANTY; without even the implied warranty of
00012   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013   GNU Lesser General Public License for more details.
00014 
00015   You should have received a copy of the GNU Lesser General Public License
00016   along with SWORD; if not, write to the Free Software
00017   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018   --------------------------------------------------------------------------*/
00019 #ifndef _SWORD_STLTOOLS_MAP_
00020 #define _SWORD_STLTOOLS_MAP_
00021 
00022 #include <map>
00023 #include "sword/streams.OStream.h"
00024 #include "sword/streams.IStream.h"
00025 
00026 namespace std {
00027 
00028         // Implementation for : "cout << map<IDX,T>"
00029 
00030         template<class IDX, class T>
00031         ostream& operator<<(ostream& lhs, const std::map<IDX,T>& rhs)
00032         {
00033                 if (rhs.empty()) lhs << "<empty>";
00034                 else
00035                 {
00036                         lhs << "<";
00037                         for(std::map<IDX,T>::const_iterator i=rhs.begin();i!=rhs.end();++i)
00038                         {
00039                                 if (i!=rhs.begin()) lhs << ", ";
00040                                 lhs << i->first << "=>" << i->second;
00041                         }
00042                         lhs << ">";
00043                 }
00044                 return lhs;
00045         }
00046 
00047         // Implementation for : "OStream << map<IDX,T>"
00048 
00049         template<class K, class V>
00050   sword::OStream& operator<<(sword::OStream& lhs, std::map<K, V>& rhs)
00051         {
00052                 lhs << rhs.size();
00053                 for (std::map<K, V>::const_iterator it = rhs.begin(); it != rhs.end(); ++it)
00054                 {
00055                         lhs << it->first;
00056                         lhs << it->second;
00057                 }
00058                 return lhs;
00059         }
00060 
00061         // Implementation for : "IStream << map<IDX,T>"
00062 
00063         template<class K, class V>
00064         sword::IStream& operator>>(sword::IStream& lhs, std::map<K, V>& rhs)
00065         {
00066                 size_t size;
00067                 lhs >> size;
00068                 for(size_t i=0; i<size; ++i)
00069                 {
00070                         K key;
00071                         V value;
00072                         lhs >> key;
00073                         lhs >> value;
00074                         rhs[key] = value;
00075                 }
00076                 return lhs;
00077         }
00078 
00079   // in<> on std::map
00080 
00081         template<class K, class V>
00082   bool in(const K& value, const std::map<K, V>& v)
00083   {
00084     return v.find(value) != v.end();
00085   }
00086 
00087 } // namespace std
00088 
00089 #endif // _SWORD_STLTOOLS_MAP_

Generated on Tue Dec 23 20:08:56 2003 for SWORD by doxygen1.3-rc2