CustusX  2021.04.21-dev+develop.337f9
An IGT application
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) 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 CXTYPECONVERSIONS_H_
13 #define CXTYPECONVERSIONS_H_
14 
15 #include "cxResourceExport.h"
16 
17 #include <string>
18 #include <vector>
19 #include <QString>
20 #include <QVariant>
21 #include <QDomDocument>
22 #include <QColor>
23 
24 #ifndef Q_MOC_RUN // workaround for bug in moc vs boost 1.48+: https://bugreports.qt-project.org/browse/QTBUG-22829
25 #include <boost/lexical_cast.hpp>
26 #endif
27 
36 template<class T>
37 std::string string_cast(const T& val)
38 {
39  return boost::lexical_cast<std::string>(val);
40 }
41 
45 template<class T>
46 QString qstring_cast(const T& val)
47 {
48  return QString::fromStdString(string_cast(val));
49 }
50 
53 class cxResource_EXPORT cstring_cast_Placeholder
54 {
55 public:
56  explicit cstring_cast_Placeholder(const QString& val) : mData(val.toStdString()) {}
57  operator const char*() const { return mData.c_str(); }
58  operator char*() const { return const_cast<char*>(mData.c_str()); } // use with extreme caution.
59  const char* c() const { return mData.c_str(); }
60 private:
61  std::string mData;
62 };
63 
68 template<class T>
70 {
72 }
73 template<> cxResource_EXPORT cstring_cast_Placeholder cstring_cast<QString>(const QString& val);
74 template<> cxResource_EXPORT cstring_cast_Placeholder cstring_cast<QVariant>(const QVariant& val);
75 
78 cxResource_EXPORT std::ostream& operator<<(std::ostream& str, const QString& qstring);
79 
83 cxResource_EXPORT std::vector<double> convertQString2DoubleVector(const QString& input, bool* ok=0);
84 
88 template<class T>
89 QString streamXml2String(T& val)
90 {
91  QDomDocument doc;
92  QDomElement root = doc.createElement("root");
93  doc.appendChild(root);
94 
95  val.addXml(root);
96  return doc.toString();
97 }
98 template<> cxResource_EXPORT QString streamXml2String(QString& val);
99 
100 cxResource_EXPORT QString color2string(QColor color);
101 cxResource_EXPORT QColor string2color(QString input, QColor defaultValue=QColor("green"));
102 
107 #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)