Home  · Classes  · Annotated Classes  · Modules  · Members  · Namespaces  · Related Pages

Map.h

Go to the documentation of this file.
00001 // --------------------------------------------------------------------------
00002 //                   OpenMS -- Open-Source Mass Spectrometry
00003 // --------------------------------------------------------------------------
00004 // Copyright The OpenMS Team -- Eberhard Karls University Tuebingen,
00005 // ETH Zurich, and Freie Universitaet Berlin 2002-2012.
00006 //
00007 // This software is released under a three-clause BSD license:
00008 //  * Redistributions of source code must retain the above copyright
00009 //    notice, this list of conditions and the following disclaimer.
00010 //  * Redistributions in binary form must reproduce the above copyright
00011 //    notice, this list of conditions and the following disclaimer in the
00012 //    documentation and/or other materials provided with the distribution.
00013 //  * Neither the name of any author or any participating institution
00014 //    may be used to endorse or promote products derived from this software
00015 //    without specific prior written permission.
00016 // For a full list of authors, refer to the file AUTHORS.
00017 // --------------------------------------------------------------------------
00018 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00019 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00020 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00021 // ARE DISCLAIMED. IN NO EVENT SHALL ANY OF THE AUTHORS OR THE CONTRIBUTING
00022 // INSTITUTIONS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00023 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00024 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
00025 // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
00026 // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
00027 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
00028 // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029 //
00030 // --------------------------------------------------------------------------
00031 // $Maintainer: Stephan Aiche$
00032 // $Authors: Marc Sturm $
00033 // --------------------------------------------------------------------------
00034 
00035 #ifndef OPENMS_DATASTRUCTURES_MAP_H
00036 #define OPENMS_DATASTRUCTURES_MAP_H
00037 
00038 #include <OpenMS/CONCEPT/Exception.h>
00039 
00040 #include <map>
00041 
00042 namespace OpenMS
00043 {
00049   template <class Key, class T>
00050   class Map :
00051     public std::map<Key, T>
00052   {
00053 public:
00054 
00063     class IllegalKey :
00064       public Exception::BaseException
00065     {
00066 public:
00067       IllegalKey(const char * file, int line, const char * function) :
00068         Exception::BaseException(file, line, function)
00069       {
00070       }
00071 
00072     };
00073 
00075 
00076     typedef std::map<Key, T> Base;
00077     typedef typename Base::value_type ValueType;
00078     typedef Key KeyType;
00079     typedef typename Base::value_type * PointerType;
00080     typedef typename Base::iterator Iterator;
00081     typedef typename Base::const_iterator ConstIterator;
00082     typedef typename Base::reverse_iterator ReverseIterator;
00083     typedef typename Base::const_reverse_iterator ConstReverseIterator;
00085 
00087     inline bool has(const Key & key) const
00088     {
00089       return Base::find(key) != Base::end();
00090     }
00091 
00097     const T & operator[](const Key & key) const;
00098 
00100     T & operator[](const Key & key);
00101   };
00102 
00103   //******************************************************************************************
00104   // Implementations of template methods
00105   //******************************************************************************************
00106 
00107   template <class Key, class T>
00108   const T & Map<Key, T>::operator[](const Key & key) const
00109   {
00110     ConstIterator it = this->find(key);
00111     if (it == Base::end())
00112     {
00113       throw IllegalKey(__FILE__, __LINE__, __PRETTY_FUNCTION__);
00114     }
00115     else
00116     {
00117       return it->second;
00118     }
00119   }
00120 
00121   template <class Key, class T>
00122   T & Map<Key, T>::operator[](const Key & key)
00123   {
00124     Iterator it = this->find(key);
00125     if (it == Base::end())
00126     {
00127       it = this->insert(ValueType(key, T())).first;
00128     }
00129     return it->second;
00130   }
00131 
00132 } // namespace OPENMS
00133 
00134 #endif // OPENMS_DATASTRUCTURES_MAP_H

OpenMS / TOPP release 1.10.0 Documentation generated on Thu Mar 7 2013 09:42:41 using doxygen 1.7.1