MeVisLab Toolbox Reference
Loading...
Searching...
No Matches
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
40namespace lightweight
41{
42
45 template <typename T> class heap_storage
46 {
47 public:
49
50 heap_storage(const T& value = T())
51 : _value(new T(value)) {};
53 : _value(new T(*other._value)) {};
55 {
56 delete _value;
57 };
58
60 {
61 *_value = *other._value;
62 return *this;
63 }
65 {
66 *_value = *other._value;
67 return *this;
68 }
69
70 bool operator==(const SelfType& other) const
71 {
72 return *_value == *other._value;
73 }
74 bool operator!=(const SelfType& other) const
75 {
76 return *_value != *other._value;
77 }
78 operator T&()
79 {
80 return *_value;
81 }
82 operator const T&() const
83 {
84 return *_value;
85 }
86
87 private:
88 T* _value;
89 };
90
91 // template helper function to access the content of a possibly heap_storage wrapped variable
92 template <typename T> T& access(T& storage)
93 {
94 return storage;
95 }
96
97 template <typename T> T& access(heap_storage<T>& storage)
98 {
99 return storage;
100 }
101
102 template <typename T> const T& access(const heap_storage<T>& storage)
103 {
104 return storage;
105 }
106
107 // template meta-function class which is used to get the leightweight type for an arbitrary type
108 // if the size of the given type is less or equal to the size of a pointer,
110 {
111 template <typename T> struct apply
112 {
113 typedef
114 typename boost::mpl::if_<typename boost::mpl::less_equal<boost::mpl::sizeof_<T>, boost::mpl::int_<8>>::type,
116 };
117 };
118
119 // a wrapper for visitors which removes heap_storage wrapping of the visited value if applicable
120 template <typename VisitorType> class lightweight_visitor
121 {
122 public:
123 typedef typename VisitorType::result_type result_type;
124
127
128 template <typename T> result_type operator()(T& value)
129 {
130 return _visitor(access(value));
131 }
132
133 private:
134 VisitorType& _visitor;
135 };
136
137 // template function that applies a visitor to the possibly heap_storage wrapped content of a variant
138 template <typename VisitorType, typename VariantType>
139 inline typename VisitorType::result_type apply_visitor(VisitorType& visitor, VariantType& value)
140 {
142 return boost::apply_visitor(lVisitor, value);
143 }
144} // namespace lightweight
145
147
148#endif // __mlLightweight_HPP_
@ T
wrapper for large objects (i.e.
heap_storage(const SelfType &other)
SelfType & operator=(const SelfType &other)
bool operator==(const SelfType &other) const
SelfType & operator=(const T &other)
heap_storage(const T &value=T())
heap_storage< T > SelfType
bool operator!=(const SelfType &other) const
VisitorType::result_type result_type
lightweight_visitor(VisitorType &visitor)
result_type operator()(T &value)
Target mlrange_cast(Source arg)
Generic version of checked ML casts.
#define VESSELGRAPH_END_NAMESPACE
#define VESSELGRAPH_BEGIN_NAMESPACE
Namespace lightweight, provides utilities to wrap large objects in a lightweight variant whose static...
T & access(T &storage)
VisitorType::result_type apply_visitor(VisitorType &visitor, VariantType &value)
boost::mpl::if_< typenameboost::mpl::less_equal< boost::mpl::sizeof_< T >, boost::mpl::int_< 8 > >::type, T, heap_storage< T > >::type type