CustusX  18.04-rc1
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 
106 template<class ENUM, unsigned COUNT>
108 {
109 public:
110  static boost::array<QString, COUNT> mText;
111 
112  static QString enum2string(const ENUM& val)
113  {
114  if (unsigned(val)<mText.size())
115  return mText[val];
116  return "UNDEFINED";
117  }
118 
119  static ENUM string2enum(const QString& val)
120  {
121  for (unsigned i=0; i<mText.size(); ++i)
122  if (mText[i].toUpper()==val.toUpper())
123  return static_cast<ENUM>(i);
124  return static_cast<ENUM>(COUNT);
125  }
126 };
127 
128 template<class ENUM>
129 ENUM string2enum(const QString& val);
130 
131 template<class ENUM>
132 QString enum2string(const ENUM& val);
133 
138 #define SNW_DECLARE_ENUM_STRING_CONVERTERS(EXPORT, NS, ENUM_NAME) \
139 namespace NS \
140 { \
141  EXPORT std::ostream& operator<<(std::ostream& s, const ENUM_NAME& val); \
142 } \
143 template<> \
144 EXPORT QString enum2string<NS::ENUM_NAME>(const NS::ENUM_NAME& val); \
145 template<> \
146 EXPORT NS::ENUM_NAME string2enum<NS::ENUM_NAME>(const QString& val); \
147 
148 //-----------------------------------------------------------------
149 
164 #define SNW_DEFINE_ENUM_STRING_CONVERTERS_BEGIN(NS, ENUM_NAME, ENUM_SIZE) \
165 template<> \
166 boost::array<QString, NS::ENUM_SIZE> EnumConverter<NS::ENUM_NAME,NS::ENUM_SIZE>::mText = \
167 { \
168 
169 #define SNW_DEFINE_ENUM_STRING_CONVERTERS_END(NS, ENUM_NAME, ENUM_SIZE) \
170 }; \
171 template<> QString enum2string<NS::ENUM_NAME>(const NS::ENUM_NAME& val) \
172 { \
173  return EnumConverter<NS::ENUM_NAME, NS::ENUM_SIZE>::enum2string(val); \
174 } \
175 template<> NS::ENUM_NAME string2enum<NS::ENUM_NAME>(const QString& val) \
176 { \
177  return EnumConverter<NS::ENUM_NAME, NS::ENUM_SIZE>::string2enum(val); \
178 } \
179 namespace NS \
180 { \
181  std::ostream& operator<<(std::ostream& s, const ENUM_NAME& val) \
182  { \
183  s << enum2string(val); \
184  return s; \
185  } \
186 } \
187 // --------------------------------------------------------
188 
193 #endif /*CXENUMCONVERTER_H_*/
static QString enum2string(const ENUM &val)
static ENUM string2enum(const QString &val)
Class for easy conversion between an enum and a QString.
static boost::array< QString, COUNT > mText