00001 /*========================================================================= 00002 00003 Program: Medical Imaging & Interaction Toolkit 00004 Language: C++ 00005 Date: $Date: 2008-06-24 19:37:48 +0200 (Di, 24 Jun 2008) $ 00006 Version: $Revision: 14641 $ 00007 00008 Copyright (c) German Cancer Research Center, Division of Medical and 00009 Biological Informatics. All rights reserved. 00010 See MITKCopyright.txt or https://www.mitk.org/copyright.html for details. 00011 00012 This software is distributed WITHOUT ANY WARRANTY; without even 00013 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 00014 PURPOSE. See the above copyright notices for more information. 00015 00016 =========================================================================*/ 00017 00018 00019 #ifndef MITKGENERICLOOKUPTABLE_H_HEADER_INCLUDED_C1061CEE 00020 #define MITKGENERICLOOKUPTABLE_H_HEADER_INCLUDED_C1061CEE 00021 00022 #include <string> 00023 #include <sstream> 00024 #include <stdlib.h> 00025 #include <map> 00026 00027 #include <itkDataObject.h> 00028 00029 #include "mitkVector.h" 00030 #include "mitkCommon.h" 00031 00032 00033 namespace mitk { 00034 00047 template <typename T> 00048 class GenericLookupTable 00049 { 00050 public: 00051 typedef unsigned int IdentifierType; 00052 typedef T ValueType; 00053 typedef std::map< IdentifierType, ValueType > LookupTableType; 00054 00055 typedef GenericLookupTable Self; 00056 00057 GenericLookupTable() {} 00058 virtual ~GenericLookupTable() 00059 { 00060 } 00061 00062 virtual const char *GetNameOfClass() const 00063 { 00064 return "GenericLookupTable"; 00065 } 00066 00067 void SetTableValue( IdentifierType id, ValueType value ) 00068 { 00069 m_LookupTable[id] = value; 00070 } 00071 00072 bool ValueExists(IdentifierType id) const 00073 { 00074 typename LookupTableType::const_iterator it = m_LookupTable.find(id); 00075 return (it != m_LookupTable.end()); 00076 } 00077 00078 ValueType GetTableValue( IdentifierType id ) const 00079 { 00080 typename LookupTableType::const_iterator it = m_LookupTable.find(id); 00081 if (it != m_LookupTable.end()) 00082 return it->second; 00083 else 00084 throw std::range_error("id does not exist in the lookup table"); 00085 } 00086 00087 const LookupTableType& GetLookupTable() const 00088 { 00089 return m_LookupTable; 00090 } 00091 00092 bool operator==( const Self& lookupTable ) const 00093 { 00094 return (m_LookupTable == lookupTable.m_LookupTable); 00095 } 00096 bool operator!=( const Self& lookupTable ) const 00097 { 00098 return !(m_LookupTable == lookupTable.m_LookupTable); 00099 } 00100 00101 virtual Self& operator=(const Self& other) // \TODO: this needs to be unit tested! 00102 { 00103 if ( this == &other ) 00104 { 00105 return *this; 00106 } 00107 else 00108 { 00109 m_LookupTable.clear(); 00110 m_LookupTable = other.m_LookupTable; 00111 return *this; 00112 } 00113 } 00114 protected: 00115 LookupTableType m_LookupTable; 00116 }; 00117 } // namespace mitk 00118 00126 #define mitkSpecializeGenericLookupTable(LookupTableName,Type) \ 00127 class MITK_CORE_EXPORT LookupTableName: public GenericLookupTable< Type > \ 00128 { \ 00129 public: \ 00130 typedef LookupTableName Self; \ 00131 typedef GenericLookupTable< Type > Superclass; \ 00132 virtual const char *GetNameOfClass() const \ 00133 {return #LookupTableName;} \ 00134 LookupTableName() {} \ 00135 virtual ~LookupTableName() {} \ 00136 }; \ 00137 std::ostream& operator<<(std::ostream& stream, const LookupTableName& /*l*/); 00138 00144 #define mitkSpecializeGenericLookupTableOperator(LookupTableName) \ 00145 std::ostream& mitk::operator<<(std::ostream& stream, const LookupTableName& /*l*/) \ 00146 { \ 00147 return stream; \ 00148 }; 00149 #endif