Medical Imaging Interaction Toolkit  2018.4.99-87d68d9f
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_HEADER_INCLUDED_C1061CEE
14 #define MITKGENERICLOOKUPTABLE_H_HEADER_INCLUDED_C1061CEE
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 
60  ValueType GetTableValue(IdentifierType id) const
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:
87  LookupTableType m_LookupTable;
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
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.
Template class for generating lookup-tables.
virtual const char * GetNameOfClass() const
const LookupTableType & GetLookupTable() const
ValueType GetTableValue(IdentifierType id) const
bool ValueExists(IdentifierType id) const
bool operator==(const Self &lookupTable) const