$extrastylesheet
Olena
User documentation 2.1
An Image Processing Platform
Milena
Getting started
API Reference Manual
All Classes
Examples
Demos
Publications
All
Classes
Namespaces
Functions
Variables
Typedefs
Enumerations
Enumerator
Friends
Groups
Pages
unary.hh
1
// Copyright (C) 2007, 2008, 2009 EPITA Research and Development Laboratory (LRDE)
2
//
3
// This file is part of Olena.
4
//
5
// Olena is free software: you can redistribute it and/or modify it under
6
// the terms of the GNU General Public License as published by the Free
7
// Software Foundation, version 2 of the License.
8
//
9
// Olena is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12
// General Public License for more details.
13
//
14
// You should have received a copy of the GNU General Public License
15
// along with Olena. If not, see <http://www.gnu.org/licenses/>.
16
//
17
// As a special exception, you may use this file as part of a free
18
// software project without restriction. Specifically, if other files
19
// instantiate templates or use macros or inline functions from this
20
// file, or you compile this file and link it with other files to produce
21
// an executable, this file does not by itself cause the resulting
22
// executable to be covered by the GNU General Public License. This
23
// exception does not however invalidate any other reasons why the
24
// executable file might be covered by the GNU General Public License.
25
26
#ifndef MLN_FUN_UNARY_HH
27
# define MLN_FUN_UNARY_HH
28
29
# include <mln/core/concept/meta_function.hh>
30
# include <mln/fun/spe/unary.hh>
31
# include <mln/trait/next/solve.hh>
32
33
34
namespace
mln
35
{
36
37
namespace
fun
38
{
39
40
// Forward declarations, for composition with unary::operator()(Fun)
41
struct
compose
;
42
43
namespace
internal
44
{
45
46
template
<
typename
U>
47
struct
unary_with
{};
48
49
}
50
51
template
<
typename
F,
typename
E = F>
52
struct
unary
:
mln::Meta_Function_v2v
< E >
53
{
54
typedef
F
flag
;
55
typedef
mln_trait_fun_param
(
flag
) param;
56
typedef
mln_trait_fun_storage
(
flag
)
storage
;
57
58
template <typename
T
>
59
struct
with
{
60
typedef
mln_trait_nunary(
internal::unary_with<F>
,
T
) def;
61
typedef
typename
def::ret
ret
;
62
};
63
64
template
<
typename
T>
65
typename
with<T>::ret::result
operator()
(
const
T
& v)
const
66
{
67
return
with<T>::def::call(
exact
(*
this
), v);
68
}
69
70
template
<
typename
T>
71
typename
with<T>::ret::template lresult_with<T>::ret
operator()
(
T
& v)
const
72
{
73
// See the commentary in next method.
74
typedef
typename
with<T>::ret fun_t;
75
fun_t f(
state
());
76
return
f.apply_rw(v);
77
}
78
79
template
<
typename
T,
typename
R>
80
void
set
(
T
& v,
const
R& r)
const
81
{
82
// Decomposing "with<T>::ret(state()).set(v, r)" into 3 lines
83
// helps g++-3.3!
84
typedef
typename
with<T>::ret fun_t;
85
fun_t f(
state
());
86
f.set(v, r);
87
}
88
89
template
<
typename
U>
90
void
init
(
const
U&
value
)
91
{
92
state_
=
mln::trait::function::internal::introspect::has_storage_t<flag, void>::compute
(value);
93
};
94
95
unary
()
96
{
97
}
98
99
template
<
typename
U>
100
unary
(
const
U& param)
101
{
102
this->
init
(param);
103
}
104
105
stored<storage>
&
state
()
106
{
107
return
state_
;
108
}
109
110
const
stored<storage>
&
state
()
const
111
{
112
return
state_
;
113
}
114
115
protected
:
116
stored<storage>
state_
;
117
};
118
119
}
// end of namespace mln::fun
120
121
namespace
trait
122
{
123
124
namespace
next
125
{
126
127
// Any type
128
template
<
typename
F,
typename
T>
129
struct
set_unary_< mln::
fun::internal::unary_with
<F>,
mln::Object
,
T
>
130
{
131
struct
ret_t
132
{
133
typedef
mln::fun::spe::unary<F, T>
ret
;
134
135
static
typename
ret::result call(
const
F& f,
const
T
& v)
136
{
137
return
ret
(f.state())(v);
138
}
139
};
140
141
typedef
ret_t
ret
;
142
};
143
144
// Meta Function
145
template
<
typename
F,
typename
G>
146
struct
set_unary_< mln::
fun::internal::unary_with
<F>,
mln::Meta_Function
, G>
147
{
148
// FIXME: Workaround for cyclic references (unary -> unary_with -> compose -> unary)
149
template
<
typename
T>
150
struct
identity
151
{
152
typedef
T
ret
;
153
};
154
155
struct
ret_t
156
{
157
typedef
typename
identity<mln::fun::compose>::ret::template with<F, G>::ret
ret
;
158
159
static
typename
ret::result call(
const
F& f,
const
G& g)
160
{
161
return
ret
()(f, g);
162
}
163
164
};
165
166
typedef
ret_t
ret
;
167
};
168
169
}
// end of namespace mln::trait::next
170
171
}
// end of namespace mln::trait
172
173
}
// end of namespace mln
174
175
#endif // ! MLN_FUN_UNARY_HH
mln
fun
unary.hh
Copyright (C) 2012 EPITA Research and Development Laboratory (LRDE)