NorMIT-nav  22.09
An IGT application
cxEnumConverter.h
Go to the documentation of this file.
1 /*=========================================================================
2 This file is part of CustusX, an Image Guided Therapy Application.
3 
4 Copyright (c) SINTEF Department of Medical Technology.
5 All rights reserved.
6 
7 CustusX is released under a BSD 3-Clause license.
8 
9 See Lisence.txt (https://github.com/SINTEFMedtek/CustusX/blob/master/License.txt) for details.
10 =========================================================================*/
11 
12 #ifndef CXENUMCONVERTER_H_
13 #define CXENUMCONVERTER_H_
14 
15 #include "cxResourceExport.h"
16 
17 #include <string>
18 #include <boost/array.hpp>
19 #include <boost/algorithm/string.hpp>
20 #include <QString>
21 #include "cxTypeConversions.h"
22 
29 //###############################################################################################
30 //# #
31 //# NB: Include the file cxEnumConversion.h instead of this file to avoid IDE/compiler warnings #
32 //# #
33 //###############################################################################################
34 
35 
114 template<class ENUM, unsigned COUNT>
116 {
117 public:
118  static boost::array<QString, COUNT> mText;
119 
120  static QString enum2string(const ENUM& val)
121  {
122  if (unsigned(val)<mText.size())
123  return mText[val];
124  return "UNDEFINED";
125  }
126 
127  static ENUM string2enum(const QString& val)
128  {
129  for (unsigned i=0; i<mText.size(); ++i)
130  if (mText[i].toUpper()==val.toUpper())
131  return static_cast<ENUM>(i);
132  return static_cast<ENUM>(COUNT);
133  }
134 };
135 
136 template<class ENUM>
137 ENUM string2enum(const QString& val);
138 
139 template<class ENUM>
140 QString enum2string(const ENUM& val);
141 
146 #define SNW_DECLARE_ENUM_STRING_CONVERTERS(EXPORT, NS, ENUM_NAME) \
147 namespace NS \
148 { \
149  EXPORT std::ostream& operator<<(std::ostream& s, const ENUM_NAME& val); \
150 } \
151 template<> \
152 EXPORT QString enum2string<NS::ENUM_NAME>(const NS::ENUM_NAME& val); \
153 template<> \
154 EXPORT NS::ENUM_NAME string2enum<NS::ENUM_NAME>(const QString& val); \
155 
156 //-----------------------------------------------------------------
157 
172 #define DEFINE_ENUM_STRING_CONVERTERS_BEGIN(NS, ENUM_NAME, ENUM_SIZE) \
173 template<> \
174 boost::array<QString, NS::ENUM_SIZE> EnumConverter<NS::ENUM_NAME,NS::ENUM_SIZE>::mText = \
175 { \
176 
177 #define DEFINE_ENUM_STRING_CONVERTERS_END(NS, ENUM_NAME, ENUM_SIZE) \
178 }; \
179 template<> QString enum2string<NS::ENUM_NAME>(const NS::ENUM_NAME& val) \
180 { \
181  return EnumConverter<NS::ENUM_NAME, NS::ENUM_SIZE>::enum2string(val); \
182 } \
183 template<> NS::ENUM_NAME string2enum<NS::ENUM_NAME>(const QString& val) \
184 { \
185  return EnumConverter<NS::ENUM_NAME, NS::ENUM_SIZE>::string2enum(val); \
186 } \
187 namespace NS \
188 { \
189  std::ostream& operator<<(std::ostream& s, const ENUM_NAME& val) \
190  { \
191  s << enum2string(val); \
192  return s; \
193  } \
194 } \
195 // --------------------------------------------------------
196 
201 #endif /*CXENUMCONVERTER_H_*/
EnumConverter
Class for easy conversion between an enum and a QString.
Definition: cxEnumConverter.h:115
EnumConverter::mText
static boost::array< QString, COUNT > mText
Definition: cxEnumConverter.h:118
string2enum
ENUM string2enum(const QString &val)
cxTypeConversions.h
EnumConverter::string2enum
static ENUM string2enum(const QString &val)
Definition: cxEnumConverter.h:127
EnumConverter::enum2string
static QString enum2string(const ENUM &val)
Definition: cxEnumConverter.h:120
enum2string
QString enum2string(const ENUM &val)