Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
mitkGenericLookupTable.h
Go to the documentation of this file.
1 /*===================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center,
6 Division of Medical and Biological Informatics.
7 All rights reserved.
8 
9 This software is distributed WITHOUT ANY WARRANTY; without
10 even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE.
12 
13 See LICENSE.txt or http://www.mitk.org for details.
14 
15 ===================================================================*/
16 
17 #ifndef MITKGENERICLOOKUPTABLE_H_HEADER_INCLUDED_C1061CEE
18 #define MITKGENERICLOOKUPTABLE_H_HEADER_INCLUDED_C1061CEE
19 
20 #include <map>
21 #include <sstream>
22 #include <stdlib.h>
23 #include <string>
24 
25 #include <itkDataObject.h>
26 
27 #include "mitkNumericTypes.h"
28 #include <MitkCoreExports.h>
29 
30 namespace mitk
31 {
44  template <typename T>
46  {
47  public:
48  typedef unsigned int IdentifierType;
49  typedef T ValueType;
50  typedef std::map<IdentifierType, ValueType> LookupTableType;
51 
53 
55  virtual ~GenericLookupTable() {}
56  virtual const char *GetNameOfClass() const { return "GenericLookupTable"; }
57  void SetTableValue(IdentifierType id, ValueType value) { m_LookupTable[id] = value; }
58  bool ValueExists(IdentifierType id) const
59  {
60  auto it = m_LookupTable.find(id);
61  return (it != m_LookupTable.end());
62  }
63 
64  ValueType GetTableValue(IdentifierType id) const
65  {
66  auto it = m_LookupTable.find(id);
67  if (it != m_LookupTable.end())
68  return it->second;
69  else
70  throw std::range_error("id does not exist in the lookup table");
71  }
72 
73  const LookupTableType &GetLookupTable() const { return m_LookupTable; }
74  bool operator==(const Self &lookupTable) const { return (m_LookupTable == lookupTable.m_LookupTable); }
75  bool operator!=(const Self &lookupTable) const { return !(m_LookupTable == lookupTable.m_LookupTable); }
76  virtual Self &operator=(const Self &other) // \TODO: this needs to be unit tested!
77  {
78  if (this == &other)
79  {
80  return *this;
81  }
82  else
83  {
84  m_LookupTable.clear();
86  return *this;
87  }
88  }
89 
90  protected:
91  LookupTableType m_LookupTable;
92  };
93 } // namespace mitk
94 
102 #define mitkSpecializeGenericLookupTable(LookupTableName, Type) \
103  \
104  class MITKCORE_EXPORT LookupTableName : public GenericLookupTable<Type> \
105  \
106  { \
107  public: \
108  typedef LookupTableName Self; \
109  typedef GenericLookupTable<Type> Superclass; \
110  virtual const char *GetNameOfClass() const { return #LookupTableName; } \
111  LookupTableName() {} \
112  virtual Superclass &operator=(const Superclass &other) { return Superclass::operator=(other); } \
113  virtual ~LookupTableName() {} \
114  }; \
115  \
116  MITKCORE_EXPORT std::ostream &operator<<(std::ostream &stream, const LookupTableName & /*l*/);
117 
123 #define mitkSpecializeGenericLookupTableOperator(LookupTableName) \
124  \
125  std::ostream &mitk::operator<<(std::ostream &stream, const LookupTableName &l) \
126  \
127  { \
128  typedef LookupTableName::LookupTableType::const_iterator IterType; \
129  IterType e = l.GetLookupTable().end(); \
130  IterType b = l.GetLookupTable().begin(); \
131  stream << "["; \
132  for (IterType i = b; i != e; ++i) \
133  { \
134  if (i != b) \
135  { \
136  stream << ", "; \
137  } \
138  stream << i->first << " -> " << i->second; \
139  } \
140  return stream << "]"; \
141  };
142 #endif
virtual const char * GetNameOfClass() const
bool operator==(const Self &lookupTable) const
std::map< IdentifierType, ValueType > LookupTableType
virtual Self & operator=(const Self &other)
void SetTableValue(IdentifierType id, ValueType value)
bool operator!=(const Self &lookupTable) const
DataCollection - Class to facilitate loading/accessing structured data.
ValueType GetTableValue(IdentifierType id) const
Template class for generating lookup-tables.
bool ValueExists(IdentifierType id) const
const LookupTableType & GetLookupTable() const