Fraxinus  16.5.0-fx-rc9
An IGT application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
cxTypeConversions.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 CXTYPECONVERSIONS_H_
34 #define CXTYPECONVERSIONS_H_
35 
36 #include "cxResourceExport.h"
37 
38 #include <string>
39 #include <vector>
40 #include <QString>
41 #include <QVariant>
42 #include <QDomDocument>
43 #include <QColor>
44 
45 #ifndef Q_MOC_RUN // workaround for bug in moc vs boost 1.48+: https://bugreports.qt-project.org/browse/QTBUG-22829
46 #include <boost/lexical_cast.hpp>
47 #endif
48 
57 template<class T>
58 std::string string_cast(const T& val)
59 {
60  return boost::lexical_cast<std::string>(val);
61 }
62 
66 template<class T>
67 QString qstring_cast(const T& val)
68 {
69  return QString::fromStdString(string_cast(val));
70 }
71 
74 class cxResource_EXPORT cstring_cast_Placeholder
75 {
76 public:
77  explicit cstring_cast_Placeholder(const QString& val) : mData(val.toStdString()) {}
78  operator const char*() const { return mData.c_str(); }
79  operator char*() const { return const_cast<char*>(mData.c_str()); } // use with extreme caution.
80  const char* c() const { return mData.c_str(); }
81 private:
82  std::string mData;
83 };
84 
89 template<class T>
91 {
93 }
94 template<> cxResource_EXPORT cstring_cast_Placeholder cstring_cast<QString>(const QString& val);
95 template<> cxResource_EXPORT cstring_cast_Placeholder cstring_cast<QVariant>(const QVariant& val);
96 
99 cxResource_EXPORT std::ostream& operator<<(std::ostream& str, const QString& qstring);
100 
104 cxResource_EXPORT std::vector<double> convertQString2DoubleVector(const QString& input, bool* ok=0);
105 
109 template<class T>
110 QString streamXml2String(T& val)
111 {
112  QDomDocument doc;
113  QDomElement root = doc.createElement("root");
114  doc.appendChild(root);
115 
116  val.addXml(root);
117  return doc.toString();
118 }
119 template<> cxResource_EXPORT QString streamXml2String(QString& val);
120 
121 cxResource_EXPORT QString color2string(QColor color);
122 cxResource_EXPORT QColor string2color(QString input, QColor defaultValue=QColor("green"));
123 
128 #endif /*CXTYPECONVERSIONS_H_*/
QString qstring_cast(const T &val)
cstring_cast_Placeholder(const QString &val)
cxResource_EXPORT QColor string2color(QString input, QColor defaultValue=QColor("green"))
cxResource_EXPORT QString color2string(QColor color)
const char * c() const
cstring_cast_Placeholder cstring_cast(const T &val)
std::string string_cast(const T &val)
cxResource_EXPORT std::ostream & operator<<(std::ostream &str, const QString &qstring)
QString streamXml2String(T &val)
cxResource_EXPORT std::vector< double > convertQString2DoubleVector(const QString &input, bool *ok=0)