MeVisLab Toolbox Reference
mlPropertyPersistence.h
Go to the documentation of this file.
1 /*************************************************************************************
2 **
3 ** Copyright 2010, MeVis Medical Solutions AG
4 **
5 ** The user may use this file in accordance with the license agreement provided with
6 ** the Software or, alternatively, in accordance with the terms contained in a
7 ** written agreement between the user and MeVis Medical Solutions AG.
8 **
9 ** For further information use the contact form at https://www.mevislab.de/contact
10 **
11 **************************************************************************************/
12 
13 #ifndef ML_PROPERTY_PERSISTENCE_H
14 #define ML_PROPERTY_PERSISTENCE_H
15 
16 
18 
19 #include "mlVesselGraphSystem.h"
20 #include "mlPropertyValue.h"
21 #include "mlPropertyTraits.h"
22 
23 #include <boost/noncopyable.hpp>
24 
25 #ifdef WIN32
26 #define DOT_TEMPLATE
27 #else
28 #define DOT_TEMPLATE template
29 #endif
30 
32 
33 class ToStringVisitor : public boost::static_visitor<std::string>
34 {
35 public:
36  template<typename T>
37  std::string operator()(const T& value) const { return StringConversion::writeToStdString(value); }
38 };
39 
40 class TypeStringVisitor : public boost::static_visitor<std::string>
41 {
42 public:
43  template<typename T>
44  std::string operator()(const T& /* not used */) const { return PropertyTraits<typename boost::remove_const<T>::type>::name(); }
45 };
46 
47 inline
48 void addToTree(TreeNode* parent, const PropertyValue& value)
49 {
50  parent->addChild(value.apply(ToStringVisitor()), value.apply(TypeStringVisitor()).c_str());
51 }
52 
53 template<typename Functor>
54 class ApplyFunctorWithType : public boost::static_visitor<>, boost::noncopyable
55 {
56 public:
57  ApplyFunctorWithType(const Functor& functor, const std::string& typeString) :
58  _functor(functor), _typeString(typeString), _applied(false) {};
59 
60  template<typename T>
61  void operator()(const T& /*t*/)
62  {
63  if (_typeString==PropertyTraits<T>::name()) {
64  _applied = true;
65  _result = _functor.DOT_TEMPLATE operator()<T>();
66  }
67  }
68 
69  bool functorWasApplied() const { return _applied; }
70  typename Functor::result_type getResult() const { return _result; }
71 
72 private:
73  Functor _functor;
74  const std::string _typeString;
75  bool _applied;
76  typename Functor::result_type _result;
77 };
78 
79 class FromStringFunctor : public boost::static_visitor<PropertyValue>
80 {
81 public:
82  FromStringFunctor(const std::string& stringRepresentation) : _stringRepresentation(stringRepresentation) {};
83 
84  template<typename T>
85  T operator()() const {
86  T value;
87  bool success = StringConversion::readFromString(_stringRepresentation, value);
88  if (!success) {
89  std::string err("Could not convert \"");
90  err += _stringRepresentation;
91  err += "\" to type ";
92  err += PropertyTraits<T>::name();
93  throw TreeNodeException(TNE_Unknown, err.c_str());
94  }
95  return value;
96  }
97 private:
98  const std::string _stringRepresentation;
99 };
100 
101 inline
102 PropertyValue fromString(const std::string& representation, const std::string& typeString)
103 {
104  ApplyFunctorWithType<FromStringFunctor> visitor(FromStringFunctor(representation), typeString);
105  boost::mpl::for_each<PropertyValue::SupportedPropertyTypes>(boost::ref(visitor));
106  if (visitor.functorWasApplied()) {
107  return visitor.getResult();
108  }
109  else {
110  throw ML_BAD_PARAMETER;
111  }
112 }
113 
114 inline
115 PropertyValue readFromTree(TreeNode* parent)
116 {
117  std::string valueString;
118  parent->readChild(valueString);
119  return fromString(valueString, parent->getLastReadChildName());
120 }
121 
123 
124 #endif // __mlPropertyPersistence_HPP
@ T
Definition: SoKeyGrabber.h:71
ApplyFunctorWithType(const Functor &functor, const std::string &typeString)
Functor::result_type getResult() const
FromStringFunctor(const std::string &stringRepresentation)
Class PropertyValue, which is used to hold properties of a given set of allowed types in typesafe way...
Class PropertyValue, which is used to hold properties of a given set of allowed types in typesafe way...
VisitorType::result_type apply(VisitorType &visitor)
std::string operator()(const T &value) const
std::string operator()(const T &) const
#define ML_BAD_PARAMETER
A bad/invalid parameter (or even an inappropriate image) has been passed to a module or an algorithm;...
Definition: mlTypeDefs.h:925
PropertyValue fromString(const std::string &representation, const std::string &typeString)
PropertyValue readFromTree(TreeNode *parent)
void addToTree(TreeNode *parent, const PropertyValue &value)
#define VESSELGRAPH_END_NAMESPACE
#define VESSELGRAPH_BEGIN_NAMESPACE
@ TNE_Unknown
Definition: mlTreeNode.h:78