ImplicitFunctionParameter.h 4.02 KB
Newer Older
1
2
3
// Mantid Repository : https://github.com/mantidproject/mantid
//
// Copyright © 2010 ISIS Rutherford Appleton Laboratory UKRI,
4
5
//   NScD Oak Ridge National Laboratory, European Spallation Source,
//   Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6
// SPDX - License - Identifier: GPL - 3.0 +
7
#pragma once
Peterson, Peter's avatar
Peterson, Peter committed
8
9
10
11

//----------------------------------------------------------------------
// Includes
//----------------------------------------------------------------------
LamarMoore's avatar
LamarMoore committed
12
#include <memory>
Peterson, Peter's avatar
Peterson, Peter committed
13
14
#include <sstream>
#include <vector>
15

16
17
#include "MantidAPI/DllConfig.h"

18
#ifndef Q_MOC_RUN
19
20
#include <boost/algorithm/string.hpp>
#include <boost/format.hpp>
21
#endif
Peterson, Peter's avatar
Peterson, Peter committed
22

23
24
#include <Poco/DOM/AutoPtr.h>
#include <Poco/DOM/DOMWriter.h>
LamarMoore's avatar
LamarMoore committed
25
#include <Poco/DOM/Document.h>
26
27
28
#include <Poco/DOM/Element.h>
#include <Poco/DOM/Text.h>

29
30
31
namespace Mantid {
namespace API {
/** Abstract parameter type for use with IImplicitFunctions.
32

33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
@author Owen Arnold, Tessella plc
@date 01/10/2010
*/

class MANTID_API_DLL ImplicitFunctionParameter {

public:
  virtual std::string getName() const = 0;

  virtual bool isValid() const = 0;

  virtual std::string toXMLString() const = 0;

  virtual ImplicitFunctionParameter *clone() const = 0;

48
  virtual ~ImplicitFunctionParameter() = default;
49
50

protected:
51
  bool m_isValid{false};
52

53
  std::string parameterXMLTemplate(const std::string &valueXMLtext) const {
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
    using namespace Poco::XML;
    AutoPtr<Document> pDoc = new Document;
    AutoPtr<Element> paramElement = pDoc->createElement("Parameter");

    pDoc->appendChild(paramElement);
    AutoPtr<Element> typeElement = pDoc->createElement("Type");
    AutoPtr<Text> typeText = pDoc->createTextNode(this->getName());
    typeElement->appendChild(typeText);
    paramElement->appendChild(typeElement);

    AutoPtr<Element> valueElement = pDoc->createElement("Value");
    AutoPtr<Text> valueText = pDoc->createTextNode(valueXMLtext);
    valueElement->appendChild(valueText);
    paramElement->appendChild(valueElement);

    std::stringstream xmlstream;

    DOMWriter writer;
    writer.writeNode(xmlstream, pDoc);
    return xmlstream.str();
  }
};

//------------------------------------------------------------------------------------
// ElementTraits TypeTraits region
//------------------------------------------------------------------------------------

/** Default ElementTraits SFINAE
Typetraits are used to provide the correct formatting based on the element type
chosen.
*/
template <typename T> struct ElementTraits {};

/** ElementTraits for boolean element types.
LamarMoore's avatar
LamarMoore committed
88
 */
89
template <> struct ElementTraits<size_t> {
90
  using ValueType = size_t;
Samuel Jones's avatar
Samuel Jones committed
91
92
  static std::string formatCS(const ValueType &value) { return boost::str(boost::format("%u,") % value); }
  static std::string format(const ValueType &value) { return boost::str(boost::format("%u") % value); }
93
94
95
};

/** ElementTraits for boolean element types.
LamarMoore's avatar
LamarMoore committed
96
 */
97
template <> struct ElementTraits<bool> {
98
  using ValueType = bool;
Samuel Jones's avatar
Samuel Jones committed
99
100
  static std::string formatCS(const ValueType &value) { return boost::str(boost::format("%u,") % value); }
  static std::string format(const ValueType &value) { return boost::str(boost::format("%u") % value); }
101
102
103
};

/** ElementTraits for double element types.
LamarMoore's avatar
LamarMoore committed
104
 */
105
template <> struct ElementTraits<double> {
106
  using ValueType = double;
Samuel Jones's avatar
Samuel Jones committed
107
108
  static std::string formatCS(const ValueType &value) { return boost::str(boost::format("%.4f,") % value); }
  static std::string format(const ValueType &value) { return boost::str(boost::format("%.4f") % value); }
109
110
111
};

/** ElementTraits for float element types.
LamarMoore's avatar
LamarMoore committed
112
 */
113
template <> struct ElementTraits<float> {
114
  using ValueType = double;
Samuel Jones's avatar
Samuel Jones committed
115
116
  static std::string formatCS(const ValueType &value) { return boost::str(boost::format("%.4f,") % value); }
  static std::string format(const ValueType &value) { return boost::str(boost::format("%.4f") % value); }
117
118
119
120
121
};

//------------------------------------------------------------------------------------
// End ElementTraits TypeTraits region
//------------------------------------------------------------------------------------
LamarMoore's avatar
LamarMoore committed
122
123
} // namespace API
} // namespace Mantid