Medical Imaging Interaction Toolkit  2023.04.00
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 (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
10 
11 ============================================================================*/
12 
13 #ifndef mitkGenericLookupTable_h
14 #define mitkGenericLookupTable_h
15 
16 #include <map>
17 #include <sstream>
18 #include <cstdlib>
19 #include <string>
20 
21 #include <itkDataObject.h>
22 
23 #include "mitkNumericTypes.h"
24 #include <MitkCoreExports.h>
25 
26 namespace mitk
27 {
40  template <typename T>
42  {
43  public:
44  typedef unsigned int IdentifierType;
45  typedef T ValueType;
46  typedef std::map<IdentifierType, ValueType> LookupTableType;
47 
49 
51  virtual ~GenericLookupTable() {}
52  virtual const char *GetNameOfClass() const { return "GenericLookupTable"; }
53  void SetTableValue(IdentifierType id, ValueType value) { m_LookupTable[id] = value; }
54  bool ValueExists(IdentifierType id) const
55  {
56  auto it = m_LookupTable.find(id);
57  return (it != m_LookupTable.end());
58  }
59 
61  {
62  auto it = m_LookupTable.find(id);
63  if (it != m_LookupTable.end())
64  return it->second;
65  else
66  throw std::range_error("id does not exist in the lookup table");
67  }
68 
69  const LookupTableType &GetLookupTable() const { return m_LookupTable; }
70  bool operator==(const Self &lookupTable) const { return (m_LookupTable == lookupTable.m_LookupTable); }
71  bool operator!=(const Self &lookupTable) const { return !(m_LookupTable == lookupTable.m_LookupTable); }
72  virtual Self &operator=(const Self &other) // \TODO: this needs to be unit tested!
73  {
74  if (this == &other)
75  {
76  return *this;
77  }
78  else
79  {
80  m_LookupTable.clear();
82  return *this;
83  }
84  }
85 
86  protected:
88  };
89 } // namespace mitk
90 
98 #define mitkSpecializeGenericLookupTable(LookupTableName, Type) \
99  \
100  class MITKCORE_EXPORT LookupTableName : public GenericLookupTable<Type> \
101  \
102  { \
103  public: \
104  typedef LookupTableName Self; \
105  typedef GenericLookupTable<Type> Superclass; \
106  virtual const char *GetNameOfClass() const { return #LookupTableName; } \
107  LookupTableName() {} \
108  virtual Superclass &operator=(const Superclass &other) { return Superclass::operator=(other); } \
109  virtual ~LookupTableName() {} \
110  }; \
111  \
112  MITKCORE_EXPORT std::ostream &operator<<(std::ostream &stream, const LookupTableName & /*l*/);
113 
119 #define mitkSpecializeGenericLookupTableOperator(LookupTableName) \
120  \
121  std::ostream &mitk::operator<<(std::ostream &stream, const LookupTableName &l) \
122  \
123  { \
124  typedef LookupTableName::LookupTableType::const_iterator IterType; \
125  IterType e = l.GetLookupTable().end(); \
126  IterType b = l.GetLookupTable().begin(); \
127  stream << "["; \
128  for (IterType i = b; i != e; ++i) \
129  { \
130  if (i != b) \
131  { \
132  stream << ", "; \
133  } \
134  stream << i->first << " -> " << i->second; \
135  } \
136  return stream << "]"; \
137  };
138 #endif
mitk::GenericLookupTable::GenericLookupTable
GenericLookupTable()
Definition: mitkGenericLookupTable.h:50
mitk::GenericLookupTable::operator=
virtual Self & operator=(const Self &other)
Definition: mitkGenericLookupTable.h:72
mitk::GenericLookupTable::SetTableValue
void SetTableValue(IdentifierType id, ValueType value)
Definition: mitkGenericLookupTable.h:53
mitk::GenericLookupTable::Self
GenericLookupTable Self
Definition: mitkGenericLookupTable.h:48
mitk::GenericLookupTable::GetLookupTable
const LookupTableType & GetLookupTable() const
Definition: mitkGenericLookupTable.h:69
mitk::GenericLookupTable::GetNameOfClass
virtual const char * GetNameOfClass() const
Definition: mitkGenericLookupTable.h:52
mitk::GenericLookupTable::ValueType
T ValueType
Definition: mitkGenericLookupTable.h:45
mitk::GenericLookupTable::operator!=
bool operator!=(const Self &lookupTable) const
Definition: mitkGenericLookupTable.h:71
mitk::GenericLookupTable::GetTableValue
ValueType GetTableValue(IdentifierType id) const
Definition: mitkGenericLookupTable.h:60
mitk
DataCollection - Class to facilitate loading/accessing structured data.
Definition: RenderingTests.dox:1
mitk::GenericLookupTable::ValueExists
bool ValueExists(IdentifierType id) const
Definition: mitkGenericLookupTable.h:54
mitk::DICOMCachedValueInfo
Definition: mitkDICOMImageBlockDescriptor.h:33
mitk::GenericLookupTable::IdentifierType
unsigned int IdentifierType
Definition: mitkGenericLookupTable.h:44
MitkCoreExports.h
mitkNumericTypes.h
mitk::GenericLookupTable::m_LookupTable
LookupTableType m_LookupTable
Definition: mitkGenericLookupTable.h:87
mitk::GenericLookupTable::operator==
bool operator==(const Self &lookupTable) const
Definition: mitkGenericLookupTable.h:70
mitk::GenericLookupTable::LookupTableType
std::map< IdentifierType, ValueType > LookupTableType
Definition: mitkGenericLookupTable.h:46
mitk::GenericLookupTable
Template class for generating lookup-tables.
Definition: mitkGenericLookupTable.h:41
mitk::GenericLookupTable::~GenericLookupTable
virtual ~GenericLookupTable()
Definition: mitkGenericLookupTable.h:51