Fraxinus  17.12-rc3
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) 2008-2014, SINTEF Department of Medical Technology
5 All rights reserved.
6 
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9 
10 1. Redistributions of source code must retain the above copyright notice,
11  this list of conditions and the following disclaimer.
12 
13 2. Redistributions in binary form must reproduce the above copyright notice,
14  this list of conditions and the following disclaimer in the documentation
15  and/or other materials provided with the distribution.
16 
17 3. Neither the name of the copyright holder nor the names of its contributors
18  may be used to endorse or promote products derived from this software
19  without specific prior written permission.
20 
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
25 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 =========================================================================*/
32 
33 #ifndef CXENUMCONVERTER_H_
34 #define CXENUMCONVERTER_H_
35 
36 #include "cxResourceExport.h"
37 
38 #include <string>
39 #include <boost/array.hpp>
40 #include <boost/algorithm/string.hpp>
41 #include <QString>
42 #include "cxTypeConversions.h"
43 
127 template<class ENUM, unsigned COUNT>
129 {
130 public:
131  static boost::array<QString, COUNT> mText;
132 
133  static QString enum2string(const ENUM& val)
134  {
135  if (unsigned(val)<mText.size())
136  return mText[val];
137  return "UNDEFINED";
138  }
139 
140  static ENUM string2enum(const QString& val)
141  {
142  for (unsigned i=0; i<mText.size(); ++i)
143  if (mText[i].toUpper()==val.toUpper())
144  return static_cast<ENUM>(i);
145  return static_cast<ENUM>(COUNT);
146  }
147 };
148 
149 template<class ENUM>
150 ENUM string2enum(const QString& val);
151 
152 template<class ENUM>
153 QString enum2string(const ENUM& val);
154 
159 #define SNW_DECLARE_ENUM_STRING_CONVERTERS(EXPORT, NS, ENUM_NAME) \
160 namespace NS \
161 { \
162  EXPORT std::ostream& operator<<(std::ostream& s, const ENUM_NAME& val); \
163 } \
164 template<> \
165 EXPORT QString enum2string<NS::ENUM_NAME>(const NS::ENUM_NAME& val); \
166 template<> \
167 EXPORT NS::ENUM_NAME string2enum<NS::ENUM_NAME>(const QString& val); \
168 
169 //-----------------------------------------------------------------
170 
185 #define SNW_DEFINE_ENUM_STRING_CONVERTERS_BEGIN(NS, ENUM_NAME, ENUM_SIZE) \
186 template<> \
187 boost::array<QString, NS::ENUM_SIZE> EnumConverter<NS::ENUM_NAME,NS::ENUM_SIZE>::mText = \
188 { \
189 
190 #define SNW_DEFINE_ENUM_STRING_CONVERTERS_END(NS, ENUM_NAME, ENUM_SIZE) \
191 }; \
192 template<> QString enum2string<NS::ENUM_NAME>(const NS::ENUM_NAME& val) \
193 { \
194  return EnumConverter<NS::ENUM_NAME, NS::ENUM_SIZE>::enum2string(val); \
195 } \
196 template<> NS::ENUM_NAME string2enum<NS::ENUM_NAME>(const QString& val) \
197 { \
198  return EnumConverter<NS::ENUM_NAME, NS::ENUM_SIZE>::string2enum(val); \
199 } \
200 namespace NS \
201 { \
202  std::ostream& operator<<(std::ostream& s, const ENUM_NAME& val) \
203  { \
204  s << enum2string(val); \
205  return s; \
206  } \
207 } \
208 // --------------------------------------------------------
209 
214 #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