MeVisLab Toolbox Reference
mlLightweight.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_LIGHTWEIGHT_H
14 #define ML_LIGHTWEIGHT_H
15 
16 
19 
20 #include "mlVesselGraphSystem.h"
21 
22 // ML includes
23 #include "VesselGraphTypes.h"
24 
25 // boost includes
27 #include <boost/ref.hpp>
28 #include <boost/tuple/tuple.hpp>
29 #include <boost/variant.hpp>
30 #include <boost/mpl/contains.hpp>
31 #include <boost/mpl/for_each.hpp>
32 #include <boost/mpl/less_equal.hpp>
33 #include <boost/mpl/integral_c.hpp>
34 #include <boost/mpl/vector.hpp>
35 #include <boost/mpl/sizeof.hpp>
37 
39 
40 namespace lightweight {
41 
44  template<typename T>
46  {
47  public:
49 
50  heap_storage(const T& value=T()) : _value(new T(value)) {};
51  heap_storage(const SelfType& other) : _value(new T(*other._value)) {};
52  ~heap_storage() { delete _value; };
53 
54  SelfType& operator=(const SelfType& other) { *_value = *other._value; return *this; }
55  SelfType& operator=(const T& other) { *_value = *other._value; return *this; }
56 
57  bool operator==(const SelfType& other) const { return *_value == *other._value; }
58  bool operator!=(const SelfType& other) const { return *_value != *other._value; }
59  operator T&() { return *_value; }
60  operator const T&() const { return *_value; }
61  private:
62  T* _value;
63  };
64 
65  // template helper function to access the content of a possibly heap_storage wrapped variable
66  template<typename T>
67  T& access(T& storage) { return storage; }
68 
69  template<typename T>
70  T& access(heap_storage<T>& storage) { return storage; }
71 
72  template<typename T>
73  const T& access(const heap_storage<T>& storage) { return storage; }
74 
75  // template meta-function class which is used to get the leightweight type for an arbitrary type
76  // if the size of the given type is less or equal to the size of a pointer,
78  {
79  template<typename T>
80  struct apply
81  {
82  typedef typename boost::mpl::if_<
83  typename boost::mpl::less_equal<boost::mpl::sizeof_<T>, boost::mpl::int_<8> >::type,
84  T,
87  };
88  };
89 
90  // a wrapper for visitors which removes heap_storage wrapping of the visited value if applicable
91  template<typename VisitorType>
93  public:
94  typedef typename VisitorType::result_type result_type;
95 
96  lightweight_visitor(VisitorType& visitor) : _visitor(visitor) {};
97 
98  template<typename T>
99  result_type operator()(T& value) { return _visitor(access(value)); }
100 
101  private:
102  VisitorType& _visitor;
103  };
104 
105  // template function that applies a visitor to the possibly heap_storage wrapped content of a variant
106  template<typename VisitorType, typename VariantType>
107  inline
108  typename VisitorType::result_type apply_visitor(VisitorType& visitor, VariantType& value) { lightweight_visitor<VisitorType> lVisitor(visitor); return boost::apply_visitor(lVisitor, value); }
109 } // namespace light_variant
110 
112 
113 #endif // __mlLightweight_HPP_
@ T
Definition: SoKeyGrabber.h:71
wrapper for large objects (i.e.
Definition: mlLightweight.h:46
heap_storage(const SelfType &other)
Definition: mlLightweight.h:51
bool operator==(const SelfType &other) const
Definition: mlLightweight.h:57
SelfType & operator=(const T &other)
Definition: mlLightweight.h:55
heap_storage(const T &value=T())
Definition: mlLightweight.h:50
heap_storage< T > SelfType
Definition: mlLightweight.h:48
bool operator!=(const SelfType &other) const
Definition: mlLightweight.h:58
SelfType & operator=(const SelfType &other)
Definition: mlLightweight.h:54
VisitorType::result_type result_type
Definition: mlLightweight.h:94
lightweight_visitor(VisitorType &visitor)
Definition: mlLightweight.h:96
result_type operator()(T &value)
Definition: mlLightweight.h:99
#define VESSELGRAPH_END_NAMESPACE
#define VESSELGRAPH_BEGIN_NAMESPACE
Namespace lightweight, provides utilities to wrap large objects in a lightweight variant whose static...
Definition: mlLightweight.h:40
VisitorType::result_type apply_visitor(VisitorType &visitor, VariantType &value)
T & access(T &storage)
Definition: mlLightweight.h:67
boost::mpl::if_< typename boost::mpl::less_equal< boost::mpl::sizeof_< T >, boost::mpl::int_< 8 > >::type, T, heap_storage< T > >::type type
Definition: mlLightweight.h:86